Search GimmeJob

Type at least 2 characters. Enter also searches by meaning.
GimmeJob
Sign in
Generative AI & LLM · Chapter 05 / 20

Generative AI & LLM

Embeddings and semantic similarity

An embedding is a list of numbers that represents the meaning of a piece of text. Embeddings are everywhere in LLM systems: retrieval in RAG pipelines, semantic caches, topic guardrails, deduplication, clustering and similarity-based test assertions. A tester meets them twice: as a component of the system under test, and as a tool for building tests. Both roles require the same understanding of what similarity scores mean and, more importantly, what they do not mean.

Prerequisites: How LLMs generate text.

What an embedding is

An embedding model is a function that maps a text to a vector of fixed length d, typically between 256 and 3,072 numbers. It is trained so that texts with similar meaning are mapped to vectors that point in similar directions. The usual training method is contrastive learning: the model sees pairs of texts that belong together (a question and its answer, a sentence and its paraphrase) and pairs that do not, and it is optimized to pull the first kind together and push the second kind apart.

Embedding models are separate from chat models. They have their own API endpoints or libraries, their own versions, their own maximum input length and their own language coverage. Given the same input and the same model version they return the same vector (up to tiny floating-point differences), which makes them much easier to test than generators.

"Can I return worn shoes?"          ──► embedding model ──► [0.021, -0.113, 0.087, … ]  (d numbers)
"Is it possible to send shoes back?" ──► embedding model ──► [0.019, -0.098, 0.091, … ]
                                                               ▲ similar direction = similar meaning
"What is your delivery time?"       ──► embedding model ──► [-0.140, 0.052, 0.006, … ]

The individual numbers have no human meaning. Only relationships between vectors, measured by a similarity function, are meaningful, and only for vectors produced by the same model version.

Measuring similarity

Three measures are common:

dot product:         a · b = Σ a_i b_i
length (norm):       |a| = sqrt(a · a)
cosine similarity:   cos(a, b) = (a · b) / (|a| |b|)          range −1 … 1; 1 = same direction
Euclidean distance:  dist(a, b) = sqrt(Σ (a_i − b_i)²)

for unit-length vectors (|a| = |b| = 1):
    cos(a, b) = a · b
    dist(a, b)² = 2 − 2 cos(a, b)        so ranking by distance equals ranking by cosine

Many embedding APIs return vectors that are already normalized to length 1. Then dot product, cosine and Euclidean distance give the same ranking, and vector databases can use the fastest one. If vectors are not normalized, a dot product mixes direction with length and can rank a long, loosely related vector above a short, closely related one. The script implements the formulas and checks the identities.

Python
editable · browser sandbox
Result
Run the code to see the result.

Output:

cosine(a, b) = 0.978   cosine(a, c) = 0.179
dot(a, b) = 0.689   (not comparable across vectors of different length)
after normalization: dot = 0.978, equal to cosine
euclidean distance of unit vectors = 0.211; sqrt(2 - 2*cos) = 0.211

How to read similarity values

Cosine similarity is defined from −1 to 1, but real embedding models use only part of that range. For many models, unrelated texts still score well above 0, and the scores of paraphrases and of merely topical neighbours overlap. The scale is model-specific: a threshold of 0.8 can be strict for one model and loose for another. Never copy a threshold from an article or from a different model; calibrate it (see below).

Similarity is not equivalence

Embeddings capture topic and phrasing much better than they capture logic. Two sentences about the same subject can have very high similarity while saying opposite things. The script below uses the simplest possible embedding, a bag-of-words count vector, to make the mechanism visible. Learned embedding models are far better at synonyms, but they show the same weaknesses on negation, numbers and roles to a smaller, still important degree.

Python
editable · browser sandbox
Result
Run the code to see the result.

Output:

