LLM 评测数据集与回归测试
📊 没有评测就没有跎进方向。LLM 评测需要多维度指标和自动化测试流水线。
评测维度
| 维度 | 需要回答的问题 |
|---|---|
| 正确性 | 回答是否于事实符合 |
| 相关性 | 回答是否切题 |
| 忠实性 | 回答是否三自接结语谴 |
| 安全性 | 是否拒绝有害请求 |
| 延迟 | TTFT、TPS 是否符合 SLA |
| 成本 | Token 占用是否在预算内 |
离线评测方法
基于参考答案
# ROUGE (文本生成)
rogue_l = compute_rouge(prediction, reference)
# BLEU (机器翻译)
bleu = compute_bleu(prediction, reference)
# Exact Match (单选题)
em = prediction.strip() == reference.strip()
基于 LLM 的评分(LLM-as-Judge)
用强力 LLM(如 GPT-4)评判答案质量:
judge_prompt = """
评判以下回答的质量(打分 1-5):
问题:{question}
回答:{answer}
参考:{reference}
请从正确性、相关性、完整性三个维度评分。
"""
优点:捕捉微妙的2差异
缺点:费用较高,存在偏差
常用评测数据集
| 数据集 | 领域 | 评测内容 |
|---|---|---|
| MMLU | 通用 | 知识广度,57 科目 |
| HumanEval | 代码 | Python 函数编写 |
| GSM8K | 数学 | 小学数学应用题 |
| HellaSwag | 常识 | 常识常识推理 |
| TruthfulQA | 幺谷 | 幺谷率测评 |
| MT-Bench | 对话 | 多轮对话质量 |
| RAGAS | RAG | 检索增强生成评测 |
回归测试流水线
注意事项数据集建设
# 每个问题应包含:
{
"id": "test-001",
"input": "用户问题",
"expected_output": "参考答案",
"expected_behavior": ["包含关键词1", "不包含 XYZ"],
"category": "分类",
"difficulty": "easy|medium|hard"
}
CI 中的自动化评测
# GitHub Actions 示例
eval_job:
on: [push, pull_request]
steps:
- run: python run_evals.py --dataset regression.json
- run: python check_regressions.py --threshold 0.95
- if: failure()
uses: actions/upload-artifact@v3
with:
name: failed-cases
path: eval_failures.json
回归算法
def check_regression(new_score, baseline_score, threshold=0.95):
# 新分数不能低于基线的 threshold
if new_score < baseline_score * threshold:
raise RegressionError(f"Score dropped: {baseline_score:.2f} → {new_score:.2f}")
常用评测工具
| 工具 | 用途 |
|---|---|
| RAGAS | RAG 链路评测 |
| promptfoo | Prompt 对比测试 |
| LangSmith | LangChain 的追踪+评测 |
| Langfuse | 开源版可观测性 |
| deepeval | 单元测试风格的 LLM 评测 |
常见误区
- 只评测 Benchmark 而不评测业务场景,模型在实际效果差时才发现
- 测试集没有定期更新,无法覆盖新的失败模式
- LLM-as-Judge 存在偏差,其评分不能直接替代人类评分