Implementation:Explodinggradients Ragas AnswerCorrectness Metric
| Field | Value |
|---|---|
| source | Repo |
| domains | Metrics, Evaluation |
| last_updated | 2026-02-10 |
Overview
AnswerCorrectness measures the correctness of a generated answer compared to a ground truth reference by combining factual statement classification (TP/FP/FN) with optional semantic similarity scoring.
Description
The AnswerCorrectness class evaluates answer quality by decomposing both the response and reference into simplified statements, classifying each statement as True Positive, False Positive, or False Negative, and computing an F-beta score. This factuality score can be optionally combined with an embedding-based semantic similarity score using configurable weights (default 0.75 factuality, 0.25 similarity). The metric inherits from MetricWithLLM, MetricWithEmbeddings, and SingleTurnMetric.
Key attributes:
- weights -- A list of two floats controlling the balance between factuality and semantic similarity (default
[0.75, 0.25]). - beta -- Float controlling precision/recall tradeoff in the F-beta score (default
1.0). - answer_similarity -- An optional
AnswerSimilarityinstance for computing semantic similarity. - correctness_prompt -- The LLM prompt used for TP/FP/FN classification.
- statement_generator_prompt -- The LLM prompt used for decomposing text into statements.
Usage
The metric requires user_input, response, and reference columns in the input sample. An LLM must be configured, and embeddings must be set if the semantic similarity weight is non-zero.
Code Reference
| Property | Value |
|---|---|
| Source Location | src/ragas/metrics/_answer_correctness.py L140-272
|
| Class Signature | class AnswerCorrectness(MetricWithLLM, MetricWithEmbeddings, SingleTurnMetric)
|
| Import | from ragas.metrics import AnswerCorrectness
|
I/O Contract
Inputs
| Parameter | Type | Required | Description |
|---|---|---|---|
| user_input | str | Yes | The question or user query |
| response | str | Yes | The generated answer to evaluate |
| reference | str | Yes | The ground truth answer |
Outputs
| Output | Type | Description |
|---|---|---|
| score | float | Weighted combination of factuality F-beta score and semantic similarity (0.0 to 1.0) |
Usage Examples
from ragas.metrics import AnswerCorrectness
from ragas.dataset_schema import SingleTurnSample
metric = AnswerCorrectness(weights=[0.75, 0.25], beta=1.0)
# metric.llm = ... # Set your LLM
# metric.embeddings = ... # Set your embeddings
sample = SingleTurnSample(
user_input="What powers the sun?",
response="The sun is powered by nuclear fusion.",
reference="The sun is powered by nuclear fusion, where hydrogen atoms fuse to form helium."
)
# score = await metric.single_turn_ascore(sample)
A pre-configured instance is available:
from ragas.metrics._answer_correctness import answer_correctness
Related Pages
- Explodinggradients_Ragas_SemanticSimilarity_Metric -- Used internally for the semantic similarity component
- Explodinggradients_Ragas_Faithfulness_Metric -- Shares the StatementGeneratorPrompt for statement decomposition
- Explodinggradients_Ragas_FactualCorrectness_Metric -- Alternative factual evaluation metric using claim decomposition and NLI