Implementation:Explodinggradients Ragas Faithfulness Metric
| Field | Value |
|---|---|
| source | Repo |
| domains | Metrics, Evaluation |
| last_updated | 2026-02-10 |
Overview
Faithfulness measures what fraction of the statements in a generated response can be inferred from the retrieved contexts, using statement decomposition followed by NLI verification.
Description
The Faithfulness class evaluates how faithful a response is to the provided contexts. It first decomposes the response into individual statements using the StatementGeneratorPrompt, then judges each statement against the retrieved contexts using an NLI prompt. The final score is the ratio of faithful statements to total statements. It inherits from MetricWithLLM and SingleTurnMetric.
The FaithfulnesswithHHEM subclass replaces the LLM-based NLI step with a Hugging Face model (vectara/hallucination_evaluation_model) for local inference with batching support.
Key attributes:
- nli_statements_prompt -- Prompt used for NLI-based faithfulness judgment.
- statement_generator_prompt -- Prompt used for decomposing the response into statements.
- max_retries -- Maximum LLM retries (default
1).
Usage
The metric requires user_input, response, and retrieved_contexts columns. An LLM must be configured.
Code Reference
| Property | Value |
|---|---|
| Source Location | src/ragas/metrics/_faithfulness.py L133-273
|
| Class Signature | class Faithfulness(MetricWithLLM, SingleTurnMetric)
|
| Import | from ragas.metrics import Faithfulness
|
I/O Contract
Inputs
| Parameter | Type | Required | Description |
|---|---|---|---|
| user_input | str | Yes | The user query |
| response | str | Yes | The generated response to evaluate |
| retrieved_contexts | List[str] | Yes | The context passages used to generate the response |
Outputs
| Output | Type | Description |
|---|---|---|
| score | float | Ratio of faithful statements to total statements (0.0 to 1.0), or NaN if no statements generated |
Usage Examples
from ragas.metrics import Faithfulness
from ragas.dataset_schema import SingleTurnSample
metric = Faithfulness()
# metric.llm = ... # Set your LLM
sample = SingleTurnSample(
user_input="Who is Albert Einstein?",
response="Albert Einstein was a German-born theoretical physicist who developed the theory of relativity.",
retrieved_contexts=[
"Albert Einstein (14 March 1879 - 18 April 1955) was a German-born theoretical physicist, widely held to be one of the greatest scientists of all time."
]
)
# score = await metric.single_turn_ascore(sample)
A pre-configured instance is available:
from ragas.metrics._faithfulness import faithfulness
Related Pages
- Explodinggradients_Ragas_FactualCorrectness_Metric -- Uses the same NLI prompt for claim verification
- Explodinggradients_Ragas_AnswerCorrectness_Metric -- Uses the same StatementGeneratorPrompt
- Explodinggradients_Ragas_NoiseSensitivity_Metric -- Reuses faithfulness prompts for noise analysis
- Explodinggradients_Ragas_MultiModalFaithfulness_Metric -- Multi-modal variant of faithfulness