Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
51 lines (40 loc) · 1.38 KB
/
Copy pathmain.py
File metadata and controls
51 lines (40 loc) · 1.38 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
51
import sys
import os
sys.path.insert(0, os.path.dirname(__file__))
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from pydantic import BaseModel
from langchain_core.messages import HumanMessage
from graph import graph, DEFAULT_PROFILE
app = FastAPI()
class ChatRequest(BaseModel):
user_id: str
message: str
@app.get("/", response_class=HTMLResponse)
def index():
html_path = os.path.join(os.path.dirname(__file__), "static", "index.html")
with open(html_path, encoding="utf-8") as f:
return f.read()
@app.post("/chat")
def chat(req: ChatRequest):
state = {
"messages": [HumanMessage(content=req.message)],
}
result = graph.invoke(
state,
config={"configurable": {"thread_id": req.user_id}},
)
return {"reply": result["reply"]}
@app.get("/debug/{user_id}")
def debug(user_id: str):
config = {"configurable": {"thread_id": user_id}}
current_state = graph.get_state(config)
if current_state.values:
msgs = current_state.values.get("messages", [])
return {
"msg_count": len(msgs),
"messages": [{"role": m.type, "content": m.content[:80]} for m in msgs],
"user_profile": current_state.values.get("user_profile", {}),
"summary": current_state.values.get("summary", ""),
}
return {"msg_count": 0, "messages": [], "user_profile": {}}