-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFastAPI.py
More file actions
48 lines (34 loc) · 1.21 KB
/
Copy pathFastAPI.py
File metadata and controls
48 lines (34 loc) · 1.21 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
try:
from dotenv import load_dotenv
load_dotenv()
except ImportError: # pragma: no cover
pass
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, Field
from fact_check import DEFAULT_TOP_K, run_fact_check
app = FastAPI(title="PaleoFactCheck API")
class QueryRequest(BaseModel):
query: str = Field(..., min_length=1, description="Paleontology claim to verify")
top_k: int = Field(DEFAULT_TOP_K, ge=1, le=50, description="Number of Chroma chunks to retrieve")
class QueryResponse(BaseModel):
answer: str
verdict: str
sources: list[str]
claim: str
@app.get("/health")
def health_check():
return {"status": "ok"}
@app.post("/ask", response_model=QueryResponse)
def ask_question(request: QueryRequest):
try:
result = run_fact_check(request.query, top_k=request.top_k)
except FileNotFoundError as exc:
raise HTTPException(status_code=503, detail=str(exc)) from exc
if not result.claim:
raise HTTPException(status_code=400, detail="Claim must not be empty after normalization.")
return QueryResponse(
answer=result.as_text(),
verdict=result.verdict,
sources=result.sources,
claim=result.claim,
)