from fastapi import APIRouter, HTTPException from pydantic import BaseModel, Field from app.services.rag_service import RagService router = APIRouter() rag_service = RagService() class QueryRequest(BaseModel): query: str = Field(..., description="用户查询") top_k: int = Field(8, description="返回条数") class PathRequest(BaseModel): source: str target: str @router.post("/query") async def query(payload: QueryRequest): """优先调用 LightRAG,失败自动降级 Supabase""" result = await rag_service.query(payload.query, payload.top_k) return {"data": result} @router.post("/graph") async def graph(payload: QueryRequest): result = await rag_service.graph(payload.query) return {"data": result} @router.post("/path") async def path(payload: PathRequest): result = await rag_service.path(payload.source, payload.target) return {"data": result} @router.get("/health/full") async def health() -> dict: """最小化健康检查,后续补充依赖自检""" try: _ = await rag_service.query("ping", top_k=1) return {"status": "ok"} except Exception as exc: # noqa: BLE001 raise HTTPException(status_code=500, detail=str(exc))