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