Ragas全面解析:五大核心评估指标优缺点对比与实战指南

Ragas全面解析:五大核心评估指标优缺点对比与实战指南

【免费下载链接】ragas Evaluation framework for your Retrieval Augmented Generation (RAG) pipelines 【免费下载链接】ragas 项目地址: https://gitcode.com/gh_mirrors/ra/ragas

你是否还在为RAG(检索增强生成)系统的评估而烦恼?不知道如何选择合适的指标来衡量系统性能?本文将深入解析Ragas框架中五大核心评估指标,帮助你全面了解不同评测维度的优缺点,轻松掌握RAG系统评估的关键要点。读完本文,你将能够:清晰区分各指标的适用场景、理解指标计算原理、掌握指标优化方法,并能根据实际需求选择最适合的评估指标组合。

指标概述

Ragas作为专门针对RAG系统的评估框架,提供了丰富的评估指标。这些指标可以分为LLM-based(基于大型语言模型)和Non-LLM-based(非基于大型语言模型)两类,分别从不同角度对RAG系统进行评估。

指标思维导图

指标设计遵循五大原则:单一维度聚焦、直观可解释、高效提示流程、鲁棒性和一致评分范围。这些原则确保了指标的可靠性和实用性,为RAG系统的评估提供了有力支持。详细的指标设计理念可参考指标概述

核心评估指标详解

Context Precision(上下文精确性)

Context Precision评估检索器在给定查询时,将相关片段排在无关片段之前的能力。它通过计算Precision@k的平均值来衡量,其中k是检索到的上下文片段总数。

优点:能够有效评估检索结果的排序质量,高精确性意味着相关信息被优先检索。 缺点:对无关片段的位置敏感,当无关片段出现在前面时,分数会显著下降;且无法评估是否遗漏了重要信息。

from ragas import SingleTurnSample
from ragas.metrics import LLMContextPrecisionWithoutReference

context_precision = LLMContextPrecisionWithoutReference(llm=evaluator_llm)

sample = SingleTurnSample(
    user_input="Where is the Eiffel Tower located?",
    response="The Eiffel Tower is located in Paris.",
    retrieved_contexts=["The Eiffel Tower is located in Paris.", "The Brandenburg Gate is located in Berlin."],
)

await context_precision.single_turn_ascore(sample)

Context Recall(上下文召回率)

Context Recall衡量成功检索到的相关文档或信息片段的比例,关注是否遗漏重要结果。计算召回率需要参考基准进行比较。

优点:确保重要信息不被遗漏,对于需要全面覆盖相关内容的场景非常重要。 缺点:高召回率可能导致检索到过多无关信息,需要与精确性指标结合使用。

组件级评估

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)

Faithfulness(忠实度)

Faithfulness衡量responseretrieved context的事实一致性,范围从0到1,分数越高表示一致性越好。如果响应中的所有主张都能得到检索上下文的支持,则认为响应是忠实的。

优点:直接评估生成内容与检索上下文的一致性,有效检测幻觉现象。 缺点:计算过程需要分解响应为单个陈述并逐一验证,可能较为耗时。

from ragas.dataset_schema import SingleTurnSample
from ragas.metrics import Faithfulness

sample = SingleTurnSample(
        user_input="When was the first super bowl?",
        response="The first superbowl was held on Jan 15, 1967",
        retrieved_contexts=[
            "The First AFL–NFL World Championship Game was an American football game played on January 15, 1967, at the Los Angeles Memorial Coliseum in Los Angeles."
        ]
    )
scorer = Faithfulness(llm=evaluator_llm)
await scorer.single_turn_ascore(sample)

Response Relevancy(响应相关性)

Response Relevancy衡量响应与用户输入的相关程度,分数越高表示与用户输入的对齐越好。如果响应不完整或包含冗余信息,分数会较低。

优点:直接反映响应与用户需求的匹配程度,有助于提升用户满意度。 缺点:仅关注相关性,不评估事实准确性,需要与其他指标配合使用。

from ragas import SingleTurnSample 
from ragas.metrics import ResponseRelevancy

sample = SingleTurnSample(
        user_input="When was the first super bowl?",
        response="The first superbowl was held on Jan 15, 1967",
        retrieved_contexts=[
            "The First AFL–NFL World Championship Game was an American football game played on January 15, 1967, at the Los Angeles Memorial Coliseum in Los Angeles."
        ]
    )

scorer = ResponseRelevancy(llm=evaluator_llm, embeddings=evaluator_embeddings)
await scorer.single_turn_ascore(sample)

Context Entities Recall(上下文实体召回率)

Context Entities Recall基于referenceretrieved_contexts中同时存在的实体数量与reference中实体总数的比例,衡量检索上下文的实体召回情况。

优点:专注于实体级别的召回评估,对于知识图谱等以实体为中心的应用场景非常重要。 缺点:仅关注实体层面,可能忽略其他重要信息。

from ragas import SingleTurnSample
from ragas.metrics import ContextEntityRecall

sample = SingleTurnSample(
    reference="The Taj Mahal is an ivory-white marble mausoleum on the right bank of the river Yamuna in the Indian city of Agra. It was commissioned in 1631 by the Mughal emperor Shah Jahan to house the tomb of his favorite wife, Mumtaz Mahal.",
    retrieved_contexts=["The Taj Mahal is a symbol of love and architectural marvel located in Agra, India. It was built by the Mughal emperor Shah Jahan in memory of his beloved wife, Mumtaz Mahal. The structure is renowned for its intricate marble work and beautiful gardens surrounding it."],
)

scorer = ContextEntityRecall(llm=evaluator_llm)

await scorer.single_turn_ascore(sample)

指标对比与选择建议

不同的评估指标各有侧重,适用于不同的场景和需求。在实际应用中,需要根据具体情况选择合适的指标组合,以全面评估RAG系统的性能。

指标核心关注点优点缺点适用场景
Context Precision检索结果排序质量评估相关信息的优先性对无关片段位置敏感需要优先展示最相关信息的场景
Context Recall相关信息覆盖程度确保重要信息不遗漏可能检索过多无关信息需要全面覆盖相关内容的场景
Faithfulness响应与上下文一致性检测幻觉现象计算耗时对事实准确性要求高的场景
Response Relevancy响应与用户输入匹配度反映用户需求满足程度不评估事实准确性评估用户体验的场景
Context Entities Recall实体级信息召回适用于实体为中心的应用忽略非实体信息知识图谱等实体相关应用

在选择指标时,建议优先考虑端到端指标,确保指标的可解释性,强调客观指标而非主观指标,并选择少量强信号指标而非大量弱信号指标。通过合理选择和组合评估指标,可以有效指导RAG系统的优化和改进。

希望本文能够帮助你更好地理解和应用Ragas评估指标,提升RAG系统的性能和质量。如果你对Ragas评估指标还有其他疑问或需要进一步的帮助,请参考官方文档社区教程

【免费下载链接】ragas Evaluation framework for your Retrieval Augmented Generation (RAG) pipelines 【免费下载链接】ragas 项目地址: https://gitcode.com/gh_mirrors/ra/ragas

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值