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 BaseMetric Class

From Leeroopedia


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

Overview

BaseMetric is the foundational base class for all v2 collection metrics, combining SimpleBaseMetric and NumericValidator with modern LLM and embedding component validation.

Description

BaseMetric inherits from both SimpleBaseMetric (providing core metric functionality such as ascore, abatch_score, score, and batch_score) and NumericValidator (providing configurable numeric range validation). During initialization, BaseMetric conditionally validates llm and embeddings attributes only if the subclass defines them, ensuring that:

  • LLM components are instances of InstructorBaseRagasLLM (modern instructor-based LLMs). Legacy wrappers are rejected with descriptive error messages.
  • Embedding components are instances of BaseRagasEmbedding (modern embeddings). Legacy wrappers are similarly rejected.

The class provides synchronous convenience methods (score, batch_score) that wrap the async counterparts using asyncio.run(), with proper detection and error messaging if called from within an already-running event loop.

Usage

BaseMetric is not intended for direct instantiation. Subclasses override ascore(**kwargs) with their specific scoring logic. Subclasses that need LLM or embedding components declare them as class-level type hints and set them in __init__ before calling super().__init__().

Code Reference

Property Value
Source Location src/ragas/metrics/collections/base.py L1--132
Signature class BaseMetric(SimpleBaseMetric, NumericValidator)
Import from ragas.metrics.collections.base import BaseMetric

I/O Contract

Constructor Parameters

Parameter Type Default Description
name str "base_metric" Metric identifier name
allowed_values Tuple[float, float] (0.0, 1.0) Min/max range for numeric validation

Key Methods

Method Signature Description
ascore async def ascore(self, **kwargs) -> MetricResult Async scoring (override in subclass)
score def score(self, **kwargs) -> MetricResult Sync wrapper around ascore
abatch_score Inherited from SimpleBaseMetric Async batch scoring
batch_score def batch_score(self, inputs) -> List[MetricResult] Sync wrapper around abatch_score

Outputs

Field Type Description
MetricResult.value float Numeric score within allowed_values range
MetricResult.reason Optional[str] Optional explanation text

Validation Methods (private)

Method Description
_validate_llm Checks that self.llm is an instance of InstructorBaseRagasLLM
_validate_embeddings Checks that self.embeddings is an instance of BaseRagasEmbedding

Usage Examples

from ragas.metrics.collections.base import BaseMetric
from ragas.metrics.result import MetricResult


class MyCustomMetric(BaseMetric):
    """A simple custom metric example."""

    def __init__(self, name: str = "my_metric", **kwargs):
        super().__init__(name=name, **kwargs)

    async def ascore(self, reference: str, response: str) -> MetricResult:
        score = 1.0 if reference.lower() == response.lower() else 0.0
        return MetricResult(value=score)


# Usage
metric = MyCustomMetric()
result = await metric.ascore(reference="hello", response="Hello")
print(f"Score: {result.value}")  # 1.0

# Sync convenience method
result = metric.score(reference="hello", response="Hello")

# Batch evaluation
results = await metric.abatch_score([
    {"reference": "a", "response": "a"},
    {"reference": "b", "response": "c"},
])

Related Pages

Page Connections

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