Skip to main content

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

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


Download files

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

Source Distribution

langchain_vercel_adapters-0.1.0.tar.gz (62.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langchain_vercel_adapters-0.1.0-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file langchain_vercel_adapters-0.1.0.tar.gz.

File metadata

File hashes

Hashes for langchain_vercel_adapters-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3c86802e8f7a779099a31427eb8d8493edac24ee78d756509893a57526b5b964
MD5 c23615318533ab614ef0269138a34c7b
BLAKE2b-256 d8c4d92437f31e94fffe754049b4950fef1a94156e408caaeb2adee8ce0d13d9

See more details on using hashes here.

File details

Details for the file langchain_vercel_adapters-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for langchain_vercel_adapters-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 23733ae9e0c2097e0b86966a52e58eb2a37cf57e87a67f985354dca50e13c9b6
MD5 ab0e53d2f584ef6df8c655c6bff405f3
BLAKE2b-256 a9b3d8d4e62e907cb358407745ca17488c914c3bc4cf0d681583a8423a96341a

See more details on using hashes here.

Supported by

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