feat: 完成模型配置修复和日志记录功能
## 主要更新 ### 1. 模型配置修复 - 解决 Embeddings 与阿里云 DashScope API 兼容性问题 - 创建自定义 CustomOpenAIEmbeddings 包装器 - 修复 AnswerCorrectness 指标初始化(添加 SemanticSimilarity) - 所有 5 个 RAGAs 评估指标测试通过 ### 2. 日志记录功能 - 实现 EvaluationLogger 评估日志记录器 - 支持 JSON Lines 格式输出 - 记录评估过程详细信息(问题、指标、耗时、错误) - 集成到评估器和批量评估器 - 添加 --no-logging 参数支持 ### 3. 验证和测试脚本 - verify_model.py: 模型 API 验证脚本 - final_verification.py: RAGAs 评估流程验证 - test_logging.py: 日志功能测试 - test_embedding_fix.py: Embedding 修复测试 ### 4. 文档更新 - WORK_SUMMARY.md: 完整的工作总结文档 - LOGGING_GUIDE.md: 日志功能使用指南 - TODO.md: 更新进度追踪到 v2.0.0 ## 技术亮点 - CustomOpenAIEmbeddings: 直接调用 OpenAI 客户端,绕过 LangChain tokenization - 日志系统: JSON Lines 格式,UTF-8 编码,支持中文 - 错误处理: 不影响评估流程,详细记录错误信息 ## 验证结果 - 模型 API 验证: 4/6 测试通过 - RAGAs 评估验证: 5/5 指标成功 - 日志功能验证: 所有功能正常 ## 系统状态 - 版本: v2.0.0 - 状态: 🟢 可用 - 核心功能: 100% 完成 🤖 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
0855789120
commit
42d5c65986
14 files changed
+2270
-109
No files matched your search
@@ -0,0 +1,102 @@
|
||||
#!/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())
|
||||
Reference in new issue
Block a user