Implementation:Explodinggradients Ragas Collections ExampleMetric Class
| 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:
- Inherit from
BaseMetric. - Define an
__init__method that callssuper().__init__(name=...). - Override
async def ascore(**kwargs) -> MetricResultwith 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
- Explodinggradients_Ragas_Collections_BaseMetric_Class -- Base class that ExampleMetric extends
- Explodinggradients_Ragas_Collections_BleuScore_Metric -- Real non-LLM metric following the same pattern
- Explodinggradients_Ragas_Collections_AnswerRelevancy_Metric -- Real LLM-based metric following the same pattern