跳到内容

上下文召回率

上下文召回率衡量成功检索到的相关文档(或信息片段)的数量。它侧重于不遗漏重要结果。更高的召回率意味着遗漏的相关文档更少。简而言之,召回率就是不遗漏任何重要的东西。由于召回率是关于不遗漏任何东西的,因此计算上下文召回率总是需要一个参考来比较。

基于 LLM 的上下文召回率

LLMContextRecall 使用 user_inputreferenceretrieved_contexts 计算,值介于 0 和 1 之间,值越高表示性能越好。此指标使用 reference 作为 reference_contexts 的代理,这使得使用更方便,因为标注参考上下文可能非常耗时。为了从 reference 中估计上下文召回率,将参考分解为若干个主张(claim),分析 reference 答案中的每个主张,以确定其是否可以归因于检索到的上下文。在理想情况下,参考答案中的所有主张都应该能够归因于检索到的上下文。

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

\[ \text{上下文召回率} = \frac{\text{检索到的上下文支持的参考中的主张数量}}{\text{参考中的主张总数}} \]

示例

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