Skip to main content

No project description provided

Project description

LangServe 🦜️🔗

Overview

LangServe is a library that allows developers to host their Langchain runnables / call into them remotely from a runnable interface.

Examples

For more examples, see the examples directory.

Server

#!/usr/bin/env python
from fastapi import FastAPI
from langchain.prompts import ChatPromptTemplate
from langchain.chat_models import ChatAnthropic, ChatOpenAI
from langserve import add_routes
from typing_extensions import TypedDict


app = FastAPI(
  title="LangChain Server",
  version="1.0",
  description="A simple api server using Langchain's Runnable interfaces",
)


add_routes(
    app,
    ChatOpenAI(),
    path="/openai",
    config_keys=[],
)

add_routes(
    app,
    ChatAnthropic(),
    path="/anthropic",
    config_keys=[],
)

# Serve a joke chain
class ChainInput(TypedDict):
    """The input to the chain."""

    topic: str
    """The topic of the joke."""

model = ChatAnthropic()
prompt = ChatPromptTemplate.from_template("tell me a joke about {topic}")
add_routes(app, prompt | model, path="/chain", input_type=ChainInput)

if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app, host="localhost", port=8000)

Client

from langchain.schema import SystemMessage, HumanMessage
from langchain.prompts import ChatPromptTemplate
from langchain.schema.runnable import RunnableMap
from langserve import RemoteRunnable

openai = RemoteRunnable("http://localhost:8000/openai/")
anthropic = RemoteRunnable("http://localhost:8000/anthropic/")
joke_chain = RemoteRunnable("http://localhost:8000/chain/")

joke_chain.invoke({"topic": "parrots"})

# or async
await joke_chain.ainvoke({"topic": "parrots"})

prompt = [
    SystemMessage(content='Act like either a cat or a parrot.'), 
    HumanMessage(content='Hello!')
]

# Supports astream
async for msg in anthropic.astream(prompt):
    print(msg, end="", flush=True)
    
prompt = ChatPromptTemplate.from_messages(
    [("system", "Tell me a long story about {topic}")]
)
    
# Can define custom chains
chain = prompt | RunnableMap({
    "openai": openai,
    "anthropic": anthropic,
})

chain.batch([{ "topic": "parrots" }, { "topic": "cats" }])

Installation

pip install langserve[all]

or use client extra for client code, and server extra for server code.

Features

  • Deploy runnables with FastAPI
  • Client can use remote runnables almost as if they were local
    • Supports async
    • Supports batch
    • Supports stream

Limitations

  • Chain callbacks cannot be passed from the client to the server

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langserve-0.0.6.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

langserve-0.0.6-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file langserve-0.0.6.tar.gz.

File metadata

  • Download URL: langserve-0.0.6.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for langserve-0.0.6.tar.gz
Algorithm Hash digest
SHA256 debc1e378ee3a4b3cb27c79dcdb9e093b974f20e6aacc26ce71089e9e64dab2f
MD5 893432100cf9012f8f27e7046ad83c32
BLAKE2b-256 ab9301394f4189ab47093fbdbb76eb477fc58cc0bcef79a84d93b5b85f17ce75

See more details on using hashes here.

File details

Details for the file langserve-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: langserve-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for langserve-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 5b4e9807747bbed8f2b4d36181054ca7131b41b045b3aef7c0d34ce3443f4165
MD5 6591a4baae6cec40146b72b271b72b0b
BLAKE2b-256 c026d7be90771fa63ebdd9855c13011dafc960ec304efd5da04516605f5bed0f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page