优化天气查询时的健壮性

This commit is contained in:
weiweiw 2025-03-09 16:34:22 +08:00
parent c49b048976
commit bad876fc98
1 changed files with 24 additions and 21 deletions

View File

@ -19,7 +19,7 @@ def weather_check(
"- '今天':获取当前实时天气\n" "- '今天':获取当前实时天气\n"
"- '明天'/'后天'获取未来24/48小时预报\n" "- '明天'/'后天'获取未来24/48小时预报\n"
"- '未来X天'获取最多X天预报'未来3天',X的抽取要符合客户意图\n" "- '未来X天'获取最多X天预报'未来3天',X的抽取要符合客户意图\n"
"- 不支持其他参数如果是其他参数则时间参数为None" "- 不支持其他参数如果是其他参数则时间参数为None\n"
) )
): ):
"""用这个工具获取指定地点和指定时间的天气""" """用这个工具获取指定地点和指定时间的天气"""
@ -32,8 +32,7 @@ def weather_check(
missing_params.append("日期参数") missing_params.append("日期参数")
if missing_params: if missing_params:
return BaseToolOutput( return BaseToolOutput(data={"error_message": f"缺少必要参数:{', '.join(missing_params)},请补充完整查询信息"},
error_message=f"缺少必要参数:{', '.join(missing_params)},请补充完整查询信息",
require_additional_input=True require_additional_input=True
) )
@ -42,13 +41,13 @@ def weather_check(
weather_type, number = parse_date_parameter(date) weather_type, number = parse_date_parameter(date)
except ValueError as e: except ValueError as e:
logging.error(f"日期参数解析失败: {str(e)}") logging.error(f"日期参数解析失败: {str(e)}")
return BaseToolOutput(str(e)) return BaseToolOutput(data={"error_message": str(e)})
# 获取API配置 # 获取API配置
tool_config = get_tool_config("weather_check") tool_config = get_tool_config("weather_check")
api_key = tool_config.get("api_key") api_key = tool_config.get("api_key")
if not api_key: if not api_key:
return BaseToolOutput("API密钥未配置请联系管理员") return BaseToolOutput(data={"error_message": "API密钥未配置请联系管理员"})
# 根据天气类型调用API # 根据天气类型调用API
if weather_type == "daily": if weather_type == "daily":
@ -56,7 +55,8 @@ def weather_check(
elif weather_type == "future": elif weather_type == "future":
return _get_future_weather(city, api_key, number) return _get_future_weather(city, api_key, number)
else: else:
return BaseToolOutput("不支持的天气类型") return BaseToolOutput(data={"error_message": "不支持的天气类型"})
def _get_current_weather(city: str, api_key: str) -> BaseToolOutput: 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: if response.status_code != 200:
logging.error(f"天气查询失败: {response.status_code}") logging.error(f"天气查询失败: {response.status_code}")
return BaseToolOutput("天气查询API请求失败") return BaseToolOutput(data={"error_message": "天气查询API请求失败"})
data = response.json() data = response.json()
weather = { weather = {
"temperature": data["results"][0]["now"]["temperature"], "temperature": data["results"][0]["now"]["temperature"],
"description": data["results"][0]["now"]["text"], "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: 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"], "后天最高温度": daily_data[2]["high"],
} }
else: else:
return BaseToolOutput("不支持的天数参数") return BaseToolOutput(data={"error_message": "不支持的天数参数"})
return BaseToolOutput(data=weather)
return BaseToolOutput(weather)
def parse_date_parameter(date: str) -> tuple: def parse_date_parameter(date: str) -> tuple:
"""解析日期参数,返回天气类型和天数""" """解析日期参数,返回天气类型和天数"""
@ -136,5 +138,6 @@ def parse_date_parameter(date: str) -> tuple:
else: else:
raise ValueError("不支持的日期参数") raise ValueError("不支持的日期参数")
if __name__ == "__main__": if __name__ == "__main__":
weather_check("合肥", "明天") weather_check("合肥", "明天")