Spaces:
Sleeping
Sleeping
import requests | |
from pydantic import BaseModel, Field | |
from config import settings | |
class WeatherInput(BaseModel): | |
city: str = Field(description="城市名稱(如 'Taipei'、'Tokyo'、'New York'}") | |
def get_weather(city): | |
""" | |
根據城市名取得即時天氣資訊 | |
:param city: 城市名稱(如 'Taipei'、'Tokyo'、'New York'} | |
:return: 天氣資訊字典,API 回傳格式 | |
""" | |
url = f"http://api.openweathermap.org/data/2.5/weather" | |
payload = { | |
"q": city, | |
"appid": settings.WEATHER_API_KEY, | |
"units": "metric", | |
"lang": "zh_tw", | |
} | |
response = requests.get(url, params=payload) | |
if response.status_code == 200: | |
return response.json() | |
else: | |
raise Exception(f"無法取得天氣資訊: {response.text}") | |