fix: 修复 Answer Correctness 合并功能并更新 coze-excel 结果
修复问题: - 修复 Answer Correctness 结果合并逻辑 - 正确处理 positive_cases 和 negative_cases 数据结构 - 重新计算统计信息 改进: - 使用 .get() 方法安全访问字典键 - 更新 mean, max, min, median, std 统计信息 - 添加更详细的错误处理 验证: - 成功合并 coze-excel 的 answer_correctness 结果 - 所有 5 个指标现在都包含在结果中 - answer_correctness 均值: 0.2797 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1102981071
commit
8e1dc57821
2 files changed
+61
-34
No files matched your search
+21
-19
@@ -11,11 +11,13 @@
|
||||
"answer_relevance",
|
||||
"context_recall",
|
||||
"faithfulness",
|
||||
"context_precision"
|
||||
],
|
||||
"metrics_disabled": [
|
||||
"context_precision",
|
||||
"answer_correctness"
|
||||
]
|
||||
],
|
||||
"metrics_disabled": [],
|
||||
"answer_correctness_merged": true,
|
||||
"answer_correctness_timestamp": "2026-01-08T09:42:55.585147",
|
||||
"answer_correctness_duration": 539.335533
|
||||
},
|
||||
"results": {
|
||||
"coze-excel": {
|
||||
@@ -103,23 +105,23 @@
|
||||
}
|
||||
},
|
||||
"answer_correctness": {
|
||||
"mean": 0.0,
|
||||
"median": 0.0,
|
||||
"std": 0.0,
|
||||
"min": 0.0,
|
||||
"max": 0.0,
|
||||
"mean": 0.2796896569063506,
|
||||
"median": 0.22223149786843527,
|
||||
"std": 0.26260285107716297,
|
||||
"min": 0.06382945738894305,
|
||||
"max": 0.9656318854641881,
|
||||
"count": 10,
|
||||
"per_question": {
|
||||
"q001": 0.0,
|
||||
"q002": 0.0,
|
||||
"q003": 0.0,
|
||||
"q004": 0.0,
|
||||
"q005": 0.0,
|
||||
"q006": 0.0,
|
||||
"q007": 0.0,
|
||||
"q008": 0.0,
|
||||
"q009": 0.0,
|
||||
"q010": 0.0
|
||||
"q001": 0.9656318854641881,
|
||||
"q002": 0.2850500392064303,
|
||||
"q003": 0.22223149786843527,
|
||||
"q004": 0.06440425871314548,
|
||||
"q005": 0.39486259254434347,
|
||||
"q006": 0.06382945738894305,
|
||||
"q007": 0.43020649681451,
|
||||
"q008": 0.2192227604293392,
|
||||
"q009": 0.07995909182295702,
|
||||
"q010": 0.07149848881121382
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
@@ -196,8 +196,8 @@ async def merge_answer_correctness(
|
||||
with open(correctness_result_path, 'r', encoding='utf-8') as f:
|
||||
correctness_data = json.load(f)
|
||||
|
||||
print(f" - 正向用例: {correctness_data['positive_cases']['count']}")
|
||||
print(f" - 负向用例: {correctness_data['negative_cases']['count']}")
|
||||
print(f" - 正向用例: {correctness_data.get('positive_cases', {}).get('count', 0)}")
|
||||
print(f" - 负向用例: {correctness_data.get('negative_cases', {}).get('count', 0)}")
|
||||
print()
|
||||
|
||||
# 合并结果
|
||||
@@ -214,29 +214,57 @@ async def merge_answer_correctness(
|
||||
# 创建问题ID到 answer_correctness 结果的映射
|
||||
correctness_map = {}
|
||||
|
||||
for result in correctness_data['positive_cases']['results']:
|
||||
# 从 positive_cases 中提取
|
||||
positive_cases = correctness_data.get('positive_cases', {})
|
||||
if 'results' in positive_cases:
|
||||
for result in positive_cases['results']:
|
||||
qid = result['question_id']
|
||||
correctness_map[qid] = result['scores']['answer_correctness']
|
||||
|
||||
for result in correctness_data['negative_cases']['results']:
|
||||
# 从 negative_cases 中提取
|
||||
negative_cases = correctness_data.get('negative_cases', {})
|
||||
if 'results' in negative_cases:
|
||||
for result in negative_cases['results']:
|
||||
qid = result['question_id']
|
||||
correctness_map[qid] = result['scores']['answer_correctness']
|
||||
|
||||
# 更新原始结果中的每个问题
|
||||
# 更新原始结果中的 answer_correctness 统计
|
||||
original_system_data = original_data['results'][system]
|
||||
|
||||
# 更新正向用例的 answer_correctness 指标
|
||||
if 'positive_cases' in original_system_data and 'answer_correctness' in original_system_data['positive_cases']:
|
||||
# 更新 per_question 数据
|
||||
per_question = original_system_data['positive_cases']['answer_correctness']['per_question']
|
||||
updated_count = 0
|
||||
for case_type in ['positive_cases', 'negative_cases']:
|
||||
if case_type in original_data['results'][system]:
|
||||
for result in original_data['results'][system][case_type]['results']:
|
||||
qid = result['question_id']
|
||||
if qid in correctness_map:
|
||||
result['scores']['answer_correctness'] = correctness_map[qid]
|
||||
for qid in correctness_map:
|
||||
if qid in per_question:
|
||||
per_question[qid] = correctness_map[qid]
|
||||
updated_count += 1
|
||||
|
||||
# 重新计算统计信息
|
||||
scores = list(per_question.values())
|
||||
if scores:
|
||||
original_system_data['positive_cases']['answer_correctness']['mean'] = sum(scores) / len(scores)
|
||||
original_system_data['positive_cases']['answer_correctness']['max'] = max(scores)
|
||||
original_system_data['positive_cases']['answer_correctness']['min'] = min(scores)
|
||||
original_system_data['positive_cases']['answer_correctness']['median'] = sorted(scores)[len(scores) // 2]
|
||||
|
||||
# 计算标准差
|
||||
if len(scores) > 1:
|
||||
mean = original_system_data['positive_cases']['answer_correctness']['mean']
|
||||
variance = sum((x - mean) ** 2 for x in scores) / len(scores)
|
||||
original_system_data['positive_cases']['answer_correctness']['std'] = variance ** 0.5
|
||||
else:
|
||||
original_system_data['positive_cases']['answer_correctness']['std'] = 0.0
|
||||
|
||||
print(f" - 更新了 {updated_count} 个问题的 answer_correctness 得分")
|
||||
print()
|
||||
|
||||
# 更新元数据
|
||||
if 'metadata' in original_data:
|
||||
original_data['metadata']['answer_correctness_merged'] = True
|
||||
original_data['metadata']['answer_correctness_timestamp'] = correctness_data['timestamp']
|
||||
original_data['metadata']['answer_correctness_duration'] = correctness_data['evaluation_duration_seconds']
|
||||
original_data['metadata']['answer_correctness_duration'] = correctness_data.get('evaluation_duration_seconds', 0)
|
||||
|
||||
# 更新指标列表
|
||||
metrics_used = original_data['metadata'].get('metrics_used', [])
|
||||
@@ -250,9 +278,6 @@ async def merge_answer_correctness(
|
||||
metrics_disabled.remove('answer_correctness')
|
||||
original_data['metadata']['metrics_disabled'] = metrics_disabled
|
||||
|
||||
print(f" - 更新了 {updated_count} 个问题的答案")
|
||||
print()
|
||||
|
||||
# 保存合并后的结果
|
||||
if output_path is None:
|
||||
output_path = original_result_path
|
||||
|
||||
Reference in new issue
Block a user