Download src/api/chat.py from UsmanAmeen/Physical-AI-Docusaurus-Backend: direct link, hf CLI and curl.
- Browser
- Download file 993 Bytes
-
https://huggingface.co/spaces/UsmanAmeen/Physical-AI-Docusaurus-Backend/resolve/main/src/api/chat.py
- Command line
-
hf download hf://spaces/UsmanAmeen/Physical-AI-Docusaurus-Backend/src/api/chat.py
-
curl -L -o chat.py https://huggingface.co/spaces/UsmanAmeen/Physical-AI-Docusaurus-Backend/resolve/main/src/api/chat.py
993 Bytes
| 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 | |
| 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.") | |