Implementation:Explodinggradients Ragas SemanticSimilarity Metric
| 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
- Explodinggradients_Ragas_AnswerCorrectness_Metric -- Uses SemanticSimilarity as a component for scoring
- Explodinggradients_Ragas_ResponseRelevancy_Metric -- Another embedding-based relevancy metric
- Explodinggradients_Ragas_StringMetrics_Module -- Non-LLM string similarity alternatives