-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmain.py
55 lines (43 loc) · 1.29 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import asyncio
from typing import AsyncIterable
from dotenv import load_dotenv
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import StreamingResponse
from langchain.callbacks import AsyncIteratorCallbackHandler
from langchain.chat_models import ChatOpenAI
from langchain.schema import HumanMessage
from pydantic import BaseModel
load_dotenv()
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class Message(BaseModel):
content: str
async def send_message(content: str) -> AsyncIterable[str]:
callback = AsyncIteratorCallbackHandler()
model = ChatOpenAI(
streaming=True,
verbose=True,
callbacks=[callback],
)
task = asyncio.create_task(
model.agenerate(messages=[[HumanMessage(content=content)]])
)
try:
async for token in callback.aiter():
yield token
except Exception as e:
print(f"Caught exception: {e}")
finally:
callback.done.set()
await task
@app.post("/stream_chat/")
async def stream_chat(message: Message):
generator = send_message(message.content)
return StreamingResponse(generator, media_type="text/event-stream")