Spaces:
Sleeping
Sleeping
| """ | |
| Medical Query Router for RAG AI Advisor | |
| """ | |
| import asyncio | |
| from fastapi import APIRouter, HTTPException | |
| from fastapi.responses import StreamingResponse | |
| import sys | |
| import os | |
| # Add src to path for imports | |
| sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) | |
| from core.agent import safe_run_agent, safe_run_agent_streaming | |
| router = APIRouter(tags=["medical"]) | |
| async def ask(query: str, session_id: str = "default"): | |
| """ | |
| Process a medical query - agent decides which tools to use | |
| Args: | |
| query: The medical question or query | |
| session_id: Optional session identifier for conversation continuity (default: "default") | |
| """ | |
| try: | |
| response = await safe_run_agent(user_input=query, session_id=session_id) | |
| return {"response": response, "session_id": session_id} | |
| except Exception as e: | |
| raise HTTPException( | |
| status_code=500, | |
| detail=f"Error processing medical query: {str(e)}" | |
| ) | |
| async def ask_stream(query: str, session_id: str = "default"): | |
| """ | |
| Process a medical query with streaming response - agent decides which tools to use | |
| Args: | |
| query: The medical question or query | |
| session_id: Optional session identifier for conversation continuity (default: "default") | |
| """ | |
| async def event_stream(): | |
| try: | |
| chunk_buffer = "" | |
| async for chunk in safe_run_agent_streaming(user_input=query, session_id=session_id): | |
| chunk_buffer += chunk | |
| # Send chunks in reasonable sizes for smoother streaming | |
| if len(chunk_buffer) >= 10: # Adjust this value as needed | |
| yield chunk_buffer | |
| chunk_buffer = "" | |
| await asyncio.sleep(0.01) # Small delay for smoother streaming | |
| # Send any remaining content | |
| if chunk_buffer: | |
| yield chunk_buffer | |
| except Exception as e: | |
| yield f"Error: {str(e)}" | |
| return StreamingResponse(event_stream(), media_type="text/markdown") | |