Context Recall(上下文召回率)
Context Recall 衡量有多少相关文档(或信息片段)被成功检索。它关注的是不要漏掉重要结果。召回率越高,被遗漏的相关文档就越少。简而言之,recall 就是不要漏掉任何重要内容。
由于关注的是不遗漏,计算 context recall 始终需要一个用于对比的 reference。基于 LLM 的 Context Recall 指标使用 reference 作为 reference_contexts 的代理,这样更容易使用,因为标注参考上下文非常耗时。要从 reference 估计 context recall,会将 reference 拆解为若干 claim,并分析每条 claim 是否可以归因于检索到的上下文。在理想情况下,参考答案中的所有 claim 都应当能归因于检索到的上下文。
Context recall 的计算公式如下:
[ \text{Context Recall} = \frac{\text{Number of claims in the reference supported by the retrieved context}}{\text{Total number of claims in the reference}} ]
示例
from openai import AsyncOpenAI
from ragas.llms import llm_factory
from ragas.metrics.collections import ContextRecall
# Setup LLM
client = AsyncOpenAI()
llm = llm_factory("gpt-4o-mini", client=client)
# Create metric
scorer = ContextRecall(llm=llm)
# Evaluate
result = await scorer.ascore(
user_input="Where is the Eiffel Tower located?",
retrieved_contexts=["Paris is the capital of France."],
reference="The Eiffel Tower is located in Paris."
)
print(f"Context Recall Score: {result.value}")
输出:
Context Recall Score: 1.0
同步用法
如果你更喜欢同步代码,可以使用 .score() 方法代替 .ascore():
result = scorer.score(
user_input="Where is the Eiffel Tower located?",
retrieved_contexts=["Paris is the capital of France."],
reference="The Eiffel Tower is located in Paris."
)
基于 LLM 的 Context Recall(旧版 API)
旧版 API
以下示例使用旧版 metrics API 模式。对于新项目,我们建议使用上文所示的 collections-based API。此 API 将在 0.4 版本中弃用,并在 1.0 版本中移除。
from ragas.dataset_schema import SingleTurnSample
from ragas.metrics import LLMContextRecall
sample = SingleTurnSample(
user_input="Where is the Eiffel Tower located?",
response="The Eiffel Tower is located in Paris.",
reference="The Eiffel Tower is located in Paris.",
retrieved_contexts=["Paris is the capital of France."],
)
context_recall = LLMContextRecall(llm=evaluator_llm)
await context_recall.single_turn_ascore(sample)
输出:
1.0
非 LLM 的 Context Recall
NonLLMContextRecall 指标使用 retrieved_contexts 和 reference_contexts 计算,取值范围为 0 到 1,值越高表示性能越好。此指标使用非 LLM 的字符串比较度量来判断检索到的上下文是否相关。你可以使用任何非 LLM 指标作为距离度量,来判断检索到的上下文是否相关。
Context recall 的计算公式如下:
[ \text{context recall} = {|\text{Number of relevant contexts retrieved}| \over |\text{Total number of reference contexts}|} ]
示例
from ragas.dataset_schema import SingleTurnSample
from ragas.metrics import NonLLMContextRecall
sample = SingleTurnSample(
retrieved_contexts=["Paris is the capital of France."],
reference_contexts=["Paris is the capital of France.", "The Eiffel Tower is one of the most famous landmarks in Paris."]
)
context_recall = NonLLMContextRecall()
await context_recall.single_turn_ascore(sample)
输出
0.5
基于 ID 的 Context Recall
基于 ID 的 Context Recall IDBasedContextRecall 通过比较检索上下文的 ID 与参考上下文 ID,提供一种直接且高效的召回率度量方式。当你的文档有唯一 ID 体系,并希望在不比较实际内容的情况下评估检索性能时,此指标特别有用。
该指标使用 retrieved_context_ids 和 reference_context_ids 计算召回率,取值范围为 0 到 1。值越高表示性能越好。它同时支持字符串和整数 ID。
基于 ID 的 context recall 计算公式如下:
[ \text{ID-Based Context Recall} = \frac{\text{Number of reference context IDs found in retrieved context IDs}}{\text{Total number of reference context IDs}} ]
示例
from ragas.dataset_schema import SingleTurnSample
from ragas.metrics import IDBasedContextRecall
sample = SingleTurnSample(
retrieved_context_ids=["doc_1", "doc_2", "doc_3"],
reference_context_ids=["doc_1", "doc_4", "doc_5", "doc_6"]
)
id_recall = IDBasedContextRecall()
await id_recall.single_turn_ascore(sample)
输出
0.25