self_check.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import logging
  2. from fastapi import APIRouter
  3. from pydantic import BaseModel
  4. from typing import Any, Dict, Optional
  5. from app.graphs.self_check_analysis_graph import get_graph
  6. from app.graphs.self_check_trend_graph import get_trend_graph
  7. logger = logging.getLogger(__name__)
  8. router = APIRouter(prefix="/api/v1", tags=["self-check"])
  9. class SelfCheckAnalysisRequest(BaseModel):
  10. scores: Dict[str, Any]
  11. question_ids: list
  12. user_id: int
  13. recent_history: Optional[list] = None
  14. class SelfCheckTrendRequest(BaseModel):
  15. history: list
  16. user_id: int
  17. @router.post("/self-check/analysis")
  18. async def self_check_analysis(req: SelfCheckAnalysisRequest):
  19. graph = get_graph()
  20. state = {
  21. "scores": req.scores,
  22. "question_ids": req.question_ids,
  23. "user_id": req.user_id,
  24. "recent_history": req.recent_history or [],
  25. "advice": None,
  26. "error": None,
  27. }
  28. result = await graph.ainvoke(state)
  29. advice = result.get("advice") or {}
  30. return {
  31. "advice_json": advice.get("advice_json"),
  32. "fallback_used": advice.get("fallback_used", True),
  33. "error": result.get("error"),
  34. }
  35. @router.post("/self-check/trend")
  36. async def self_check_trend(req: SelfCheckTrendRequest):
  37. graph = get_trend_graph()
  38. state = {"history": req.history, "user_id": req.user_id, "insight": None, "error": None}
  39. result = await graph.ainvoke(state)
  40. insight = result.get("insight") or {}
  41. return {
  42. "aiInsight": insight.get("aiInsight", ""),
  43. "trendSummary": insight.get("trendSummary", ""),
  44. "error": result.get("error"),
  45. }