forked from MigoXLab/dingo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataset_rag_eval_baseline.py
More file actions
229 lines (197 loc) · 8.08 KB
/
dataset_rag_eval_baseline.py
File metadata and controls
229 lines (197 loc) · 8.08 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
"""
使用扩展的 SummaryModel 进行 RAG 批量评估
展示如何自动收集和计算指标平均分数
特点:
1. 使用标准 Dingo 框架(InputArgs + Executor)
2. 自动收集每个评估的分数
3. 自动计算平均值、最小值、最大值、标准差
4. 结果自动保存到 summary.json 中
评测数据集:
fiqa.jsonl 的字段: user_input, reference, response, retrieved_contexts
- user_input: 问题
- reference: 标准答案
- response: RAG生成的答案
- retrieved_contexts: 检索的上下文
ragflow_eval_data_50.jsonl 的字段: question, response, retrieved_contexts, reference
- question: 问题
- response: RAG生成的答案
- retrieved_contexts: 检索的上下文
- reference: 标准答案
使用方法:
python dataset_rag_eval_with_metrics.py
"""
import os
from pathlib import Path
from dingo.config import InputArgs
from dingo.exec import Executor
from dingo.io.output.summary_model import SummaryModel
# 获取项目根目录
PROJECT_ROOT = Path(__file__).parent.parent.parent
# 配置(从环境变量读取)
OPENAI_MODEL = os.getenv("OPENAI_MODEL", "gpt-4o-mini")
OPENAI_URL = os.getenv("OPENAI_BASE_URL", "https://api.openai.com/v1")
OPENAI_KEY = os.getenv("OPENAI_API_KEY", "")
EMBEDDING_MODEL = os.getenv("EMBEDDING_MODEL", "doubao-embedding-large-text-250515")
# 数据文件路径
INPUT_DATA_PATH = str(PROJECT_ROOT / "test/data/fiqa.jsonl") # 或 "test/data/ragflow_eval_data_50.jsonl"
def print_metrics_summary(summary: SummaryModel):
"""打印指标统计摘要(支持按字段分组)"""
# print(summary.to_dict()) # 如果需要看完整输出,取消注释
if not summary.metrics_score_stats:
print("⚠️ 没有指标统计数据")
return
print("\n" + "=" * 80)
print("📊 RAG 评估指标统计")
print("=" * 80)
# 遍历每个字段组
for field_key, metrics in summary.metrics_score_stats.items():
print(f"\n📁 字段组: {field_key}")
print("-" * 80)
# 打印该字段组的每个指标详细统计
for metric_name, stats in metrics.items():
# 简化指标名称显示
display_name = metric_name.replace("LLMRAG", "")
print(f"\n {display_name}:")
print(f" 平均分: {stats.get('score_average', 0):.2f}")
print(f" 最小分: {stats.get('score_min', 0):.2f}")
print(f" 最大分: {stats.get('score_max', 0):.2f}")
print(f" 样本数: {stats.get('score_count', 0)}")
if 'score_std_dev' in stats:
print(f" 标准差: {stats.get('score_std_dev', 0):.2f}")
# 打印该字段组的总平均分
overall_avg = summary.get_metrics_score_overall_average(field_key)
print(f"\n 🎯 该字段组总平均分: {overall_avg:.2f}")
# 打印该字段组的指标排名(从高到低)
metrics_summary = summary.get_metrics_score_summary(field_key)
sorted_metrics = sorted(metrics_summary.items(), key=lambda x: x[1], reverse=True)
print("\n 📈 指标排名(从高到低):")
for i, (metric_name, avg_score) in enumerate(sorted_metrics, 1):
display_name = metric_name.replace("LLMRAG", "")
print(f" {i}. {display_name}: {avg_score:.2f}")
# 如果有多个字段组,打印总体统计
if len(summary.metrics_score_stats) > 1:
print("\n" + "=" * 80)
print("🌍 所有字段组总体统计")
print("=" * 80)
for field_key in summary.metrics_score_stats.keys():
overall_avg = summary.get_metrics_score_overall_average(field_key)
print(f" {field_key}: {overall_avg:.2f}")
print("\n" + "=" * 80)
def run_rag_evaluation():
"""
运行 RAG 评估并自动收集指标统计
"""
print("=" * 80)
print("使用 Dingo 框架进行 RAG 评估(自动收集指标统计)")
print("=" * 80)
print(f"数据文件: {INPUT_DATA_PATH}")
print(f"模型: {OPENAI_MODEL}")
print(f"API: {OPENAI_URL}")
print(f"Embedding模型: {EMBEDDING_MODEL}")
print(f"Embedding API: {OPENAI_URL}")
print("=" * 80)
llm_config = {
"model": OPENAI_MODEL,
"key": OPENAI_KEY,
"api_url": OPENAI_URL,
}
llm_config_embedding = {
"model": OPENAI_MODEL,
"key": OPENAI_KEY,
"api_url": OPENAI_URL, # LLM 服务地址
"embedding_config": { # ⭐ 必需:Embedding 配置
"model": EMBEDDING_MODEL,
"api_url": OPENAI_URL, # 如果同一服务提供 embedding
"key": OPENAI_KEY
},
"parameters": {
"strictness": 3,
"threshold": 5
}
}
# 构建配置
input_data = {
"task_name": "rag_evaluation_with_metrics",
"input_path": INPUT_DATA_PATH,
"output_path": "outputs/",
# "log_level": "INFO",
"dataset": {
"source": "local",
"format": "jsonl",
},
"executor": {
"max_workers": 10, # RAG 评估建议串行执行
"batch_size": 10,
"result_save": {
"good": True,
"bad": True,
"all_labels": True
}
},
"evaluator": [
{
# fiqa.jsonl 的字段: user_input, reference, response, retrieved_contexts
"fields": {
"prompt": "user_input", # 问题
"content": "response", # RAG生成的答案
"context": "retrieved_contexts", # 检索的上下文
"reference": "reference" # 标准答案(可选)
},
# # ragflow_eval_data_50.jsonl 的字段: question, response, retrieved_contexts, reference
# "fields": {
# "prompt": "question", # 问题
# "content": "response", # RAG生成的答案
# "context": "retrieved_contexts", # 检索的上下文
# "reference": "reference" # 标准答案(可选)
# },
"evals": [
{
"name": "LLMRAGFaithfulness",
"config": llm_config
},
{
"name": "LLMRAGContextPrecision",
"config": llm_config
},
{
"name": "LLMRAGContextRecall",
"config": llm_config
},
{
"name": "LLMRAGContextRelevancy",
"config": llm_config
},
# Answer Relevancy 需要 Embedding API
# 如果您的 API 支持 embeddings 端点,可以启用此项
{
"name": "LLMRAGAnswerRelevancy",
"config": llm_config_embedding
}
]
}
]
}
# 创建 InputArgs 并执行
print("\n开始评估...")
input_args = InputArgs(**input_data)
executor = Executor.exec_map["local"](input_args)
summary = executor.execute()
# 打印基本统计信息
print("\n" + "=" * 80)
print("✅ 评估完成!")
print("=" * 80)
print(f"输出目录: {summary.output_path}")
print(f"总数据量: {summary.total}")
print(f"通过: {summary.num_good}")
print(f"未通过: {summary.num_bad}")
print(f"通过率: {summary.score}%")
# 打印指标统计摘要(使用新功能)
print_metrics_summary(summary)
print(f"\n💾 详细结果已保存到: {summary.output_path}/summary.json")
print(" metrics_score (层级结构):")
print(" - stats: 每个指标的详细统计")
print(" - summary: 各指标平均分汇总")
print(" - overall_average: 总平均分")
return summary
if __name__ == "__main__":
summary = run_rag_evaluation()