-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.py
More file actions
377 lines (306 loc) · 11.4 KB
/
benchmark.py
File metadata and controls
377 lines (306 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
"""
RAG 系统优化对比测试脚本
测试 20 个问题,对比三种方案的效果:
1. Baseline(无优化)- 直接用原始问题检索 + 简单 prompt
2. + Query Rewrite - 添加查询重写
3. + Prompt 优化 - 完整优化版本
"""
import os
from http import HTTPStatus
import dashscope
from langchain_community.vectorstores import FAISS
from langchain_community.embeddings import HuggingFaceEmbeddings
import json
# API 配置 - 从环境变量读取
DASHSCOPE_API_KEY = os.getenv("DASHSCOPE_API_KEY")
if not DASHSCOPE_API_KEY:
raise ValueError("请设置环境变量 DASHSCOPE_API_KEY")
# 加载 FAISS 向量存储
# 注意:向量库已离线构建,此处仅演示加载流程
vectorstore = FAISS.load_local(
"faiss_index",
embeddings=HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2"),
allow_dangerous_deserialization=True
)
# 测试问题列表(20 个机器学习相关问题)
TEST_QUESTIONS = [
"What is regression?",
"What is classification?",
"What is overfitting?",
"What is k-nearest neighbor?",
"What is decision tree?",
"What is logistic regression?",
"What is support vector machine?",
"What is naive bayes?",
"What is linear discriminant analysis?",
"What is cross-validation?",
"What is precision and recall?",
"What is F1 score?",
"What is confusion matrix?",
"What is bias-variance tradeoff?",
"What is regularization?",
"What is gradient descent?",
"What is feature engineering?",
"What is ensemble learning?",
"What is bagging and boosting?",
"What is neural network?",
]
def evaluate_answer(answer: str) -> bool:
"""
简单评估答案是否有效
规则:
- 不是"我不知道"或类似拒答
- 长度超过 20 字
"""
if not answer:
return False
answer_lower = answer.lower()
# 检查是否是拒答
refuse_phrases = ["我不知道", "i don't know", "无法回答", "资料不足", "没有相关信息"]
for phrase in refuse_phrases:
if phrase in answer_lower:
return False
# 检查长度
if len(answer) < 20:
return False
return True
# ==================== 方案 1: Baseline(无优化)====================
def ask_rag_baseline(question, k=5):
"""基础版本 - 无优化"""
docs = vectorstore.similarity_search(question, k=k)
if not docs:
return {"answer": "未找到相关文档", "success": False}
context = "\n\n".join([doc.page_content[:300] for doc in docs])
prompt = f"""根据以下资料回答问题:
{context}
问题:{question}
回答:"""
response = dashscope.Generation.call(
model="qwen-plus",
prompt=prompt,
api_key=DASHSCOPE_API_KEY,
temperature=0.5
)
if response.status_code == HTTPStatus.OK:
answer = response.output.text
success = evaluate_answer(answer)
return {"answer": answer, "success": success}
else:
return {"answer": f"请求失败:{response.message}", "success": False}
# ==================== 方案 2: + Query Rewrite ====================
def rewrite_query(question: str) -> str:
"""重写查询以提高语义搜索效果"""
prompt = f"""Rewrite the following question to improve semantic search.
Add related terms, synonyms, and more specific expressions.
Keep it concise but informative.
Original question:
{question}
Rewritten query:"""
response = dashscope.Generation.call(
model="qwen-plus",
prompt=prompt,
api_key=DASHSCOPE_API_KEY,
temperature=0
)
if response.status_code == HTTPStatus.OK:
return response.output.text.strip()
return question
def ask_rag_with_rewrite(question, k=5):
"""方案 2 - 添加 Query Rewrite"""
new_query = rewrite_query(question)
docs = vectorstore.similarity_search(new_query, k=k)
if not docs:
return {"answer": "未找到相关文档", "success": False, "rewritten_query": new_query}
context = "\n\n".join([doc.page_content[:300] for doc in docs])
prompt = f"""根据以下资料回答问题:
{context}
问题:{question}
回答:"""
response = dashscope.Generation.call(
model="qwen-plus",
prompt=prompt,
api_key=DASHSCOPE_API_KEY,
temperature=0.5
)
if response.status_code == HTTPStatus.OK:
answer = response.output.text
success = evaluate_answer(answer)
return {"answer": answer, "success": success, "rewritten_query": new_query}
else:
return {"answer": f"请求失败:{response.message}", "success": False}
# ==================== 方案 3: + Prompt 优化(完整版)====================
def translate_to_chinese(text: str) -> str:
"""将英文资料翻译成中文"""
prompt = f"请把以下内容翻译成中文:\n\n{text}"
response = dashscope.Generation.call(
model="qwen-plus",
prompt=prompt,
api_key=DASHSCOPE_API_KEY,
temperature=0
)
if response.status_code == HTTPStatus.OK:
return response.output.text.strip()
return text
def ask_rag_full_optimized(question, k=5):
"""方案 3 - 完整优化版本(Query Rewrite + Prompt 优化 + 翻译)"""
new_query = rewrite_query(question)
docs = vectorstore.similarity_search(new_query, k=k)
if not docs:
return {"answer": "未找到相关文档", "success": False, "rewritten_query": new_query}
context = "\n\n".join([doc.page_content[:300] for doc in docs])
context_cn = translate_to_chinese(context)
prompt = f"""你是一个机器学习助教,请根据提供的资料回答问题。
规则:
1. 必须全部使用中文回答(不能出现英文句子)
2. 如果资料是英文,请翻译后再回答
3. 优先使用资料内容
4. 可以进行合理补充解释
5. 不要轻易回答"我不知道"
资料:
{context_cn}
问题:
{question}
请用中文回答:"""
response = dashscope.Generation.call(
model="qwen-plus",
prompt=prompt,
api_key=DASHSCOPE_API_KEY,
temperature=0.1,
max_tokens=500
)
if response.status_code == HTTPStatus.OK:
answer = response.output.text
success = evaluate_answer(answer)
return {"answer": answer, "success": success, "rewritten_query": new_query}
else:
return {"answer": f"请求失败:{response.message}", "success": False}
# ==================== 运行测试 ====================
def run_benchmark():
"""运行完整的基准测试"""
print("=" * 60)
print("RAG 系统优化对比测试")
print("=" * 60)
print(f"测试问题数量:{len(TEST_QUESTIONS)}")
print()
results = {
"baseline": {"success": 0, "fail": 0, "details": []},
"with_rewrite": {"success": 0, "fail": 0, "details": []},
"full_optimized": {"success": 0, "fail": 0, "details": []},
}
for i, q in enumerate(TEST_QUESTIONS):
print(f"\n[{i+1}/{len(TEST_QUESTIONS)}] 问题:{q}")
# 方案 1
print(" 测试 Baseline...", end=" ")
r1 = ask_rag_baseline(q)
results["baseline"]["details"].append({
"question": q,
"success": r1["success"],
"answer_preview": r1["answer"][:50] if r1["answer"] else ""
})
if r1["success"]:
results["baseline"]["success"] += 1
print("✅")
else:
results["baseline"]["fail"] += 1
print("❌")
# 方案 2
print(" 测试 +Query Rewrite...", end=" ")
r2 = ask_rag_with_rewrite(q)
results["with_rewrite"]["details"].append({
"question": q,
"success": r2["success"],
"answer_preview": r2["answer"][:50] if r2["answer"] else ""
})
if r2["success"]:
results["with_rewrite"]["success"] += 1
print("✅")
else:
results["with_rewrite"]["fail"] += 1
print("❌")
# 方案 3
print(" 测试 完整优化...", end=" ")
r3 = ask_rag_full_optimized(q)
results["full_optimized"]["details"].append({
"question": q,
"success": r3["success"],
"answer_preview": r3["answer"][:50] if r3["answer"] else ""
})
if r3["success"]:
results["full_optimized"]["success"] += 1
print("✅")
else:
results["full_optimized"]["fail"] += 1
print("❌")
# 计算比例
total = len(TEST_QUESTIONS)
summary = {
"baseline": round(results["baseline"]["success"] / total * 100),
"with_rewrite": round(results["with_rewrite"]["success"] / total * 100),
"full_optimized": round(results["full_optimized"]["success"] / total * 100),
}
return results, summary
def generate_readme_table(summary: dict) -> str:
"""生成 README 中的对比表格"""
table = """| 方法 | 能回答比例 |
|----------------------|------------|
| baseline(无优化) | {}% |
| + query rewrite | {}% |
| + prompt 优化 | {}% |""".format(
summary['baseline'],
summary['with_rewrite'],
summary['full_optimized']
)
return table
def update_readme(table: str, readme_path: str = "README.md"):
"""更新 README.md 文件中的表格"""
import os
if not os.path.exists(readme_path):
print(f"警告:{readme_path} 不存在,将创建新文件")
content = f"# RAG 系统优化对比测试\n\n## 测试结果\n\n{table}\n"
with open(readme_path, "w", encoding="utf-8") as f:
f.write(content)
return
with open(readme_path, "r", encoding="utf-8") as f:
content = f.read()
# 查找并替换现有的表格部分
import re
pattern = r'(\| 方法.*?\|.*?能回答比例.*?\n\|.*?\n(\|.*?\n)+)'
if re.search(pattern, content, re.MULTILINE):
content = re.sub(pattern, table, content, flags=re.MULTILINE)
print("✓ 已更新 README.md 中的表格")
else:
# 如果没有找到现有表格,在"优化效果对比"标题下插入
if "### 优化效果对比" in content:
content = content.replace(
"### 优化效果对比\n",
f"### 优化效果对比\n\n{table}\n"
)
print("✓ 已在 README.md 中添加表格")
else:
# 添加到文件末尾
content += f"\n## 测试结果\n\n{table}\n"
print("✓ 已在 README.md 末尾添加表格")
with open(readme_path, "w", encoding="utf-8") as f:
f.write(content)
if __name__ == "__main__":
results, summary = run_benchmark()
print("\n" + "=" * 60)
print("测试结果汇总")
print("=" * 60)
print()
# 简洁版表格(匹配图片格式)
print("| 方法 | 能回答比例 |")
print("|----------------------|------------|")
print(f"| baseline(无优化) | {summary['baseline']}% |")
print(f"| + query rewrite | {summary['with_rewrite']}% |")
print(f"| + prompt 优化 | {summary['full_optimized']}% |")
print()
print("测试完成!")
# 保存详细结果到 JSON
with open("benchmark_results.json", "w", encoding="utf-8") as f:
json.dump({"results": results, "summary": summary}, f, ensure_ascii=False, indent=2)
print("✓ 详细结果已保存到 benchmark_results.json")
# 生成并更新 README 表格
table = generate_readme_table(summary)
update_readme(table)
print("✓ README.md 已更新")