complete_agent_flow_rule.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. """
  2. 完整的智能体工作流 (Complete Agent Flow)
  3. =====================================
  4. 此工作流整合了规划、大纲生成和指标计算四个核心智能体,实现完整的报告生成流程。
  5. 包含的智能体:
  6. 1. PlanningAgent (规划智能体) - 分析状态并做出决策
  7. 2. OutlineAgent (大纲生成智能体) - 生成报告结构和指标需求
  8. 3. MetricCalculationAgent (指标计算智能体) - 执行标准指标计算
  9. 4. RulesEngineMetricCalculationAgent (规则引擎指标计算智能体) - 执行规则引擎指标计算
  10. 工作流程:
  11. 1. 规划节点 → 分析当前状态,决定下一步行动
  12. 2. 大纲生成节点 → 生成报告大纲和指标需求
  13. 3. 指标判断节点 → 根据大纲确定需要计算的指标
  14. 4. 指标计算节点 → 执行具体的指标计算任务
  15. 技术特点:
  16. - 基于LangGraph的状态机工作流
  17. - 支持条件路由和状态管理
  18. - 完善的错误处理机制
  19. - 详细的执行日志记录
  20. 作者: Big Agent Team
  21. 版本: 1.0.0
  22. 创建时间: 2024-12-20
  23. """
  24. import asyncio
  25. from typing import Dict, Any, List
  26. from datetime import datetime
  27. from langgraph.graph import StateGraph, START, END
  28. from llmops.workflow_state import (
  29. IntegratedWorkflowState,
  30. create_initial_integrated_state,
  31. get_calculation_progress,
  32. update_state_with_outline_generation,
  33. update_state_with_planning_decision,
  34. update_state_with_data_classified,
  35. convert_numpy_types,
  36. )
  37. from llmops.agents.outline_agent import generate_report_outline
  38. from llmops.agents.planning_agent import plan_next_action
  39. from llmops.agents.rules_engine_metric_calculation_agent import RulesEngineMetricCalculationAgent
  40. from llmops.agents.data_manager import DataManager
  41. import os
  42. from llmops.agents.data_classify_agent import data_classify
  43. from llmops.config import DEEPSEEK_API_KEY
  44. class CompleteAgentFlow:
  45. """完整的智能体工作流"""
  46. def __init__(self, api_key: str, base_url: str = "https://api.deepseek.com"):
  47. """
  48. 初始化完整的工作流
  49. Args:
  50. api_key: DeepSeek API密钥
  51. base_url: DeepSeek API基础URL
  52. """
  53. self.api_key = api_key
  54. self.base_url = base_url
  55. # 初始规则引擎智能体
  56. self.rules_engine_agent = RulesEngineMetricCalculationAgent(api_key, base_url)
  57. # 创建工作流图
  58. self.workflow = self._create_workflow()
  59. def _create_workflow(self) -> StateGraph:
  60. """创建LangGraph工作流"""
  61. workflow = StateGraph(IntegratedWorkflowState)
  62. # 添加节点
  63. workflow.add_node("planning_node", self._planning_node)
  64. workflow.add_node("outline_generator", self._outline_generator_node)
  65. workflow.add_node("metric_calculator", self._metric_calculator_node)
  66. workflow.add_node("data_classify", self._data_classify_node)
  67. # 设置入口点
  68. workflow.set_entry_point("planning_node")
  69. # 添加条件边 - 基于规划决策路由
  70. workflow.add_conditional_edges(
  71. "planning_node",
  72. self._route_from_planning,
  73. {
  74. "outline_generator": "outline_generator",
  75. "metric_calculator": "metric_calculator",
  76. "data_classify": "data_classify",
  77. END: END
  78. }
  79. )
  80. # 从各个节点返回规划节点重新决策
  81. workflow.add_edge("data_classify", "planning_node")
  82. workflow.add_edge("outline_generator", "planning_node")
  83. workflow.add_edge("metric_calculator", "planning_node")
  84. return workflow
  85. def _route_from_planning(self, state: IntegratedWorkflowState) -> str:
  86. """
  87. 从规划节点路由到下一个节点
  88. Args:
  89. state: 当前状态
  90. Returns:
  91. 目标节点名称
  92. """
  93. print(f"\n🔍 [路由决策] 步骤={state['planning_step']}, "
  94. f"数据集分类打标数量={len(state.get('data_set_classified', []))}",
  95. f"大纲={state.get('outline_draft') is not None}, "
  96. f"指标需求={len(state.get('metrics_requirements', []))}")
  97. # 防止无限循环
  98. if state['planning_step'] > 30:
  99. print("⚠️ 规划步骤超过30次,强制结束流程")
  100. return END
  101. # 数据分类打标数量为0 → 分类打标
  102. if len(state.get("data_set_classified", [])) == 0:
  103. print("→ 路由到 data_classify(分类打标)")
  104. return "data_classify"
  105. # 如果大纲为空 → 生成大纲
  106. if not state.get("outline_draft"):
  107. print("→ 路由到 outline_generator(生成大纲)")
  108. return "outline_generator"
  109. # 如果指标需求为空但大纲已生成 → 评估指标需求
  110. if not state.get("metrics_requirements") and state.get("outline_draft"):
  111. print("→ 路由到 metric_evaluator(评估指标需求)")
  112. return "metric_evaluator"
  113. # 计算覆盖率
  114. progress = get_calculation_progress(state)
  115. coverage = progress["coverage_rate"]
  116. print(f" 指标覆盖率 = {coverage:.2%}")
  117. # 如果有待计算指标且覆盖率 < 100% → 计算指标
  118. if state.get("pending_metric_ids") and coverage < 1.0:
  119. print(f"→ 路由到 metric_calculator(计算指标,覆盖率={coverage:.2%})")
  120. return "metric_calculator"
  121. # 检查是否应该结束流程
  122. pending_ids = state.get("pending_metric_ids", [])
  123. failed_attempts = state.get("failed_metric_attempts", {})
  124. max_retries = 3
  125. # 计算还有哪些指标可以重试(未达到最大重试次数)
  126. retryable_metrics = [
  127. mid for mid in pending_ids
  128. if failed_attempts.get(mid, 0) < max_retries
  129. ]
  130. # 如果覆盖率 >= 80%,或者没有可重试的指标 → 结束流程
  131. if coverage >= 0.8 or not retryable_metrics:
  132. reason = "覆盖率达到80%" if coverage >= 0.8 else "没有可重试指标"
  133. print(f"→ 结束流程(覆盖率={coverage:.2%},原因:{reason})")
  134. return END
  135. # 默认返回规划节点
  136. return "planning_node"
  137. async def _planning_node(self, state: IntegratedWorkflowState) -> IntegratedWorkflowState:
  138. """规划节点:分析状态并做出决策"""
  139. try:
  140. print("🧠 正在执行规划分析...")
  141. # 使用规划智能体做出决策
  142. decision = await plan_next_action(
  143. question=state["question"],
  144. industry=state["industry"],
  145. current_state=state,
  146. api_key=self.api_key
  147. )
  148. # 更新状态
  149. new_state = update_state_with_planning_decision(state, {
  150. "decision": decision.decision,
  151. "next_route": self._decision_to_route(decision.decision),
  152. "metrics_to_compute": decision.metrics_to_compute
  153. })
  154. # 添加决策消息
  155. decision_msg = self._format_decision_message(decision)
  156. new_state["messages"].append({
  157. "role": "assistant",
  158. "content": decision_msg,
  159. "timestamp": datetime.now().isoformat()
  160. })
  161. print(f"✅ 规划决策完成:{decision.decision}")
  162. return convert_numpy_types(new_state)
  163. except Exception as e:
  164. print(f"❌ 规划节点执行失败: {e}")
  165. new_state = state.copy()
  166. new_state["errors"].append(f"规划节点错误: {str(e)}")
  167. return convert_numpy_types(new_state)
  168. async def _outline_generator_node(self, state: IntegratedWorkflowState) -> IntegratedWorkflowState:
  169. """大纲生成节点"""
  170. try:
  171. print("📝 正在生成报告大纲...")
  172. # 生成大纲(支持重试机制)
  173. outline = await generate_report_outline(
  174. question=state["question"],
  175. industry=state["industry"],
  176. sample_data=state["data_set"][:3], # 使用前3个样本
  177. api_key=self.api_key,
  178. max_retries=3, # 最多重试5次
  179. retry_delay=3.0 # 每次重试间隔3秒
  180. )
  181. # 更新状态
  182. new_state = update_state_with_outline_generation(state, outline)
  183. print(f"✅ 大纲生成完成:{outline.report_title}")
  184. print(f" 包含 {len(outline.sections)} 个章节,{len(outline.global_metrics)} 个指标需求")
  185. # 分析并打印AI的指标选择推理过程
  186. self._print_ai_selection_analysis(outline)
  187. return convert_numpy_types(new_state)
  188. except Exception as e:
  189. print(f"❌ 大纲生成失败: {e}")
  190. new_state = state.copy()
  191. new_state["errors"].append(f"大纲生成错误: {str(e)}")
  192. return convert_numpy_types(new_state)
  193. async def _data_classify_node(self, state: IntegratedWorkflowState) -> IntegratedWorkflowState:
  194. """数据分类打标节点"""
  195. try:
  196. print("📝 正在对数据进行分类打标...")
  197. # 对数据进行分类打标
  198. data_set_classified = await data_classify(
  199. industry=state["industry"],
  200. data_set=state["data_set"],
  201. file_name=state["file_name"]
  202. )
  203. # 更新状态
  204. new_state = update_state_with_data_classified(state, data_set_classified)
  205. print(f"✅ 数据分类打标完成,打标记录数: {len(data_set_classified)}")
  206. return convert_numpy_types(new_state)
  207. except Exception as e:
  208. print(f"❌ 数据分类打标失败: {e}")
  209. new_state = state.copy()
  210. new_state["errors"].append(f"数据分类打标错误: {str(e)}")
  211. return convert_numpy_types(new_state)
  212. def _print_ai_selection_analysis(self, outline):
  213. """打印AI指标选择的推理过程分析 - 完全通用版本"""
  214. print()
  215. print('╔══════════════════════════════════════════════════════════════════════════════╗')
  216. print('║ 🤖 AI指标选择分析 ║')
  217. print('╚══════════════════════════════════════════════════════════════════════════════╝')
  218. print()
  219. # 计算总指标数 - outline可能是字典格式,需要适配
  220. if hasattr(outline, 'sections'):
  221. # Pydantic模型格式
  222. total_metrics = sum(len(section.metrics_needed) for section in outline.sections)
  223. sections = outline.sections
  224. else:
  225. # 字典格式
  226. total_metrics = sum(len(section.get('metrics_needed', [])) for section in outline.get('sections', []))
  227. sections = outline.get('sections', [])
  228. # 获取可用指标总数(这里可以从状态或其他地方动态获取)
  229. available_count = 26 # 这个可以从API调用中动态获取
  230. print('📊 选择统计:')
  231. print(' ┌─────────────────────────────────────────────────────────────────────┐')
  232. print(' │ 系统可用指标: {}个 │ AI本次选择: {}个 │ 选择率: {:.1f}% │'.format(
  233. available_count, total_metrics, total_metrics/available_count*100 if available_count > 0 else 0))
  234. print(' └─────────────────────────────────────────────────────────────────────┘')
  235. print()
  236. print('📋 AI决策过程:')
  237. print(' 大模型已根据用户需求从{}个可用指标中选择了{}个最相关的指标。'.format(available_count, total_metrics))
  238. print(' 选择过程完全由大模型基于语义理解和业务逻辑进行,不涉及任何硬编码规则。')
  239. print()
  240. print('🔍 选择结果:')
  241. print(' • 总章节数: {}个'.format(len(sections)))
  242. print(' • 平均每章节指标数: {:.1f}个'.format(total_metrics/len(sections) if sections else 0))
  243. print(' • 选择策略: 基于用户需求的相关性分析')
  244. print()
  245. print('🎯 AI Agent核心能力:')
  246. print(' • 语义理解: 理解用户查询的业务意图和分析需求')
  247. print(' • 智能筛选: 从海量指标中挑选最相关的组合')
  248. print(' • 逻辑推理: 为每个分析维度提供充分的选择依据')
  249. print(' • 动态适配: 根据不同场景自动调整选择策略')
  250. print()
  251. print('💡 关键洞察:')
  252. print(' AI Agent通过大模型的推理能力,实现了超越传统规则引擎的智能化指标选择,')
  253. print(' 能够根据具体业务场景动态调整分析框架,确保分析的针对性和有效性。')
  254. print()
  255. async def _metric_calculator_node(self, state: IntegratedWorkflowState) -> IntegratedWorkflowState:
  256. """指标计算节点"""
  257. try:
  258. # 检查计算模式
  259. use_rules_engine_only = state.get("use_rules_engine_only", False)
  260. use_traditional_engine_only = state.get("use_traditional_engine_only", False)
  261. if use_rules_engine_only:
  262. print("🧮 正在执行规则引擎指标计算(专用模式)...")
  263. elif use_traditional_engine_only:
  264. print("🧮 正在执行传统引擎指标计算(专用模式)...")
  265. else:
  266. print("🧮 正在执行指标计算...")
  267. new_state = state.copy()
  268. # 使用规划决策指定的指标批次,如果没有指定则使用所有待计算指标
  269. current_batch = state.get("current_batch_metrics", [])
  270. if current_batch:
  271. pending_ids = current_batch
  272. print(f"🧮 本次计算批次包含 {len(pending_ids)} 个指标")
  273. else:
  274. pending_ids = state.get("pending_metric_ids", [])
  275. print(f"🧮 计算所有待计算指标,共 {len(pending_ids)} 个")
  276. if not pending_ids:
  277. print("⚠️ 没有待计算的指标")
  278. return convert_numpy_types(new_state)
  279. # 获取指标需求信息
  280. metrics_requirements = state.get("metrics_requirements", [])
  281. if not metrics_requirements:
  282. print("⚠️ 没有指标需求信息")
  283. return convert_numpy_types(new_state)
  284. # 计算成功和失败的指标
  285. successful_calculations = 0
  286. failed_calculations = 0
  287. # 遍历待计算的指标(创建副本避免修改时遍历的问题)
  288. for metric_id in pending_ids.copy():
  289. try:
  290. # 找到对应的指标需求
  291. metric_req = next((m for m in metrics_requirements if m.metric_id == metric_id), None)
  292. if not metric_req:
  293. # 修复:找不到指标需求时,创建临时的指标需求结构,避免跳过指标
  294. print(f"⚠️ 指标 {metric_id} 找不到需求信息,创建临时配置继续计算")
  295. metric_req = type('MetricRequirement', (), {
  296. 'metric_id': metric_id,
  297. 'metric_name': metric_id.replace('metric-', '') if metric_id.startswith('metric-') else metric_id,
  298. 'calculation_logic': f'计算 {metric_id}',
  299. 'required_fields': ['transactions'],
  300. 'dependencies': []
  301. })()
  302. print(f"🧮 计算指标: {metric_id} - {metric_req.metric_name}")
  303. # 根据模式决定使用哪种计算方式
  304. if use_rules_engine_only:
  305. # 只使用规则引擎计算
  306. use_rules_engine = True
  307. print(f" 使用规则引擎模式")
  308. elif use_traditional_engine_only:
  309. # 只使用传统引擎计算
  310. use_rules_engine = False
  311. print(f" 使用传统引擎模式")
  312. else:
  313. # 自动选择计算方式:优先使用规则引擎,只在规则引擎不可用时使用传统计算
  314. use_rules_engine = True # 默认使用规则引擎计算所有指标
  315. if use_rules_engine:
  316. # 使用规则引擎计算
  317. # 现在metric_id已经是知识ID,直接使用它作为配置名
  318. config_name = metric_id # metric_id 已经是知识ID,如 "metric-分析账户数量"
  319. intent_result = {
  320. "target_configs": [config_name],
  321. "intent_category": "指标计算"
  322. }
  323. print(f" 使用知识ID: {config_name}")
  324. # 将打好标的数据集传入指标计算函数中
  325. data_set_classified = state.get("data_set_classified", [])
  326. results = await self.rules_engine_agent.calculate_metrics(intent_result, data_set_classified)
  327. else:
  328. # 使用传统指标计算(模拟)
  329. # 这里简化处理,实际应该根据配置文件调用相应的API
  330. results = {
  331. "success": True,
  332. "results": [{
  333. "config_name": metric_req.metric_id,
  334. "result": {
  335. "success": True,
  336. "data": f"传统引擎计算结果:{metric_req.metric_name}",
  337. "value": 100.0 # 模拟数值
  338. }
  339. }]
  340. }
  341. # 处理计算结果
  342. calculation_success = False
  343. for result in results.get("results", []):
  344. if result.get("result", {}).get("success"):
  345. # 计算成功
  346. new_state["computed_metrics"][metric_id] = result["result"]
  347. successful_calculations += 1
  348. calculation_success = True
  349. print(f"✅ 指标 {metric_id} 计算成功")
  350. break # 找到一个成功的就算成功
  351. else:
  352. # 计算失败
  353. failed_calculations += 1
  354. print(f"❌ 指标 {metric_id} 计算失败")
  355. # 初始化失败尝试记录
  356. if "failed_metric_attempts" not in new_state:
  357. new_state["failed_metric_attempts"] = {}
  358. # 根据计算结果处理指标
  359. if calculation_success:
  360. # 计算成功:从待计算列表中移除
  361. if metric_id in new_state["pending_metric_ids"]:
  362. new_state["pending_metric_ids"].remove(metric_id)
  363. # 重置失败计数
  364. new_state["failed_metric_attempts"].pop(metric_id, None)
  365. else:
  366. # 计算失败:记录失败次数,不从待计算列表移除
  367. new_state["failed_metric_attempts"][metric_id] = new_state["failed_metric_attempts"].get(metric_id, 0) + 1
  368. max_retries = 3
  369. if new_state["failed_metric_attempts"][metric_id] >= max_retries:
  370. print(f"⚠️ 指标 {metric_id} 已达到最大重试次数 ({max_retries}),从待计算列表中移除")
  371. if metric_id in new_state["pending_metric_ids"]:
  372. new_state["pending_metric_ids"].remove(metric_id)
  373. except Exception as e:
  374. print(f"❌ 计算指标 {metric_id} 时发生异常: {e}")
  375. failed_calculations += 1
  376. # 初始化失败尝试记录
  377. if "failed_metric_attempts" not in new_state:
  378. new_state["failed_metric_attempts"] = {}
  379. # 记录失败次数
  380. new_state["failed_metric_attempts"][metric_id] = new_state["failed_metric_attempts"].get(metric_id, 0) + 1
  381. max_retries = 3
  382. if new_state["failed_metric_attempts"][metric_id] >= max_retries:
  383. print(f"⚠️ 指标 {metric_id} 异常已达到最大重试次数 ({max_retries}),从待计算列表中移除")
  384. if metric_id in new_state["pending_metric_ids"]:
  385. new_state["pending_metric_ids"].remove(metric_id)
  386. # 更新计算结果统计
  387. new_state["calculation_results"] = {
  388. "total_configs": len(pending_ids),
  389. "successful_calculations": successful_calculations,
  390. "failed_calculations": failed_calculations
  391. }
  392. # 添加消息
  393. if use_rules_engine_only:
  394. message_content = f"🧮 规则引擎指标计算完成:{successful_calculations} 成功,{failed_calculations} 失败"
  395. elif use_traditional_engine_only:
  396. message_content = f"🧮 传统引擎指标计算完成:{successful_calculations} 成功,{failed_calculations} 失败"
  397. else:
  398. message_content = f"🧮 指标计算完成:{successful_calculations} 成功,{failed_calculations} 失败"
  399. new_state["messages"].append({
  400. "role": "assistant",
  401. "content": message_content,
  402. "timestamp": datetime.now().isoformat()
  403. })
  404. if use_rules_engine_only:
  405. print(f"✅ 规则引擎指标计算完成:{successful_calculations} 成功,{failed_calculations} 失败")
  406. elif use_traditional_engine_only:
  407. print(f"✅ 传统引擎指标计算完成:{successful_calculations} 成功,{failed_calculations} 失败")
  408. else:
  409. print(f"✅ 指标计算完成:{successful_calculations} 成功,{failed_calculations} 失败")
  410. return convert_numpy_types(new_state)
  411. except Exception as e:
  412. print(f"❌ 指标计算节点失败: {e}")
  413. new_state = state.copy()
  414. new_state["errors"].append(f"指标计算错误: {str(e)}")
  415. return convert_numpy_types(new_state)
  416. def _decision_to_route(self, decision: str) -> str:
  417. """将规划决策转换为路由"""
  418. decision_routes = {
  419. "data_classify": "data_classify",
  420. "generate_outline": "outline_generator",
  421. "compute_metrics": "metric_calculator",
  422. "finalize_report": END # 直接结束流程
  423. }
  424. return decision_routes.get(decision, "planning_node")
  425. def _format_decision_message(self, decision: Any) -> str:
  426. """格式化决策消息"""
  427. try:
  428. decision_type = getattr(decision, 'decision', 'unknown')
  429. reasoning = getattr(decision, 'reasoning', '')
  430. if decision_type == "compute_metrics" and hasattr(decision, 'metrics_to_compute'):
  431. metrics = decision.metrics_to_compute
  432. return f"🧮 规划决策:计算 {len(metrics)} 个指标"
  433. elif decision_type == "finalize_report":
  434. return f"✅ 规划决策:生成最终报告"
  435. elif decision_type == "generate_outline":
  436. return f"📋 规划决策:生成大纲"
  437. else:
  438. return f"🤔 规划决策:{decision_type}"
  439. except:
  440. return "🤔 规划决策已完成"
  441. async def run_workflow(self, question: str, industry: str, data: List[Dict[str, Any]], file_name: str, session_id: str = None, use_rules_engine_only: bool = False, use_traditional_engine_only: bool = False) -> Dict[str, Any]:
  442. """
  443. 运行完整的工作流
  444. Args:
  445. question: 用户查询
  446. industry: 行业
  447. data: 数据集
  448. file_name: 数据文件名称
  449. session_id: 会话ID
  450. use_rules_engine_only: 是否只使用规则引擎指标计算
  451. use_traditional_engine_only: 是否只使用传统引擎指标计算
  452. Returns:
  453. 工作流结果
  454. """
  455. try:
  456. print("🚀 启动完整智能体工作流...")
  457. print(f"问题:{question}")
  458. print(f"行业:{industry}")
  459. print(f"数据文件:{file_name}")
  460. print(f"数据条数:{len(data)}")
  461. if use_rules_engine_only:
  462. print("计算模式:只使用规则引擎")
  463. elif use_traditional_engine_only:
  464. print("计算模式:只使用传统引擎")
  465. else:
  466. print("计算模式:标准模式")
  467. # 创建初始状态
  468. initial_state = create_initial_integrated_state(question, industry, data, file_name, session_id)
  469. # 设置计算模式标记
  470. if use_rules_engine_only:
  471. initial_state["use_rules_engine_only"] = True
  472. initial_state["use_traditional_engine_only"] = False
  473. elif use_traditional_engine_only:
  474. initial_state["use_rules_engine_only"] = False
  475. initial_state["use_traditional_engine_only"] = True
  476. else:
  477. initial_state["use_rules_engine_only"] = False
  478. initial_state["use_traditional_engine_only"] = False
  479. # 编译工作流
  480. app = self.workflow.compile()
  481. # 执行工作流
  482. result = await app.ainvoke(initial_state)
  483. print("✅ 工作流执行完成")
  484. return {
  485. "success": True,
  486. "result": result,
  487. "answer": result.get("answer"),
  488. "report": result.get("report_draft"),
  489. "session_id": result.get("session_id"),
  490. "execution_summary": {
  491. "planning_steps": result.get("planning_step", 0),
  492. "outline_generated": result.get("outline_draft") is not None,
  493. "metrics_computed": len(result.get("computed_metrics", {})),
  494. "completion_rate": result.get("completeness_score", 0)
  495. }
  496. }
  497. except Exception as e:
  498. print(f"❌ 工作流执行失败: {e}")
  499. return {
  500. "success": False,
  501. "error": str(e),
  502. "result": None
  503. }
  504. # 便捷函数
  505. async def run_complete_agent_flow(question: str, industry: str, data: List[Dict[str, Any]], file_name: str, api_key: str, session_id: str = None, use_rules_engine_only: bool = False, use_traditional_engine_only: bool = False) -> Dict[str, Any]:
  506. """
  507. 运行完整智能体工作流的便捷函数
  508. Args:
  509. question: 用户查询
  510. data: 数据集
  511. file_name: 数据文件名称
  512. api_key: API密钥
  513. session_id: 会话ID
  514. use_rules_engine_only: 是否只使用规则引擎指标计算
  515. use_traditional_engine_only: 是否只使用传统引擎指标计算
  516. Returns:
  517. 工作流结果
  518. """
  519. workflow = CompleteAgentFlow(api_key)
  520. return await workflow.run_workflow(question, industry, data, file_name, session_id, use_rules_engine_only, use_traditional_engine_only)
  521. # 主函数用于测试
  522. async def main():
  523. """主函数:执行系统测试"""
  524. print("🚀 执行CompleteAgentFlow系统测试")
  525. print("=" * 50)
  526. # 导入配置
  527. import config
  528. if not DEEPSEEK_API_KEY:
  529. print("❌ 未找到API密钥")
  530. return
  531. # 行业
  532. industry = "农业"
  533. # 测试文件
  534. file_name = "交易流水样例数据.csv"
  535. curr_dir = os.path.dirname(os.path.abspath(__file__))
  536. file_path = os.path.join(curr_dir, "..", "data_files", file_name)
  537. # 加载测试数据集并展示两条样例
  538. test_data = DataManager.load_data_from_csv_file(file_path)
  539. print(f"📊 读取测试数据文件: {file_name} 数据, 加载 {len(test_data)} 条记录")
  540. print(f"测试数据样例: {test_data[0:1]}")
  541. # 执行测试
  542. result = await run_complete_agent_flow(
  543. question="请生成一份详细的农业经营贷流水分析报告,需要包含:1.总收入和总支出统计 2.收入笔数和支出笔数 3.各类型收入支出占比分析 4.交易对手收入支出TOP3排名 5.按月份的收入支出趋势分析 6.账户数量和交易时间范围统计 7.资金流入流出月度统计等全面指标",
  544. industry = industry,
  545. data=test_data,
  546. file_name=file_name,
  547. api_key=config.DEEPSEEK_API_KEY,
  548. session_id="direct-test"
  549. )
  550. print(f"📋 结果: {'✅ 成功' if result.get('success') else '❌ 失败'}")
  551. if result.get('success'):
  552. summary = result.get('execution_summary', {})
  553. print(f" 规划步骤: {summary.get('planning_steps', 0)}")
  554. print(f" 指标计算: {summary.get('metrics_computed', 0)}")
  555. print("🎉 测试成功!")
  556. return result
  557. if __name__ == "__main__":
  558. import asyncio
  559. asyncio.run(main())