Commit b38bc992 by 文靖昊

地区编码工具bug修复

parent afda82ab
...@@ -23,15 +23,18 @@ class AreaCodeTool: ...@@ -23,15 +23,18 @@ class AreaCodeTool:
self._build_name_maps() self._build_name_maps()
def _build_name_maps(self): def _build_name_maps(self):
"""构建区域名称到代码的映射""" """
构建区域名称到代码的映射
目前读取excel表格就能获取省市县的映射,也可能市省、省市的映射
我们可以直接以省过滤,就能得到市级、市县的映射、
再根据市级过滤、得到县级映射
"""
self.full_name_map = dict(zip(self.df['name'], self.df['code'])) self.full_name_map = dict(zip(self.df['name'], self.df['code']))
# 构建省级映射 # 构建二级映射
self.province_map = {} self.second_map = {}
# 构建市级映射 # 构建三级映射
self.city_map = {} self.third_map = {}
# 构建区县级映射
self.district_map = {}
for _, row in self.df.iterrows(): for _, row in self.df.iterrows():
name = row['name'].strip() name = row['name'].strip()
...@@ -39,22 +42,25 @@ class AreaCodeTool: ...@@ -39,22 +42,25 @@ class AreaCodeTool:
parts = name.split('省' if '省' in name else '市') parts = name.split('省' if '省' in name else '市')
if '省' in name: if '省' in name:
province = parts[0] + '省'
self.province_map[province] = code
if len(parts) > 1 and parts[1]: if len(parts) > 1 and parts[1]:
city_parts = parts[1].split('市') self.second_map[parts[1]] = code
if city_parts[0]: #考虑直辖县的情况
city = city_parts[0] + '市' if '市' in name:
self.city_map[city] = code city_parts = parts[1].split('市')
if len(city_parts) > 1 and city_parts[1]:
district = city_parts[1]
self.third_map[district] = code
if '自治州' in name:
city_parts = parts[1].split('自治州')
if len(city_parts) > 1 and city_parts[1]: if len(city_parts) > 1 and city_parts[1]:
district = city_parts[1] district = city_parts[1]
self.district_map[district] = code self.third_map[district] = code
else: else:
# 处理直辖市等特殊情况 # 处理直辖市等特殊情况
if parts[0]: if len(parts) > 1 and parts[1]:
self.city_map[parts[0] + '市'] = code self.third_map[parts[1]] = code
def find_code(self, area_name: str) -> List[Tuple[str, str]]: def find_code(self, area_name: str) -> List[Tuple[str, str]]:
""" """
...@@ -72,18 +78,16 @@ class AreaCodeTool: ...@@ -72,18 +78,16 @@ class AreaCodeTool:
if area_name in self.full_name_map: if area_name in self.full_name_map:
results.append((area_name, self.full_name_map[area_name])) results.append((area_name, self.full_name_map[area_name]))
return results return results
# 尝试省级匹配
if area_name.endswith('省') and area_name in self.province_map:
results.append((area_name, self.province_map[area_name]))
# 尝试市级匹配 # 尝试二级匹配
if area_name.endswith('市') and area_name in self.city_map: if area_name.endswith('市') and area_name in self.second_map:
results.append((area_name, self.city_map[area_name])) results.append((area_name, self.second_map[area_name]))
return results
# 尝试区县级匹配 # 尝试三级级匹配
if area_name in self.district_map: if area_name in self.third_map:
results.append((area_name, self.district_map[area_name])) results.append((area_name, self.third_map[area_name]))
return results
# 模糊匹配 # 模糊匹配
if not results: if not results:
......
import time
from typing import Dict, List, Tuple, Any, Optional from typing import Dict, List, Tuple, Any, Optional
from pydantic import BaseModel, Field from pydantic import BaseModel, Field
from typing import Type from typing import Type
...@@ -78,6 +79,7 @@ class RegionRateTool(BaseRateTool): ...@@ -78,6 +79,7 @@ class RegionRateTool(BaseRateTool):
def get_region_online_rate(self, start_time: str, end_time: str, region_name: str="") -> Dict[str, Any]: def get_region_online_rate(self, start_time: str, end_time: str, region_name: str="") -> Dict[str, Any]:
# 查询数据 # 查询数据
agent_start = time.time()
print(f"查询地区在线率: {region_name}, 时间范围: {start_time} 至 {end_time}") print(f"查询地区在线率: {region_name}, 时间范围: {start_time} 至 {end_time}")
code = "" code = ""
if region_name != "": if region_name != "":
...@@ -88,7 +90,11 @@ class RegionRateTool(BaseRateTool): ...@@ -88,7 +90,11 @@ class RegionRateTool(BaseRateTool):
'message': f'未找到匹配的区域代码: {region_name}' 'message': f'未找到匹配的区域代码: {region_name}'
} }
code = codes[0][1] code = codes[0][1]
print(code)
start = time.time()
df = self.client.query_rates_sync(code, start_time, end_time) df = self.client.query_rates_sync(code, start_time, end_time)
end = time.time()
print(f"query_rates_sync client spent time:{end-start}")
print(f"地区在线率接口调用结果: {df}") print(f"地区在线率接口调用结果: {df}")
# 准备数据 # 准备数据
if df.type != 1 or len(df.resultdata) == 0: if df.type != 1 or len(df.resultdata) == 0:
...@@ -106,7 +112,8 @@ class RegionRateTool(BaseRateTool): ...@@ -106,7 +112,8 @@ class RegionRateTool(BaseRateTool):
'end': end_time 'end': end_time
} }
} }
end = time.time()
print(f"once agent spent time:{end - agent_start}")
return data return data
class RankingRateArgs(BaseModel): class RankingRateArgs(BaseModel):
......
...@@ -139,7 +139,7 @@ class RateAgentV2: ...@@ -139,7 +139,7 @@ class RateAgentV2:
agent = create_structured_chat_agent(llm, tools, prompt) agent = create_structured_chat_agent(llm, tools, prompt)
self.agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=verbose, return_intermediate_steps=True) self.agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=verbose, return_intermediate_steps=True,handle_parsing_errors=True)
def exec(self, prompt_args: dict = {}, stream: bool = False): def exec(self, prompt_args: dict = {}, stream: bool = False):
return self.agent_executor.invoke(input=prompt_args) return self.agent_executor.invoke(input=prompt_args)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment