-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
41 lines (29 loc) · 1.04 KB
/
Copy pathmain.py
File metadata and controls
41 lines (29 loc) · 1.04 KB
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
import asyncio
from contextlib import asynccontextmanager
from fastapi import FastAPI, Query
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from sse_starlette.sse import EventSourceResponse
from rag import initialize_rag
@asynccontextmanager
async def lifespan(app: FastAPI):
app.state.rag_chain = initialize_rag()
yield
app = FastAPI(title="Lab Assistant AI", lifespan=lifespan)
app.mount("/static", StaticFiles(directory="static"), name="static")
@app.get("/")
async def index():
return FileResponse("static/index.html")
@app.get("/chat")
async def chat(query: str = Query(..., min_length=1)):
chain = app.state.rag_chain
async def event_generator():
try:
async for token in chain.astream(query):
yield {"data": token}
await asyncio.sleep(0)
yield {"data": "[DONE]"}
except Exception as e:
yield {"data": f"Error: {str(e)}"}
yield {"data": "[DONE]"}
return EventSourceResponse(event_generator())