Download app/api/v1/ask.py from GodRad/rag_saas: direct link, hf CLI and curl.
- Browser
- Download file 1.29 kB
-
https://huggingface.co/spaces/GodRad/rag_saas/resolve/main/app/api/v1/ask.py
- Command line
-
hf download hf://spaces/GodRad/rag_saas/app/api/v1/ask.py
-
curl -L -o ask.py https://huggingface.co/spaces/GodRad/rag_saas/resolve/main/app/api/v1/ask.py
1.29 kB
| from fastapi import APIRouter, Depends, HTTPException | |
| from typing import List, Optional | |
| from pydantic import BaseModel | |
| from app.api import deps | |
| from app.services.retrieval import RetrievalService | |
| from app.services.generation import GenerationService | |
| from app.api.v1.admin import increment_metric | |
| router = APIRouter() | |
| retrieval_service = RetrievalService() | |
| generation_service = GenerationService() | |
| class AskRequest(BaseModel): | |
| query: str | |
| strict_citations: bool = True | |
| class AskResponse(BaseModel): | |
| answer: str | |
| citations: List[dict] = [] | |
| confidence: float | |
| async def ask_question(request: AskRequest, tenant_id: str = Depends(deps.get_tenant_id)): | |
| # 1. Retrieve context | |
| results = await retrieval_service.retrieve(tenant_id, request.query) | |
| increment_metric("retrieval_total") | |
| if not results: | |
| return AskResponse( | |
| answer="I couldn't find any relevant information to answer your question.", | |
| citations=[], | |
| confidence=0.0 | |
| ) | |
| # 2. Generate answer | |
| answer_data = await generation_service.generate_answer(request.query, results) | |
| increment_metric("generation_total") | |
| return AskResponse(**answer_data) | |