Spaces:
Sleeping
Sleeping
Commit ·
b965fde
1
Parent(s): 89ca989
Add /chat/stream endpoint for Flutter compatibility
Browse files
app.py
CHANGED
|
@@ -17,7 +17,9 @@ import traceback
|
|
| 17 |
|
| 18 |
from fastapi import FastAPI, HTTPException
|
| 19 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 20 |
from pydantic import BaseModel
|
|
|
|
| 21 |
import google.generativeai as genai
|
| 22 |
|
| 23 |
from supabase_client import SupabaseClient
|
|
@@ -306,6 +308,47 @@ async def delete_session(session_id: str):
|
|
| 306 |
raise HTTPException(500, str(e))
|
| 307 |
|
| 308 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 309 |
if __name__ == "__main__":
|
| 310 |
import uvicorn
|
| 311 |
logger.info("Starting AGROW Chatbot Service")
|
|
|
|
| 17 |
|
| 18 |
from fastapi import FastAPI, HTTPException
|
| 19 |
from fastapi.middleware.cors import CORSMiddleware
|
| 20 |
+
from fastapi.responses import StreamingResponse
|
| 21 |
from pydantic import BaseModel
|
| 22 |
+
import asyncio
|
| 23 |
import google.generativeai as genai
|
| 24 |
|
| 25 |
from supabase_client import SupabaseClient
|
|
|
|
| 308 |
raise HTTPException(500, str(e))
|
| 309 |
|
| 310 |
|
| 311 |
+
@app.post("/chat/stream")
|
| 312 |
+
async def chat_stream(request: ChatRequest):
|
| 313 |
+
"""Stream chat response for Flutter app."""
|
| 314 |
+
logger.info(f"Stream chat - Session: {request.session_id}")
|
| 315 |
+
|
| 316 |
+
try:
|
| 317 |
+
history = supabase.get_messages(request.session_id)
|
| 318 |
+
|
| 319 |
+
supabase.add_message(
|
| 320 |
+
session_id=request.session_id,
|
| 321 |
+
role="user",
|
| 322 |
+
content=request.message
|
| 323 |
+
)
|
| 324 |
+
|
| 325 |
+
response_text, context_used = generate_response(
|
| 326 |
+
request.message, history, request.field_context
|
| 327 |
+
)
|
| 328 |
+
|
| 329 |
+
assistant_msg_id = supabase.add_message(
|
| 330 |
+
session_id=request.session_id,
|
| 331 |
+
role="assistant",
|
| 332 |
+
content=response_text,
|
| 333 |
+
context_used=context_used
|
| 334 |
+
)
|
| 335 |
+
|
| 336 |
+
supabase.update_session_timestamp(request.session_id)
|
| 337 |
+
|
| 338 |
+
async def stream_response():
|
| 339 |
+
yield f"data: {json.dumps({'type': 'metadata', 'session_id': request.session_id, 'message_id': assistant_msg_id})}\n\n"
|
| 340 |
+
for i in range(0, len(response_text), 15):
|
| 341 |
+
yield f"data: {json.dumps({'type': 'chunk', 'text': response_text[i:i+15]})}\n\n"
|
| 342 |
+
await asyncio.sleep(0.03)
|
| 343 |
+
yield f"data: {json.dumps({'type': 'done', 'full_text': response_text})}\n\n"
|
| 344 |
+
|
| 345 |
+
return StreamingResponse(stream_response(), media_type="text/event-stream")
|
| 346 |
+
|
| 347 |
+
except Exception as e:
|
| 348 |
+
logger.error(f"Stream error: {e}")
|
| 349 |
+
raise HTTPException(500, str(e))
|
| 350 |
+
|
| 351 |
+
|
| 352 |
if __name__ == "__main__":
|
| 353 |
import uvicorn
|
| 354 |
logger.info("Starting AGROW Chatbot Service")
|