Skip to content

Commit 7d37a3b

Browse files
Merge pull request xinnan-tech#965 from xinnan-tech/py_test
Py test
2 parents 70c3772 + 1879c62 commit 7d37a3b

File tree

2 files changed

+47
-22
lines changed

2 files changed

+47
-22
lines changed

main/xiaozhi-server/core/connection.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,12 @@ def _initialize_components(self, private_config):
226226
"""加载意图识别"""
227227
self._initialize_intent()
228228
"""加载位置信息"""
229-
self.client_ip_info = get_ip_info(self.client_ip, self.logger)
230-
if self.client_ip_info is not None and "city" in self.client_ip_info:
231-
self.logger.bind(tag=TAG).info(f"Client ip info: {self.client_ip_info}")
232-
self.prompt = self.prompt + f"\nuser location:{self.client_ip_info}"
233-
234-
self.dialogue.update_system_message(self.prompt)
229+
# self.client_ip_info = get_ip_info(self.client_ip, self.logger)
230+
# if self.client_ip_info is not None and "city" in self.client_ip_info:
231+
# self.logger.bind(tag=TAG).info(f"Client ip info: {self.client_ip_info}")
232+
# self.prompt = self.prompt + f"\nuser location:{self.client_ip_info}"
233+
#
234+
# self.dialogue.update_system_message(self.prompt)
235235

236236
def _initialize_private_config(self):
237237
read_config_from_api = self.config.get("read_config_from_api", False)

main/xiaozhi-server/plugins_func/functions/get_weather.py

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from bs4 import BeautifulSoup
33
from config.logger import setup_logging
44
from plugins_func.register import register_function, ToolType, ActionResponse, Action
5+
from core.utils.util import get_ip_info
56

67
TAG = __name__
78
logger = setup_logging()
@@ -11,9 +12,12 @@
1112
"function": {
1213
"name": "get_weather",
1314
"description": (
14-
"获取某个地点的天气,用户应提供一个位置,比如用户说杭州天气,参数为:杭州。"
15+
"获取某个地点的天气信息。当用户询问与天气相关的问题(例如'今天的天气怎么样?'、'明天会下雨吗?'、'广州天气如何?'等),"
16+
"或对话中包含'天气'字眼时,调用此功能。用户可以提供具体位置(如城市名),"
17+
"如果未提供位置,则自动获取用户当前位置查询天气。"
1518
"如果用户说的是省份,默认用省会城市。如果用户说的不是省份或城市而是一个地名,"
1619
"默认用该地所在省份的省会城市。"
20+
"当IP解析失败会使用默认地址"
1721
),
1822
"parameters": {
1923
"type": "object",
@@ -57,6 +61,7 @@
5761
"900": "热", "901": "冷", "999": "未知"
5862
}
5963

64+
6065
def fetch_city_info(location, api_key):
6166
url = f"https://geoapi.qweather.com/v2/city/lookup?key={api_key}&location={location}&lang=zh"
6267
response = requests.get(url, headers=HEADERS).json()
@@ -97,27 +102,47 @@ def parse_weather_info(soup):
97102
def get_weather(conn, location: str = None, lang: str = "zh_CN"):
98103
api_key = conn.config["plugins"]["get_weather"]["api_key"]
99104
default_location = conn.config["plugins"]["get_weather"]["default_location"]
100-
location = location or conn.client_ip_info.get("city") or default_location
101-
logger.bind(tag=TAG).debug(f"获取天气: {location}")
105+
print("用户设置的default_location为:", default_location)
106+
client_ip = conn.client_ip
107+
print("用户提供的location为:", location)
108+
# 优先使用用户提供的location参数
109+
if not location:
110+
# 通过客户端IP解析城市
111+
if client_ip:
112+
# 动态解析IP对应的城市信息
113+
ip_info = get_ip_info(client_ip, logger)
114+
print("解析用户IP后的城市为:", ip_info)
115+
location = ip_info.get("city") if ip_info and "city" in ip_info else None
116+
else:
117+
# 若IP解析失败或无IP,使用默认位置
118+
location = default_location
119+
print("解析失败,将使用默认地址", location)
102120

103121
city_info = fetch_city_info(location, api_key)
104122
if not city_info:
105123
return ActionResponse(Action.REQLLM, f"未找到相关的城市: {location},请确认地点是否正确", None)
106-
107124
soup = fetch_weather_page(city_info['fxLink'])
108125
if not soup:
109126
return ActionResponse(Action.REQLLM, None, "请求失败")
110-
111127
city_name, current_abstract, current_basic, temps_list = parse_weather_info(soup)
112-
weather_report = f"根据下列数据,用{lang}回应用户的查询天气请求:\n{city_name}未来7天天气:\n"
113-
for i, (date, weather, high, low) in enumerate(temps_list):
114-
if high and low:
115-
weather_report += f"{date}: {low}{high}, {weather}\n"
116-
weather_report += (
117-
f"当前天气: {current_abstract}\n"
118-
f"当前天气参数: {current_basic}\n"
119-
f"(确保只报告指定单日的天气情况,除非未来会出现异常天气;或者用户明确要求想要了解多日天气,如果未指定,默认报告今天的天气。"
120-
"参数为0的值不需要报告给用户,每次都报告体感温度,根据语境选择合适的参数内容告知用户,并对参数给出相应评价)"
121-
)
122128

123-
return ActionResponse(Action.REQLLM, weather_report, None)
129+
print(f"当前查询的城市名称是: {city_name}")
130+
131+
weather_report = f"您查询的位置是:{city_name}\n\n当前天气: {current_abstract}\n"
132+
133+
# 添加有效的当前天气参数
134+
if current_basic:
135+
weather_report += "详细参数:\n"
136+
for key, value in current_basic.items():
137+
if value != "0": # 过滤无效值
138+
weather_report += f" · {key}: {value}\n"
139+
140+
# 添加7天预报
141+
weather_report += "\n未来7天预报:\n"
142+
for date, weather, high, low in temps_list:
143+
weather_report += f"{date}: {weather},气温 {low}~{high}\n"
144+
145+
# 提示语
146+
weather_report += "\n(如需某一天的具体天气,请告诉我日期)"
147+
148+
return ActionResponse(Action.REQLLM, weather_report, None)

0 commit comments

Comments
 (0)