Skip to content

Commit 5a4d9d7

Browse files
committed
feat: add source_url in each cache
1 parent 1c669f2 commit 5a4d9d7

1 file changed

Lines changed: 114 additions & 0 deletions

File tree

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import json
2+
from pathlib import Path
3+
import re
4+
5+
6+
def extract_tag_from_filename(filename: str) -> str:
7+
"""从文件名中提取tag。
8+
9+
文件名格式: crawl_ths_{tag}_{序号}.json
10+
例如: crawl_ths_company_02.json -> company
11+
crawl_ths_position_01.json -> position
12+
"""
13+
pattern = r'crawl_ths_(.+?)_\d+\.json'
14+
match = re.match(pattern, filename)
15+
if match:
16+
return match.group(1)
17+
return ""
18+
19+
20+
def add_source_url_to_records(tool_cache_dir: str):
21+
"""遍历tool_cache目录下所有JSON文件,在每条记录的tool_result前添加来源URL。
22+
23+
Args:
24+
tool_cache_dir: tool_cache目录的路径
25+
"""
26+
cache_path = Path(tool_cache_dir)
27+
28+
if not cache_path.exists():
29+
print(f"错误: 目录不存在 {tool_cache_dir}")
30+
return
31+
32+
# 获取所有一级目录下的JSON文件
33+
json_files = list(cache_path.glob("*.json"))
34+
35+
if not json_files:
36+
print(f"警告: 在 {tool_cache_dir} 下未找到JSON文件")
37+
return
38+
39+
print(f"找到 {len(json_files)} 个JSON文件")
40+
41+
for json_file in json_files:
42+
print(f"\n处理文件: {json_file.name}")
43+
44+
# 从文件名中提取tag
45+
tag = extract_tag_from_filename(json_file.name)
46+
if not tag:
47+
print(f" 跳过: 无法从文件名 {json_file.name} 中提取tag")
48+
continue
49+
50+
print(f" 提取的tag: {tag}")
51+
52+
# 读取JSON文件
53+
try:
54+
with open(json_file, 'r', encoding='utf-8') as f:
55+
data = json.load(f)
56+
except Exception as e:
57+
print(f" 错误: 读取文件失败 - {e}")
58+
continue
59+
60+
if not isinstance(data, list):
61+
print(f" 跳过: 文件内容不是列表格式")
62+
continue
63+
64+
# 处理每条记录
65+
modified_count = 0
66+
for item in data:
67+
if not isinstance(item, dict):
68+
continue
69+
70+
# 检查必要字段
71+
if 'tool_args' not in item or 'tool_result' not in item:
72+
continue
73+
74+
tool_args = item['tool_args']
75+
if 'code' not in tool_args:
76+
continue
77+
78+
code = tool_args['code']
79+
tool_result = item['tool_result']
80+
81+
# 构造来源URL
82+
source_url = f"https://basic.10jqka.com.cn/{code}/{tag}.html#stockpage"
83+
source_prefix = f"> 以下内容来自:{source_url}\n\n"
84+
85+
# 检查是否已经添加过来源信息
86+
if not tool_result.startswith("> 以下内容来自:"):
87+
item['tool_result'] = source_prefix + tool_result
88+
modified_count += 1
89+
90+
print(f" 修改了 {modified_count} 条记录")
91+
92+
# 写回文件
93+
try:
94+
with open(json_file, 'w', encoding='utf-8') as f:
95+
json.dump(data, f, ensure_ascii=False, indent=2)
96+
print(f" ✓ 成功保存文件")
97+
except Exception as e:
98+
print(f" 错误: 保存文件失败 - {e}")
99+
100+
101+
def main():
102+
# 设置tool_cache目录路径
103+
tool_cache_dir = "/mnt/data_cpfs/taoshuchang.tsc/deepresearch/finance-mcp/tool_cache"
104+
105+
print("开始处理tool_cache目录下的JSON文件...")
106+
print(f"目标目录: {tool_cache_dir}\n")
107+
108+
add_source_url_to_records(tool_cache_dir)
109+
110+
print("\n处理完成!")
111+
112+
113+
if __name__ == "__main__":
114+
main()

0 commit comments

Comments
 (0)