RAG 失敗可能來自沒有召回、召回錯誤版本、上下文被截斷,或模型沒有遵守引用格式。分開評測檢索與生成,才能修復正確層次。
核心概念
建立帶有預期文件 ID 的問題集,分別記錄召回命中、引用覆蓋、答案忠實度與拒答是否合理。資料沒有證據時,明確回傳缺少依據才是正確結果。
可執行範例
def citation_coverage(answer: str, expected_sources: set[str]) -> float:
cited = {token.strip('[]()') for token in answer.split() if token.startswith('[') and token.endswith(']')}
return len(cited & expected_sources) / len(expected_sources) if expected_sources else 1.0
assert citation_coverage('see [doc-1]', {'doc-1'}) == 1.0
步驟與適用情境
- 為每個問題定義預期文件 ID 與不可接受的答案範圍。
- 先單獨執行召回,記錄 top-k,再執行生成。
- 分類無命中、錯版本、缺少引用和未授權候選的失敗。
- 把失敗樣本加入下一輪回歸集,不要只靠改提示詞掩蓋檢索缺陷。
適合合規問答、內部手冊與需要證據的研究工具。
官方參考
OpenAI Evals 與 OpenAI Retrieval。
最後核對日期:2026-11-16。引用行為與檢索 API 可能變更,請以連結文件為準。
程式碼範例
def citation_coverage(answer: str, expected_sources: set[str]) -> float:
cited = {token.strip('[]()') for token in answer.split() if token.startswith('[') and token.endswith(']')}
return len(cited & expected_sources) / len(expected_sources) if expected_sources else 1.0
assert citation_coverage('见 [doc-1]', {'doc-1'}) == 1.0
