test_react_agent.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # test_react_agent.py
  2. from dotenv import load_dotenv
  3. from hello_agents import HelloAgentsLLM, ToolRegistry
  4. from my_react_agent import MyReActAgent
  5. # 加载环境变量
  6. load_dotenv()
  7. def test_react_agent():
  8. """测试MyReActAgent的功能"""
  9. # 创建LLM实例
  10. llm = HelloAgentsLLM()
  11. # 创建工具注册表
  12. tool_registry = ToolRegistry()
  13. # 注册一些基础工具用于测试
  14. print("🔧 注册测试工具...")
  15. # 注册计算器工具
  16. try:
  17. from hello_agents import calculate
  18. tool_registry.register_function("calculate", "执行数学计算,支持基本的四则运算", calculate)
  19. print("✅ 计算器工具注册成功")
  20. except ImportError:
  21. print("⚠️ 计算器工具未找到,跳过注册")
  22. # 注册搜索工具(如果可用)
  23. try:
  24. from hello_agents import search
  25. tool_registry.register_function("search", "搜索互联网信息", search)
  26. print("✅ 搜索工具注册成功")
  27. except ImportError:
  28. print("⚠️ 搜索工具未找到,跳过注册")
  29. # 创建自定义ReActAgent
  30. agent = MyReActAgent(
  31. name="我的推理行动助手",
  32. llm=llm,
  33. tool_registry=tool_registry,
  34. max_steps=5
  35. )
  36. print("\n" + "="*60)
  37. print("开始测试 MyReActAgent")
  38. print("="*60)
  39. # 测试1:数学计算问题
  40. print("\n📊 测试1:数学计算问题")
  41. math_question = "请帮我计算:(25 + 15) × 3 - 8 的结果是多少?"
  42. try:
  43. result1 = agent.run(math_question)
  44. print(f"\n🎯 测试1结果: {result1}")
  45. except Exception as e:
  46. print(f"❌ 测试1失败: {e}")
  47. # 测试2:需要搜索的问题
  48. print("\n🔍 测试2:信息搜索问题")
  49. search_question = "Python编程语言是什么时候发布的?请告诉我具体的年份。"
  50. try:
  51. result2 = agent.run(search_question)
  52. print(f"\n🎯 测试2结果: {result2}")
  53. except Exception as e:
  54. print(f"❌ 测试2失败: {e}")
  55. # 测试3:复合问题(需要多步推理)
  56. print("\n🧠 测试3:复合推理问题")
  57. complex_question = "如果一个班级有30个学生,其中60%是女生,那么男生有多少人?请先计算女生人数,再计算男生人数。"
  58. try:
  59. result3 = agent.run(complex_question)
  60. print(f"\n🎯 测试3结果: {result3}")
  61. except Exception as e:
  62. print(f"❌ 测试3失败: {e}")
  63. # 查看对话历史
  64. print(f"\n📝 对话历史记录: {len(agent.get_history())} 条消息")
  65. # 显示工具使用统计
  66. print(f"\n🛠️ 可用工具数量: {len(tool_registry.tools)}")
  67. print("已注册的工具:")
  68. for tool_name in tool_registry.tools.keys():
  69. print(f" - {tool_name}")
  70. print("\n🎉 测试完成!")
  71. def test_custom_prompt():
  72. """测试自定义提示词的ReActAgent"""
  73. print("\n" + "="*60)
  74. print("测试自定义提示词的 MyReActAgent")
  75. print("="*60)
  76. # 创建LLM和工具注册表
  77. llm = HelloAgentsLLM()
  78. tool_registry = ToolRegistry()
  79. # 注册计算器工具
  80. try:
  81. from hello_agents import calculate
  82. tool_registry.register_tool("calculate", calculate, "数学计算工具")
  83. except ImportError:
  84. pass
  85. # 自定义提示词(更简洁的版本)
  86. custom_prompt = """你是一个数学专家AI助手。
  87. 可用工具:{tools}
  88. 请按以下格式回应:
  89. Thought: [你的思考]
  90. Action: [tool_name[input] 或 Finish[答案]]
  91. 问题:{question}
  92. 历史:{history}
  93. 开始:"""
  94. # 创建使用自定义提示词的Agent
  95. custom_agent = MyReActAgent(
  96. name="数学专家助手",
  97. llm=llm,
  98. tool_registry=tool_registry,
  99. max_steps=3,
  100. custom_prompt=custom_prompt
  101. )
  102. # 测试数学问题
  103. math_question = "计算 15 × 8 + 32 ÷ 4 的结果"
  104. try:
  105. result = custom_agent.run(math_question)
  106. print(f"\n🎯 自定义提示词测试结果: {result}")
  107. except Exception as e:
  108. print(f"❌ 自定义提示词测试失败: {e}")
  109. if __name__ == "__main__":
  110. # 运行基础测试
  111. test_react_agent()
  112. # 运行自定义提示词测试
  113. test_custom_prompt()
  114. print("\n✨ 所有测试完成!")