Skip to content

Commit 8da231d

Browse files
Implementation of Noise sensitivity metrics from RAGChecker (#1190)
Solves: - #1185 - Took inspiration from RAGChecker from AWS Noise sensitivity [noise sensitivity](https://github.com/amazon-science/RAGChecker/tree/main/ragchecker) metrics. - Have tested it locally, it is working giving the results. ### Input ```python from datasets import Dataset from ragas.metrics import noise_sensitivity_relevant, noise_sensitivity_irrelevant from ragas import evaluate data_sample = { "question": ["What is the Life Insurance Corporation of India (LIC) known for?"], "ground_truth": ["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."], "answer": ["The Life Insurance Corporation of India (LIC) is the largest insurance company in India, known for its vast portfolio of investments. LIC contributs to the financial stability of the country."], "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 a huge investments.", "As the largest institutional investor in India, LIC manages a 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 the secors like finance, technology, manufacturing etc"]] } dataset = Dataset.from_dict(data_sample) metrics = [noise_sensitivity_relevant, noise_sensitivity_irrelevant] score = evaluate(dataset,metrics=metrics) score.to_pandas() ``` --------- Co-authored-by: Shahules786 <[email protected]>
1 parent d5b60bb commit 8da231d

File tree

6 files changed

+379
-3
lines changed

6 files changed

+379
-3
lines changed

docs/concepts/metrics/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Just like in any machine learning system, the performance of individual componen
1515
- [Context precision](context_precision.md)
1616
- [Context utilization](context_utilization.md)
1717
- [Context entity recall](context_entities_recall.md)
18+
- [Noise Sensitivity](noise_sensitivity.md)
1819
- [Summarization Score](summarization_score.md)
1920

2021
```{toctree}
@@ -36,6 +37,7 @@ context_precision
3637
context_utilization
3738
context_recall
3839
context_entities_recall
40+
noise_sensitivity
3941
semantic_similarity
4042
answer_correctness
4143
critique
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
2+
3+
# Noise Sensitivity
4+
5+
Noise sensitivity measures how often a system makes errors by providing incorrect responses when utilizing either relevant or irrelevant retrieved documents. The score ranges from 0 to 1, with lower values indicating better performance. Noise sensitivity is computed using the question, ground truth, answer, and the retrieved context.
6+
7+
To estimate noise sensitivity, each claim in the generated answer is examined to determine whether it is correct based on the ground truth and whether it can be attributed to the relevant (or irrelevant) retrieved context. Ideally, all claims in the answer should be supported by the relevant retrieved context.
8+
9+
10+
```{math}
11+
\text{noise sensitivity (relevant)} = {|\text{Number of incorrect claims in answer}| \over |\text{Number of claims in the Answer}|}
12+
```
13+
14+
```{Hint}
15+
16+
Question: What is the Life Insurance Corporation of India (LIC) known for?
17+
18+
Ground truth: 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.
19+
20+
Relevant Retrieval:
21+
- The Life Insurance Corporation of India (LIC) was established in 1956 following the nationalization of the insurance industry in India.
22+
- LIC is the largest insurance company in India, with a vast network of policyholders and a significant role in the financial sector.
23+
- As the largest institutional investor in India, LIC manages a substantial life fund, contributing to the financial stability of the country.
24+
25+
Irrelevant Retrieval:
26+
- The Indian economy is one of the fastest-growing major economies in the world, thanks to the secors like finance, technology, manufacturing etc.
27+
```
28+
29+
30+
## Example
31+
32+
```{code-block} python
33+
:caption: Noise Sensitivity
34+
from datasets import Dataset
35+
from ragas.metrics import noise_sensitivity_relevant, noise_sensitivity_irrelevant
36+
from ragas import evaluate
37+
38+
data_sample = {
39+
"question": ["What is the Life Insurance Corporation of India (LIC) known for?"],
40+
"ground_truth": ["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."],
41+
"answer": ["The Life Insurance Corporation of India (LIC) is the largest insurance company in India, known for its vast portfolio of investments. LIC contributs to the financial stability of the country."],
42+
"contexts": [[
43+
"The Life Insurance Corporation of India (LIC) was established in 1956 following the nationalization of the insurance industry in India.",
44+
"LIC is the largest insurance company in India, with a vast network of policyholders and a huge investments.",
45+
"As the largest institutional investor in India, LIC manages a substantial funds, contributing to the financial stability of the country.",
46+
"The Indian economy is one of the fastest-growing major economies in the world, thanks to the secors like finance, technology, manufacturing etc"
47+
]]
48+
}
49+
50+
dataset = Dataset.from_dict(data_sample)
51+
metrics = [noise_sensitivity_relevant, noise_sensitivity_irrelevant]
52+
score = evaluate(dataset,metrics=metrics)
53+
score.to_pandas()
54+
```
55+
56+
## Calculation
57+
58+
Let's examine how noise sensitivity in relevant context was calculated:
59+
60+
- **Step 1:** Identify the relevant contexts from which the ground truth can be inferred.
61+
62+
- Ground Truth:
63+
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.
64+
65+
- Contexts:
66+
- Context 1: `The Life Insurance Corporation of India (LIC) was established in 1956` following the nationalization of the insurance industry in India.
67+
- Context 2: `LIC is the largest insurance company in India`, with a vast network of policyholders and a significant role in the financial sector.
68+
- Context 3: `As the largest institutional investor in India, LIC manages a substantial funds`, contributing to the financial stability of the country.
69+
70+
- **Step 2:** Verify if the claims in the generated answer can be inferred from the relevant context.
71+
72+
- Answer:
73+
The Life Insurance Corporation of India (LIC) is the largest insurance company in India, known for its vast portfolio of investments. LIC contributs to the financial stability of the country.
74+
75+
- Contexts:
76+
- Context 1: The Life Insurance Corporation of India (LIC) was established in 1956 following the nationalization of the insurance industry in India.
77+
- Context 2: `LIC is the largest insurance company in India`, with a vast network of policyholders and a significant role in the financial sector.
78+
- Context 3: `As the largest institutional investor in India, LIC manages a substantial funds`, `contributing to the financial stability of the country`.
79+
80+
81+
- **Step 3:** Identify any incorrect claims in the answer (i.e., answer statements that are not supported by the ground truth).
82+
83+
- Ground Truth:
84+
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.
85+
86+
- Answer:
87+
The Life Insurance Corporation of India (LIC) is the largest insurance company in India, known for its vast portfolio of investments. `LIC contributs to the financial stability of the country`.
88+
89+
Explanation: The ground truth does not mention anything about LIC contributing to the financial stability of the country. Therefore, this statement in the answer is incorrect.
90+
91+
Incorrect Statement: 1
92+
Total claims: 3
93+
94+
- **Step 4:** Calculate noise sensitivity using the formula:
95+
```{math}
96+
\text{noise sensitivity} = { \text{1} \over \text{3} } = 0.333
97+
```
98+
This results in a noise sensitivity score of 0.333, indicating that one out of three claims in the answer was incorrect.
99+
100+
101+
Credits: Noise senstivity was introduced in [RAGChecker](https://github.com/amazon-science/RAGChecker/tree/main/ragchecker)

src/ragas/async_utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Async utils."""
2+
23
import asyncio
34
from typing import Any, Coroutine, List
45

src/ragas/integrations/langchain.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ def __init__(self, metric: Metric, **kwargs: t.Any):
4848
t.cast(MetricWithLLM, self.metric).llm = LangchainLLMWrapper(llm)
4949
if isinstance(self.metric, MetricWithEmbeddings):
5050
embeddings = get_or_init(kwargs, "embeddings", OpenAIEmbeddings)
51-
t.cast(
52-
MetricWithEmbeddings, self.metric
53-
).embeddings = LangchainEmbeddingsWrapper(embeddings)
51+
t.cast(MetricWithEmbeddings, self.metric).embeddings = (
52+
LangchainEmbeddingsWrapper(embeddings)
53+
)
5454
self.metric.init(run_config)
5555

5656
@property

src/ragas/metrics/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
)
1414
from ragas.metrics._context_recall import ContextRecall, context_recall
1515
from ragas.metrics._faithfulness import Faithfulness, FaithulnesswithHHEM, faithfulness
16+
from ragas.metrics._noise_sensitivity import (
17+
NoiseSensitivity,
18+
noise_sensitivity_irrelevant,
19+
noise_sensitivity_relevant,
20+
)
1621
from ragas.metrics._rubrics_based import (
1722
LabelledRubricsScore,
1823
ReferenceFreeRubricsScore,
@@ -43,6 +48,9 @@
4348
"context_entity_recall",
4449
"SummarizationScore",
4550
"summarization_score",
51+
"NoiseSensitivity",
52+
"noise_sensitivity_irrelevant",
53+
"noise_sensitivity_relevant",
4654
"labelled_rubrics_score",
4755
"reference_free_rubrics_score",
4856
"ReferenceFreeRubricsScore",

0 commit comments

Comments
 (0)