File size: 993 Bytes
39af4d2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
from typing import Optional
from src.services.rag_service import rag_service
router = APIRouter()
class ChatRequest(BaseModel):
question: str
highlighted_text: Optional[str] = None
class ChatResponse(BaseModel):
answer: str
@router.post("/chat", response_model=ChatResponse, tags=["Chat"])
async def chat_endpoint(request: ChatRequest):
"""
Handles user questions by passing them to the RAG service to generate an answer.
"""
try:
answer = rag_service.get_answer(
question=request.question,
highlighted_text=request.highlighted_text
)
return ChatResponse(answer=answer)
except Exception as e:
# The service layer should handle its own errors, but we have a fallback.
print(f"An unexpected error occurred in the chat endpoint: {e}")
raise HTTPException(status_code=500, detail="An internal error occurred.")
|