跳转到内容

噪声敏感度

NoiseSensitivity(噪声敏感度)衡量系统在利用相关或不相关检索文档时,提供不正确响应从而犯错的频率。分数范围从0到1,值越低表示性能越好。噪声敏感度使用 user_input(用户输入)、reference(参考答案)、response(响应)和 retrieved_contexts(检索到的上下文)来计算。

为了评估噪声敏感度,需要检查生成响应中的每个声明,以确定它是否基于真实答案是正确的,以及它是否可以归因于相关(或不相关)的检索上下文。理想情况下,答案中的所有声明都应由相关的检索上下文支持。

\[ \text{噪声敏感度 (相关)} = {|\text{响应中不正确声明的总数}| \over |\text{响应中声明的总数}|} \]

示例

from openai import AsyncOpenAI
from ragas.llms import llm_factory
from ragas.metrics.collections import NoiseSensitivity

# Setup LLM
client = AsyncOpenAI()
llm = llm_factory("gpt-4o-mini", client=client)

# Create metric
scorer = NoiseSensitivity(llm=llm)

# Evaluate
result = await scorer.ascore(
    user_input="What is the Life Insurance Corporation of India (LIC) known for?",
    response="The Life Insurance Corporation of India (LIC) is the largest insurance company in India, known for its vast portfolio of investments. LIC contributes to the financial stability of the country.",
    reference="The Life Insurance Corporation of India (LIC) is the largest insurance company in India, established in 1956 through the nationalization of the insurance industry. It is known for managing a large portfolio of investments.",
    retrieved_contexts=[
        "The Life Insurance Corporation of India (LIC) was established in 1956 following the nationalization of the insurance industry in India.",
        "LIC is the largest insurance company in India, with a vast network of policyholders and huge investments.",
        "As the largest institutional investor in India, LIC manages substantial funds, contributing to the financial stability of the country.",
        "The Indian economy is one of the fastest-growing major economies in the world, thanks to sectors like finance, technology, manufacturing etc."
    ]
)
print(f"Noise Sensitivity Score: {result.value}")

输出

Noise Sensitivity Score: 0.3333333333333333

要计算不相关上下文的噪声敏感度,您可以将 mode 参数设置为 irrelevant

scorer = NoiseSensitivity(llm=llm, mode="irrelevant")
result = await scorer.ascore(
    user_input="What is the Life Insurance Corporation of India (LIC) known for?",
    response="The Life Insurance Corporation of India (LIC) is the largest insurance company in India, known for its vast portfolio of investments. LIC contributes to the financial stability of the country.",
    reference="The Life Insurance Corporation of India (LIC) is the largest insurance company in India, established in 1956 through the nationalization of the insurance industry. It is known for managing a large portfolio of investments.",
    retrieved_contexts=[
        "The Life Insurance Corporation of India (LIC) was established in 1956 following the nationalization of the insurance industry in India.",
        "LIC is the largest insurance company in India, with a vast network of policyholders and huge investments.",
        "As the largest institutional investor in India, LIC manages substantial funds, contributing to the financial stability of the country.",
        "The Indian economy is one of the fastest-growing major economies in the world, thanks to sectors like finance, technology, manufacturing etc."
    ]
)
print(f"Noise Sensitivity (Irrelevant) Score: {result.value}")

输出

Noise Sensitivity (Irrelevant) Score: 0.0

同步用法

如果你偏好同步代码,可以使用 .score() 方法来代替 .ascore()

result = scorer.score(
    user_input="What is the Life Insurance Corporation of India (LIC) known for?",
    response="The Life Insurance Corporation of India (LIC) is the largest insurance company in India...",
    reference="The Life Insurance Corporation of India (LIC) is the largest insurance company...",
    retrieved_contexts=[...]
)

计算方法

示例

问题:印度人寿保险公司(LIC)以什么闻名?

真实答案:印度人寿保险公司(LIC)是印度最大的保险公司,成立于1956年,通过保险业的国有化而建立。它以管理庞大的投资组合而闻名。

相关检索: - 印度人寿保险公司(LIC)成立于1956年,是在印度保险业国有化之后成立的。 - LIC是印度最大的保险公司,拥有庞大的投保人网络,在金融领域扮演着重要角色。 - 作为印度最大的机构投资者,LIC管理着庞大的寿险基金,为国家的金融稳定做出了贡献。

