wheather_tool.py 806 B

1234567891011121314151617181920212223
  1. import requests
  2. from langchain_core.tools import tool
  3. @tool
  4. def get_weather(location: str) -> str:
  5. """
  6. 查询指定城市或地区的实时天气。
  7. 当用户询问某个城市、地区或国家的天气、温度、气候状况时,
  8. 应该调用这个工具。参数 location 是地点名称,例如:北京、上海、
  9. 成都、Tokyo、New York。如果用户没有提供地点,应该先追问地点。
  10. """
  11. url = f"https://wttr.in/{location}?format=j1&lang=zh"
  12. response = requests.get(url, timeout=10)
  13. response.raise_for_status()
  14. data = response.json()
  15. current = data["current_condition"][0]
  16. weather_desc = current["weatherDesc"][0]["value"]
  17. temp = current["temp_C"]
  18. return f"{location} 当前天气:{weather_desc},温度:{temp}°C"