Add your description here
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file langchain_vercel_adapters-0.1.0.tar.gz.
File metadata
- Download URL: langchain_vercel_adapters-0.1.0.tar.gz
- Upload date:
- Size: 62.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3c86802e8f7a779099a31427eb8d8493edac24ee78d756509893a57526b5b964
|
|
| MD5 |
c23615318533ab614ef0269138a34c7b
|
|
| BLAKE2b-256 |
d8c4d92437f31e94fffe754049b4950fef1a94156e408caaeb2adee8ce0d13d9
|
File details
Details for the file langchain_vercel_adapters-0.1.0-py3-none-any.whl.
File metadata
- Download URL: langchain_vercel_adapters-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
23733ae9e0c2097e0b86966a52e58eb2a37cf57e87a67f985354dca50e13c9b6
|
|
| MD5 |
ab0e53d2f584ef6df8c655c6bff405f3
|
|
| BLAKE2b-256 |
a9b3d8d4e62e907cb358407745ca17488c914c3bc4cf0d681583a8423a96341a
|