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

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


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

Overview

The StringMetrics module provides three v2 class-based string comparison metrics -- ExactMatch, StringPresence, and NonLLMStringSimilarity -- that evaluate text without requiring LLM or embedding components.

Description

This module contains lightweight, deterministic string comparison metrics extending BaseMetric:

  • ExactMatch -- Returns 1.0 if reference and response are identical strings, 0.0 otherwise. Uses simple Python equality comparison.
  • StringPresence -- Returns 1.0 if the reference string is found within the response text, 0.0 otherwise. Uses Python's in operator.
  • NonLLMStringSimilarity -- Computes normalized string distance similarity using the rapidfuzz library. Supports four distance measures defined by the DistanceMeasure enum: LEVENSHTEIN, HAMMING, JARO, and JARO_WINKLER. The score is computed as 1 - normalized_distance.

All three metrics are pure async, require no external AI services, and produce scores in the 0.0--1.0 range.

Usage

Instantiate any of the three classes directly. ExactMatch and StringPresence have no extra parameters. NonLLMStringSimilarity accepts an optional distance_measure parameter (defaults to Levenshtein) and requires the rapidfuzz package.

Code Reference

Property Value
Source Location src/ragas/metrics/collections/_string.py L1--215
Signatures class ExactMatch(BaseMetric), class StringPresence(BaseMetric), class NonLLMStringSimilarity(BaseMetric)
Import from ragas.metrics.collections import ExactMatch, StringPresence, NonLLMStringSimilarity
Enum from ragas.metrics.collections._string import DistanceMeasure

I/O Contract

Inputs (all three classes)

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

Constructor Parameters (NonLLMStringSimilarity)

Parameter Type Default Description
name str "non_llm_string_similarity" Metric name
distance_measure DistanceMeasure DistanceMeasure.LEVENSHTEIN Distance algorithm to use

DistanceMeasure Enum Values

Value Algorithm
LEVENSHTEIN Levenshtein edit distance
HAMMING Hamming distance (equal-length strings)
JARO Jaro similarity
JARO_WINKLER Jaro-Winkler similarity

Outputs

Field Type Description
MetricResult.value float Score in range 0.0--1.0

Usage Examples

from ragas.metrics.collections import ExactMatch, StringPresence, NonLLMStringSimilarity
from ragas.metrics.collections._string import DistanceMeasure

# ExactMatch
exact = ExactMatch()
result = await exact.ascore(reference="Hello World", response="Hello World")
print(f"Exact Match: {result.value}")  # 1.0

result = await exact.ascore(reference="Hello", response="hello")
print(f"Exact Match: {result.value}")  # 0.0

# StringPresence
presence = StringPresence()
result = await presence.ascore(reference="Paris", response="The capital is Paris.")
print(f"String Present: {result.value}")  # 1.0

# NonLLMStringSimilarity with Levenshtein
sim = NonLLMStringSimilarity()
result = await sim.ascore(
    reference="The capital of France is Paris.",
    response="Paris is the capital of France."
)
print(f"Levenshtein Similarity: {result.value}")

# Using Jaro-Winkler distance
sim_jw = NonLLMStringSimilarity(distance_measure=DistanceMeasure.JARO_WINKLER)
result = await sim_jw.ascore(reference="kitten", response="sitting")
print(f"Jaro-Winkler Similarity: {result.value}")

# Batch evaluation
results = await sim.abatch_score([
    {"reference": "cat", "response": "cats"},
    {"reference": "dog", "response": "dogs"},
])

Related Pages

Page Connections

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