UsmanAmeen's picture
ready to deploy backend
39af4d2
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.")