2026-01-07 22:33:35 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
"""
|
|
|
|
|
评估单个 RAG 系统
|
|
|
|
|
|
|
|
|
|
用于调试和单系统测试
|
|
|
|
|
"""
|
|
|
|
|
import sys
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
# 添加项目根目录到 Python 路径
|
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
|
import argparse
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
from src.evaluators.batch_evaluator import BatchEvaluator
|
|
|
|
|
from src.reporters.json_reporter import JSONReporter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parse_args():
|
|
|
|
|
"""解析命令行参数"""
|
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
|
description='评估单个 RAG 系统'
|
|
|
|
|
)
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'--system',
|
|
|
|
|
type=str,
|
|
|
|
|
required=True,
|
2026-01-08 01:16:34 +08:00
|
|
|
help='要评估的系统名称 (coze, kbs, zht, zhtv2, zht-excel, zht-text, zhtv2-excel, zhtv2-text)'
|
2026-01-07 22:33:35 +08:00
|
|
|
)
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'--test-set',
|
|
|
|
|
type=str,
|
|
|
|
|
default='data/test_sets/ragas_test_cases_v3.1.json',
|
|
|
|
|
help='测试集路径 (默认: data/test_sets/ragas_test_cases_v3.1.json)'
|
|
|
|
|
)
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'--output',
|
|
|
|
|
type=str,
|
|
|
|
|
default=None,
|
|
|
|
|
help='输出报告路径 (默认: data/results/{system}_report_YYYYMMDD_HHMMSS.json)'
|
|
|
|
|
)
|
2026-01-08 01:02:19 +08:00
|
|
|
parser.add_argument(
|
|
|
|
|
'--no-logging',
|
|
|
|
|
action='store_true',
|
|
|
|
|
help='禁用详细日志记录(默认启用)'
|
|
|
|
|
)
|
2026-01-07 22:33:35 +08:00
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
|
"""主函数"""
|
|
|
|
|
args = parse_args()
|
|
|
|
|
|
|
|
|
|
print("=" * 80)
|
|
|
|
|
print(f"催收 RAG 评估系统 - 单系统评估: {args.system.upper()}")
|
|
|
|
|
print("=" * 80)
|
|
|
|
|
print(f"启动时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
|
|
|
|
|
print(f"测试集: {args.test_set}")
|
|
|
|
|
print("LLM 和 Embedding 配置从 .env 文件读取")
|
|
|
|
|
print("=" * 80)
|
|
|
|
|
|
|
|
|
|
# 验证测试集文件存在
|
|
|
|
|
test_set_path = Path(args.test_set)
|
|
|
|
|
if not test_set_path.exists():
|
|
|
|
|
print(f"\n[ERROR] 测试集文件不存在: {args.test_set}")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# 创建批量评估器(只评估一个系统)
|
|
|
|
|
# LLM 和 Embedding 配置从 .env 文件读取
|
|
|
|
|
print(f"\n初始化评估器...")
|
2026-01-08 01:02:19 +08:00
|
|
|
enable_logging = not args.no_logging
|
|
|
|
|
evaluator = BatchEvaluator([args.system], str(test_set_path), enable_logging=enable_logging)
|
2026-01-07 22:33:35 +08:00
|
|
|
print(f"[OK] 评估器初始化完成")
|
2026-01-08 01:02:19 +08:00
|
|
|
if enable_logging:
|
|
|
|
|
print(f"[INFO] 详细日志将保存到 logs/ 目录")
|
|
|
|
|
else:
|
|
|
|
|
print(f"[INFO] 详细日志记录已禁用")
|
2026-01-07 22:33:35 +08:00
|
|
|
|
|
|
|
|
# 执行评估
|
|
|
|
|
print(f"\n开始评估...")
|
|
|
|
|
start_time = datetime.now()
|
|
|
|
|
|
|
|
|
|
all_results = await evaluator.evaluate_all_systems()
|
|
|
|
|
|
|
|
|
|
end_time = datetime.now()
|
|
|
|
|
duration = (end_time - start_time).total_seconds()
|
|
|
|
|
|
|
|
|
|
print(f"\n评估完成! 耗时: {duration:.2f} 秒")
|
|
|
|
|
|
|
|
|
|
# 生成报告
|
|
|
|
|
if args.output:
|
|
|
|
|
output_path = args.output
|
|
|
|
|
else:
|
|
|
|
|
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
|
|
|
|
output_path = f"data/results/{args.system}_report_{timestamp}.json"
|
|
|
|
|
|
|
|
|
|
reporter = JSONReporter()
|
|
|
|
|
reporter.generate_report(all_results, output_path)
|
|
|
|
|
|
|
|
|
|
print("\n" + "=" * 80)
|
|
|
|
|
print("评估完成!")
|
|
|
|
|
print(f"报告已保存至: {output_path}")
|
|
|
|
|
print("=" * 80)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
asyncio.run(main())
|