162 lines
4.9 KiB
Python
162 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)
|