Spaces:
Runtime error
Runtime error
Download test_agent_workflow.py from Macbath/indicator-modeling: direct link, hf CLI and curl.
- Browser
- Download file 6.29 kB
-
https://huggingface.co/spaces/Macbath/indicator-modeling/resolve/main/test_agent_workflow.py
- Command line
-
hf download hf://spaces/Macbath/indicator-modeling/test_agent_workflow.py
-
curl -L -o test_agent_workflow.py https://huggingface.co/spaces/Macbath/indicator-modeling/resolve/main/test_agent_workflow.py
6.29 kB
| """ | |
| 测试脚本 - 验证 RequirementAgent 和 RecommendationAgent 的工作流 | |
| """ | |
| import sys | |
| import os | |
| # 添加项目根目录到路径 | |
| sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) | |
| from agents.requirement_agent import RequirementAgent | |
| from agents.recommendation_agent import RecommendationAgent | |
| from models.indicator import Indicator | |
| def test_requirement_agent_parse(): | |
| """测试 RequirementAgent 的响应解析""" | |
| print("=" * 60) | |
| print("测试 1: RequirementAgent._parse_response()") | |
| print("=" * 60) | |
| agent = RequirementAgent(api_key="test_key") | |
| # 测试1: 正常响应 | |
| test_response_1 = ''' | |
| ```json | |
| { | |
| "business_domain": "电商", | |
| "analysis_purpose": "分析用户购买行为,优化商品推荐策略" | |
| } | |
| ``` | |
| ''' | |
| try: | |
| result = agent._parse_response(test_response_1) | |
| assert "business_domain" in result | |
| assert "analysis_purpose" in result | |
| assert result["business_domain"] == "电商" | |
| assert "分析用户购买行为" in result["analysis_purpose"] | |
| print("✅ 测试1.1 通过:正常解析 JSON 响应") | |
| print(f" 结果: {result}") | |
| except Exception as e: | |
| print(f"❌ 测试1.1 失败: {e}") | |
| return False | |
| # 测试2: 无代码块的响应 | |
| test_response_2 = ''' | |
| { | |
| "business_domain": "金融", | |
| "analysis_purpose": "监控风险指标" | |
| } | |
| ''' | |
| try: | |
| result = agent._parse_response(test_response_2) | |
| assert result["business_domain"] == "金融" | |
| print("✅ 测试1.2 通过:解析无代码块的响应") | |
| except Exception as e: | |
| print(f"❌ 测试1.2 失败: {e}") | |
| return False | |
| # 测试3: 缺少字段时使用默认值 | |
| test_response_3 = ''' | |
| { | |
| "business_domain": "" | |
| } | |
| ''' | |
| try: | |
| result = agent._parse_response(test_response_3) | |
| assert result["business_domain"] == "通用" | |
| assert "分析业务指标" in result["analysis_purpose"] | |
| print("✅ 测试1.3 通过:缺少字段时使用默认值") | |
| except Exception as e: | |
| print(f"❌ 测试1.3 失败: {e}") | |
| return False | |
| print("\n✅ 所有 RequirementAgent 测试通过!\n") | |
| return True | |
| def test_recommendation_agent_run(): | |
| """测试 RecommendationAgent.run() 的输入参数""" | |
| print("=" * 60) | |
| print("测试 2: RecommendationAgent.run()") | |
| print("=" * 60) | |
| agent = RecommendationAgent(api_key="test_key") | |
| # 测试1: 正确输入 | |
| input_data = { | |
| "business_domain": "电商", | |
| "analysis_purpose": "分析用户购买行为", | |
| "top_k": 8 | |
| } | |
| try: | |
| # 验证输入验证通过 | |
| is_valid = agent._validate_input(input_data) | |
| assert is_valid == True | |
| print("✅ 测试2.1 通过:输入验证通过") | |
| except Exception as e: | |
| print(f"❌ 测试2.1 失败: {e}") | |
| return False | |
| # 测试2: 缺少必需字段 | |
| input_data_invalid = { | |
| "business_domain": "电商" | |
| # 缺少 analysis_purpose | |
| } | |
| try: | |
| is_valid = agent._validate_input(input_data_invalid) | |
| assert is_valid == False | |
| print("✅ 测试2.2 通过:缺少字段时验证失败") | |
| except Exception as e: | |
| print(f"❌ 测试2.2 失败: {e}") | |
| return False | |
| print("\n✅ 所有 RecommendationAgent 输入验证测试通过!\n") | |
| return True | |
| def test_app_step1_logic(): | |
| """测试 app.py 中 Step 1 的逻辑流程""" | |
| print("=" * 60) | |
| print("测试 3: app.py Step 1 逻辑流程") | |
| print("=" * 60) | |
| # 模拟 Step 1 的输出 | |
| mock_req_result = { | |
| "status": "success", | |
| "data": { | |
| "business_domain": "电商", | |
| "analysis_purpose": "分析用户购买行为" | |
| } | |
| } | |
| # 模拟 Step 1 的逻辑 | |
| try: | |
| if mock_req_result and mock_req_result.get("status") == "success": | |
| req_data = mock_req_result.get("data", {}) | |
| business_domain = req_data.get("business_domain", "通用") | |
| analysis_purpose = req_data.get("analysis_purpose", "") | |
| # 验证提取的字段 | |
| assert business_domain == "电商" | |
| assert analysis_purpose == "分析用户购买行为" | |
| print("✅ 测试3.1 通过:成功提取 business_domain 和 analysis_purpose") | |
| # 模拟传递给 RecommendationAgent | |
| rec_input = { | |
| "business_domain": business_domain, | |
| "analysis_purpose": analysis_purpose, | |
| "top_k": 8 | |
| } | |
| assert "business_domain" in rec_input | |
| assert "analysis_purpose" in rec_input | |
| print("✅ 测试3.2 通过:成功构建 RecommendationAgent 输入") | |
| else: | |
| print("❌ 测试3 失败:模拟的 RequirementAgent 结果不正确") | |
| return False | |
| except Exception as e: | |
| print(f"❌ 测试3 失败: {e}") | |
| return False | |
| print("\n✅ 所有 app.py Step 1 逻辑测试通过!\n") | |
| return True | |
| def main(): | |
| """运行所有测试""" | |
| print("\n" + "=" * 60) | |
| print("开始测试 Agent 工作流") | |
| print("=" * 60 + "\n") | |
| results = [] | |
| # 运行测试 | |
| results.append(("RequirementAgent 解析", test_requirement_agent_parse())) | |
| results.append(("RecommendationAgent 输入验证", test_recommendation_agent_run())) | |
| results.append(("app.py Step 1 逻辑", test_app_step1_logic())) | |
| # 汇总结果 | |
| print("=" * 60) | |
| print("测试汇总") | |
| print("=" * 60) | |
| all_passed = True | |
| for name, passed in results: | |
| status = "✅ 通过" if passed else "❌ 失败" | |
| print(f"{status}: {name}") | |
| if not passed: | |
| all_passed = False | |
| print("\n" + "=" * 60) | |
| if all_passed: | |
| print("🎉 所有测试通过!Agent 工作流修改正确。") | |
| else: | |
| print("⚠️ 部分测试失败,请检查代码。") | |
| print("=" * 60 + "\n") | |
| return 0 if all_passed else 1 | |
| if __name__ == "__main__": | |
| exit_code = main() | |
| sys.exit(exit_code) | |