Update app.py
Browse files
app.py
CHANGED
|
@@ -81,49 +81,40 @@ async def upload_document(
|
|
| 81 |
|
| 82 |
@app.post("/chat")
|
| 83 |
async def chat_endpoint(request: ChatRequest):
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
"
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
event_generator(),
|
| 119 |
-
media_type="text/event-stream",
|
| 120 |
-
headers={
|
| 121 |
-
"Cache-Control": "no-cache",
|
| 122 |
-
"Connection": "keep-alive",
|
| 123 |
-
"Content-Encoding": "none", # [TRICK 2] Disable compression
|
| 124 |
-
"X-Accel-Buffering": "no",
|
| 125 |
-
},
|
| 126 |
-
)
|
| 127 |
|
| 128 |
|
| 129 |
# ---------------- STT ---------------- #
|
|
|
|
| 81 |
|
| 82 |
@app.post("/chat")
|
| 83 |
async def chat_endpoint(request: ChatRequest):
|
| 84 |
+
"""
|
| 85 |
+
Standard Chat Endpoint (Non-Streaming).
|
| 86 |
+
Waits for the LLM to finish and returns the full JSON response.
|
| 87 |
+
"""
|
| 88 |
+
try:
|
| 89 |
+
# 1. Setup Config & Inputs
|
| 90 |
+
config = {"configurable": {"thread_id": request.thread_id}}
|
| 91 |
+
|
| 92 |
+
inputs = {
|
| 93 |
+
"query": request.query,
|
| 94 |
+
"RAG": request.use_rag,
|
| 95 |
+
"web_search": request.use_web,
|
| 96 |
+
"model_name": request.model_name,
|
| 97 |
+
"context": [],
|
| 98 |
+
"metadata": [],
|
| 99 |
+
"web_context": "",
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
# 2. Invoke the Graph (Waits for completion)
|
| 103 |
+
# using ainvoke is better for FastAPI to prevent blocking the server
|
| 104 |
+
result = await rag_app.ainvoke(inputs, config=config)
|
| 105 |
+
|
| 106 |
+
# 3. Extract the last message (AI Response)
|
| 107 |
+
last_message = result['response'][-1]
|
| 108 |
+
|
| 109 |
+
# 4. Return standard JSON
|
| 110 |
+
return {
|
| 111 |
+
"response": last_message.content,
|
| 112 |
+
"thread_id": request.thread_id
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
except Exception as e:
|
| 116 |
+
print(f"Error generation response: {e}")
|
| 117 |
+
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
|
| 120 |
# ---------------- STT ---------------- #
|