generate_qa_pair.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. from fastapi import APIRouter, HTTPException
  2. from app.const import MAX_INPUT_CARD_IDS
  3. from app.endpoints.schema import QARequest, QAResponse, QAResponseBody
  4. from app.pipeline.workflow import Workflow
  5. router = APIRouter()
  6. @router.post("/generate_qa_pair", response_model=QAResponse)
  7. async def generate_qa_pair(request_body: QARequest):
  8. if len(request_body.card_ids) > MAX_INPUT_CARD_IDS:
  9. raise HTTPException(
  10. status_code=400,
  11. detail=f"Number of card IDs exceeds the maximum limit of {MAX_INPUT_CARD_IDS}.",
  12. )
  13. try:
  14. workflow = Workflow()
  15. input_args = {
  16. "dashboard_id": request_body.dashboard_id,
  17. "card_ids": request_body.card_ids,
  18. "bbk": request_body.bbk_id,
  19. "user_request": request_body.user_request or "",
  20. }
  21. result = await workflow.execute_workflow(input_args)
  22. qa_response = QAResponse(
  23. returnCode="SUCCESS",
  24. body=QAResponseBody(
  25. dashboard_id=request_body.dashboard_id,
  26. card_ids=request_body.card_ids,
  27. qa_pairs=result,
  28. bbk_id=request_body.bbk_id,
  29. ),
  30. )
  31. return qa_response
  32. except Exception as e:
  33. raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")