
🧠 记忆锚点:检索和生成分段评,分切片看失败;指标只负责报警,样例归因才决定改哪里。
💡 答案要点
RAG 评估 Pipeline 架构:
┌─────────────────────────────────────────────────────────────┐
│ RAG 评估 Pipeline │
└─────────────────────────────────────────────────────────────┘
测试数据集 → 批量评估 → 指标计算 → 质量报告 → 上线/打回
↓ ↓ ↓ ↓
500题QA RAGAS+自研 多维度评分 P80阈值判断
多场景覆盖 TruLens 可视化 根因分析评估测试集构建:
| 类型 | 数量 | 说明 |
|---|---|---|
| 简单事实型 | 100题 | 直接检索可回答 |
| 多跳推理型 | 150题 | 需跨文档推理 |
| 对比型 | 100题 | 多个候选答案选最优 |
| 边界型 | 50题 | 上下文缺失、超长输入 |
| 对抗型 | 100题 | 干扰信息、注入攻击 |
| 总计 | 500题 | 覆盖主流场景 |
自动化评估流程:
展开 Python 代码示例(56 行)
python
from ragas import evaluate
from ragas.metrics import (
faithfulness, answer_relevancy,
context_precision, context_recall
)
def rag_evaluation_pipeline(question, answer, contexts, ground_truth):
"""完整 RAG 评估 Pipeline"""
# 1. 批量评估
result = evaluate(
dataset=[{
"user_input": question,
"retrieved_contexts": contexts,
"response": answer,
"reference": ground_truth
}],
metrics=[
faithfulness, # 忠实度
answer_relevancy, # 回答相关性
context_precision, # 上下文精度
context_recall # 上下文召回
]
)
# 2. 提取指标
scores = {
"faithfulness": result["faithfulness"],
"answer_relevancy": result["answer_relevancy"],
"context_precision": result["context_precision"],
"context_recall": result["context_recall"]
}
# 3. 阈值判断
thresholds = {"faithfulness": 0.8, "answer_relevancy": 0.8,
"context_precision": 0.7, "context_recall": 0.8}
passed = all(scores[k] >= thresholds[k] for k in thresholds)
return {"scores": scores, "passed": passed, "result": result}
# CI/CD 集成示例
def ci_cd_gate():
results = []
for qa in test_dataset:
r = rag_evaluation_pipeline(qa.question, qa.answer, qa.contexts, qa.ground_truth)
results.append(r)
# 计算整体通过率
pass_rate = sum(1 for r in results if r["passed"]) / len(results)
if pass_rate < 0.85:
print(f"❌ 通过率 {pass_rate:.1%} < 85%,不允许上线")
return False
print(f"✅ 通过率 {pass_rate:.1%} >= 85%,允许上线")
return True评估结果 → RAG 迭代优化:
| 低分指标 | 根因分析 | 优化方案 |
|---|---|---|
| Faithfulness 低 | 检索到无关内容 / LLM 幻觉 | 提升检索精度 + 加 Prompt 约束 |
| Answer Relevancy 低 | 答案答非所问 | 优化 Prompt + 重写答案 |
| Context Precision 低 | top-k 中混入了无关文档 | 优化 rerank / 调整 top-k |
| Context Recall 低 | 相关文档没被召回 | 优化 embedding / 扩展检索策略 |
监控告警配置:
yaml
# 生产监控告警规则
alerts:
- metric: faithfulness
threshold: 0.75
action: 触发自动审查 + 邮件告警
- metric: answer_relevancy
threshold: 0.70
action: 自动降级到备用模型
- metric: context_precision
threshold: 0.65
action: 自动触发重检索面试话术:
"我的 RAG 评估 Pipeline 分三层:测试集层(500题覆盖5种场景)、评估层(RAGAS+自研指标)、决策层(P80阈值判断)。每次代码变更先跑评估,通过率 > 85% 才能上线。评估结果直接驱动迭代优化:Faithfulness 低就先优化检索,Context Recall 低就优化 embedding 策略。生产环境用 TruLens 实时监控,每天早上看一次 P50/P95 分数,异常立即告警。"