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

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


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

Overview

ExampleMetric is a template v2 metric class that demonstrates how to create new metrics by extending BaseMetric with minimal boilerplate.

Description

ExampleMetric extends BaseMetric to serve as a reference implementation and developer guide for creating new v2 collection metrics. It illustrates the minimal pattern required:

  1. Inherit from BaseMetric.
  2. Define an __init__ method that calls super().__init__(name=...).
  3. Override async def ascore(**kwargs) -> MetricResult with custom scoring logic.

The example scoring logic is intentionally trivial: it returns the length of the response divided by 100, capped at 1.0. This placeholder demonstrates the return type and value range expectations without requiring any external dependencies, LLM, or embeddings.

By inheriting from BaseMetric, ExampleMetric automatically gains batch processing (abatch_score), sync wrappers (score, batch_score), numeric validation, and type safety.

Usage

This class is primarily intended as a template for developers. Instantiate directly for testing, or copy and modify for creating new production metrics.

Code Reference

Property Value
Source Location src/ragas/metrics/collections/example_metric.py L1--47
Signature class ExampleMetric(BaseMetric)
Import from ragas.metrics.collections.example_metric import ExampleMetric

I/O Contract

Inputs

Parameter Type Required Description
user_input str Yes The original question (unused in example logic)
response str Yes The response text to evaluate

Constructor Parameters

Parameter Type Default Description
name str "example_metric" Metric name

Outputs

Field Type Description
MetricResult.value float Score in range 0.0--1.0 (response length / 100, capped at 1.0)

Usage Examples

from ragas.metrics.collections.example_metric import ExampleMetric

# Basic usage
metric = ExampleMetric()
result = await metric.ascore(
    user_input="What is Python?",
    response="Python is a programming language."
)
print(f"Example Score: {result.value}")  # len("Python is...") / 100

# Sync usage
result = metric.score(
    user_input="Hello",
    response="A" * 150  # Longer than 100 chars
)
print(f"Score (capped): {result.value}")  # 1.0

# Batch evaluation
results = await metric.abatch_score([
    {"user_input": "Q1", "response": "Short"},
    {"user_input": "Q2", "response": "A much longer response text here"},
])
for r in results:
    print(r.value)

# Template for creating your own metric:
from ragas.metrics.collections.base import BaseMetric
from ragas.metrics.result import MetricResult

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

    async def ascore(self, user_input: str, response: str) -> MetricResult:
        # Your custom scoring logic here
        score = 0.5  # Replace with actual computation
        return MetricResult(value=score)

Related Pages

Page Connections

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