junaid17 commited on
Commit
21ca625
·
verified ·
1 Parent(s): b0c4dde

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -33
app.py CHANGED
@@ -79,42 +79,49 @@ async def upload_document(
79
 
80
  # ... (keep existing imports) ...
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 ---------------- #
 
79
 
80
  # ... (keep existing imports) ...
81
 
82
+ @app.post("/chat/stream")
83
+ async def chat_stream_endpoint(request: ChatRequest):
84
  """
85
+ Streaming Chat Endpoint.
86
+ Streams tokens/chunks as they are generated by LangGraph.
87
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
+ async def event_generator():
90
+ try:
91
+ config = {"configurable": {"thread_id": request.thread_id}}
92
+
93
+ inputs = {
94
+ "query": request.query,
95
+ "RAG": request.use_rag,
96
+ "web_search": request.use_web,
97
+ "model_name": request.model_name,
98
+ "context": [],
99
+ "metadata": [],
100
+ "web_context": "",
101
+ }
102
+
103
+ async for event in rag_app.astream(inputs, config=config, stream_mode="values"):
104
+ if "response" in event:
105
+ msg = event["response"][-1]
106
+
107
+ if hasattr(msg, "content") and msg.content:
108
+ chunk = {
109
+ "type": "chunk",
110
+ "content": msg.content
111
+ }
112
+ yield json.dumps(chunk) + "\n"
113
+
114
+ # signal end of stream
115
+ yield json.dumps({"type": "done"}) + "\n"
116
+
117
+ except Exception as e:
118
+ error_chunk = {"type": "error", "message": str(e)}
119
+ yield json.dumps(error_chunk) + "\n"
120
+
121
+ return StreamingResponse(
122
+ event_generator(),
123
+ media_type="text/plain"
124
+ )
125
 
126
 
127
  # ---------------- STT ---------------- #