| 12345678910111213141516171819202122232425 |
- from fastapi import APIRouter, HTTPException
- from app.services.qa import get_answer, chat_history
- from app.schemas.chat import ChatRequest, ChatResponse
- import time
- router = APIRouter()
- @router.post("/chat/")
- async def chat(request: ChatRequest, session_id: str = "default_session"):
- start_time = time.time()
-
- try:
- answer = get_answer(session_id, request.message)
- latency_ms = int((time.time() - start_time) * 1000)
- return ChatResponse(answer=answer, latency_ms=latency_ms)
- except Exception as e:
- raise HTTPException(status_code=500, detail=f"Ollama call failed with error: {str(e)}")
- @router.get("/health")
- async def health():
- return {"status": "healthy"}
- @router.get("/sessions")
- async def list_sessions():
- return {"sessions": {sid: len(history) for sid, history in chat_history.items()}}
|