天气接口优化
This commit is contained in:
parent
53d1700d29
commit
97a99b5917
|
|
@ -13,11 +13,13 @@ from .tools_registry import BaseToolOutput, regist_tool
|
||||||
@regist_tool(title="天气查询")
|
@regist_tool(title="天气查询")
|
||||||
def weather_check(
|
def weather_check(
|
||||||
city: str = Field(description="City name,include city and county,like '厦门'"),
|
city: str = Field(description="City name,include city and county,like '厦门'"),
|
||||||
date: str = Field(description="日期参数,支持以下格式:\n"
|
date: str = Field(
|
||||||
|
default=None,
|
||||||
|
description="日期参数,支持以下格式:\n"
|
||||||
"- '今天':获取当前实时天气\n"
|
"- '今天':获取当前实时天气\n"
|
||||||
"- '明天'/'后天':获取未来24/48小时预报\n"
|
"- '明天'/'后天':获取未来24/48小时预报\n"
|
||||||
"- '未来X天':获取最多7天预报(如'未来3天')\n"
|
"- '未来X天':获取最多X天预报(如'未来3天')\n"
|
||||||
# "- 具体日期:格式为YYYY-MM-DD(如'2024-07-05')"
|
"- 不支持其他参数,如果是其他参数,则时间参数为None"
|
||||||
)
|
)
|
||||||
):
|
):
|
||||||
"""用这个工具获取指定地点和指定时间的天气"""
|
"""用这个工具获取指定地点和指定时间的天气"""
|
||||||
|
|
@ -69,7 +71,7 @@ def weather_check(
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
data = response.json()
|
data = response.json()
|
||||||
logging.info(f"response.json():{data}")
|
logging.info(f"response.json():{data}")
|
||||||
if number == "1":
|
if number == 1:
|
||||||
weather = {
|
weather = {
|
||||||
"date":"明天",
|
"date":"明天",
|
||||||
"low_temperature": data["results"][0]["daily"][1]["low"],
|
"low_temperature": data["results"][0]["daily"][1]["low"],
|
||||||
|
|
@ -85,6 +87,9 @@ def weather_check(
|
||||||
}
|
}
|
||||||
elif number == 3:
|
elif number == 3:
|
||||||
weather = {
|
weather = {
|
||||||
|
"今天天气": data["results"][0]["daily"][0]["text_day"],
|
||||||
|
"今天最低温度": data["results"][0]["daily"][0]["low"],
|
||||||
|
"今天最高温度": data["results"][0]["daily"][0]["high"],
|
||||||
"明天天气": data["results"][0]["daily"][1]["text_day"],
|
"明天天气": data["results"][0]["daily"][1]["text_day"],
|
||||||
"明天最低温度": data["results"][0]["daily"][1]["low"],
|
"明天最低温度": data["results"][0]["daily"][1]["low"],
|
||||||
"明天最高温度": data["results"][0]["daily"][1]["high"],
|
"明天最高温度": data["results"][0]["daily"][1]["high"],
|
||||||
|
|
@ -102,9 +107,9 @@ def weather_check(
|
||||||
def parse_date_parameter(date_str: str) -> tuple:
|
def parse_date_parameter(date_str: str) -> tuple:
|
||||||
"""解析日期参数返回API参数"""
|
"""解析日期参数返回API参数"""
|
||||||
# 处理自然语言
|
# 处理自然语言
|
||||||
if date_str == "今天":
|
if date_str in("今天", "今日"):
|
||||||
return "daily", 0
|
return "daily", 0
|
||||||
elif date_str == "明天":
|
elif date_str in("明天", "明日"):
|
||||||
return "future", 1
|
return "future", 1
|
||||||
elif date_str == "后天":
|
elif date_str == "后天":
|
||||||
return "future", 2
|
return "future", 2
|
||||||
|
|
@ -112,4 +117,8 @@ def parse_date_parameter(date_str: str) -> tuple:
|
||||||
days = int(date_str[2:-1])
|
days = int(date_str[2:-1])
|
||||||
if days < 1 or days > 3:
|
if days < 1 or days > 3:
|
||||||
raise ValueError("未来预报仅支持1-3天")
|
raise ValueError("未来预报仅支持1-3天")
|
||||||
return "future", days
|
return "future", days
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
weather_check("合肥","明天")
|
||||||
Loading…
Reference in New Issue