Spaces:
Sleeping
Sleeping
Delete weather_query.py
Browse files- weather_query.py +0 -71
weather_query.py
DELETED
|
@@ -1,71 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import requests
|
| 3 |
-
from lagent.actions.base_action import BaseAction, tool_api
|
| 4 |
-
from lagent.schema import ActionReturn, ActionStatusCode
|
| 5 |
-
|
| 6 |
-
class WeatherQuery(BaseAction):
|
| 7 |
-
def __init__(self):
|
| 8 |
-
super().__init__()
|
| 9 |
-
self.api_key = os.getenv("weather_token")
|
| 10 |
-
print(self.api_key)
|
| 11 |
-
if not self.api_key:
|
| 12 |
-
raise EnvironmentError("未找到环境变量 'token'。请设置你的和风天气 API Key 到 'weather_token' 环境变量中,比如export weather_token='xxx' ")
|
| 13 |
-
|
| 14 |
-
@tool_api
|
| 15 |
-
def run(self, location: str) -> dict:
|
| 16 |
-
"""
|
| 17 |
-
查询实时天气信息。
|
| 18 |
-
|
| 19 |
-
Args:
|
| 20 |
-
location (str): 要查询的地点名称、LocationID 或经纬度坐标(如 "101010100" 或 "116.41,39.92")。
|
| 21 |
-
|
| 22 |
-
Returns:
|
| 23 |
-
dict: 包含天气信息的字典
|
| 24 |
-
* location: 地点名称
|
| 25 |
-
* weather: 天气状况
|
| 26 |
-
* temperature: 当前温度
|
| 27 |
-
* wind_direction: 风向
|
| 28 |
-
* wind_speed: 风速(公里/小时)
|
| 29 |
-
* humidity: 相对湿度(%)
|
| 30 |
-
* report_time: 数据报告时间
|
| 31 |
-
"""
|
| 32 |
-
try:
|
| 33 |
-
# 如果 location 不是坐标格式(例如 "116.41,39.92"),则调用 GeoAPI 获取 LocationID
|
| 34 |
-
if not ("," in location and location.replace(",", "").replace(".", "").isdigit()):
|
| 35 |
-
# 使用 GeoAPI 获取 LocationID
|
| 36 |
-
geo_url = f"https://geoapi.qweather.com/v2/city/lookup?location={location}&key={self.api_key}"
|
| 37 |
-
geo_response = requests.get(geo_url)
|
| 38 |
-
geo_data = geo_response.json()
|
| 39 |
-
|
| 40 |
-
if geo_data.get("code") != "200" or not geo_data.get("location"):
|
| 41 |
-
raise Exception(f"GeoAPI 返回错误码:{geo_data.get('code')} 或未找到位置")
|
| 42 |
-
|
| 43 |
-
location = geo_data["location"][0]["id"]
|
| 44 |
-
|
| 45 |
-
# 构建天气查询的 API 请求 URL
|
| 46 |
-
weather_url = f"https://devapi.qweather.com/v7/weather/now?location={location}&key={self.api_key}"
|
| 47 |
-
response = requests.get(weather_url)
|
| 48 |
-
data = response.json()
|
| 49 |
-
|
| 50 |
-
# 检查 API 响应码
|
| 51 |
-
if data.get("code") != "200":
|
| 52 |
-
raise Exception(f"Weather API 返回错误码:{data.get('code')}")
|
| 53 |
-
|
| 54 |
-
# 解析和组织天气信息
|
| 55 |
-
weather_info = {
|
| 56 |
-
"location": location,
|
| 57 |
-
"weather": data["now"]["text"],
|
| 58 |
-
"temperature": data["now"]["temp"] + "°C",
|
| 59 |
-
"wind_direction": data["now"]["windDir"],
|
| 60 |
-
"wind_speed": data["now"]["windSpeed"] + " km/h",
|
| 61 |
-
"humidity": data["now"]["humidity"] + "%",
|
| 62 |
-
"report_time": data["updateTime"]
|
| 63 |
-
}
|
| 64 |
-
|
| 65 |
-
return {"result": weather_info}
|
| 66 |
-
|
| 67 |
-
except Exception as exc:
|
| 68 |
-
return ActionReturn(
|
| 69 |
-
errmsg=f"WeatherQuery 异常:{exc}",
|
| 70 |
-
state=ActionStatusCode.HTTP_ERROR
|
| 71 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|