docs: 完善项目文档体系
## 新增文档 ### 核心文档 - README.md: 项目主要说明文档,包含快速开始、功能介绍、使用指南 - docs/ARCHITECTURE.md: 架构设计文档,详细说明系统架构和技术选型 - docs/DEVELOPMENT_GUIDE.md: 开发指南,包含环境搭建、代码规范、测试指南 - docs/progress/TODO.md: 项目待办事项和进度追踪 ### 工作总结 - docs/SESSION_SUMMARY_2025-01-07.md: 测试集精修工作总结 ## 新增代码模块 ### 评估器模块 - src/evaluators/ragas_evaluator.py: RAGAs评估器实现 - src/evaluators/batch_evaluator.py: 批量评估器 ### 报告模块 - src/reporters/json_reporter.py: JSON格式报告生成器 ### 转换器模块 - src/transformers/data_converter.py: 数据格式转换器 ### 工具模块 - src/utils/llm_config.py: LLM配置管理 ## 文档特点 - 📖 完整的项目说明和快速开始指南 - 🏗️ 详细的架构设计和技术选型说明 - 👨💻 实用的开发指南和最佳实践 - 📋 清晰的待办事项和进度追踪 - 🎯 明确的里程碑和优先级 🤖 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
f10d5492a1
commit
7fe9a7241e
16 files changed
+3023
-1
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,112 @@
|
||||
"""
|
||||
LLM 配置模块:初始化评估所需的 LLM 和 Embedding 模型
|
||||
"""
|
||||
import os
|
||||
from typing import Dict
|
||||
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
|
||||
from ragas.llms import LangchainLLMWrapper
|
||||
|
||||
|
||||
def get_evaluator_llm(model: str = "gpt-4o-mini", temperature: float = 0.0):
|
||||
"""
|
||||
获取评估用 LLM
|
||||
|
||||
Args:
|
||||
model: 模型名称,默认 gpt-4o-mini(平衡成本和质量)
|
||||
temperature: 温度参数,默认 0.0(确保评估一致性)
|
||||
|
||||
Returns:
|
||||
RAGAs 兼容的 LLM 包装器
|
||||
"""
|
||||
# 检查 API key
|
||||
if not os.getenv('OPENAI_API_KEY'):
|
||||
raise ValueError(
|
||||
"OPENAI_API_KEY not found in environment variables. "
|
||||
"Please set it before running evaluation."
|
||||
)
|
||||
|
||||
# 初始化 LangChain LLM
|
||||
llm = ChatOpenAI(
|
||||
model=model,
|
||||
temperature=temperature,
|
||||
timeout=30,
|
||||
max_retries=3
|
||||
)
|
||||
|
||||
# 包装为 RAGAs 兼容格式
|
||||
return LangchainLLMWrapper(llm)
|
||||
|
||||
|
||||
def get_evaluator_embeddings(model: str = "text-embedding-3-small"):
|
||||
"""
|
||||
获取评估用 Embedding 模型
|
||||
|
||||
Args:
|
||||
model: Embedding 模型名称
|
||||
|
||||
Returns:
|
||||
OpenAI Embeddings 实例
|
||||
"""
|
||||
# 检查 API key
|
||||
if not os.getenv('OPENAI_API_KEY'):
|
||||
raise ValueError(
|
||||
"OPENAI_API_KEY not found in environment variables. "
|
||||
"Please set it before running evaluation."
|
||||
)
|
||||
|
||||
return OpenAIEmbeddings(model=model)
|
||||
|
||||
|
||||
def estimate_cost(num_samples: int, num_metrics: int = 5) -> Dict[str, float]:
|
||||
"""
|
||||
估算评估成本
|
||||
|
||||
Args:
|
||||
num_samples: 样本数量
|
||||
num_metrics: 指标数量
|
||||
|
||||
Returns:
|
||||
成本估算字典
|
||||
"""
|
||||
# 每个指标的平均 token 消耗(估算)
|
||||
tokens_per_metric = {
|
||||
'context_precision': 500,
|
||||
'context_recall': 800,
|
||||
'faithfulness': 700,
|
||||
'answer_relevance': 600, # + embedding
|
||||
'answer_correctness': 900, # + embedding
|
||||
}
|
||||
|
||||
# GPT-4o-mini 定价(2025年1月)
|
||||
# Input: $0.150 / 1M tokens
|
||||
# Output: $0.600 / 1M tokens
|
||||
input_price_per_1m = 0.150
|
||||
output_price_per_1m = 0.600
|
||||
|
||||
# Embedding 定价
|
||||
# text-embedding-3-small: $0.020 / 1M tokens
|
||||
embedding_price_per_1m = 0.020
|
||||
|
||||
# 估算总 token 消耗
|
||||
total_tokens = sum(tokens_per_metric.values()) * num_samples
|
||||
# 假设 input:output = 3:1
|
||||
input_tokens = total_tokens * 0.75
|
||||
output_tokens = total_tokens * 0.25
|
||||
|
||||
# LLM 成本
|
||||
llm_cost = (input_tokens / 1_000_000 * input_price_per_1m +
|
||||
output_tokens / 1_000_000 * output_price_per_1m)
|
||||
|
||||
# Embedding 成本(answer_relevance 和 answer_correctness 各需要 2 次)
|
||||
embedding_tokens = num_samples * 2 * 2 * 500 # 估算每次 500 tokens
|
||||
embedding_cost = embedding_tokens / 1_000_000 * embedding_price_per_1m
|
||||
|
||||
total_cost = llm_cost + embedding_cost
|
||||
|
||||
return {
|
||||
'total_cost': round(total_cost, 4),
|
||||
'llm_cost': round(llm_cost, 4),
|
||||
'embedding_cost': round(embedding_cost, 4),
|
||||
'total_tokens': int(total_tokens),
|
||||
'cost_per_sample': round(total_cost / num_samples, 6) if num_samples > 0 else 0
|
||||
}
|
||||
Reference in new issue
Block a user