57 lines
1.9 KiB
Python
57 lines
1.9 KiB
Python
"""BaoDan Workflow 调用封装。"""
|
||
import requests
|
||
from flask import current_app
|
||
|
||
|
||
class WorkflowHelper:
|
||
"""Workflow API 调用封装。"""
|
||
|
||
def run_workflow(self, inputs: dict, user_id: str) -> dict:
|
||
"""
|
||
执行 Workflow 获取推荐方案。
|
||
|
||
Args:
|
||
inputs: 客户信息和保险需求
|
||
user_id: 用户 ID
|
||
|
||
Returns:
|
||
Workflow 执行结果
|
||
"""
|
||
base_url = current_app.config.get("BAODAN_API_URL", "http://localhost:5001")
|
||
api_key = current_app.config.get("BAODAN_WORKFLOW_API_KEY", "")
|
||
|
||
if not api_key:
|
||
return {"code": 5001, "message": "Workflow API Key 未配置"}
|
||
|
||
try:
|
||
resp = requests.post(
|
||
f"{base_url}/v1/workflows/run",
|
||
json={
|
||
"inputs": inputs,
|
||
"response_mode": "blocking",
|
||
"user": f"recommend_{user_id}",
|
||
},
|
||
headers={"Authorization": f"Bearer {api_key}"},
|
||
timeout=120,
|
||
)
|
||
|
||
if resp.status_code != 200:
|
||
return {"code": 5001, "message": f"Workflow 调用失败: HTTP {resp.status_code}"}
|
||
|
||
result = resp.json()
|
||
|
||
# 解析 Workflow 输出
|
||
if result.get("status") == "succeeded":
|
||
outputs = result.get("data", {}).get("outputs", {})
|
||
return {"code": 0, "data": outputs}
|
||
else:
|
||
error = result.get("error", "未知错误")
|
||
return {"code": 5001, "message": f"Workflow 执行失败: {error}"}
|
||
|
||
except requests.Timeout:
|
||
return {"code": 5001, "message": "Workflow 调用超时(120秒)"}
|
||
except requests.RequestException as e:
|
||
return {"code": 5001, "message": f"Workflow 调用异常: {str(e)}"}
|
||
except Exception as e:
|
||
return {"code": 9999, "message": f"未知错误: {str(e)}"}
|