RAG-03重排组装-Citation-Packing-RAG

本文属于 RAG 工程框架中的「3 重排与证据组装」环节,聚焦「Citation Packing(引用组装)」方法。可先阅读 RAG-00.方法概述 再进入本篇。

原理

把答案拆分为声明(claim),为每个声明绑定最小证据片段,保证可追溯与可审计。

关键技术/实现路径

  • claim 切分与证据匹配。
  • 一句一证据或多句一证据策略。
  • 引用冲突检测与去重。

优缺点

  • 优点:提升可信度和可审计性。
  • 缺点:实现复杂度提升。

性能与资源

  • 中等开销,适合高可信场景。

应用场景

  • 合规问答、法律和医疗知识助手。

统一合成数据示例

输入数据片段

1
2
3
4
5
6
7
{
"answer_claims": ["机票上限2000元", "30天内提交"],
"evidence_pool": [
{"doc_id": "D01", "span_id": "span1", "text": "机票报销上限 2000 元"},
{"doc_id": "D01", "span_id": "span2", "text": "需在出差结束后 30 天内提交"}
]
}

中间结果(claim–证据最小对齐)

1
2
3
4
[
{"claim": "机票上限2000元", "matched_span": "span1", "overlap_score": 0.98},
{"claim": "30天内提交", "matched_span": "span2", "overlap_score": 0.95}
]

最终生成示例(含引用)

1
{"answer":"机票上限2000元,需30天内提交。","citations":[{"doc_id":"D01","evidence_span":"span1"},{"doc_id":"D01","evidence_span":"span2"}]}

原始发表与工程实现

  • 代表性原始发表:Attributed QA 系列。
  • 核心解决问题:解决答案与证据绑定。
  • 成熟实现工具:LlamaIndex CitationQueryEngine, Haystack。

详细原理拆解

  • claim-evidence 绑定,提升 citation precision 与答案可审计性。
  • 典型实现可拆为:输入预处理 -> 方法核心计算 -> 候选/证据构建 -> 生成与引用。
  • 工程调优重点:质量(准确率/引用率)与成本(时延/token)的联合优化。
1
2
3
4
5
flowchart LR
In[输入 Query 与知识] --> Core[方法核心计算]
Core --> Rank[匹配/路由/排序]
Rank --> Build[证据组装]
Build --> Out[答案与引用]

工程落地扩展示例

伪代码

1
2
3
4
5
6
7
8
def citation_pack(draft_answer, evidence_spans, aligner):
claims = split_claims(draft_answer)
packed = []
for c in claims:
span = aligner.best_match(c, evidence_spans) # 每句绑定最小证据
packed.append({"claim": c, "cite": span.id})
assert_no_unsupported_claims(packed)
return packed

参数示例

1
2
3
4
claim_splitter: sentence_or_llm
aligner: cosine_or_entailment
min_support_score: 0.85
on_missing: regenerate_or_abstain

常见失败案例

  • 失败模式 1:一句 claim 合并两个事实,只能绑一条证据,审计仍不可信。
  • 失败模式 2:对齐阈值过低,错误 span 被引用为支持。
  • 失败模式 3:证据池来自多个版本制度,span 与 claim 版本不一致

Demo 数据带入计算示例

1
2
c1「上限2000」↔ span1 重叠 0.98;c2「30天内」↔ span2 重叠 0.95。
citation precision = 支持 claim 数/总 claim = 2/2=1.0;若 c3 无 span 则 precision 下降,应触发拒答或重写。
-------------本文结束感谢您的阅读-------------