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 Collections ResponseGroundedness Metric

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


Field Value
source Repo
domains Metrics, Evaluation
last_updated 2026-02-10 00:00 GMT

Overview

ResponseGroundedness is a v2 class-based metric that evaluates how well a response is grounded in the retrieved contexts using a dual-judge LLM evaluation system.

Description

ResponseGroundedness extends BaseMetric and requires a modern InstructorBaseRagasLLM. The metric implements NVIDIA's dual-judge approach, architecturally identical to ContextRelevance but focused on evaluating groundedness rather than relevance:

  1. Judge 1 evaluates groundedness using ResponseGroundednessJudge1Prompt with structured instructions.
  2. Judge 2 provides an alternative perspective using ResponseGroundednessJudge2Prompt.
  3. The final score is the average of both judges, converted from a 0/1/2 integer rating scale to a 0.0--1.0 float scale.

Rating interpretation: 0 = not grounded (response contains claims not supported by contexts), 1 = partially grounded, 2 = fully grounded (all claims in the response are supported by the contexts).

Each judge has built-in retry logic (configurable via max_retries, default 5) for handling invalid ratings or LLM failures. If a judge fails after all retries, it returns NaN, and the averaging logic gracefully falls back to the other judge's score. If both fail, the result is NaN.

Edge cases: empty response or empty contexts return 0.0. Retrieved contexts are joined with newline separators before evaluation.

Usage

Instantiate with a required llm parameter and optional max_retries. Call ascore(response, retrieved_contexts). Note that unlike some other metrics, this one does not require user_input -- it evaluates the response directly against the contexts.

Code Reference

Property Value
Source Location src/ragas/metrics/collections/response_groundedness/metric.py L1--174
Signature class ResponseGroundedness(BaseMetric)
Import from ragas.metrics.collections import ResponseGroundedness

I/O Contract

Inputs

Parameter Type Required Description
response str Yes The response to evaluate for groundedness
retrieved_contexts List[str] Yes The contexts to check groundedness against

Constructor Parameters

Parameter Type Default Description
llm InstructorBaseRagasLLM (required) Modern instructor-based LLM for dual-judge evaluation
name str "response_groundedness" Metric name
max_retries int 5 Maximum retry attempts per judge for invalid ratings

Outputs

Field Type Description
MetricResult.value float Groundedness score in range 0.0--1.0 (higher is better)

Internal Methods

Method Description
_get_judge_rating Gets a rating (0, 1, or 2) from a single judge with retry logic
_average_scores Averages two judge scores, handling NaN gracefully

Usage Examples

from openai import AsyncOpenAI
from ragas.llms.base import llm_factory
from ragas.metrics.collections import ResponseGroundedness

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

# Create metric
metric = ResponseGroundedness(llm=llm)

# Single evaluation
result = await metric.ascore(
    response="Einstein was born in Germany in 1879.",
    retrieved_contexts=[
        "Albert Einstein was born in Ulm, Germany on March 14, 1879.",
        "He developed the theory of general relativity.",
    ]
)
print(f"Response Groundedness: {result.value}")

# Evaluate an ungrounded response
result = await metric.ascore(
    response="Einstein was born in France in 1900.",
    retrieved_contexts=[
        "Albert Einstein was born in Ulm, Germany on March 14, 1879.",
    ]
)
print(f"Groundedness (should be low): {result.value}")

# With custom retry count
robust_metric = ResponseGroundedness(llm=llm, max_retries=10)
result = await robust_metric.ascore(
    response="The Earth orbits the Sun.",
    retrieved_contexts=["The Earth revolves around the Sun in an elliptical orbit."]
)

# Batch evaluation
results = await metric.abatch_score([
    {
        "response": "Water boils at 100C.",
        "retrieved_contexts": ["Water boils at 100 degrees Celsius at sea level."],
    },
    {
        "response": "Water freezes at 10C.",
        "retrieved_contexts": ["Water freezes at 0 degrees Celsius."],
    },
])
for r in results:
    print(f"Score: {r.value}")

Related Pages

Page Connections

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