Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Explodinggradients Ragas AnswerCorrectness Metric

From Leeroopedia
Revision as of 14:53, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Explodinggradients_Ragas_AnswerCorrectness_Metric.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


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 AnswerSimilarity instance 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

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment