multi_agent_coordinator.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. """
  2. 多智能体协调器
  3. 管理天气查询智能体和穿衣建议智能体的协作
  4. """
  5. from hello_agents import SimpleAgent, HelloAgentsLLM
  6. from hello_agents.tools import MCPTool
  7. from fashion_agent import FashionAgent
  8. import os
  9. from dotenv import load_dotenv
  10. load_dotenv()
  11. class MultiAgentCoordinator:
  12. """多智能体协调器"""
  13. def __init__(self):
  14. """初始化协调器"""
  15. # 创建主协调智能体
  16. self.coordinator = SimpleAgent(
  17. name="智能体协调器",
  18. llm=HelloAgentsLLM(
  19. api_key=os.environ.get("LLM_API_KEY"),
  20. base_url=os.environ.get("LLM_BASE_URL"),
  21. model=os.environ.get("LLM_MODEL_ID")
  22. )
  23. )
  24. # 创建天气查询智能体
  25. self.weather_agent = self._create_weather_agent()
  26. # 创建穿衣建议智能体
  27. self.fashion_agent = FashionAgent()
  28. # 设置协调器的系统提示词
  29. self._setup_coordinator_prompt()
  30. def _create_weather_agent(self):
  31. """创建天气查询智能体"""
  32. weather_agent = SimpleAgent(
  33. name="天气查询助手",
  34. llm=HelloAgentsLLM(
  35. api_key=os.environ.get("LLM_API_KEY"),
  36. base_url=os.environ.get("LLM_BASE_URL"),
  37. model=os.environ.get("LLM_MODEL_ID")
  38. )
  39. )
  40. # 配置MCP工具使用本地的weather_mcp.py服务器
  41. mcp_tool = MCPTool(
  42. name="query_weather",
  43. server_command=["python", "weather_mcp.py"]
  44. )
  45. weather_agent.add_tool(mcp_tool)
  46. # 设置天气智能体的系统提示词
  47. weather_agent.system_prompt = """你是一个天气查询助手。你可以使用query_weather工具查询指定城市的天气信息。
  48. 请根据用户提供的城市名称查询天气,并返回详细的天气信息。"""
  49. return weather_agent
  50. def _setup_coordinator_prompt(self):
  51. """设置协调器的系统提示词"""
  52. system_prompt = """你是一个智能体协调器,负责管理天气查询智能体和穿衣建议智能体的协作。
  53. 你的工作流程:
  54. 1. 接收用户关于天气和穿衣建议的查询
  55. 2. 调用天气查询智能体获取指定城市的天气信息
  56. 3. 将天气信息传递给穿衣建议智能体
  57. 4. 整合两个智能体的结果,提供完整的天气和穿衣建议
  58. 协作规则:
  59. - 首先获取准确的天气信息
  60. - 然后基于天气信息提供专业的穿衣建议
  61. - 确保信息的准确性和实用性
  62. - 提供清晰、完整的最终结果
  63. 请按照这个流程处理用户的查询。"""
  64. self.coordinator.system_prompt = system_prompt
  65. def process_query(self, query):
  66. """
  67. 处理用户查询,协调多个智能体完成任务
  68. Args:
  69. query: 用户查询字符串
  70. Returns:
  71. 包含天气信息和穿衣建议的完整结果
  72. """
  73. print("=== 开始处理查询 ===")
  74. print(f"用户查询: {query}")
  75. print()
  76. # 步骤1: 使用天气智能体查询天气
  77. print("步骤1: 查询天气信息...")
  78. weather_response = self.weather_agent.run(query)
  79. print(f"天气查询结果: {weather_response}")
  80. print()
  81. # 步骤2: 使用穿衣建议智能体提供建议
  82. print("步骤2: 生成穿衣建议...")
  83. fashion_advice = self.fashion_agent.get_fashion_advice(weather_response)
  84. print(f"穿衣建议: {fashion_advice}")
  85. print()
  86. # 步骤3: 整合结果
  87. print("步骤3: 整合最终结果...")
  88. final_result = self._format_final_result(weather_response, fashion_advice)
  89. return final_result
  90. def _format_final_result(self, weather_info, fashion_advice):
  91. """
  92. 格式化最终结果
  93. Args:
  94. weather_info: 天气信息
  95. fashion_advice: 穿衣建议
  96. Returns:
  97. 格式化的完整结果
  98. """
  99. result = f"""🎯 智能体协作完成!以下是您的完整天气和穿衣建议:
  100. 🌤️ 天气信息:
  101. {weather_info}
  102. 👗 穿衣建议:
  103. {fashion_advice}
  104. 💡 温馨提示:
  105. - 请根据实际体感温度调整穿着
  106. - 考虑当天的具体活动安排
  107. - 如有特殊需求,可进一步咨询"""
  108. return result
  109. def get_weather_only(self, city_name):
  110. """
  111. 仅获取天气信息(不包含穿衣建议)
  112. Args:
  113. city_name: 城市名称
  114. Returns:
  115. 天气信息
  116. """
  117. query = f"查询{city_name}的天气"
  118. return self.weather_agent.run(query)
  119. def get_fashion_advice_only(self, weather_info):
  120. """
  121. 基于现有天气信息获取穿衣建议
  122. Args:
  123. weather_info: 天气信息字符串
  124. Returns:
  125. 穿衣建议
  126. """
  127. return self.fashion_agent.get_fashion_advice(weather_info)
  128. def main():
  129. """测试函数"""
  130. # 创建多智能体协调器
  131. coordinator = MultiAgentCoordinator()
  132. # 测试查询
  133. test_query = "查询上海的天气并给出穿衣建议"
  134. print("=== 多智能体协调器测试 ===")
  135. result = coordinator.process_query(test_query)
  136. print(result)
  137. if __name__ == "__main__":
  138. main()