test_simple_agent.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # test_simple_agent.py
  2. from dotenv import load_dotenv
  3. from hello_agents import HelloAgentsLLM, ToolRegistry
  4. from hello_agents.tools import CalculatorTool
  5. from my_simple_agent import MySimpleAgent
  6. # 加载环境变量
  7. load_dotenv()
  8. # 创建LLM实例
  9. llm = HelloAgentsLLM()
  10. # 测试1:基础对话Agent(无工具)
  11. print("=== 测试1:基础对话 ===")
  12. basic_agent = MySimpleAgent(
  13. name="基础助手",
  14. llm=llm,
  15. system_prompt="你是一个友好的AI助手,请用简洁明了的方式回答问题。"
  16. )
  17. response1 = basic_agent.run("你好,请介绍一下自己")
  18. print(f"基础对话响应: {response1}\n")
  19. # 测试2:带工具的Agent
  20. print("=== 测试2:工具增强对话 ===")
  21. tool_registry = ToolRegistry()
  22. calculator = CalculatorTool()
  23. tool_registry.register_tool(calculator)
  24. enhanced_agent = MySimpleAgent(
  25. name="增强助手",
  26. llm=llm,
  27. system_prompt="你是一个智能助手,可以使用工具来帮助用户。",
  28. tool_registry=tool_registry,
  29. enable_tool_calling=True
  30. )
  31. response2 = enhanced_agent.run("请帮我计算 15 * 8 + 32")
  32. print(f"工具增强响应: {response2}\n")
  33. # 测试3:流式响应
  34. print("=== 测试3:流式响应 ===")
  35. print("流式响应: ", end="")
  36. for chunk in basic_agent.stream_run("请解释什么是人工智能"):
  37. pass # 内容已在stream_run中实时打印
  38. # 测试4:动态添加工具
  39. print("\n=== 测试4:动态工具管理 ===")
  40. print(f"添加工具前: {basic_agent.has_tools()}")
  41. basic_agent.add_tool(calculator)
  42. print(f"添加工具后: {basic_agent.has_tools()}")
  43. print(f"可用工具: {basic_agent.list_tools()}")
  44. # 查看对话历史
  45. print(f"\n对话历史: {len(basic_agent.get_history())} 条消息")