chat.py 850 B

12345678910111213141516171819202122232425
  1. from fastapi import APIRouter, HTTPException
  2. from app.services.qa import get_answer, chat_history
  3. from app.schemas.chat import ChatRequest, ChatResponse
  4. import time
  5. router = APIRouter()
  6. @router.post("/chat/")
  7. async def chat(request: ChatRequest, session_id: str = "default_session"):
  8. start_time = time.time()
  9. try:
  10. answer = get_answer(session_id, request.message)
  11. latency_ms = int((time.time() - start_time) * 1000)
  12. return ChatResponse(answer=answer, latency_ms=latency_ms)
  13. except Exception as e:
  14. raise HTTPException(status_code=500, detail=f"Ollama call failed with error: {str(e)}")
  15. @router.get("/health")
  16. async def health():
  17. return {"status": "healthy"}
  18. @router.get("/sessions")
  19. async def list_sessions():
  20. return {"sessions": {sid: len(history) for sid, history in chat_history.items()}}