langserve icon indicating copy to clipboard operation
langserve copied to clipboard

How to use langchain memory components such as "ConversationBufferWindowMemory" with Langserve

Open Omarks1 opened this issue 1 year ago • 1 comments

How can I utilize Langchain memory components like "ConversationBufferWindowMemory" in Langserve. I am using a chat history as mentioned in the agent_with_history example in the following way:

from fastapi import FastAPI
from typing import List
from langserve import add_routes
import uvicorn
from pydantic import BaseModel
from credentials import *
from LLm_credentials import *
from agents1 import Agent_Executor
from langchain.pydantic_v1 import BaseModel, Field
from langchain_core.messages import AIMessage, HumanMessage
from typing import Any, List, Union
from dotenv import load_dotenv
load_dotenv()


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


### The Schema Section
class Input(BaseModel):
    input: str
    chat_history: List[Union[HumanMessage, AIMessage]] = Field(
        ...,
        extra={"widget": {"type": "chat", "input": "input", "output": "output"}},)


class Output(BaseModel):
    output: Any


### The routes Section
add_routes(
    app,
    Agent_Executor.with_types(input_type=Input, output_type=Output),
    path="/agent")


### The Call Section
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="localhost", port=8000)

Omarks1 avatar Jun 06 '24 10:06 Omarks1