0.89  'The refund was approved.' vs 'The refund was not approved.'
0.83  'Returns are accepted within 30 days.' vs 'Returns are accepted within 60 days.'
1.00  'Ship the order from Kyiv to Lviv.' vs 'Ship the order from Lviv to Kyiv.'
0.31  'Can I send the shoes back?' vs 'Is it possible to return the shoes?'

The first three pairs mean different things and score high; the last pair means the same thing and scores low. A learned model fixes the last case (it knows that "send back" and "return" are related) but keeps scoring the first three pairs as close, because they share topic, vocabulary and structure.

Failure classExample pairWhy similarity misleadsBetter oracle
Negation"approved" vs "not approved"One short word flips the meaningFact or claim check, judge
Numbers and units"30 days" vs "60 days"Numbers carry little weight in the vectorExtract the number and compare exactly
Roles and order"Kyiv to Lviv" vs "Lviv to Kyiv"Same words, different relationStructured extraction, judge
Scope and quantifiers"all items" vs "some items"Quantifiers are small tokensRubric that names the quantifier
Entity swaps"order A-1042" vs "order A-1024"IDs look alikeExact match on IDs

Consequence for testing: a similarity score can tell you that an answer is about the right thing. It cannot tell you that the answer is right. Use similarity for retrieval, clustering and as a cheap first filter; use fact checks, structured extraction or validated judges for correctness (Code-based scorers and metrics, LLM-as-a-judge).

Calibrating a similarity threshold

When a system or a test uses a similarity threshold, for example "treat the answer as matching the reference if cosine ≥ t" or "serve a cached answer if the new question is within t of a cached one", the threshold is a binary classifier and must be calibrated like one:

1. Collect pairs from the real domain (hundreds, not dozens), including hard negatives:
   same topic, different meaning.
2. Have humans label each pair: equivalent (1) or not (0).
3. Compute the similarity of every pair with the exact model version you will use.
4. Sweep thresholds; at each one compute precision, recall, F1 and false-positive rate.
5. Choose the threshold from the cost of errors, not from the prettiest metric.
6. Confirm the choice on a separate held-out set of labelled pairs.

The script sweeps thresholds over a small labelled set. The scores are example data in a realistic range.

Python
editable · browser sandbox
Result
Run the code to see the result.

Output:

threshold  precision  recall   F1    false-positive rate
  0.76      0.63      1.00   0.77        0.58
  0.77      0.63      1.00   0.77        0.58
  0.78      0.63      1.00   0.77        0.58
  0.79      0.67      1.00   0.80        0.50
  0.80      0.65      0.92   0.76        0.50
  0.81      0.69      0.92   0.79        0.42
  0.82      0.73      0.92   0.81        0.33
  0.83      0.71      0.83   0.77        0.33
  0.84      0.77      0.83   0.80        0.25
  0.85      0.75      0.75   0.75        0.25
  0.86      0.82      0.75   0.78        0.17
  0.87      0.89      0.67   0.76        0.08
  0.88      0.88      0.58   0.70        0.08
  0.89      0.86      0.50   0.63        0.08
  0.90      1.00      0.42   0.59        0.00
lowest threshold with precision >= 0.90: 0.90 (recall 0.42)

How to interpret this table:

  • Overlap is the norm. Equivalent and non-equivalent pairs share the range 0.78–0.89. No threshold separates them perfectly; every choice trades false positives against false negatives.
  • The right threshold depends on the cost of each error. For a semantic cache, a false positive serves the wrong answer to a user ("cancel my order" answered with the cached reply to "don't cancel my order"), so high precision matters most, even at the price of fewer cache hits. For a test assertion that only pre-filters answers for a judge, recall matters more.
  • Small samples mislead. With 24 pairs, one pair moves precision by several points. Use hundreds of pairs and report the uncertainty as a confidence interval (Statistics for evaluation shows how).

To retrieve the documents most similar to a query, a system compares the query vector with stored vectors.

  • Exact k-nearest-neighbour (kNN) search compares the query with every stored vector. It is always correct but its cost grows linearly with the number of vectors.
  • Approximate nearest-neighbour (ANN) search uses an index to look at only part of the collection. Common index types are graph-based (HNSW) and cluster-based (IVF, "inverted file"). ANN is much faster and occasionally misses true nearest neighbours.

