rules_engine_metric_calculation_agent.py 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. """
  2. 规则引擎指标计算Agent (Rules Engine Metric Calculation Agent)
  3. ===========================================================
  4. 此Agent负责根据意图识别结果执行规则引擎模式的指标计算任务。
  5. 核心功能:
  6. 1. 配置文件加载:读取和解析规则引擎指标计算配置文件
  7. 2. API调用管理:根据配置文件调用规则引擎API
  8. 3. 结果处理:处理API返回的数据,提取关键指标
  9. 4. 错误处理:处理API调用失败、网络异常等错误情况
  10. 5. 结果验证:验证计算结果的合理性和完整性
  11. 工作流程:
  12. 1. 接收意图识别结果和用户参数
  13. 2. 加载对应的规则引擎指标计算配置文件
  14. 3. 构造API请求参数(id和input)
  15. 4. 调用远程规则引擎服务
  16. 5. 解析和验证返回结果
  17. 6. 返回结构化的计算结果
  18. 技术实现:
  19. - 支持动态加载JSON配置文件
  20. - 使用requests库进行HTTP API调用
  21. - 集成LangChain用于复杂计算逻辑(可选)
  22. - 完善的错误处理和超时机制
  23. - 支持多种计算方法(标准、高级、自定义)
  24. 配置文件结构:
  25. - id: 规则引擎执行ID
  26. - input: 数据文件路径
  27. - description: 规则描述
  28. API接口:
  29. POST http://localhost:8081/api/rules/executeKnowledge
  30. 请求体:
  31. {
  32. "id": "知识ID",
  33. "input": {
  34. "动态字段名": [...] // 根据知识的inputField字段动态确定,如"transactions"或"resultTag"
  35. }
  36. }
  37. 作者: Big Agent Team
  38. 版本: 1.0.0
  39. 创建时间: 2024-12-19
  40. """
  41. import os
  42. import json
  43. import requests
  44. from datetime import datetime
  45. from typing import Dict, List, Any, Optional
  46. from langchain_openai import ChatOpenAI
  47. from langchain_core.prompts import ChatPromptTemplate
  48. import re
  49. from llmops.config import RULES_ENGINE_BASE_URL
  50. class RulesEngineMetricCalculationAgent:
  51. """规则引擎指标计算Agent"""
  52. def __init__(self, api_key: str, base_url: str = "https://api.deepseek.com"):
  53. # 获取可用的知识元数据
  54. self.available_knowledge = self._load_available_knowledge()
  55. # 加载数据文件映射
  56. self.data_files = self._load_data_files()
  57. # 初始化API调用跟踪
  58. self.api_calls = []
  59. # 加载配置文件
  60. self.configs = {}
  61. def _load_data_files(self) -> Dict[str, str]:
  62. """加载数据文件映射"""
  63. data_files = {}
  64. data_dir = "data_files"
  65. if os.path.exists(data_dir):
  66. for file in os.listdir(data_dir):
  67. if file.endswith('.json'):
  68. try:
  69. # 提取文件名,用于匹配配置文件
  70. key = file.replace('.json', '')
  71. data_files[key] = os.path.join(data_dir, file)
  72. except Exception as e:
  73. print(f"处理数据文件 {file} 失败: {e}")
  74. return data_files
  75. def _select_data_file(self, input_filename: str) -> Optional[str]:
  76. """
  77. 根据输入文件名选择对应的数据文件
  78. Args:
  79. input_filename: 配置文件中的input字段值
  80. Returns:
  81. 数据文件路径,如果找不到则返回None
  82. """
  83. # input字段直接指定数据文件名
  84. if input_filename in self.data_files:
  85. return self.data_files[input_filename]
  86. # 如果找不到精确匹配,尝试模糊匹配
  87. for key, file_path in self.data_files.items():
  88. if input_filename in key or key in input_filename:
  89. return file_path
  90. return None
  91. def _load_table_data(self, data_file_path: str) -> List[Dict[str, Any]]:
  92. """加载数据文件中的JSON数据"""
  93. try:
  94. with open(data_file_path, 'r', encoding='utf-8') as f:
  95. data = json.load(f)
  96. if isinstance(data, list):
  97. return data
  98. elif isinstance(data, dict):
  99. # 如果是字典,尝试提取其中的数组数据
  100. for key, value in data.items():
  101. if isinstance(value, list):
  102. return value
  103. return []
  104. else:
  105. return []
  106. except Exception as e:
  107. print(f"加载数据文件 {data_file_path} 失败: {e}")
  108. return []
  109. def _load_available_knowledge(self) -> List[Dict[str, Any]]:
  110. """
  111. 从规则引擎获取可用的知识元数据
  112. Returns:
  113. 知识元数据列表,包含id、description和inputField
  114. """
  115. try:
  116. url = f"{RULES_ENGINE_BASE_URL}/api/rules/getKnowledgeMeta"
  117. headers = {
  118. "Accept": "*/*",
  119. "Accept-Encoding": "gzip, deflate, br",
  120. "Connection": "keep-alive",
  121. "Content-Type": "application/json",
  122. "User-Agent": "PostmanRuntime-ApipostRuntime/1.1.0"
  123. }
  124. response = requests.post(url, headers=headers, json={}, timeout=30)
  125. if response.status_code == 200:
  126. knowledge_meta = response.json()
  127. if isinstance(knowledge_meta, list):
  128. print(f"✅ 成功获取 {len(knowledge_meta)} 个知识元数据")
  129. return knowledge_meta
  130. else:
  131. print(f"⚠️ 知识元数据格式异常: {knowledge_meta}")
  132. return []
  133. else:
  134. print(f"❌ 获取知识元数据失败,状态码: {response.status_code}")
  135. print(f"响应内容: {response.text}")
  136. return []
  137. except Exception as e:
  138. print(f"❌ 获取知识元数据时发生错误: {str(e)}")
  139. return []
  140. def _get_input_field_for_knowledge(self, knowledge_id: str) -> str:
  141. """
  142. 根据知识ID获取对应的inputField字段名
  143. Args:
  144. knowledge_id: 知识ID
  145. Returns:
  146. inputField字段名,默认为"transactions"
  147. """
  148. for knowledge in self.available_knowledge:
  149. if knowledge.get("id") == knowledge_id:
  150. input_field = knowledge.get("inputField", "transactions")
  151. print(f"🔗 知识 {knowledge_id} 使用输入字段: {input_field}")
  152. return input_field
  153. print(f"⚠️ 未找到知识 {knowledge_id} 的输入字段,使用默认值: transactions")
  154. return "transactions" # 默认值
  155. async def calculate_metrics(self, intent_result: Dict[str, Any], classified_data: List[Dict[str, Any]] = None) -> Dict[str, Any]:
  156. """
  157. 根据意图识别结果进行规则引擎指标计算
  158. Args:
  159. intent_result: 意图识别结果
  160. classified_data: 分类后的数据,如果提供则优先使用,否则从intent_result或文件加载
  161. Returns:
  162. 指标计算结果
  163. """
  164. try:
  165. results = []
  166. target_configs = intent_result.get("target_configs", [])
  167. if not target_configs:
  168. return {
  169. "success": False,
  170. "message": "没有找到需要调用的配置文件",
  171. "results": []
  172. }
  173. for config_name in target_configs:
  174. if config_name in self.configs:
  175. # 使用传统的JSON配置文件方式
  176. config = self.configs[config_name]
  177. result = await self._call_rules_engine_api(config, intent_result, config_name)
  178. results.append({
  179. "config_name": config_name,
  180. "result": result
  181. })
  182. elif config_name.startswith("metric-"):
  183. # 直接使用知识ID调用API,无需配置文件
  184. result = await self._call_rules_engine_api_by_knowledge_id(config_name, intent_result, classified_data)
  185. results.append({
  186. "config_name": config_name,
  187. "result": result
  188. })
  189. else:
  190. results.append({
  191. "config_name": config_name,
  192. "error": f"配置文件 {config_name} 不存在,且不是有效的知识ID"
  193. })
  194. return {
  195. "success": True,
  196. "results": results,
  197. "total_configs": len(target_configs),
  198. "successful_calculations": len([r for r in results if "result" in r])
  199. }
  200. except Exception as e:
  201. print(f"规则引擎指标计算失败: {e}")
  202. return {
  203. "success": False,
  204. "message": f"规则引擎指标计算过程中发生错误: {str(e)}",
  205. "results": []
  206. }
  207. async def _call_rules_engine_api(self, config: Dict[str, Any], intent_result: Dict[str, Any], config_name: str) -> Dict[str, Any]:
  208. """
  209. 调用规则引擎API
  210. Args:
  211. config: 配置文件
  212. intent_result: 意图识别结果
  213. Returns:
  214. API调用结果
  215. """
  216. try:
  217. # 记录API调用开始
  218. start_time = datetime.now()
  219. # 规则引擎API配置
  220. method = "POST"
  221. url = f"{RULES_ENGINE_BASE_URL}/api/rules/executeKnowledge"
  222. headers = {
  223. "Accept": "*/*",
  224. "Accept-Encoding": "gzip, deflate, br",
  225. "Connection": "keep-alive",
  226. "Content-Type": "application/json",
  227. "User-Agent": "PostmanRuntime-ApipostRuntime/1.1.0"
  228. }
  229. timeout = 180 # 3分钟超时
  230. # 准备请求数据
  231. request_data = self._prepare_rules_engine_request_data(config, intent_result, config_name)
  232. # 调用API
  233. json_data = request_data.get("json", {})
  234. response = requests.post(url, headers=headers, json=json_data, timeout=timeout)
  235. # 处理响应
  236. if response.status_code == 200:
  237. try:
  238. response_data = response.json()
  239. # 记录API调用结果
  240. end_time = datetime.now()
  241. call_id = f"rules_api_{config_name}_{'{:.2f}'.format((end_time - start_time).total_seconds())}"
  242. api_call_info = {
  243. "call_id": call_id,
  244. "timestamp": end_time.isoformat(),
  245. "agent": "RulesEngineMetricCalculationAgent",
  246. "api_endpoint": url,
  247. "config_name": config_name,
  248. "request": {
  249. "method": method,
  250. "url": url,
  251. "headers": headers,
  252. "json_data": json_data,
  253. "start_time": start_time.isoformat()
  254. },
  255. "response": {
  256. "status_code": response.status_code,
  257. "data": response_data,
  258. "end_time": end_time.isoformat(),
  259. "duration": (end_time - start_time).total_seconds()
  260. },
  261. "success": True
  262. }
  263. self.api_calls.append(api_call_info)
  264. # 保存API结果到文件
  265. api_results_dir = "api_results"
  266. os.makedirs(api_results_dir, exist_ok=True)
  267. timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
  268. filename = f"{timestamp}_{call_id}.json"
  269. filepath = os.path.join(api_results_dir, filename)
  270. try:
  271. with open(filepath, 'w', encoding='utf-8') as f:
  272. json.dump(api_call_info, f, ensure_ascii=False, indent=2)
  273. print(f"[RULES_API_RESULT] 保存规则引擎API结果文件: {filepath}")
  274. except Exception as e:
  275. print(f"[ERROR] 保存规则引擎API结果文件失败: {filepath}, 错误: {str(e)}")
  276. return {
  277. "success": True,
  278. "data": response_data,
  279. "status_code": response.status_code
  280. }
  281. except json.JSONDecodeError:
  282. # 记录API调用结果(JSON解析失败)
  283. end_time = datetime.now()
  284. call_id = f"rules_api_{config_name}_{'{:.2f}'.format((end_time - start_time).total_seconds())}"
  285. api_call_info = {
  286. "call_id": call_id,
  287. "timestamp": end_time.isoformat(),
  288. "agent": "RulesEngineMetricCalculationAgent",
  289. "api_endpoint": url,
  290. "config_name": config_name,
  291. "request": {
  292. "method": method,
  293. "url": url,
  294. "headers": headers,
  295. "json_data": json_data,
  296. "start_time": start_time.isoformat()
  297. },
  298. "response": {
  299. "status_code": response.status_code,
  300. "data": response.text,
  301. "error": "JSON解析失败",
  302. "end_time": end_time.isoformat(),
  303. "duration": (end_time - start_time).total_seconds()
  304. },
  305. "success": False
  306. }
  307. self.api_calls.append(api_call_info)
  308. # 保存API结果到文件
  309. api_results_dir = "api_results"
  310. os.makedirs(api_results_dir, exist_ok=True)
  311. timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
  312. filename = f"{timestamp}_{call_id}.json"
  313. filepath = os.path.join(api_results_dir, filename)
  314. try:
  315. with open(filepath, 'w', encoding='utf-8') as f:
  316. json.dump(api_call_info, f, ensure_ascii=False, indent=2)
  317. print(f"[RULES_API_RESULT] 保存规则引擎API结果文件: {filepath}")
  318. except Exception as e:
  319. print(f"[ERROR] 保存规则引擎API结果文件失败: {filepath}, 错误: {str(e)}")
  320. return {
  321. "success": True,
  322. "data": response.text,
  323. "status_code": response.status_code
  324. }
  325. else:
  326. # 记录API调用结果(HTTP错误)
  327. end_time = datetime.now()
  328. call_id = f"rules_api_{config_name}_{'{:.2f}'.format((end_time - start_time).total_seconds())}"
  329. api_call_info = {
  330. "call_id": call_id,
  331. "timestamp": end_time.isoformat(),
  332. "agent": "RulesEngineMetricCalculationAgent",
  333. "api_endpoint": url,
  334. "config_name": config_name,
  335. "request": {
  336. "method": method,
  337. "url": url,
  338. "headers": headers,
  339. "json_data": json_data,
  340. "start_time": start_time.isoformat()
  341. },
  342. "response": {
  343. "status_code": response.status_code,
  344. "error": response.text,
  345. "end_time": end_time.isoformat(),
  346. "duration": (end_time - start_time).total_seconds()
  347. },
  348. "success": False
  349. }
  350. self.api_calls.append(api_call_info)
  351. # 保存API结果到文件
  352. api_results_dir = "api_results"
  353. os.makedirs(api_results_dir, exist_ok=True)
  354. filename = f"{call_id}.json"
  355. filepath = os.path.join(api_results_dir, filename)
  356. try:
  357. with open(filepath, 'w', encoding='utf-8') as f:
  358. json.dump(api_call_info, f, ensure_ascii=False, indent=2)
  359. print(f"[RULES_API_RESULT] 保存规则引擎API结果文件: {filepath}")
  360. except Exception as e:
  361. print(f"[ERROR] 保存规则引擎API结果文件失败: {filepath}, 错误: {str(e)}")
  362. return {
  363. "success": False,
  364. "message": f"规则引擎API调用失败,状态码: {response.status_code}",
  365. "response": response.text
  366. }
  367. except requests.exceptions.Timeout:
  368. # 记录API调用结果(超时)
  369. end_time = datetime.now()
  370. call_id = f"rules_api_{config_name}_{'{:.2f}'.format((end_time - start_time).total_seconds())}"
  371. api_call_info = {
  372. "call_id": call_id,
  373. "timestamp": end_time.isoformat(),
  374. "agent": "RulesEngineMetricCalculationAgent",
  375. "api_endpoint": url,
  376. "config_name": config_name,
  377. "request": {
  378. "method": method,
  379. "url": url,
  380. "headers": headers,
  381. "json_data": json_data if 'json_data' in locals() else None,
  382. "start_time": start_time.isoformat()
  383. },
  384. "response": {
  385. "error": "API调用超时",
  386. "end_time": end_time.isoformat(),
  387. "duration": (end_time - start_time).total_seconds()
  388. },
  389. "success": False
  390. }
  391. self.api_calls.append(api_call_info)
  392. return {
  393. "success": False,
  394. "message": "规则引擎API调用超时"
  395. }
  396. except requests.exceptions.RequestException as e:
  397. # 记录API调用结果(请求异常)
  398. end_time = datetime.now()
  399. call_id = f"rules_api_{config_name}_{'{:.2f}'.format((end_time - start_time).total_seconds())}"
  400. api_call_info = {
  401. "call_id": call_id,
  402. "timestamp": end_time.isoformat(),
  403. "agent": "RulesEngineMetricCalculationAgent",
  404. "api_endpoint": url,
  405. "config_name": config_name,
  406. "request": {
  407. "method": method,
  408. "url": url,
  409. "headers": headers,
  410. "json_data": json_data if 'json_data' in locals() else None,
  411. "start_time": start_time.isoformat()
  412. },
  413. "response": {
  414. "error": str(e),
  415. "end_time": end_time.isoformat(),
  416. "duration": (end_time - start_time).total_seconds()
  417. },
  418. "success": False
  419. }
  420. self.api_calls.append(api_call_info)
  421. return {
  422. "success": False,
  423. "message": f"规则引擎API调用异常: {str(e)}"
  424. }
  425. except Exception as e:
  426. # 记录API调用结果(其他异常)
  427. end_time = datetime.now()
  428. call_id = f"rules_api_{config_name}_{'{:.2f}'.format((end_time - start_time).total_seconds())}"
  429. api_call_info = {
  430. "call_id": call_id,
  431. "timestamp": end_time.isoformat(),
  432. "agent": "RulesEngineMetricCalculationAgent",
  433. "api_endpoint": url,
  434. "config_name": config_name,
  435. "request": {
  436. "method": method,
  437. "url": url,
  438. "headers": headers,
  439. "json_data": json_data if 'json_data' in locals() else None,
  440. "start_time": start_time.isoformat()
  441. },
  442. "response": {
  443. "error": str(e),
  444. "end_time": end_time.isoformat(),
  445. "duration": (end_time - start_time).total_seconds()
  446. },
  447. "success": False
  448. }
  449. self.api_calls.append(api_call_info)
  450. return {
  451. "success": False,
  452. "message": f"处理规则引擎API调用时发生错误: {str(e)}"
  453. }
  454. def _prepare_rules_engine_request_data(self, config: Dict[str, Any], intent_result: Dict[str, Any], config_name: str) -> Dict[str, Any]:
  455. """
  456. 准备规则引擎API请求数据
  457. Args:
  458. config: 配置文件
  459. intent_result: 意图识别结果
  460. config_name: 配置文件名
  461. Returns:
  462. 请求数据
  463. """
  464. # 从配置文件中获取id和input
  465. request_id = config.get("id", "")
  466. input_filename = config.get("input", "")
  467. # 加载对应的数据文件
  468. input_data = {}
  469. if input_filename:
  470. data_file_path = self._select_data_file(input_filename)
  471. if data_file_path:
  472. input_data = self._load_table_data(data_file_path)
  473. else:
  474. print(f"警告:找不到配置文件 {config_name} 对应的数据文件: {input_filename}")
  475. # 构造API请求体
  476. request_data = {
  477. "id": request_id,
  478. "input": input_data
  479. }
  480. return {"json": request_data}
  481. async def _call_rules_engine_api_by_knowledge_id(self, knowledge_id: str, intent_result: Dict[str, Any], classified_data: List[Dict[str, Any]] = None) -> Dict[str, Any]:
  482. """
  483. 直接通过知识ID调用规则引擎API
  484. Args:
  485. knowledge_id: 知识ID,如 "metric-分析账户数量"
  486. intent_result: 意图识别结果(用于获取数据文件信息)
  487. classified_data: 分类后的数据,如果提供则优先使用,否则从intent_result或文件加载
  488. Returns:
  489. API调用结果
  490. """
  491. # 记录API调用开始
  492. start_time = datetime.now()
  493. # 规则引擎API配置
  494. method = "POST"
  495. url = f"{RULES_ENGINE_BASE_URL}/api/rules/executeKnowledge"
  496. headers = {
  497. "Accept": "*/*",
  498. "Accept-Encoding": "gzip, deflate, br",
  499. "Connection": "keep-alive",
  500. "Content-Type": "application/json",
  501. "User-Agent": "PostmanRuntime-ApipostRuntime/1.1.0"
  502. }
  503. timeout = 180 # 3分钟超时
  504. try:
  505. # 根据知识ID获取正确的输入字段名
  506. input_field_name = self._get_input_field_for_knowledge(knowledge_id)
  507. # 构造请求数据 - 优先使用传入的分类后数据
  508. input_data = {}
  509. # 检查是否有传入的分类后数据
  510. if classified_data and len(classified_data) > 0:
  511. # 使用传入的分类后数据
  512. print(f" 使用传入的分类后数据,记录数: {len(classified_data)}")
  513. input_data = {input_field_name: classified_data}
  514. else:
  515. # 回退到文件加载方式(向后兼容)
  516. input_filename = intent_result.get("data_file", "加工数据-流水分析-农业打标.json")
  517. print(f" 未找到分类后数据,回退到文件加载: {input_filename}")
  518. if input_filename:
  519. data_file_path = self._select_data_file(input_filename)
  520. if data_file_path:
  521. raw_data = self._load_table_data(data_file_path)
  522. # 使用正确的字段名包装数据
  523. input_data = {input_field_name: raw_data}
  524. else:
  525. print(f"警告:找不到数据文件: {input_filename}")
  526. input_data = {input_field_name: []}
  527. # 构造API请求体
  528. request_data = {
  529. "id": knowledge_id, # 直接使用知识ID
  530. "input": input_data
  531. }
  532. # 调用API
  533. json_data = request_data
  534. response = requests.post(url, headers=headers, json=json_data, timeout=timeout)
  535. # 处理响应(复用现有的响应处理逻辑)
  536. if response.status_code == 200:
  537. try:
  538. response_data = response.json()
  539. # 记录API调用结果
  540. end_time = datetime.now()
  541. call_id = f"rules_api_{knowledge_id}_{'{:.2f}'.format((end_time - start_time).total_seconds())}"
  542. api_call_info = {
  543. "call_id": call_id,
  544. "timestamp": end_time.isoformat(),
  545. "agent": "RulesEngineMetricCalculationAgent",
  546. "api_endpoint": url,
  547. "config_name": knowledge_id,
  548. "request": {
  549. "method": method,
  550. "url": url,
  551. "headers": headers,
  552. "json_data": json_data,
  553. "start_time": start_time.isoformat()
  554. },
  555. "response": {
  556. "status_code": response.status_code,
  557. "data": response_data,
  558. "end_time": end_time.isoformat(),
  559. "duration": (end_time - start_time).total_seconds()
  560. },
  561. "success": True
  562. }
  563. self.api_calls.append(api_call_info)
  564. # 保存API结果到文件
  565. api_results_dir = "api_results"
  566. os.makedirs(api_results_dir, exist_ok=True)
  567. timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
  568. filename = f"{timestamp}_{call_id}.json"
  569. filepath = os.path.join(api_results_dir, filename)
  570. try:
  571. with open(filepath, 'w', encoding='utf-8') as f:
  572. json.dump(api_call_info, f, ensure_ascii=False, indent=2)
  573. print(f"[RULES_API_RESULT] 保存规则引擎API结果文件: {filepath}")
  574. except Exception as e:
  575. print(f"[ERROR] 保存规则引擎API结果文件失败: {filepath}, 错误: {str(e)}")
  576. return {
  577. "success": True,
  578. "data": response_data,
  579. "status_code": response.status_code
  580. }
  581. except json.JSONDecodeError:
  582. # 记录JSON解析失败的API调用
  583. end_time = datetime.now()
  584. call_id = f"rules_api_{knowledge_id}_{'{:.2f}'.format((end_time - start_time).total_seconds())}"
  585. api_call_info = {
  586. "call_id": call_id,
  587. "timestamp": end_time.isoformat(),
  588. "agent": "RulesEngineMetricCalculationAgent",
  589. "api_endpoint": url,
  590. "config_name": knowledge_id,
  591. "request": {
  592. "method": method,
  593. "url": url,
  594. "headers": headers,
  595. "json_data": json_data,
  596. "start_time": start_time.isoformat()
  597. },
  598. "response": {
  599. "status_code": response.status_code,
  600. "data": response.text,
  601. "error": "JSON解析失败",
  602. "end_time": end_time.isoformat(),
  603. "duration": (end_time - start_time).total_seconds()
  604. },
  605. "success": False
  606. }
  607. self.api_calls.append(api_call_info)
  608. # 保存API结果到文件
  609. api_results_dir = "api_results"
  610. os.makedirs(api_results_dir, exist_ok=True)
  611. timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
  612. filename = f"{timestamp}_{call_id}.json"
  613. filepath = os.path.join(api_results_dir, filename)
  614. try:
  615. with open(filepath, 'w', encoding='utf-8') as f:
  616. json.dump(api_call_info, f, ensure_ascii=False, indent=2)
  617. print(f"[RULES_API_RESULT] 保存规则引擎API结果文件: {filepath}")
  618. except Exception as e:
  619. print(f"[ERROR] 保存规则引擎API结果文件失败: {filepath}, 错误: {str(e)}")
  620. return {
  621. "success": True,
  622. "data": response.text,
  623. "status_code": response.status_code
  624. }
  625. else:
  626. # 记录失败的API调用
  627. end_time = datetime.now()
  628. call_id = f"rules_api_{knowledge_id}_{'{:.2f}'.format((end_time - start_time).total_seconds())}"
  629. api_call_info = {
  630. "call_id": call_id,
  631. "timestamp": end_time.isoformat(),
  632. "agent": "RulesEngineMetricCalculationAgent",
  633. "api_endpoint": url,
  634. "config_name": knowledge_id,
  635. "request": {
  636. "method": method,
  637. "url": url,
  638. "headers": headers,
  639. "json_data": json_data,
  640. "start_time": start_time.isoformat()
  641. },
  642. "response": {
  643. "status_code": response.status_code,
  644. "error": response.text,
  645. "end_time": end_time.isoformat(),
  646. "duration": (end_time - start_time).total_seconds()
  647. },
  648. "success": False
  649. }
  650. self.api_calls.append(api_call_info)
  651. # 保存API结果到文件
  652. api_results_dir = "api_results"
  653. os.makedirs(api_results_dir, exist_ok=True)
  654. filename = f"{call_id}.json"
  655. filepath = os.path.join(api_results_dir, filename)
  656. try:
  657. with open(filepath, 'w', encoding='utf-8') as f:
  658. json.dump(api_call_info, f, ensure_ascii=False, indent=2)
  659. print(f"[RULES_API_RESULT] 保存规则引擎API结果文件: {filepath}")
  660. except Exception as e:
  661. print(f"[ERROR] 保存规则引擎API结果文件失败: {filepath}, 错误: {str(e)}")
  662. return {
  663. "success": False,
  664. "message": f"规则引擎API调用失败,状态码: {response.status_code}",
  665. "response": response.text
  666. }
  667. except requests.exceptions.Timeout:
  668. # 记录API调用结果(超时)
  669. end_time = datetime.now()
  670. call_id = f"rules_api_{knowledge_id}_{'{:.2f}'.format((end_time - start_time).total_seconds())}"
  671. api_call_info = {
  672. "call_id": call_id,
  673. "timestamp": end_time.isoformat(),
  674. "agent": "RulesEngineMetricCalculationAgent",
  675. "api_endpoint": url,
  676. "config_name": knowledge_id,
  677. "request": {
  678. "method": method,
  679. "url": url,
  680. "headers": headers,
  681. "json_data": json_data if 'json_data' in locals() else None,
  682. "start_time": start_time.isoformat()
  683. },
  684. "response": {
  685. "error": "API调用超时",
  686. "end_time": end_time.isoformat(),
  687. "duration": (end_time - start_time).total_seconds()
  688. },
  689. "success": False
  690. }
  691. self.api_calls.append(api_call_info)
  692. # 保存API结果到文件
  693. api_results_dir = "api_results"
  694. os.makedirs(api_results_dir, exist_ok=True)
  695. filename = f"{call_id}.json"
  696. filepath = os.path.join(api_results_dir, filename)
  697. try:
  698. with open(filepath, 'w', encoding='utf-8') as f:
  699. json.dump(api_call_info, f, ensure_ascii=False, indent=2)
  700. print(f"[RULES_API_RESULT] 保存规则引擎API结果文件: {filepath}")
  701. except Exception as e:
  702. print(f"[ERROR] 保存规则引擎API结果文件失败: {filepath}, 错误: {str(e)}")
  703. return {
  704. "success": False,
  705. "message": "规则引擎API调用超时"
  706. }
  707. except requests.exceptions.RequestException as e:
  708. # 记录API调用结果(请求异常)
  709. end_time = datetime.now()
  710. call_id = f"rules_api_{knowledge_id}_{'{:.2f}'.format((end_time - start_time).total_seconds())}"
  711. api_call_info = {
  712. "call_id": call_id,
  713. "timestamp": end_time.isoformat(),
  714. "agent": "RulesEngineMetricCalculationAgent",
  715. "api_endpoint": url,
  716. "config_name": knowledge_id,
  717. "request": {
  718. "method": method,
  719. "url": url,
  720. "headers": headers,
  721. "json_data": json_data if 'json_data' in locals() else None,
  722. "start_time": start_time.isoformat()
  723. },
  724. "response": {
  725. "error": str(e),
  726. "end_time": end_time.isoformat(),
  727. "duration": (end_time - start_time).total_seconds()
  728. },
  729. "success": False
  730. }
  731. self.api_calls.append(api_call_info)
  732. # 保存API结果到文件
  733. api_results_dir = "api_results"
  734. os.makedirs(api_results_dir, exist_ok=True)
  735. filename = f"{call_id}.json"
  736. filepath = os.path.join(api_results_dir, filename)
  737. try:
  738. with open(filepath, 'w', encoding='utf-8') as f:
  739. json.dump(api_call_info, f, ensure_ascii=False, indent=2)
  740. print(f"[RULES_API_RESULT] 保存规则引擎API结果文件: {filepath}")
  741. except Exception as e:
  742. print(f"[ERROR] 保存规则引擎API结果文件失败: {filepath}, 错误: {str(e)}")
  743. return {
  744. "success": False,
  745. "message": f"规则引擎API调用异常: {str(e)}"
  746. }
  747. except Exception as e:
  748. # 记录API调用结果(其他异常)
  749. end_time = datetime.now()
  750. call_id = f"rules_api_{knowledge_id}_{'{:.2f}'.format((end_time - start_time).total_seconds())}"
  751. api_call_info = {
  752. "call_id": call_id,
  753. "timestamp": end_time.isoformat(),
  754. "agent": "RulesEngineMetricCalculationAgent",
  755. "api_endpoint": url,
  756. "config_name": knowledge_id,
  757. "request": {
  758. "method": method,
  759. "url": url,
  760. "headers": headers,
  761. "json_data": json_data if 'json_data' in locals() else None,
  762. "start_time": start_time.isoformat()
  763. },
  764. "response": {
  765. "error": str(e),
  766. "end_time": end_time.isoformat(),
  767. "duration": (end_time - start_time).total_seconds()
  768. },
  769. "success": False
  770. }
  771. self.api_calls.append(api_call_info)
  772. # 保存API结果到文件
  773. api_results_dir = "api_results"
  774. os.makedirs(api_results_dir, exist_ok=True)
  775. filename = f"{call_id}.json"
  776. filepath = os.path.join(api_results_dir, filename)
  777. try:
  778. with open(filepath, 'w', encoding='utf-8') as f:
  779. json.dump(api_call_info, f, ensure_ascii=False, indent=2)
  780. print(f"[RULES_API_RESULT] 保存规则引擎API结果文件: {filepath}")
  781. except Exception as e:
  782. print(f"[ERROR] 保存规则引擎API结果文件失败: {filepath}, 错误: {str(e)}")
  783. return {
  784. "success": False,
  785. "message": f"处理规则引擎API调用时发生错误: {str(e)}"
  786. }