不相关检索: - 印度经济是世界上增长最快的主要经济体之一,这得益于金融、技术、制造业等行业。

让我们来看看如何计算相关上下文中的噪声敏感度

  • 第1步: 识别可以推断出真实答案的相关上下文。

    • 真实答案:印度人寿保险公司(LIC)是印度最大的保险公司,成立于1956年,通过保险业的国有化而建立。它以管理庞大的投资组合而闻名。

    • 上下文

      • 上下文1:印度人寿保险公司(LIC)成立于1956年,是在印度保险业国有化之后成立的。
      • 上下文2:LIC是印度最大的保险公司,拥有庞大的投保人网络,在金融领域扮演着重要角色。
      • 上下文3:作为印度最大的机构投资者,LIC管理着庞大的基金,为国家的金融稳定做出了贡献。
  • 第2步: 验证生成答案中的声明是否可以从相关上下文中推断出来。

    • 答案:印度人寿保险公司(LIC)是印度最大的保险公司,以其庞大的投资组合而闻名。LIC为国家的金融稳定做出了贡献。

    • 上下文

      • 上下文1:印度人寿保险公司(LIC)成立于1956年,是在印度保险业国有化之后成立的。
      • 上下文2:LIC是印度最大的保险公司,拥有庞大的投保人网络,在金融领域扮演着重要角色。
      • 上下文3:作为印度最大的机构投资者,LIC管理着庞大的基金,为国家的金融稳定做出了贡献。
  • 第3步: 识别答案中的任何不正确声明(即,未被真实答案支持的答案陈述)。

    • 真实答案:印度人寿保险公司(LIC)是印度最大的保险公司,成立于1956年,通过保险业的国有化而建立。它以管理庞大的投资组合而闻名。

    • 答案:印度人寿保险公司(LIC)是印度最大的保险公司,以其庞大的投资组合而闻名。LIC为国家的金融稳定做出了贡献。

    解释:真实答案没有提及任何关于LIC为国家金融稳定做出贡献的信息。因此,答案中的这个陈述是不正确的。

    不正确的陈述:1 总声明数:3

  • 第4步: 使用公式计算噪声敏感度

    \[ \text{噪声敏感度} = { \text{1} \over \text{3} } = 0.333 \]

这得出的噪声敏感度分数为0.333,表明答案中三个声明中有一个是不正确的。

旧版指标 API

以下示例使用旧版指标 API 模式。对于新项目,我们建议使用上面显示的基于集合的 API。

弃用时间表

此 API 将在 0.4 版本中被弃用,并在 1.0 版本中被移除。请迁移到上面显示的基于集合的 API。

使用 SingleTurnSample 的示例

from ragas.dataset_schema import SingleTurnSample
from ragas.metrics import NoiseSensitivity

sample = SingleTurnSample(
    user_input="What is the Life Insurance Corporation of India (LIC) known for?",
    response="The Life Insurance Corporation of India (LIC) is the largest insurance company in India, known for its vast portfolio of investments. LIC contributes to the financial stability of the country.",
    reference="The Life Insurance Corporation of India (LIC) is the largest insurance company in India, established in 1956 through the nationalization of the insurance industry. It is known for managing a large portfolio of investments.",
    retrieved_contexts=[
        "The Life Insurance Corporation of India (LIC) was established in 1956 following the nationalization of the insurance industry in India.",
        "LIC is the largest insurance company in India, with a vast network of policyholders and huge investments.",
        "As the largest institutional investor in India, LIC manages substantial funds, contributing to the financial stability of the country.",
        "The Indian economy is one of the fastest-growing major economies in the world, thanks to sectors like finance, technology, manufacturing etc."
    ]
)

scorer = NoiseSensitivity(llm=evaluator_llm)
await scorer.single_turn_ascore(sample)

输出

0.3333333333333333

要计算不相关上下文的噪声敏感度,您可以将 mode 参数设置为 irrelevant

scorer = NoiseSensitivity(mode="irrelevant")
await scorer.single_turn_ascore(sample)

致谢:噪声敏感度是在 RAGChecker 中引入的。