## 主要功能 ### 并行评估优化 ⚡ - 实现指标并行执行,提升评估速度 1.25-1.42x - 创建 ParallelRAGAsEvaluator 并行评估器 - 使用 asyncio.gather() 实现真正的并行 ### 可配置指标功能 🎛️ - 支持通过参数启用/禁用特定评估指标 - 创建 ConfigurableRAGAsEvaluator 可配置评估器 - 灵活控制评估速度和质量(2.6-8x 加速) ### 交互式批处理脚本 🖥️ - 更新 evaluate_all_types_advanced.bat 支持优化选项 - 添加 3 种评估模式:完整/快速/极速 - 自动时间预估功能 ## 性能提升 | 场景 | 原版 | 优化后 | 节省 | |------|------|--------|------| | 单问题 | 8.3分钟 | 1-3分钟 | 5-7分钟 | | 15问题 | 125分钟 | 15-48分钟 | 77-110分钟 | | 8系统×15问题 | 16.7小时 | 2-6.4小时 | 10-14.7小时 | ## 新增文件 **核心代码**: - src/evaluators/parallel_ragas_evaluator.py - 并行评估器 - src/evaluators/configurable_evaluator.py - 可配置评估器 **测试脚本**: - scripts/test_single_evaluation.py - 单系统评估测试 - scripts/test_report_save.py - 报告保存测试 - scripts/test_parallel_performance.py - 性能对比测试 - scripts/evaluate_single_system_parallel.py - 并行评估脚本 - scripts/evaluate_with_config.py - 可配置评估脚本 - scripts/evaluate_all_types.bat - 基础批处理脚本 - scripts/evaluate_all_types_advanced.bat - 增强批处理脚本(v2) **文档**: - docs/PERFORMANCE_ANALYSIS.md - 性能分析 - docs/METRICS_EXPLANATION.md - 指标说明 - docs/PARALLEL_OPTIMIZATION.md - 并行优化方案 - docs/PARALLEL_QUICK_START.md - 并行优化快速开始 - docs/DISABLE_METRICS_GUIDE.md - 禁用指标指南 - docs/OPTIMIZATION_SUMMARY.md - 优化功能总结 - docs/SINGLE_EVALUATION_TEST.md - 单系统测试报告 ## 使用方法 ### 批处理脚本(推荐) ```bash scripts\evaluate_all_types_advanced.bat ``` ### 命令行脚本 ```bash # 快速评估(禁用 answer_correctness) python scripts/evaluate_with_config.py --system zht-excel --disable answer_correctness # 极速评估(仅3个核心指标) python scripts/evaluate_with_config.py --system zht-excel \ --enable context_precision faithfulness answer_relevance # 并行评估 python scripts/evaluate_single_system_parallel.py --system zht-excel ``` ## 技术亮点 - 使用 asyncio.gather() 实现指标并行执行 - 支持通过参数灵活配置评估指标 - 保留完整的日志记录功能 - 向后兼容原有评估脚本 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
163 lines
4.9 KiB
Python
163 lines
4.9 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
测试报告保存功能
|
|
|
|
使用已有的评估结果测试报告生成和保存
|
|
"""
|
|
import sys
|
|
import os
|
|
import json
|
|
from pathlib import Path
|
|
from datetime import datetime
|
|
|
|
# 设置 Windows 控制台编码为 UTF-8
|
|
if sys.platform == 'win32':
|
|
import io
|
|
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
|
|
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8')
|
|
|
|
# 添加项目根目录到 Python 路径
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
|
|
def create_mock_result():
|
|
"""创建模拟评估结果"""
|
|
return {
|
|
'system': 'test-single-excel',
|
|
'timestamp': datetime.now().isoformat(),
|
|
'positive_cases': {
|
|
'count': 1,
|
|
'results': [{
|
|
'question_id': 'q001',
|
|
'scores': {
|
|
'context_precision': 1.0,
|
|
'context_recall': 0.5,
|
|
'faithfulness': 1.0,
|
|
'answer_relevance': 0.5525,
|
|
'answer_correctness': 0.5350
|
|
},
|
|
'duration': 497.35,
|
|
'category': 'positive',
|
|
'type': '简单检索'
|
|
}]
|
|
},
|
|
'negative_cases': {
|
|
'count': 0,
|
|
'results': []
|
|
}
|
|
}
|
|
|
|
|
|
def main():
|
|
print()
|
|
print("=" * 80)
|
|
print("测试报告保存功能")
|
|
print("=" * 80)
|
|
print()
|
|
|
|
# 创建模拟结果
|
|
print("步骤 1: 创建模拟评估结果")
|
|
result = create_mock_result()
|
|
print(f"✅ 模拟结果已创建")
|
|
print(f" 系统名: {result['system']}")
|
|
print(f" 正向用例数: {result['positive_cases']['count']}")
|
|
print(f" 负向用例数: {result['negative_cases']['count']}")
|
|
print()
|
|
|
|
# 构建完整结果结构
|
|
print("步骤 2: 构建完整结果结构")
|
|
full_result = {
|
|
'metadata': {
|
|
'version': '1.0',
|
|
'evaluation_date': datetime.now().isoformat(),
|
|
'test_set': 'data/test_sets/ragas_test_cases_v3.1.json',
|
|
'systems': ['test-single-excel']
|
|
},
|
|
'results': {
|
|
'test-single-excel': result
|
|
}
|
|
}
|
|
print(f"✅ 完整结果结构已创建")
|
|
print()
|
|
|
|
# 生成报告
|
|
print("步骤 3: 生成 JSON 报告")
|
|
from src.reporters.json_reporter import JSONReporter
|
|
|
|
reporter = JSONReporter()
|
|
output_path = f'data/results/test-single-excel_test_{datetime.now().strftime("%Y%m%d_%H%M%S")}.json'
|
|
|
|
try:
|
|
reporter.generate_report(all_results=full_result, output_path=output_path)
|
|
print(f"✅ 报告已成功生成")
|
|
print(f" 路径: {output_path}")
|
|
print()
|
|
|
|
# 验证文件
|
|
print("步骤 4: 验证报告文件")
|
|
if Path(output_path).exists():
|
|
file_size = Path(output_path).stat().st_size / 1024
|
|
print(f"✅ 文件存在")
|
|
print(f" 大小: {file_size:.2f} KB")
|
|
|
|
# 读取并验证内容
|
|
with open(output_path, 'r', encoding='utf-8') as f:
|
|
report_data = json.load(f)
|
|
|
|
print(f"✅ JSON 格式有效")
|
|
|
|
# 检查必要字段
|
|
checks = [
|
|
('metadata', '元数据'),
|
|
('results', '结果数据'),
|
|
('comparison', '对比信息'),
|
|
('analysis_by_question_type', '问题类型分析')
|
|
]
|
|
|
|
all_valid = True
|
|
for field, desc in checks:
|
|
if field in report_data:
|
|
print(f"✅ {desc}: 存在")
|
|
else:
|
|
print(f"❌ {desc}: 缺失")
|
|
all_valid = False
|
|
|
|
print()
|
|
|
|
if all_valid:
|
|
print("=" * 80)
|
|
print("✅ 所有测试通过!")
|
|
print("=" * 80)
|
|
print()
|
|
print("报告内容摘要:")
|
|
print(f" 版本: {report_data['metadata'].get('version')}")
|
|
print(f" 评估时间: {report_data['metadata'].get('evaluation_date')}")
|
|
print(f" 系统: {', '.join(report_data['metadata'].get('systems', []))}")
|
|
|
|
if 'test-single-excel' in report_data['results']:
|
|
sys_result = report_data['results']['test-single-excel']
|
|
if 'overall_score' in sys_result:
|
|
print(f" 总体得分: {sys_result['overall_score']:.4f}")
|
|
print()
|
|
return 0
|
|
else:
|
|
print("❌ 部分测试失败")
|
|
return 1
|
|
|
|
else:
|
|
print("❌ 文件不存在")
|
|
return 1
|
|
|
|
except Exception as e:
|
|
print(f"❌ 生成报告时出错: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
return 1
|
|
|
|
|
|
if __name__ == '__main__':
|
|
exit_code = main()
|
|
input("\n按 Enter 键退出...")
|
|
sys.exit(exit_code)
|