461 lines
12 KiB
Markdown
461 lines
12 KiB
Markdown
# RAGAs 评估性能分析
|
||||
|
|
|
|||
|
|
**分析日期**: 2026-01-08
|
|||
|
|
**测试场景**: 单问题评估(test-single-excel)
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 📊 性能数据总览
|
|||
|
|
|
|||
|
|
### 单问题评估耗时
|
|||
|
|
|
|||
|
|
| 指标 | 耗时(秒) | 占比 | 累计耗时 |
|
|||
|
|
|------|-----------|------|---------|
|
|||
|
|
| **context_precision** | 14.94 | 3.0% | 14.94 |
|
|||
|
|
| **context_recall** | 62.33 | 12.5% | 77.27 |
|
|||
|
|
| **faithfulness** | 89.17 | 17.9% | 166.44 |
|
|||
|
|
| **answer_relevance** | 23.74 | 4.8% | 190.18 |
|
|||
|
|
| **answer_correctness** | 307.17 | 61.5% | 497.35 |
|
|||
|
|
| **总计** | **497.35** | **100%** | - |
|
|||
|
|
|
|||
|
|
**平均耗时**: 约 500 秒/问题(8.3 分钟/问题)
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 🔍 慢速原因分析
|
|||
|
|
|
|||
|
|
### 1. 多个 LLM API 调用
|
|||
|
|
|
|||
|
|
每个 RAGAs 指标都需要调用 LLM API 进行评估,以下是各指标的工作原理:
|
|||
|
|
|
|||
|
|
#### context_precision(上下文精确度)
|
|||
|
|
- **耗时**: 14.94秒
|
|||
|
|
- **原理**: 评估检索到的上下文是否包含回答问题所需的信息
|
|||
|
|
- **API 调用**:
|
|||
|
|
- 1 次 LLM 调用:判断每个检索上下文片段是否相关
|
|||
|
|
- **影响因素**:
|
|||
|
|
- 检索上下文数量(本测试:1个)
|
|||
|
|
- LLM 响应速度
|
|||
|
|
|
|||
|
|
#### context_recall(上下文召回率)
|
|||
|
|
- **耗时**: 62.33秒
|
|||
|
|
- **原理**: 评估参考答案中的信息是否在检索上下文中
|
|||
|
|
- **API 调用**:
|
|||
|
|
- 1 次 LLM 调用:提取参考答案中的关键陈述
|
|||
|
|
- N 次 LLM 调用:验证每个陈述是否在上下文中(N = 陈述数量)
|
|||
|
|
- **影响因素**:
|
|||
|
|
- 参考答案的长度和复杂性
|
|||
|
|
- 需要验证的陈述数量(可能 5-10 个)
|
|||
|
|
- **为什么慢**:
|
|||
|
|
- 需要多轮 LLM 调用
|
|||
|
|
- 每个陈述都需要单独验证
|
|||
|
|
|
|||
|
|
#### faithfulness(忠实度)
|
|||
|
|
- **耗时**: 89.17秒
|
|||
|
|
- **原理**: 评估回答是否忠于检索上下文(无幻觉)
|
|||
|
|
- **API 调用**:
|
|||
|
|
- 1 次 LLM 调用:从回答中提取事实陈述
|
|||
|
|
- N 次 LLM 调用:验证每个陈述是否由上下文支持
|
|||
|
|
- **影响因素**:
|
|||
|
|
- 回答的长度
|
|||
|
|
- 需要验证的陈述数量(通常 5-15 个)
|
|||
|
|
- **为什么慢**:
|
|||
|
|
- 陈述提取和验证都需要 LLM 调用
|
|||
|
|
- 复杂推理需要更长时间
|
|||
|
|
|
|||
|
|
#### answer_relevance(答案相关性)
|
|||
|
|
- **耗时**: 23.74秒
|
|||
|
|
- **原理**: 评估回答是否与问题相关
|
|||
|
|
- **API 调用**:
|
|||
|
|
- 1 次 Embedding 调用:计算问题和回答的相似度
|
|||
|
|
- 或者:1 次 LLM 调用生成问题,然后比较相似度
|
|||
|
|
- **影响因素**:
|
|||
|
|
- Embedding 模型速度
|
|||
|
|
- 文本长度
|
|||
|
|
|
|||
|
|
#### answer_correctness(答案正确性)
|
|||
|
|
- **耗时**: 307.17秒(最慢!)
|
|||
|
|
- **原理**: 评估回答与参考答案的相似度和准确性
|
|||
|
|
- **API 调用**:
|
|||
|
|
- **多次** Embedding 调用:计算语义相似度
|
|||
|
|
- **多次** LLM 调用:逐句评估准确性和完整性
|
|||
|
|
- 可能包含:NLI(自然语言推理)评估
|
|||
|
|
- **影响因素**:
|
|||
|
|
- 文本长度(问题和回答)
|
|||
|
|
- 相似度计算方法
|
|||
|
|
- LLM 推理深度
|
|||
|
|
- **为什么最慢**:
|
|||
|
|
- 涉及多个维度的评估
|
|||
|
|
- 可能需要句子级别的对比
|
|||
|
|
- 语义相似度计算复杂度高
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
### 2. LLM API 响应慢
|
|||
|
|
|
|||
|
|
#### 当前配置
|
|||
|
|
- **API 提供商**: 阿里云 DashScope
|
|||
|
|
- **模型**: qwen-max(或其他)
|
|||
|
|
- **网络延迟**: 国内 API
|
|||
|
|
- **并发限制**: 可能有限流
|
|||
|
|
|
|||
|
|
#### 响应时间估算
|
|||
|
|
```
|
|||
|
|
单次 LLM 调用平均耗时:2-5 秒
|
|||
|
|
|
|||
|
|
context_recall: ~15 次 LLM 调用 = 62 秒 (平均 4.1 秒/次)
|
|||
|
|
faithfulness: ~20 次 LLM 调用 = 89 秒 (平均 4.5 秒/次)
|
|||
|
|
answer_correctness: ~70 次 API 调用 = 307 秒 (平均 4.4 秒/次)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
### 3. 串行执行
|
|||
|
|
|
|||
|
|
当前实现:
|
|||
|
|
```python
|
|||
|
|
# 串行执行每个指标
|
|||
|
|
for metric_name, metric in self.metrics.items():
|
|||
|
|
score = await metric.single_turn_ascore(sample)
|
|||
|
|
results[metric_name] = score
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**问题**:
|
|||
|
|
- 5 个指标依次执行,无法并行
|
|||
|
|
- 即使某些指标相互独立,也必须等待前一个完成
|
|||
|
|
|
|||
|
|
**潜在优化**:
|
|||
|
|
- 并行执行独立的指标(理论上可加速到 500/5 = 100 秒)
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
### 4. 无缓存机制
|
|||
|
|
|
|||
|
|
**问题**:
|
|||
|
|
- 相同问题的重复评估会重新调用 LLM
|
|||
|
|
- Embedding 结果未缓存
|
|||
|
|
- 中间结果未保存
|
|||
|
|
|
|||
|
|
**影响**:
|
|||
|
|
- 测试和调试时重复评估浪费时间
|
|||
|
|
- 无法利用历史评估结果
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 💡 优化建议
|
|||
|
|
|
|||
|
|
### 🔴 短期优化(立即可用)
|
|||
|
|
|
|||
|
|
#### 1. 使用更快的模型
|
|||
|
|
|
|||
|
|
**当前**:qwen-max(质量高,速度慢)
|
|||
|
|
**建议**:qwen-plus 或 qwen-turbo(速度更快)
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
# 在 .env 中修改
|
|||
|
|
EVALUATOR_MODEL=qwen-turbo # 或 qwen-plus
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**预期效果**:
|
|||
|
|
- 速度提升 2-3 倍
|
|||
|
|
- 单问题评估从 500 秒降到 150-250 秒
|
|||
|
|
|
|||
|
|
#### 2. 禁用最慢的指标
|
|||
|
|
|
|||
|
|
**answer_correctness** 占据 61.5% 的时间,可以考虑:
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
# 在 ragas_evaluator.py 中注释掉
|
|||
|
|
self.metrics = {
|
|||
|
|
'context_precision': LLMContextPrecisionWithReference(llm=self.llm),
|
|||
|
|
'context_recall': LLMContextRecall(llm=self.llm),
|
|||
|
|
'faithfulness': Faithfulness(llm=self.llm),
|
|||
|
|
'answer_relevance': ResponseRelevancy(llm=self.llm, embeddings=self.embeddings),
|
|||
|
|
# 'answer_correctness': AnswerCorrectness(...) # 暂时禁用
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**预期效果**:
|
|||
|
|
- 单问题评估从 500 秒降到 190 秒(减少 62%)
|
|||
|
|
|
|||
|
|
#### 3. 减少评估样本数量
|
|||
|
|
|
|||
|
|
**当前**:15 个问题/系统
|
|||
|
|
**建议**:先用 5 个问题快速测试
|
|||
|
|
|
|||
|
|
**预期效果**:
|
|||
|
|
- 总时间从 2 小时降到 40 分钟
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
### 🟡 中期优化(需要开发)
|
|||
|
|
|
|||
|
|
#### 1. 并行执行指标
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
import asyncio
|
|||
|
|
|
|||
|
|
async def evaluate_single_optimized(self, sample_data: Dict):
|
|||
|
|
"""并行评估所有指标"""
|
|||
|
|
|
|||
|
|
sample = SingleTurnSample(...)
|
|||
|
|
|
|||
|
|
# 创建所有评估任务
|
|||
|
|
tasks = {
|
|||
|
|
metric_name: metric.single_turn_ascore(sample)
|
|||
|
|
for metric_name, metric in self.metrics.items()
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
# 并行执行
|
|||
|
|
results = await asyncio.gather(*tasks.values())
|
|||
|
|
|
|||
|
|
return dict(zip(self.metrics.keys(), results))
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**预期效果**:
|
|||
|
|
- 理论上可加速到 500/5 = 100 秒(理想情况)
|
|||
|
|
- 实际可能 150-200 秒(考虑 API 并发限制)
|
|||
|
|
|
|||
|
|
#### 2. 添加缓存机制
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
import hashlib
|
|||
|
|
import json
|
|||
|
|
from pathlib import Path
|
|||
|
|
|
|||
|
|
class CachedRAGAsEvaluator:
|
|||
|
|
def __init__(self, cache_dir='cache/evaluations'):
|
|||
|
|
self.cache_dir = Path(cache_dir)
|
|||
|
|
self.cache_dir.mkdir(parents=True, exist_ok=True)
|
|||
|
|
|
|||
|
|
def _get_cache_key(self, sample: Dict, metric_name: str):
|
|||
|
|
"""生成缓存键"""
|
|||
|
|
data = {
|
|||
|
|
'user_input': sample['user_input'],
|
|||
|
|
'response': sample['response'],
|
|||
|
|
'reference': sample['reference'],
|
|||
|
|
'metric': metric_name
|
|||
|
|
}
|
|||
|
|
return hashlib.md5(json.dumps(data, sort_keys=True).encode()).hexdigest()
|
|||
|
|
|
|||
|
|
async def evaluate_with_cache(self, sample, metric_name):
|
|||
|
|
cache_key = self._get_cache_key(sample, metric_name)
|
|||
|
|
cache_file = self.cache_dir / f'{cache_key}.json'
|
|||
|
|
|
|||
|
|
# 检查缓存
|
|||
|
|
if cache_file.exists():
|
|||
|
|
with open(cache_file) as f:
|
|||
|
|
return json.load(f)['score']
|
|||
|
|
|
|||
|
|
# 评估并保存
|
|||
|
|
score = await metric.single_turn_ascore(sample)
|
|||
|
|
|
|||
|
|
with open(cache_file, 'w') as f:
|
|||
|
|
json.dump({'score': score}, f)
|
|||
|
|
|
|||
|
|
return score
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**预期效果**:
|
|||
|
|
- 重复评估无需调用 LLM
|
|||
|
|
- 测试和调试时节省大量时间
|
|||
|
|
|
|||
|
|
#### 3. 批量 Embedding
|
|||
|
|
|
|||
|
|
**问题**:当前每个文本单独调用 Embedding API
|
|||
|
|
**优化**:批量计算 Embedding
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
# 批量计算所有文本的 embeddings
|
|||
|
|
texts = [sample['user_input'], sample['response'], sample['reference']]
|
|||
|
|
embeddings = await self.embeddings.embed_texts(texts)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**预期效果**:
|
|||
|
|
- 减少 API 调用次数
|
|||
|
|
- 降低网络延迟
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
### 🟢 长期优化(架构改进)
|
|||
|
|
|
|||
|
|
#### 1. 使用本地模型
|
|||
|
|
|
|||
|
|
**方案**:部署本地 LLM(如 Llama、Qwen本地版)
|
|||
|
|
|
|||
|
|
**优点**:
|
|||
|
|
- 无 API 网络延迟
|
|||
|
|
- 无并发限制
|
|||
|
|
- 可完全控制性能
|
|||
|
|
|
|||
|
|
**缺点**:
|
|||
|
|
- 需要 GPU 资源
|
|||
|
|
- 模型质量可能不如云端
|
|||
|
|
|
|||
|
|
#### 2. 分布式评估
|
|||
|
|
|
|||
|
|
**方案**:使用多个 Worker 并行评估不同问题
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
# 架构
|
|||
|
|
Questions Queue → Worker Pool → Results Aggregator
|
|||
|
|
↓
|
|||
|
|
[Worker 1, Worker 2, ...]
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**预期效果**:
|
|||
|
|
- 8 核机器理论上可加速 6-7 倍
|
|||
|
|
- 15 个问题从 2 小时降到 20 分钟
|
|||
|
|
|
|||
|
|
#### 3. 增量评估
|
|||
|
|
|
|||
|
|
**方案**:只评估新增或变化的问题
|
|||
|
|
|
|||
|
|
**实现**:
|
|||
|
|
- 记录每个问题的哈希值
|
|||
|
|
- 只评估哈希值变化的问题
|
|||
|
|
- 复用历史结果
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 📈 性能优化效果预估
|
|||
|
|
|
|||
|
|
### 场景 1:快速测试(立即可用)
|
|||
|
|
|
|||
|
|
| 优化项 | 耗时(秒) | 累计(秒) | 减少比例 |
|
|||
|
|
|-------|-----------|-----------|----------|
|
|||
|
|
| 使用 qwen-turbo | 167 | 167 | -66% |
|
|||
|
|
| 禁用 answer_correctness | 190 | 190 | -62% |
|
|||
|
|
| 两者结合 | 63 | 63 | -87% |
|
|||
|
|
|
|||
|
|
**结论**:结合两者,单问题评估从 500 秒降到 63 秒(约 1 分钟)
|
|||
|
|
|
|||
|
|
### 场景 2:中期优化(需要开发)
|
|||
|
|
|
|||
|
|
| 优化项 | 耗时(秒) | 减少比例 |
|
|||
|
|
|-------|-----------|----------|
|
|||
|
|
| 并行执行 | 150 | -70% |
|
|||
|
|
| 添加缓存 | 0(命中缓存) | -100% |
|
|||
|
|
| 批量 Embedding | 400 | -20% |
|
|||
|
|
|
|||
|
|
### 场景 3:长期优化(架构改进)
|
|||
|
|
|
|||
|
|
| 优化项 | 15 问题总耗时 | 加速比 |
|
|||
|
|
|-------|--------------|--------|
|
|||
|
|
| 当前(串行) | 125 分钟 | 1x |
|
|||
|
|
| 并行指标 | 40 分钟 | 3.1x |
|
|||
|
|
| 分布式评估 | 20 分钟 | 6.3x |
|
|||
|
|
| 本地模型 | 30 分钟 | 4.2x |
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 🎯 推荐方案
|
|||
|
|
|
|||
|
|
### 方案 A:快速验证(推荐用于测试)
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
# 1. 使用更快的模型
|
|||
|
|
EVALUATOR_MODEL=qwen-turbo
|
|||
|
|
|
|||
|
|
# 2. 减少评估指标(暂时禁用 answer_correctness)
|
|||
|
|
# 3. 减少样本数量(5 个问题)
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**效果**:
|
|||
|
|
- 单问题:约 1 分钟
|
|||
|
|
- 5 问题:约 5 分钟
|
|||
|
|
- 15 问题:约 15 分钟
|
|||
|
|
|
|||
|
|
### 方案 B:平衡方案(推荐用于正式评估)
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
# 1. 使用 qwen-plus(质量和速度平衡)
|
|||
|
|
EVALUATOR_MODEL=qwen-plus
|
|||
|
|
|
|||
|
|
# 2. 保留所有指标
|
|||
|
|
|
|||
|
|
# 3. 实现并行执行
|
|||
|
|
# 4. 添加缓存机制
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**效果**:
|
|||
|
|
- 单问题:约 2-3 分钟
|
|||
|
|
- 15 问题:约 30-45 分钟
|
|||
|
|
|
|||
|
|
### 方案 C:大规模评估(推荐用于系统对比)
|
|||
|
|
|
|||
|
|
```python
|
|||
|
|
# 1. 实现分布式评估
|
|||
|
|
# 2. 使用更快的模型
|
|||
|
|
# 3. 添加缓存和增量评估
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**效果**:
|
|||
|
|
- 8 系统 × 15 问题 = 120 问题
|
|||
|
|
- 从 16 小时降到 2-3 小时
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 🛠️ 快速优化实施指南
|
|||
|
|
|
|||
|
|
### 步骤 1:修改模型配置
|
|||
|
|
|
|||
|
|
编辑 `.env` 文件:
|
|||
|
|
```bash
|
|||
|
|
# 原来
|
|||
|
|
EVALUATOR_MODEL=qwen-max
|
|||
|
|
|
|||
|
|
# 改为
|
|||
|
|
EVALUATOR_MODEL=qwen-turbo
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 步骤 2:禁用慢速指标(可选)
|
|||
|
|
|
|||
|
|
编辑 `src/evaluators/ragas_evaluator.py`:
|
|||
|
|
```python
|
|||
|
|
self.metrics = {
|
|||
|
|
'context_precision': LLMContextPrecisionWithReference(llm=self.llm),
|
|||
|
|
'context_recall': LLMContextRecall(llm=self.llm),
|
|||
|
|
'faithfulness': Faithfulness(llm=self.llm),
|
|||
|
|
'answer_relevance': ResponseRelevancy(llm=self.llm, embeddings=self.embeddings),
|
|||
|
|
# 暂时注释掉最慢的指标
|
|||
|
|
# 'answer_correctness': AnswerCorrectness(
|
|||
|
|
# llm=self.llm,
|
|||
|
|
# embeddings=self.embeddings,
|
|||
|
|
# answer_similarity=answer_similarity
|
|||
|
|
# )
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 步骤 3:减少评估样本
|
|||
|
|
|
|||
|
|
编辑测试集,只选择前 5 个问题进行快速测试。
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 📊 总结
|
|||
|
|
|
|||
|
|
### 为什么评估这么慢?
|
|||
|
|
|
|||
|
|
1. **5 个指标需要多次 LLM 调用**(估计 100+ 次 API 调用)
|
|||
|
|
2. **单次 LLM 调用平均 4-5 秒**
|
|||
|
|
3. **answer_correctness 指标特别慢**(占总时间的 61.5%)
|
|||
|
|
4. **串行执行,无并行化**
|
|||
|
|
5. **无缓存机制**
|
|||
|
|
|
|||
|
|
### 能否优化?
|
|||
|
|
|
|||
|
|
✅ **可以!** 通过以下方法:
|
|||
|
|
|
|||
|
|
| 方法 | 难度 | 效果 | 推荐度 |
|
|||
|
|
|------|------|------|--------|
|
|||
|
|
| 使用更快的模型 | ⭐ | 2-3x 加速 | ⭐⭐⭐⭐⭐ |
|
|||
|
|
| 禁用慢速指标 | ⭐ | 2.6x 加速 | ⭐⭐⭐⭐ |
|
|||
|
|
| 并行执行 | ⭐⭐⭐ | 3-5x 加速 | ⭐⭐⭐⭐ |
|
|||
|
|
| 添加缓存 | ⭐⭐ | 重复评估接近 0 | ⭐⭐⭐⭐⭐ |
|
|||
|
|
| 分布式评估 | ⭐⭐⭐⭐ | 6-8x 加速 | ⭐⭐⭐ |
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
**文档版本**: v1.0
|
|||
|
|
**最后更新**: 2026-01-08
|