-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.py
74 lines (60 loc) · 2.39 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import os
import logging
from dotenv import load_dotenv
from fastapi import FastAPI, Request, WebSocket, WebSocketDisconnect
from fastapi.templating import Jinja2Templates
from schemas import ChatResponse
from callback import StreamingLLMCallbackHandler
from websockets.exceptions import ConnectionClosedOK
from query_data import get_chain
# Load the environment variables from the .env file
load_dotenv()
# Access the variables using os.environ
openai_api_key = os.environ.get("OPENAI_API_KEY")
templates = Jinja2Templates(directory="templates")
app = FastAPI()
@app.get("/")
async def index(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
@app.get("/test")
async def test_chat(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
stream_handler = StreamingLLMCallbackHandler(websocket)
qa_chain = get_chain(stream_handler)
while True:
try:
# Receive and send back the client message
user_msg = await websocket.receive_text()
resp = ChatResponse(sender="human", message=user_msg, type="stream")
await websocket.send_json(resp.dict())
# Construct a response
start_resp = ChatResponse(sender="bot", message="", type="start")
await websocket.send_json(start_resp.dict())
# Send the message to the chain and feed the response back to the client
output = await qa_chain.acall(
{
"input": user_msg,
}
)
# Send the end-response back to the client
end_resp = ChatResponse(sender="bot", message="", type="end")
await websocket.send_json(end_resp.dict())
except WebSocketDisconnect:
logging.info("WebSocketDisconnect")
# TODO try to reconnect with back-off
break
except ConnectionClosedOK:
logging.info("ConnectionClosedOK")
# TODO handle this?
break
except Exception as e:
logging.error(e)
resp = ChatResponse(
sender="bot",
message="Sorry, something went wrong. Try again.",
type="error",
)
await websocket.send_json(resp.dict())