在探索和优化 RAG 系统的过程中,如何有效评估其性能是迭代优化的关键。没有评测就无法对比不同方案的优劣,也无法知道优化是否真的有效。
构建评测数据集需要包含四列:
question:要评估的问题ground_truths:问题的真实答案answer:RAG 预测的答案contexts:RAG 用于生成答案的相关上下文列表Your task is to formulate exactly {num_questions} questions from given context
and provide the answer to each one.
End each question with a '?' character and then in a newline write
the answer to that question using only the context provided.
Separate each question/answer pair by "XXX"
Each question must start with "question:".
Each answer must start with "answer:".
Rules:
1. The question should make sense to humans even when read without the given context.
2. The question should be fully answered from the given context.
3. The question should be framed from a part of context that contains important information.
4. The answer should not contain any links.
5. The question should be of moderate difficulty.
6. Avoid compound questions that can be split into multiple.
7. The question should not exceed 10 words, abbreviate where possible.
context: {context}主要分为两类:独立评估和端到端评估。
独立评估分别对检索模块和生成模块单独评估。
答案相关性 (Answer Relevancy):评估生成答案和问题的相关性,评分 0-1:
忠实度 (Faithfulness):评估生成答案和检索上下文的事实一致性:
答案正确性:评估生成答案和基准答案的匹配程度,评分 0-1。
端到端评估对 RAG 生成的最终答案进行整体评估:
RAGAS 是一个基于大语言模型的全自动 RAG 评估框架:
核心指标:
ARES 通过少量标注数据 + 合成数据实现高效评估:
核心流程:
优点:比全人工标注成本低很多,精度接近全人工评估。
RAG-Fusion 是一种检索优化策略,解决了用户查询表达不完整、单一查询覆盖不足的问题。
RAG-Fusion 通过多查询生成 + 结果融合提升召回质量:
RRF 不依赖检索分数的绝对值,只利用相对排名,融合效果稳定:
1def reciprocal_rank_fusion(results_list, k=60):
2 """
3 results_list: 每个查询返回的文档ID列表(按相关性排序)
4 """
5 scores = {}
6 for doc_ids in results_list:
7 for rank, doc_id in enumerate(doc_ids):
8 if doc_id not in scores:
9 scores[doc_id] = 0
10 scores[doc_id] += 1 / (rank + k)
11 # 按融合分数降序排序
12 return sorted(scores.items(), key=lambda x: x[1], reverse=True)优点:
缺点:
Self-RAG 让大模型自己对检索结果进行筛选和反思,解决了传统 RAG 无差别使用所有检索结果的问题。
传统 RAG 不管检索到的上下文是否相关,都全部拼接到 Prompt 中让大模型生成。Self-RAG 让大模型主动判断是否需要检索,以及检索结果是否可用:
Self-RAG 在模型词汇表中加入了特殊的反思令牌:
Retrieve:表示是否需要检索;Critique:对检索结果的相关性、完整性进行评价。模型会生成反思令牌来控制检索流程,实现自我反思。
| 痛点 | 解决方案 |
|---|---|
| 内容缺失(答案不在知识库中却给出错误回答) | 优化数据源质量,改进提示明确让模型在不知道时承认 |
| 关键文档排名低(正确答案在检索结果中但排名靠后) | 增加重排序步骤,调参 chunk_size 和 top_k,使用参数搜索找最优 |
| 检索到文档但生成没用到 | 优化检索策略,微调 embedding 模型,提示压缩去除噪声 |
| 无法从上下文中提取答案(干扰信息太多) | 清理数据,使用 LongLLMLingua 提示压缩,LongContextReorder 重排序 |
| 格式错误(无法生成要求的格式) | 更精确的提示,提供示例,使用输出解析器(Pydantic/Guardrails) |
| 回答过于笼统,细节不足 | 查询优化(路由/改写/分解),HyDE 查询改写,逐步深入检索 |
| 大数据处理性能瓶颈 | 并行处理,多分片并行 ingestion,提高处理速度 |
| 结构化数据查询难 | Chain-of-table,Mix-Self-Consistency,结合程序合成 |
| 复杂 PDF 表格提取难 | 使用版面分析+表格识别,EmbeddedTablesUnstructuredRetriever |
| 主模型故障 | 备用模型路由(Neutrino/OpenRouter) |
| 安全挑战 | Llama Guard 输入输出审核,内容分类 |
回答要点:
回答要点:
回答要点:
回答要点:
回答要点: