优化天气查询时的健壮性
This commit is contained in:
parent
c49b048976
commit
bad876fc98
|
|
@ -12,15 +12,15 @@ from .tools_registry import BaseToolOutput, regist_tool
|
|||
|
||||
@regist_tool(title="天气查询")
|
||||
def weather_check(
|
||||
city: str = Field(description="城市名称,包括市和县,例如 '厦门'"),
|
||||
date: str = Field(
|
||||
default=None,
|
||||
description="日期参数,支持以下格式:\n"
|
||||
"- '今天':获取当前实时天气\n"
|
||||
"- '明天'/'后天':获取未来24/48小时预报\n"
|
||||
"- '未来X天':获取最多X天预报(如'未来3天'),X的抽取要符合客户意图\n"
|
||||
"- 不支持其他参数,如果是其他参数,则时间参数为None"
|
||||
)
|
||||
city: str = Field(description="城市名称,包括市和县,例如 '厦门'"),
|
||||
date: str = Field(
|
||||
default=None,
|
||||
description="日期参数,支持以下格式:\n"
|
||||
"- '今天':获取当前实时天气\n"
|
||||
"- '明天'/'后天':获取未来24/48小时预报\n"
|
||||
"- '未来X天':获取最多X天预报(如'未来3天'),X的抽取要符合客户意图\n"
|
||||
"- 不支持其他参数,如果是其他参数,则时间参数为None\n"
|
||||
)
|
||||
):
|
||||
"""用这个工具获取指定地点和指定时间的天气"""
|
||||
|
||||
|
|
@ -32,23 +32,22 @@ def weather_check(
|
|||
missing_params.append("日期参数")
|
||||
|
||||
if missing_params:
|
||||
return BaseToolOutput(
|
||||
error_message=f"缺少必要参数:{', '.join(missing_params)},请补充完整查询信息",
|
||||
require_additional_input=True
|
||||
)
|
||||
return BaseToolOutput(data={"error_message": f"缺少必要参数:{', '.join(missing_params)},请补充完整查询信息"},
|
||||
require_additional_input=True
|
||||
)
|
||||
|
||||
print(f"city:{city}, date:{date}")
|
||||
try:
|
||||
weather_type, number = parse_date_parameter(date)
|
||||
except ValueError as e:
|
||||
logging.error(f"日期参数解析失败: {str(e)}")
|
||||
return BaseToolOutput(str(e))
|
||||
return BaseToolOutput(data={"error_message": str(e)})
|
||||
|
||||
# 获取API配置
|
||||
tool_config = get_tool_config("weather_check")
|
||||
api_key = tool_config.get("api_key")
|
||||
if not api_key:
|
||||
return BaseToolOutput("API密钥未配置,请联系管理员")
|
||||
return BaseToolOutput(data={"error_message": "API密钥未配置,请联系管理员"})
|
||||
|
||||
# 根据天气类型调用API
|
||||
if weather_type == "daily":
|
||||
|
|
@ -56,7 +55,8 @@ def weather_check(
|
|||
elif weather_type == "future":
|
||||
return _get_future_weather(city, api_key, number)
|
||||
else:
|
||||
return BaseToolOutput("不支持的天气类型")
|
||||
return BaseToolOutput(data={"error_message": "不支持的天气类型"})
|
||||
|
||||
|
||||
def _get_current_weather(city: str, api_key: str) -> BaseToolOutput:
|
||||
"""获取当前实时天气"""
|
||||
|
|
@ -66,14 +66,15 @@ def _get_current_weather(city: str, api_key: str) -> BaseToolOutput:
|
|||
|
||||
if response.status_code != 200:
|
||||
logging.error(f"天气查询失败: {response.status_code}")
|
||||
return BaseToolOutput("天气查询API请求失败")
|
||||
return BaseToolOutput(data={"error_message": "天气查询API请求失败"})
|
||||
|
||||
data = response.json()
|
||||
weather = {
|
||||
"temperature": data["results"][0]["now"]["temperature"],
|
||||
"description": data["results"][0]["now"]["text"],
|
||||
}
|
||||
return BaseToolOutput(weather)
|
||||
return BaseToolOutput(data=weather)
|
||||
|
||||
|
||||
def _get_future_weather(city: str, api_key: str, days: int) -> BaseToolOutput:
|
||||
"""获取未来天气预报"""
|
||||
|
|
@ -115,9 +116,10 @@ def _get_future_weather(city: str, api_key: str, days: int) -> BaseToolOutput:
|
|||
"后天最高温度": daily_data[2]["high"],
|
||||
}
|
||||
else:
|
||||
return BaseToolOutput("不支持的天数参数")
|
||||
return BaseToolOutput(data={"error_message": "不支持的天数参数"})
|
||||
|
||||
return BaseToolOutput(data=weather)
|
||||
|
||||
return BaseToolOutput(weather)
|
||||
|
||||
def parse_date_parameter(date: str) -> tuple:
|
||||
"""解析日期参数,返回天气类型和天数"""
|
||||
|
|
@ -136,5 +138,6 @@ def parse_date_parameter(date: str) -> tuple:
|
|||
else:
|
||||
raise ValueError("不支持的日期参数")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
weather_check("合肥","明天")
|
||||
weather_check("合肥", "明天")
|
||||
|
|
|
|||
Loading…
Reference in New Issue