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 StringMetrics Module

From Leeroopedia


Field Value
source Repo
domains Metrics, NLP
last_updated 2026-02-10

Overview

The StringMetrics module provides lightweight, non-LLM string comparison metrics: ExactMatch, StringPresence, and NonLLMStringSimilarity with configurable distance measures.

Description

This module contains three simple string comparison metric classes and a supporting enum:

  • ExactMatch -- Returns 1.0 if reference equals response exactly, 0.0 otherwise.
  • StringPresence -- Returns 1.0 if the reference string is present anywhere within the response, 0.0 otherwise.
  • NonLLMStringSimilarity -- Computes normalized string similarity using configurable distance measures from the rapidfuzz library. Returns 1 - normalized_distance.
  • DistanceMeasure (Enum) -- Supported distance measures: LEVENSHTEIN, HAMMING, JARO, JARO_WINKLER.

All metric classes inherit only from SingleTurnMetric and do not require an LLM or embeddings.

Dependencies: NonLLMStringSimilarity requires the rapidfuzz package (pip install rapidfuzz).

Usage

All metrics require reference and response columns.

Code Reference

Property Value
Source Location src/ragas/metrics/_string.py L11-100
Class Signatures class ExactMatch(SingleTurnMetric), class StringPresence(SingleTurnMetric), class NonLLMStringSimilarity(SingleTurnMetric)
Import from ragas.metrics import ExactMatch, StringPresence, NonLLMStringSimilarity

I/O Contract

Inputs

Parameter Type Required Description
reference str Yes The ground truth reference text
response str Yes The generated response to evaluate

Outputs

Output Type Description
score (ExactMatch) float 1.0 if exact match, 0.0 otherwise
score (StringPresence) float 1.0 if reference is substring of response, 0.0 otherwise
score (NonLLMStringSimilarity) float Normalized similarity score (0.0 to 1.0) based on the selected distance measure

Usage Examples

from ragas.metrics import ExactMatch, StringPresence, NonLLMStringSimilarity
from ragas.metrics._string import DistanceMeasure
from ragas.dataset_schema import SingleTurnSample

# Exact match
exact = ExactMatch()
sample = SingleTurnSample(reference="Paris", response="Paris")
# score = await exact.single_turn_ascore(sample)  # 1.0

# String presence
presence = StringPresence()
sample = SingleTurnSample(reference="Paris", response="The capital is Paris, France.")
# score = await presence.single_turn_ascore(sample)  # 1.0

# String similarity with Levenshtein distance
similarity = NonLLMStringSimilarity(distance_measure=DistanceMeasure.LEVENSHTEIN)
sample = SingleTurnSample(reference="Paris", response="Pris")
# score = await similarity.single_turn_ascore(sample)  # ~0.8

Related Pages

Page Connections

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