test_my_calculator.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # test_my_calculator.py
  2. from dotenv import load_dotenv
  3. from my_calculator_tool import create_calculator_registry
  4. # 加载环境变量
  5. load_dotenv()
  6. def test_calculator_tool():
  7. """测试自定义计算器工具"""
  8. # 创建包含计算器的注册表
  9. registry = create_calculator_registry()
  10. print("🧪 测试自定义计算器工具\n")
  11. # 简单测试用例
  12. test_cases = [
  13. "2 + 3", # 基本加法
  14. "10 - 4", # 基本减法
  15. "5 * 6", # 基本乘法
  16. "15 / 3", # 基本除法
  17. "sqrt(16)", # 平方根
  18. ]
  19. for i, expression in enumerate(test_cases, 1):
  20. print(f"测试 {i}: {expression}")
  21. result = registry.execute_tool("my_calculator", expression)
  22. print(f"结果: {result}\n")
  23. def test_with_simple_agent():
  24. """测试与SimpleAgent的集成"""
  25. from hello_agents import HelloAgentsLLM
  26. # 创建LLM客户端
  27. llm = HelloAgentsLLM()
  28. # 创建包含计算器的注册表
  29. registry = create_calculator_registry()
  30. print("🤖 与SimpleAgent集成测试:")
  31. # 模拟SimpleAgent使用工具的场景
  32. user_question = "请帮我计算 sqrt(16) + 2 * 3"
  33. print(f"用户问题: {user_question}")
  34. # 使用工具计算
  35. calc_result = registry.execute_tool("my_calculator", "sqrt(16) + 2 * 3")
  36. print(f"计算结果: {calc_result}")
  37. # 构建最终回答
  38. final_messages = [
  39. {"role": "user", "content": f"计算结果是 {calc_result},请用自然语言回答用户的问题:{user_question}"}
  40. ]
  41. print("\n🎯 SimpleAgent的回答:")
  42. response = llm.think(final_messages)
  43. for chunk in response:
  44. print(chunk, end="", flush=True)
  45. print("\n")
  46. if __name__ == "__main__":
  47. test_calculator_tool()
  48. test_with_simple_agent()