ANN quality is measured as recall@k against exact search: for a sample of queries, the share of the true top-k results that the index also returns. Index parameters trade recall for speed: for IVF, nprobe is the number of clusters searched; for HNSW, ef_search is the size of the candidate list. The simulation below builds a simple IVF index over random unit vectors and measures recall at several nprobe values.

Python
editable · browser sandbox
Result
Run the code to see the result.

Output:

nprobe  scanned  recall@10
     1      6%   0.24
     2     13%   0.45
     4     26%   0.63
     8     51%   0.90
    16    100%   1.00

Random vectors have no cluster structure, so this is close to a worst case; real embeddings cluster by topic and usually reach high recall while scanning a smaller fraction. The lesson is the method, not the numbers: measure index recall on your own data, because a misconfigured index silently removes relevant documents before the language model ever sees them.

Other vector-store behaviours to test:

  • Metadata filters. Production queries usually filter by tenant, language, product or permission. Test that filters are applied before results are returned (a user must never receive another tenant's document), and that filtering does not collapse recall when few documents match.
  • Freshness. After a document is updated or deleted, the old vector must disappear. Test the update and delete paths, not only inserts.
  • Model consistency. Query and document vectors must come from the same embedding model version. Store the model name and version with the index and fail loudly on a mismatch.

Using embeddings in test engineering

Embeddings are also a practical tool for testers:

TaskHow embeddings helpCaution
Deduplicating an evaluation datasetCases with very high similarity (for example above a calibrated 0.95) are probably duplicates; review and mergeNear-duplicates can differ in one critical detail; review before deleting
Clustering failures for error analysisEmbed failing outputs or inputs and cluster them to see failure groupsClusters reflect topic, not cause; name them after reading examples
Coverage analysisEmbed production queries and evaluation cases; production clusters without nearby cases are coverage gapsRequires access to (anonymized) production data
Semantic cache testingGenerate pairs of near-identical questions with different meaning and check the cache does not answer one with the otherFalse hits are user-visible defects
Input drift detectionCompare the distribution of today's queries with last month'sUse with care; see Production quality

A coverage check with a real embedding model takes only a few lines. Run it on your own machine:

# pip install sentence-transformers
from sentence_transformers import SentenceTransformer

model = SentenceTransformer("paraphrase-multilingual-MiniLM-L12-v2")   # supports English and Ukrainian

eval_cases = ["Can I return worn shoes?", "How long does delivery take?", "Do you ship to Poland?"]
production_queries = ["чи можна повернути взуття після 40 днів", "my parcel is late", "do you have vegan sneakers?"]

case_vectors = model.encode(eval_cases, normalize_embeddings=True)
query_vectors = model.encode(production_queries, normalize_embeddings=True)
similarity = query_vectors @ case_vectors.T                   # cosine, because vectors are normalized

for query, row in zip(production_queries, similarity):
    nearest = row.argmax()
    flag = "COVERED" if row[nearest] >= 0.6 else "GAP"      # calibrate this threshold on labelled pairs
    print(f"{flag:<8} {row[nearest]:.2f}  {query!r} -> nearest case {eval_cases[nearest]!r}")

Choosing and testing an embedding model

Public leaderboards such as MTEB compare embedding models across many tasks and languages and are a good starting point. They do not replace testing on your own data, because retrieval quality depends heavily on domain vocabulary, document style and language mix. Evaluate candidates with the retrieval metrics from Evaluating RAG systems on labelled queries from your domain, and also compare:

  • maximum input length: text beyond it is truncated, often silently, so long chunks lose their end;
  • languages: check every language your users write in, and cross-language retrieval if users ask in one language about documents in another;
  • dimensions, latency and cost: larger vectors cost more storage and search time;
  • versioning: switching models means re-embedding the whole corpus, because vectors from different models are not comparable.

A typical API call looks like this (OpenAI Python SDK; other providers are similar):

from openai import OpenAI

client = OpenAI()                                  # reads OPENAI_API_KEY from the environment
response = client.embeddings.create(
    model="text-embedding-3-small",                # record the exact model name with the index
    input=["Can I return worn shoes?", "Is it possible to send shoes back?"],
)
vectors = [item.embedding for item in response.data]
print(len(vectors), "vectors of", len(vectors[0]), "dimensions")

Switching to another embedding model

A model switch is a migration:

  1. Measure retrieval quality (recall@k, MRR, nDCG) of the old and the new model on the same labelled queries, in every supported language, including cross-language queries.
  2. Measure end-to-end answer quality on the RAG evaluation set, and the latency and cost of embedding and search.
  3. Check the new maximum input length against your chunk sizes.
  4. Re-embed the full corpus into a new index instead of mixing vectors; store the model version in the index metadata and assert at query time that query and index use the same model.
  5. Switch only if the new model is at least as good on the agreed metrics with statistical confidence and no language or segment regresses beyond the tolerance; keep the old index for rollback.

Example project

The method of this chapter is implemented and tested in examples/llm-testing/03-similarity-thresholds: similarity measures, a threshold-calibration command-line tool with a labelled data set that includes hard negatives, and recall measurement of an approximate index. All tests run offline, without a model or API keys:

uv sync --project examples/llm-testing
uv run --project examples/llm-testing pytest examples/llm-testing/03-similarity-thresholds

Common mistakes

  • Reusing a similarity threshold from another model, another domain or an article.
  • Using cosine similarity as a correctness oracle for answers where negation, numbers or roles matter.
  • Mixing vectors from different embedding models or versions in one index.
  • Ignoring input truncation of long chunks by the embedding model.
  • Not measuring ANN recall and not testing metadata filters, updates and deletes.
  • Using dot product on vectors that are not normalized and getting a length-biased ranking.

Summary

  • An embedding maps text to a vector; similar meaning gives similar direction. Vectors are only comparable within one model version.
  • Cosine similarity, dot product and Euclidean distance are equivalent for normalized vectors. Similarity scales are model-specific.
  • Similarity is not equivalence: negation, numbers, roles, quantifiers and IDs are where it fails. Use it for topic, not truth.
  • Any similarity threshold is a classifier; calibrate it on labelled pairs and choose it from the cost of errors.
  • Approximate vector search trades recall for speed; measure index recall, filters, freshness and model consistency.
  • Embeddings are also a testing tool for deduplication, failure clustering and coverage analysis.

Source registry

8 chapter references
MTEB: Massive Text Embedding Benchmark

Benchmark for comparing embedding models across tasks and languages; a starting point before domain-specific retrieval evaluation.

Muennighoff et al. — arXiv / EACL 2023 · paper
Source ↗
MTEB leaderboard

Current comparison of embedding models by task and language.

Hugging Face · benchmark
Source ↗
Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks

Foundational method for sentence embeddings trained so that cosine similarity reflects meaning.

Reimers, Gurevych — arXiv / EMNLP 2019 · paper
Source ↗
Sentence Transformers documentation

Library used in the coverage-analysis example, including multilingual models.

SBERT / Hugging Face · official docs
Source ↗
Embeddings guide

API reference for creating embeddings, vector length and normalization behaviour.

OpenAI · official docs
Source ↗
Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs

The HNSW graph index used by most vector databases; explains the recall-speed trade-off.

Malkov, Yashunin — arXiv / IEEE TPAMI · paper
Source ↗
The Faiss library

Reference for vector index types (IVF, HNSW, quantization) and how to evaluate their recall.

Douze et al. — arXiv / Meta · paper
Source ↗
pgvector — vector similarity search for Postgres

Widely used vector store; documents exact and approximate (HNSW, IVFFlat) search and filtering.

pgvector · tool
Source ↗