-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
50 lines (39 loc) · 1.34 KB
/
main.py
File metadata and controls
50 lines (39 loc) · 1.34 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
42
43
44
45
46
47
48
49
50
# main.py
from fastapi import HTTPException
from typing import List
from fastapi import FastAPI
from fastapi.responses import StreamingResponse, PlainTextResponse
from pydantic import BaseModel
from rag_handler_milvus import stream_rag_answer, get_chat_history
from extract_topic import extract_topics_from_text
app = FastAPI()
class Question(BaseModel):
user_id: str
question: str
is_new_topic: bool
keywords: List[str]
class TextInput(BaseModel):
text: str
@app.post("/chat")
async def ask_question(payload: Question):
if not payload.user_id or not payload.question:
raise HTTPException(status_code=400, detail="user_id and question are required.")
user_id = payload.user_id
question = payload.question
is_new_topic = payload.is_new_topic
keywords = payload.keywords
stream_generator = await stream_rag_answer(user_id, question, is_new_topic)
return StreamingResponse(
content=stream_generator(),
media_type="text/plain"
)
@app.post("/subject")
async def extract_topic(input: TextInput):
return await extract_topics_from_text(input.text)
@app.get("/history/{user_id}")
async def get_history(user_id: str):
history = await get_chat_history(user_id)
return history
@app.get("/health")
async def check_health():
return PlainTextResponse(content="ok", status_code=200)