batch_runner_all.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #!/usr/bin/env python3
  2. """
  3. 批量运行器 - 批量执行Complete Agent Flow
  4. ========================================
  5. 此脚本可以批量运行多次完整的智能体工作流,每次运行会创建独立的日志文件夹。
  6. 使用方法:
  7. python batch_runner.py
  8. 配置参数:
  9. - 运行次数: RUNS = 10
  10. - 行业: INDUSTRY = "农业"
  11. - 数据文件: DATA_FILE = "data_files/交易流水样例数据.csv"
  12. - 查询问题: QUESTION = "请生成一份详细的农业经营贷流水分析报告..."
  13. 文件夹结构:
  14. api_results_1/ # 第一次运行的日志
  15. api_results_2/ # 第二次运行的日志
  16. ...
  17. api_results_10/ # 第十次运行的日志
  18. 作者: Big Agent Team
  19. 版本: 1.0.0
  20. 创建时间: 2024-12-20
  21. """
  22. import asyncio
  23. import os
  24. from datetime import datetime
  25. from typing import List, Dict, Any
  26. import sys
  27. import os
  28. # 添加项目根目录到路径,以便导入config
  29. current_dir = os.path.dirname(os.path.abspath(__file__))
  30. parent_dir = os.path.dirname(current_dir)
  31. sys.path.insert(0, parent_dir)
  32. os.environ["LANGCHAIN_TRACING_V2"] = "false"
  33. os.environ["LANGCHAIN_API_KEY"] = ""
  34. # 禁用 LangGraph 的追踪
  35. os.environ["LANGSMITH_TRACING"] = "false"
  36. # 根据执行方式选择导入方式
  37. if __name__ == "__main__":
  38. # 直接执行文件时,使用绝对导入
  39. from llmops.complete_agent_flow_rule import run_complete_agent_flow, run_flow
  40. from llmops.agents.data_manager import DataManager
  41. else:
  42. # 作为模块导入时,使用相对导入
  43. from .complete_agent_flow_rule import run_complete_agent_flow
  44. from .agents.data_manager import DataManager
  45. import config
  46. # ========== 配置参数 ==========
  47. RUNS = 1 # 运行次数
  48. INDUSTRY = "农业" # 行业
  49. ORIGINAL_DATA_FILE="data_files/11111.png" # 原始测试数据文件PDF
  50. DATA_FILE = "data_files/交易流水样例数据.csv" # 数据文件路径
  51. QUESTION = "请生成一份详细的农业经营贷流水分析报告,需要包含:1.总收入和总支出统计 2.收入笔数和支出笔数 3.各类型收入支出占比分析 4.交易对手收入支出TOP3排名 5.按月份的收入支出趋势分析 6.账户数量和交易时间范围统计 7.资金流入流出月度统计等全面指标" # 分析查询
  52. # ==============================
  53. async def run_single_flow(run_id: str, question: str, industry: str, file_name: str) -> Dict[str, Any]:
  54. """
  55. 运行单个工作流实例
  56. Args:
  57. run_id: 运行ID
  58. question: 用户查询
  59. industry: 行业
  60. file_name: 文件名
  61. Returns:
  62. 运行结果
  63. """
  64. print(f"\n{'='*60}")
  65. print(f"🚀 开始运行 #{run_id}")
  66. print(f"📁 日志文件夹: api_results_{run_id}")
  67. print(f"{'='*60}")
  68. # 设置环境变量,让所有agent使用正确的文件夹
  69. os.environ['FLOW_RUN_ID'] = run_id
  70. try:
  71. result = await run_flow(
  72. question=question,
  73. industry=industry,
  74. original_file_path=file_name,
  75. api_key=config.DEEPSEEK_API_KEY,
  76. session_id=f"batch-run-{run_id}"
  77. )
  78. if result.get('success'):
  79. summary = result.get('execution_summary', {})
  80. print(f"✅ 运行 #{run_id} 成功完成")
  81. print(f" 规划步骤: {summary.get('planning_steps', 0)}")
  82. print(f" 指标计算: {summary.get('metrics_computed', 0)}")
  83. else:
  84. print(f"❌ 运行 #{run_id} 失败: {result.get('error', '未知错误')}")
  85. return result
  86. except Exception as e:
  87. print(f"❌ 运行 #{run_id} 发生异常: {e}")
  88. return {
  89. "success": False,
  90. "error": str(e),
  91. "run_id": run_id
  92. }
  93. async def data_standardize():
  94. """
  95. 提取数据,进行标准化处理
  96. """
  97. from llmops.agents.data_stardard import TransactionParserAgent
  98. from llmops.config import DEEPSEEK_API_KEY, multimodal_api_url
  99. # 数据标准化agent
  100. standard_agent = TransactionParserAgent(
  101. api_key=DEEPSEEK_API_KEY,
  102. multimodal_api_url=multimodal_api_url
  103. )
  104. # 执行解析
  105. full_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", ORIGINAL_DATA_FILE)
  106. try:
  107. result = await standard_agent.run_workflow_task(full_path)
  108. if result["status"] == "success":
  109. print(f"🎯 数据标准化任务完成!")
  110. # 标准化后的文件
  111. standard_file_path = result['file_path']
  112. return standard_file_path
  113. else:
  114. print(f"❌ 数据标准化任务失败: {result['message']}")
  115. raise ValueError("数据标准化异常")
  116. except Exception as e:
  117. print(f"数据标准化处理异常:{e}")
  118. raise e
  119. async def run_batch(runs: int, question: str, industry: str, data_file: str):
  120. """
  121. 批量运行工作流
  122. Args:
  123. runs: 运行次数
  124. question: 用户查询
  125. industry: 行业
  126. data_file: 原始数据文件路径,格式支持pdf/img/csv
  127. """
  128. print("🚀 批量运行器启动")
  129. print(f"📊 计划运行次数: {runs}")
  130. print(f"🏭 行业: {industry}")
  131. print(f"📁 数据文件: {data_file}")
  132. print(f"❓ 查询: {question}")
  133. print(f"{'='*80}")
  134. # 检查API密钥
  135. if not config.DEEPSEEK_API_KEY:
  136. print("❌ 未找到API密钥,请检查config.py")
  137. return
  138. # 运行结果统计
  139. successful_runs = 0
  140. failed_runs = 0
  141. results = []
  142. # 运行总时长,秒
  143. total_time = 0
  144. import time
  145. # 逐个运行
  146. for i in range(1, runs + 1):
  147. run_id = str(i)
  148. # 设置环境变量,让所有agent使用正确的文件夹
  149. os.environ['FLOW_RUN_ID'] = run_id
  150. start_time = time.perf_counter()
  151. # 单次执行
  152. result = await run_single_flow(run_id, question, industry, data_file)
  153. end_time = time.perf_counter()
  154. total_time += (end_time - start_time)
  155. results.append(result)
  156. if result.get('success'):
  157. successful_runs += 1
  158. else:
  159. failed_runs += 1
  160. # 添加短暂延迟,避免API调用过于频繁
  161. if i < runs: # 最后一次不需要延迟
  162. await asyncio.sleep(1)
  163. # 输出统计结果
  164. print(f"\n{'='*80}")
  165. print("📊 批量运行完成统计")
  166. print(f"{'='*80}")
  167. print(f"总运行次数: {runs}")
  168. print(f"总运行总用时: {total_time:.2f}秒,单次用时:{total_time/runs:.2f}秒")
  169. print(f"成功次数: {successful_runs}")
  170. print(f"失败次数: {failed_runs}")
  171. print(f"成功率: {successful_runs/runs*100:.1f}%")
  172. # 显示各运行的日志文件夹
  173. print(f"\n📁 日志文件夹列表:")
  174. for i in range(1, runs + 1):
  175. folder_name = f"api_results_{i}"
  176. status = "✅" if results[i-1].get('success') else "❌"
  177. print(f" {status} {folder_name}")
  178. print("\n🎉 批量运行完成!")
  179. print(f"💡 提示: 每次运行的完整日志保存在对应的 api_results_[数字] 文件夹中")
  180. def main():
  181. """主函数"""
  182. print("🚀 使用配置参数运行批量任务")
  183. print(f"📊 运行次数: {RUNS}")
  184. print(f"🏭 行业: {INDUSTRY}")
  185. print(f"📁 数据文件: {ORIGINAL_DATA_FILE}")
  186. print(f"❓ 查询: {QUESTION[:50]}...")
  187. print("-" * 80)
  188. curr_dir = os.path.dirname(os.path.abspath(__file__))
  189. file_path = os.path.join(curr_dir, "..", ORIGINAL_DATA_FILE)
  190. # 运行批量任务
  191. asyncio.run(run_batch(
  192. runs=RUNS,
  193. question=QUESTION,
  194. industry=INDUSTRY,
  195. data_file=file_path
  196. ))
  197. if __name__ == "__main__":
  198. main()