LangChain Vercel Adapters
This library provides a lightweight wrapper to integrate langchain-based messages into a Vercel's Chat SDK.
Features
- Convert LLM streaming output into vercel's data stream protocol
Installation
pip install langchain-vercel-adapters
Quickstart
Here is a simple example of a fastapi server:
pip install langchain-vercel-adapters langchain langchain_anthropic
Server example:
import os
from typing import AsyncGenerator, cast
from langchain_core.prompts import ChatPromptTemplate
from langchain_anthropic import ChatAnthropic
from langchain_core.messages import AIMessageChunk
from fastapi import FastAPI
from fastapi.responses import StreamingResponse
from langchain_vercel_adapters import (
serialize_to_data_stream_protocol,
VercelTypes as V
)
ANTHROPIC_API_KEY = os.environ.get("ANTHROPIC_API_KEY")
app = FastAPI()
llm = ChatAnthropic(
model_name="claude-3-sonnet-20240229",
api_key=ANTHROPIC_API_KEY,
)
async def stream_joke(topic: str) -> AsyncGenerator[AIMessageChunk, None]:
prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a joke teller. You are given a topic and you need to tell a joke about it."),
("user", "tell me a joke about {topic}"),
]
)
chain = prompt | llm
async for event in chain.astream({"topic": topic}):
event = cast(AIMessageChunk, event)
yield event
@app.post("/api/chat/{topic}")
async def handle_chat_data(request: V.ChatMessages, topic: str):
messages = request.messages
stream = stream_joke(topic)
stream = serialize_to_data_stream_protocol(stream)
response = StreamingResponse(stream, media_type="text/event-stream")
response.headers['x-vercel-ai-data-stream'] = 'v1'
return response
Metadata
Release files for langchain-vercel-adapters 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| langchain_vercel_adapters-0.1.0.tar.gz | 62.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| langchain_vercel_adapters-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 70.6 kB
Release files / langchain_vercel_adapters-0.1.0.tar.gz
| Download URL | langchain_vercel_adapters-0.1.0.tar.gz |
|---|---|
| Size | 62.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3c86802e8f7a779099a31427eb8d8493edac24ee78d756509893a57526b5b964
|
|
BLAKE2b-256 checksum How to use checksums |
d8c4d92437f31e94fffe754049b4950fef1a94156e408caaeb2adee8ce0d13d9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.7.3
|
Release files / langchain_vercel_adapters-0.1.0-py3-none-any.whl
| Download URL | langchain_vercel_adapters-0.1.0-py3-none-any.whl |
|---|---|
| Size | 8.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
23733ae9e0c2097e0b86966a52e58eb2a37cf57e87a67f985354dca50e13c9b6
|
|
BLAKE2b-256 checksum How to use checksums |
a9b3d8d4e62e907cb358407745ca17488c914c3bc4cf0d681583a8423a96341a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.7.3
|