Embedding models and similarity

How text becomes vectors, what cosine similarity means, how to choose a model, and where embeddings mislead.

Intermediate about 4 h

The picture

  1. 01

    Text

    a chunk or a query

  2. 02

    Encoder

    transformer

  3. 03

    Pool + normalise

    one vector

  4. 04

    Compare

    cosine / dot

  5. 05

    Rank

    top-k neighbours

What an embedding is

An embedding model maps text to a point in a high-dimensional space so that texts with similar meaning land close together. Retrieval then becomes nearest-neighbour search. For unit-length vectors, cosine similarity equals the dot product.

import numpy as np
from sentence_transformers import SentenceTransformer

model = SentenceTransformer("BAAI/bge-small-en-v1.5")
docs = [
    "Employees accrue 1.5 days of paid leave per month.",
    "Invoices are payable within 30 days of receipt.",
    "The cafeteria is open from 8 to 4 on weekdays.",
]
D = model.encode(docs, normalize_embeddings=True)              # (3, 384), unit length
q = model.encode("How much holiday do I get?", normalize_embeddings=True)
scores = D @ q                                                 # cosine similarity
for i in np.argsort(-scores):
    print(round(float(scores[i]), 3), docs[i])

Some models want a prefix on queries or documents (for example an instruction for the query side). Check the model card, because skipping it quietly lowers quality.

Query–document asymmetry

A question and its answer are phrased differently, so they are not necessarily close. Models trained for retrieval are asymmetric: they learn that a short question should sit near a long passage that answers it. Use a retrieval-trained model, not a generic sentence-similarity one.

Choosing a model

  1. Start from the leaderboard to shortlist, filtering by size, language and licence.
  2. Evaluate on your own data. Write 30 to 100 real queries with their correct passages and measure recall@k for each shortlisted model.
  3. Weigh dimension (storage and speed) and context length (how long a chunk it can embed).

Matryoshka-trained models let you truncate vectors (for example 1024 to 256 dimensions) with modest quality loss, cutting storage and search cost.

Where embeddings mislead

  • Negation and numbers: "refunds are allowed" and "refunds are not allowed" are very close.
  • Rare identifiers: part numbers, error codes and names are poorly represented. This is why you add keyword search (see hybrid search).
  • Domain shift: a general model can be weak on legal, medical or code text.
  • Anisotropy: raw similarity scores are not calibrated probabilities. A score of 0.8 means different things in different models.

Common mistakes

  • Comparing un-normalised vectors with a dot product and calling it cosine.
  • Re-embedding the whole corpus after changing the model and forgetting to re-embed queries with the same one.
  • Choosing a model by leaderboard rank alone.

Exercises

  1. Build a 50-query test set over a small corpus and compare two embedding models on recall@5.
  2. Find three query/passage pairs where embeddings fail, and explain each failure.

You are done when you can…

  • Embed a corpus and retrieve the nearest passages for a query with correct normalisation
  • Explain why a query and its answer passage can be far apart in embedding space
  • Choose an embedding model using your own labelled queries, not only a leaderboard