From bad876fc9849aa29fc7168e01ce1b57579d4fce2 Mon Sep 17 00:00:00 2001 From: weiweiw <14335254+weiweiw22@user.noreply.gitee.com> Date: Sun, 9 Mar 2025 16:34:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=A4=A9=E6=B0=94=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E6=97=B6=E7=9A=84=E5=81=A5=E5=A3=AE=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../agent/tools_factory/weather_check.py | 45 ++++++++++--------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/libs/chatchat-server/chatchat/server/agent/tools_factory/weather_check.py b/libs/chatchat-server/chatchat/server/agent/tools_factory/weather_check.py index 510535a..eb73b63 100644 --- a/libs/chatchat-server/chatchat/server/agent/tools_factory/weather_check.py +++ b/libs/chatchat-server/chatchat/server/agent/tools_factory/weather_check.py @@ -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("合肥","明天") \ No newline at end of file + weather_check("合肥", "明天")