test_report_agent.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import os
  2. import json
  3. from hello_agents import HelloAgentsLLM, SimpleAgent
  4. from agents.agent_prompts import REPORT_AGENT_PROMPT
  5. if __name__ == "__main__":
  6. llm = HelloAgentsLLM()
  7. report_agent = SimpleAgent(
  8. name="ReportAgent",
  9. system_prompt=REPORT_AGENT_PROMPT,
  10. llm=llm,
  11. enable_tool_calling=False
  12. )
  13. task_result = [
  14. {
  15. 'task': '分析不同年龄段用户的偏好',
  16. 'result': {
  17. 'text': '各年龄段平均消费金额相近,均在58-61之间。商品类别偏好显示,所有年龄段均最偏好服装(Clothing),占比约44%-46%;其次是配饰(Accessories),占比约29%-34%;鞋类(Footwear)和外套(Outerwear)偏好相对较低。其中,20-30岁用户更偏好配饰,40-50岁用户更偏好鞋类,青少年(<20)和老年人(60+)对外套的偏好相对较高。',
  18. 'visualization_url': ['figures/age_group_distribution.png', 'figures/average_spending_by_age_group.png', 'figures/category_preference_by_age_group.png']
  19. }
  20. }
  21. ]
  22. print(f"\n任务结果: {task_result}")
  23. final_result = report_agent.run(json.dumps(task_result, ensure_ascii=False))
  24. # 清理报告内容,确保以"# 执行摘要"开头
  25. if "# 执行摘要" in final_result:
  26. # 找到"# 执行摘要"的位置
  27. start_idx = final_result.find("# 执行摘要")
  28. final_result = final_result[start_idx:]
  29. print(f"\n最终分析报告: \n{final_result}")
  30. # 保存报告到文件
  31. os.makedirs("out", exist_ok=True)
  32. with open("out/analysis_report.md", "w", encoding="utf-8") as f:
  33. f.write(final_result)