-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds playground chat widget for message list inputs, additional globa…
…l callback for chunks (#489) Co-authored-by: Eugene Yurtsev <[email protected]>
- Loading branch information
1 parent
8ea0cb4
commit ce727fc
Showing
11 changed files
with
426 additions
and
313 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#!/usr/bin/env python | ||
"""Example of a simple chatbot that just passes current conversation | ||
state back and forth between server and client. | ||
""" | ||
from typing import List, Union | ||
|
||
from fastapi import FastAPI | ||
from fastapi.middleware.cors import CORSMiddleware | ||
from langchain.chat_models import ChatAnthropic | ||
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage | ||
from langchain_core.output_parsers import StrOutputParser | ||
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder | ||
|
||
from langserve import add_routes | ||
from langserve.pydantic_v1 import BaseModel, Field | ||
|
||
app = FastAPI( | ||
title="LangChain Server", | ||
version="1.0", | ||
description="Spin up a simple api server using Langchain's Runnable interfaces", | ||
) | ||
|
||
|
||
# Set all CORS enabled origins | ||
app.add_middleware( | ||
CORSMiddleware, | ||
allow_origins=["*"], | ||
allow_credentials=True, | ||
allow_methods=["*"], | ||
allow_headers=["*"], | ||
expose_headers=["*"], | ||
) | ||
|
||
|
||
# Declare a chain | ||
prompt = ChatPromptTemplate.from_messages( | ||
[ | ||
("system", "You are a helpful assisstant named Cob."), | ||
MessagesPlaceholder(variable_name="messages"), | ||
] | ||
) | ||
|
||
chain = prompt | ChatAnthropic(model="claude-2") | StrOutputParser() | ||
|
||
|
||
class InputChat(BaseModel): | ||
"""Input for the chat endpoint.""" | ||
|
||
messages: List[Union[HumanMessage, AIMessage, SystemMessage]] = Field( | ||
..., | ||
description="The chat messages representing the current conversation.", | ||
extra={"widget": {"type": "chat", "input": "messages"}}, | ||
) | ||
|
||
|
||
add_routes( | ||
app, | ||
chain.with_types(input_type=InputChat), | ||
) | ||
|
||
if __name__ == "__main__": | ||
import uvicorn | ||
|
||
uvicorn.run(app, host="localhost", port=8000) |
File renamed without changes.
Oops, something went wrong.