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 SemanticSimilarity Metric

From Leeroopedia


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

Overview

SemanticSimilarity (also aliased as AnswerSimilarity) computes cosine similarity between the embeddings of a generated response and a reference ground truth.

Description

The SemanticSimilarity class scores the semantic similarity between a reference answer and a generated response using embedding-based cosine similarity. It supports both standard embedding models and cross-encoder models (via HuggingFace). The metric can optionally apply a threshold to produce binary output. It inherits from MetricWithEmbeddings and SingleTurnMetric.

Key attributes:

  • is_cross_encoder -- Boolean indicating if a cross-encoder model is used (auto-detected from HuggingFace embeddings).
  • threshold -- Optional float threshold for binary classification (default None).

The AnswerSimilarity subclass provides a backward-compatible alias with name="answer_similarity".

Usage

The metric requires reference and response columns. Embeddings must be configured.

Code Reference

Property Value
Source Location src/ragas/metrics/_answer_similarity.py L25-116
Class Signature class SemanticSimilarity(MetricWithEmbeddings, SingleTurnMetric)
Import from ragas.metrics import SemanticSimilarity

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 float Cosine similarity score (0.0 to 1.0), or binary (0/1) if threshold is set

Usage Examples

from ragas.metrics import SemanticSimilarity
from ragas.dataset_schema import SingleTurnSample

metric = SemanticSimilarity(threshold=None)
# metric.embeddings = ...  # Set your embeddings

sample = SingleTurnSample(
    reference="The Eiffel Tower is located in Paris, France.",
    response="The Eiffel Tower can be found in Paris."
)
# score = await metric.single_turn_ascore(sample)

A pre-configured instance is available:

from ragas.metrics._answer_similarity import answer_similarity

Related Pages

Page Connections

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