102 lines
3.4 KiB
Python
102 lines
3.4 KiB
Python
#!/usr/bin/env python
|
|||
|
|
# -*- coding: utf-8 -*-
|
||
|
|
"""
|
||
|
|
测试日志记录功能
|
||
|
|
|
||
|
|
使用一个简单的测试用例验证日志记录是否正常工作
|
||
|
|
"""
|
||
|
|
import sys
|
||
|
|
import os
|
||
|
|
|
||
|
|
# 设置 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__))))
|
||
|
|
|
||
|
|
import asyncio
|
||
|
|
import json
|
||
|
|
from pathlib import Path
|
||
|
|
from src.utils.evaluation_logger import EvaluationLogger
|
||
|
|
from src.evaluators.ragas_evaluator import RAGAsEvaluator
|
||
|
|
|
||
|
|
|
||
|
|
async def test_logging():
|
||
|
|
"""测试日志记录功能"""
|
||
|
|
print("=" * 80)
|
||
|
|
print("测试日志记录功能")
|
||
|
|
print("=" * 80)
|
||
|
|
|
||
|
|
# 创建日志记录器
|
||
|
|
logger = EvaluationLogger(system_name="test")
|
||
|
|
print(f"\n日志文件: {logger.get_log_file_path()}")
|
||
|
|
|
||
|
|
# 创建评估器(带日志记录)
|
||
|
|
print("\n初始化评估器...")
|
||
|
|
evaluator = RAGAsEvaluator(logger=logger)
|
||
|
|
print("✅ 评估器初始化成功")
|
||
|
|
|
||
|
|
# 创建测试样本
|
||
|
|
print("\n创建测试样本...")
|
||
|
|
test_sample = {
|
||
|
|
"question_id": "test_001",
|
||
|
|
"category": "positive",
|
||
|
|
"type": "simple_retrieval",
|
||
|
|
"user_input": "什么是催收业务?",
|
||
|
|
"response": "催收业务是指金融机构或专业催收公司对逾期未还款项进行追讨的业务活动。",
|
||
|
|
"reference": "催收业务是金融领域中针对逾期债务进行追讨和管理的服务。",
|
||
|
|
"retrieved_contexts": [
|
||
|
|
"催收业务主要包括电话催收、上门催收、法律催收等方式。",
|
||
|
|
"催收需要遵守相关法律法规,不得采用暴力、威胁等非法手段。"
|
||
|
|
]
|
||
|
|
}
|
||
|
|
|
||
|
|
# 执行评估
|
||
|
|
print("\n开始评估...")
|
||
|
|
scores = await evaluator.evaluate_single(test_sample)
|
||
|
|
|
||
|
|
# 输出结果
|
||
|
|
print("\n" + "=" * 80)
|
||
|
|
print("评估结果")
|
||
|
|
print("=" * 80)
|
||
|
|
for metric, score in scores.items():
|
||
|
|
if score is not None:
|
||
|
|
print(f"{metric}: {score:.4f}")
|
||
|
|
else:
|
||
|
|
print(f"{metric}: 失败")
|
||
|
|
|
||
|
|
# 读取并显示日志文件的前几行
|
||
|
|
print("\n" + "=" * 80)
|
||
|
|
print("日志文件内容预览(前5条)")
|
||
|
|
print("=" * 80)
|
||
|
|
|
||
|
|
log_file = Path(logger.get_log_file_path())
|
||
|
|
if log_file.exists():
|
||
|
|
with open(log_file, 'r', encoding='utf-8') as f:
|
||
|
|
lines = f.readlines()
|
||
|
|
for i, line in enumerate(lines[:5], 1):
|
||
|
|
log_entry = json.loads(line)
|
||
|
|
print(f"\n日志条目 #{i}:")
|
||
|
|
print(f" 类型: {log_entry.get('type')}")
|
||
|
|
print(f" 时间: {log_entry.get('timestamp')}")
|
||
|
|
if log_entry.get('type') == 'metric_evaluation':
|
||
|
|
print(f" 指标: {log_entry.get('metric_name')}")
|
||
|
|
print(f" 得分: {log_entry.get('score')}")
|
||
|
|
print(f" LLM 调用次数: {len(log_entry.get('llm_calls', []))}")
|
||
|
|
print(f" Embedding 调用次数: {len(log_entry.get('embedding_calls', []))}")
|
||
|
|
|
||
|
|
print(f"\n完整日志文件: {log_file}")
|
||
|
|
print(f"总日志条目数: {len(lines)}")
|
||
|
|
else:
|
||
|
|
print("❌ 日志文件不存在")
|
||
|
|
|
||
|
|
print("\n" + "=" * 80)
|
||
|
|
print("测试完成!")
|
||
|
|
print("=" * 80)
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == '__main__':
|
||
|
|
asyncio.run(test_logging())
|