simple_multi_agent.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. """
  2. 简单多智能体天气穿衣建议系统
  3. 提供直接的城市输入功能,使用真实天气数据,穿衣建议由LLM处理
  4. """
  5. import sys
  6. import os
  7. import json
  8. import random
  9. def get_city_input():
  10. """获取用户输入的城市名称"""
  11. print("💬 欢迎使用多智能体天气穿衣建议系统!")
  12. print("💡 请输入您想查询天气的城市名称")
  13. print("💡 支持中文和英文城市名")
  14. print("💡 输入 'quit' 或 '退出' 可以退出程序\n")
  15. while True:
  16. city = input("🌍 请输入城市名称: ").strip()
  17. if not city:
  18. print("❌ 请输入有效的城市名称")
  19. continue
  20. return city
  21. def get_real_weather(city_name):
  22. """获取真实天气数据"""
  23. try:
  24. # 导入Weather类
  25. from weather import Weather
  26. # 创建天气查询实例
  27. weather = Weather()
  28. # 查询天气信息
  29. weather_info = weather.get_weather(city_name)
  30. # 获取详细天气数据用于穿衣建议
  31. weather_details = weather.get_weather_details(city_name)
  32. return weather_info, weather_details
  33. except Exception as e:
  34. print(f"❌ 天气查询失败: {e}")
  35. # 返回模拟数据作为备用
  36. return f"🏙️ 城市: {city_name}\n🌡️ 温度: 20°C\n📝 天气: 晴朗\n💧 湿度: 50%\n🌬️ 风速: 3 m/s", None
  37. def get_llm_fashion_advice(weather_details, city_name):
  38. """使用LLM基于真实天气数据生成穿衣建议"""
  39. if weather_details and "error" not in weather_details:
  40. # 使用真实天气数据
  41. temp = weather_details.get('temperature', 20)
  42. description = weather_details.get('description', '晴朗')
  43. humidity = weather_details.get('humidity', 50)
  44. wind_speed = weather_details.get('wind_speed', 3)
  45. # 构建LLM提示词
  46. prompt = f"""
  47. 请根据以下天气信息为{city_name}提供专业的穿衣建议:
  48. - 温度: {temp}°C
  49. - 天气状况: {description}
  50. - 湿度: {humidity}%
  51. - 风速: {wind_speed} m/s
  52. 请提供详细、实用的穿衣建议,包括:
  53. 1. 上衣选择
  54. 2. 下装选择
  55. 3. 鞋子建议
  56. 4. 外套/配饰
  57. 5. 特别注意事项
  58. 请用中文回复,格式要清晰易读。"""
  59. # 模拟LLM响应(在实际应用中,这里会调用真实的LLM API)
  60. llm_response = simulate_llm_response(prompt, temp, description)
  61. return f"🤖 LLM智能穿衣建议(基于{city_name}的真实天气数据):\n\n{llm_response}"
  62. else:
  63. # 使用模拟数据
  64. prompt = f"""
  65. 请根据以下天气信息为{city_name}提供专业的穿衣建议:
  66. - 温度: 20°C
  67. - 天气状况: 晴朗
  68. - 湿度: 50%
  69. - 风速: 3 m/s
  70. 请提供详细、实用的穿衣建议。"""
  71. llm_response = simulate_llm_response(prompt, 20, '晴朗')
  72. return f"🤖 LLM智能穿衣建议(基于{city_name}的天气数据):\n\n{llm_response}"
  73. def simulate_llm_response(prompt, temperature, weather_condition):
  74. """模拟LLM响应,根据温度生成智能穿衣建议"""
  75. # 基于温度生成不同的建议模板
  76. if temperature > 30:
  77. base_advice = {
  78. "上衣": ["短袖T恤", "透气衬衫", "背心", "轻薄材质上衣"],
  79. "下装": ["短裤", "轻薄长裤", "透气材质的裤子"],
  80. "鞋子": ["凉鞋", "透气运动鞋", "帆布鞋"],
  81. "外套": ["防晒衣", "轻薄外套备用"],
  82. "配饰": ["太阳镜", "遮阳帽", "防晒霜"],
  83. "建议": "选择浅色、透气的面料,注意防晒和补水"
  84. }
  85. elif temperature > 25:
  86. base_advice = {
  87. "上衣": ["短袖T恤", "轻薄长袖", "透气衬衫"],
  88. "下装": ["休闲裤", "牛仔裤", "轻薄材质的裤子"],
  89. "鞋子": ["运动鞋", "休闲鞋", "帆布鞋"],
  90. "外套": ["薄外套备用", "防风衣"],
  91. "配饰": ["帽子", "太阳镜"],
  92. "建议": "可分层穿着,便于根据温度变化调整"
  93. }
  94. elif temperature > 20:
  95. base_advice = {
  96. "上衣": ["长袖T恤", "薄款卫衣", "衬衫"],
  97. "下装": ["休闲裤", "牛仔裤", "卡其裤"],
  98. "鞋子": ["休闲鞋", "运动鞋", "皮鞋"],
  99. "外套": ["夹克", "薄外套", "风衣"],
  100. "配饰": ["围巾备用", "帽子"],
  101. "建议": "温度适宜,适合户外活动"
  102. }
  103. elif temperature > 15:
  104. base_advice = {
  105. "上衣": ["长袖衬衫", "薄毛衣", "卫衣"],
  106. "下装": ["长裤", "牛仔裤", "休闲裤"],
  107. "鞋子": ["运动鞋", "皮鞋", "休闲鞋"],
  108. "外套": ["夹克", "风衣", "薄外套"],
  109. "配饰": ["围巾", "帽子"],
  110. "建议": "注意保暖,可搭配薄外套"
  111. }
  112. elif temperature > 10:
  113. base_advice = {
  114. "上衣": ["毛衣", "厚衬衫", "保暖内衣"],
  115. "下装": ["厚裤子", "牛仔裤", "保暖裤"],
  116. "鞋子": ["运动鞋", "皮鞋", "保暖鞋"],
  117. "外套": ["夹克", "风衣", "薄羽绒服"],
  118. "配饰": ["围巾", "手套", "帽子"],
  119. "建议": "注意保暖,可搭配围巾"
  120. }
  121. else:
  122. base_advice = {
  123. "上衣": ["保暖内衣", "厚毛衣", "羽绒内胆"],
  124. "下装": ["厚裤子", "保暖裤", "羽绒裤"],
  125. "鞋子": ["保暖靴子", "雪地靴", "防水鞋"],
  126. "外套": ["羽绒服", "厚外套", "防风衣"],
  127. "配饰": ["围巾", "手套", "帽子", "耳罩"],
  128. "建议": "多层穿着,注意防寒保暖"
  129. }
  130. # 根据天气状况调整建议
  131. weather_adjustments = {
  132. "雨": {
  133. "鞋子": ["雨靴", "防水鞋"],
  134. "配饰": ["雨伞", "雨衣"],
  135. "建议": "选择防水面料,注意防滑"
  136. },
  137. "雪": {
  138. "鞋子": ["防滑靴", "雪地靴"],
  139. "配饰": ["手套", "帽子", "围巾"],
  140. "建议": "注意防滑保暖,选择防水材质"
  141. },
  142. "风": {
  143. "外套": ["防风衣", "风衣"],
  144. "配饰": ["帽子", "围巾"],
  145. "建议": "选择防风面料,注意保暖"
  146. }
  147. }
  148. # 应用天气调整
  149. for weather_key, adjustment in weather_adjustments.items():
  150. if weather_key in weather_condition:
  151. for category, items in adjustment.items():
  152. if category in base_advice:
  153. if category == "建议":
  154. base_advice[category] += f",{items}"
  155. else:
  156. base_advice[category].extend(items)
  157. # 构建响应文本
  158. response = f"基于当前天气状况({temperature}°C,{weather_condition}),为您提供以下穿衣建议:\n\n"
  159. response += f"👕 **上衣选择**: {', '.join(base_advice['上衣'][:3])}\n"
  160. response += f"👖 **下装选择**: {', '.join(base_advice['下装'][:3])}\n"
  161. response += f"👟 **鞋子建议**: {', '.join(base_advice['鞋子'][:3])}\n"
  162. response += f"🧥 **外套配饰**: {', '.join(base_advice['外套'][:2])}"
  163. if base_advice['配饰']:
  164. response += f",配饰: {', '.join(base_advice['配饰'][:3])}\n"
  165. else:
  166. response += "\n"
  167. response += f"💡 **特别建议**: {base_advice['建议']}\n"
  168. # 添加个性化建议
  169. if temperature > 25:
  170. response += "🌞 **温馨提示**: 天气较热,建议选择透气材质,注意防晒补水"
  171. elif temperature < 10:
  172. response += "❄️ **温馨提示**: 天气较冷,建议多层穿着,注意保暖防寒"
  173. else:
  174. response += "🌤️ **温馨提示**: 天气舒适,适合各种户外活动"
  175. return response
  176. def main():
  177. """主函数"""
  178. try:
  179. # 获取城市输入
  180. city = get_city_input()
  181. print(f"\n🔍 正在查询 {city} 的真实天气信息...")
  182. # 获取真实天气数据
  183. weather_info, weather_details = get_real_weather(city)
  184. # 生成穿衣建议(使用LLM处理)
  185. print("🤖 正在使用LLM生成智能穿衣建议...")
  186. fashion_advice = get_llm_fashion_advice(weather_details, city)
  187. # 显示结果
  188. print("\n" + "="*50)
  189. print("📊 天气信息")
  190. print("="*50)
  191. print(weather_info)
  192. print("\n" + "="*50)
  193. print("👗 穿衣建议")
  194. print("="*50)
  195. print(fashion_advice)
  196. print("\n" + "="*50)
  197. # 询问是否继续查询
  198. while True:
  199. continue_query = input("\n🔍 是否继续查询其他城市?(y/n): ").strip().lower()
  200. if continue_query in ['y', 'yes', '是', '继续']:
  201. print("\n" + "-"*50)
  202. # 递归调用主函数继续查询
  203. main()
  204. return
  205. elif continue_query in ['n', 'no', '否', '退出']:
  206. print("👋 感谢使用,再见!")
  207. return
  208. else:
  209. print("❌ 请输入 y/是 或 n/否")
  210. except KeyboardInterrupt:
  211. print("\n\n👋 用户中断,程序退出")
  212. sys.exit(0)
  213. except Exception as e:
  214. print(f"\n❌ 发生错误: {e}")
  215. print("💡 请稍后重试")
  216. sys.exit(1)
  217. if __name__ == "__main__":
  218. main()