跳转到内容

上下文召回率

上下文召回率(Context Recall)衡量的是检索到了多少相关的文档(或信息片段)。它关注的是不遗漏重要的结果。召回率越高,意味着遗漏的相关文档越少。简而言之,召回率就是确保不遗漏任何重要信息。

由于召回率关注的是不遗漏任何信息,因此计算上下文召回率总是需要一个参考标准来进行比较。基于 LLM 的上下文召回率指标使用 reference(参考答案)作为 reference_contexts(参考上下文)的代理,这使得它更易于使用,因为标注参考上下文可能非常耗时。为了从 reference 中估算上下文召回率,参考答案会被分解成多个声明(claims),然后分析每个声明是否能从检索到的上下文中得到支持。在理想情况下,参考答案中的所有声明都应该能从检索到的上下文中找到依据。

上下文召回率的计算公式如下

\[ \text{上下文召回率} = \frac{\text{参考答案中能被检索上下文支持的声明数量}}{\text{参考答案中的声明总数}} \]

示例

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 的上下文召回率 (旧版 API)

旧版 API

以下示例使用的是旧版的指标 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 的上下文召回率

NonLLMContextRecall 指标使用 retrieved_contextsreference_contexts 计算,其值范围在 0 到 1 之间,值越高表示性能越好。该指标使用非 LLM 的字符串比较方法来判断检索到的上下文是否相关。你可以使用任何非 LLM 的度量方法作为距离度量来判断检索到的上下文是否相关。

上下文召回率的计算公式如下

\[ \text{上下文召回率} = {|\text{检索到的相关上下文数量}| \over |\text{参考上下文总数}|} \]

示例

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 的上下文召回率

基于 ID 的上下文召回率 IDBasedContextRecall 通过比较检索上下文的 ID 与参考上下文的 ID,提供了一种直接高效的召回率衡量方法。当你的文档有唯一的 ID 系统,并且希望在不比较实际内容的情况下评估检索性能时,此指标尤其有用。

该指标使用 retrieved_context_ids 和 reference_context_ids 计算召回率,值范围在 0 到 1 之间。值越高表示性能越好。它同时支持字符串和整型 ID。

基于 ID 的上下文召回率的计算公式如下

\[ \text{基于 ID 的上下文召回率} = \frac{\text{在检索上下文 ID 中找到的参考上下文 ID 数量}}{\text{参考上下文 ID 总数}} \]

示例

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