Skip to content
AI360XpertBeta

Embeddings

An embedding turns a word or passage into a list of numbers positioned so that closeness means relatedness, which lets search match meaning instead of exact words.

In embedding space, closeness is meaning: a paraphrase sits 45 degrees from the query while an unrelated text sits at a right angle
In embedding space, closeness is meaning: a paraphrase sits 45 degrees from the query while an unrelated text sits at a right angle

Why Does This Exist?

Keyword search matches strings, not intent. "How do I reset my password" and "forgot login credentials" ask the same thing and share almost no words, so a keyword index files them as unrelated. You've felt this failure: a help center that answers your question somewhere yet returns nothing for how you phrased it.

Embeddings change what gets compared. A trained model converts each piece of text into a fixed-length vector of numbers, positioned so that texts with similar meaning land close together. Once both phrases are points in one shared space, "do these mean the same thing" becomes "how far apart are these points", which your machine answers with arithmetic in microseconds.

This page covers the object and the comparison: what an embedding is, why cosine similarity is the usual ruler, and one example computed end to end with the picture drawn to match. Storing millions of these vectors is the job vector databases exist to do, and searching them fast is approximate nearest neighbor search. Both pages assume the mechanism here already clicked, so start here.

One prerequisite, kept short: a dot product multiplies two vectors element by element and adds the results, so for a=[2,1]a = [2, 1] and b=[1,3]b = [1, 3] it is 2×1+1×3=52 \times 1 + 1 \times 3 = 5. Large means the arrows point the same way, zero means they sit at right angles. The full treatment is dot product.

Think of It Like This

A library shelved by what books are about

Imagine a library that ignores titles and shelves every book by what it is about. All the bread-baking books land together even though their titles share no words, and the plumbing manuals sit far away. To find what you need, you walk to the spot your question belongs and grab whatever is nearby. That walk is retrieval, and nearness on the shelves is relevance.

The shelves stop short of the real thing in two ways. A shelf has three dimensions and a librarian choosing spots by judgment. An embedding space has hundreds of dimensions, and no person chose any position: the model learned all of them from text, and no single direction means anything a person would recognize. Only the distances between points carry meaning, never one coordinate read alone.

How It Actually Works

From text to vector

An embedding model is a trained neural network that reads text and returns a fixed-length vector. Write it as E(t)=vE(t) = v, where EE is the model, tt is your input text of any length, and v=[v1,v2,…,vd]v = [v_1, v_2, \ldots, v_d] is the output: a list of dd numbers, with dd fixed per model. One word and one paragraph both come back as dd numbers; what differs is where each lands. Nothing about a single viv_i is interpretable, and that is by design: training optimizes the relative positions, not the axes.

Why cosine similarity

Length confuses the comparison, so the standard ruler factors it out. For vectors aa and bb, with a⋅ba \cdot b the dot product, ∥a∥\lVert a \rVert and ∥b∥\lVert b \rVert their lengths, and θ\theta (theta) the angle between them:

cos⁡(θ)=a⋅b∥a∥∥b∥\cos(\theta) = \frac{a \cdot b}{\lVert a \rVert \lVert b \rVert}

In plain words: alignment divided by lengths. A long passage and a short phrase can mean the same thing, so you compare directions, not magnitudes. The score is 11 for identical direction, 00 for a right angle, and negative past that. Length itself is ∥a∥=a12+a22+⋯+ad2\lVert a \rVert = \sqrt{a_1^2 + a_2^2 + \cdots + a_d^2}, the ordinary straight-line distance from the origin. The catalogue of rulers, and when each wins, lives in similarity metrics, with the math underneath in distance and similarity metrics.

Worked example

Give the model only two dimensions so the whole thing fits on this page. Real models use hundreds; the arithmetic is identical.

  • query a=[2,1]a = [2, 1], standing in for "reset my password"
  • paraphrase b=[1,3]b = [1, 3], standing in for "forgot login credentials"
  • unrelated c=[−1,2]c = [-1, 2], standing in for "garden hose"

First the dot products: a⋅b=2×1+1×3=2+3=5a \cdot b = 2 \times 1 + 1 \times 3 = 2 + 3 = 5, and a⋅c=2×(−1)+1×2=−2+2=0a \cdot c = 2 \times (-1) + 1 \times 2 = -2 + 2 = 0. That zero already whispers the ending.

Then the lengths: ∥a∥=22+12=5≈2.2361\lVert a \rVert = \sqrt{2^2 + 1^2} = \sqrt{5} \approx 2.2361, ∥b∥=12+32=10≈3.1623\lVert b \rVert = \sqrt{1^2 + 3^2} = \sqrt{10} \approx 3.1623, and ∥c∥=(−1)2+22=5≈2.2361\lVert c \rVert = \sqrt{(-1)^2 + 2^2} = \sqrt{5} \approx 2.2361.

Then the cosines. For the paraphrase: 5/(2.2361×3.1623)=5/7.0711≈0.70715 / (2.2361 \times 3.1623) = 5 / 7.0711 \approx 0.7071, which equals 1/21/\sqrt{2}, an angle of 45∘45^\circ. For the unrelated text: 0/(2.2361×2.2361)=0.00 / (2.2361 \times 2.2361) = 0.0, a clean right angle of 90∘90^\circ.

The ranking is decided: 0.70710.7071 beats 0.00.0, so retrieval returns the paraphrase first. The diagram draws these exact three arrows to scale, and the snippet below reruns the same numbers. Notice what the zero didn't need: aa and cc share no words, yet the score calls them unrelated numerically instead of by word overlap.

The trade-off, and when to skip vectors

What you buy is recall on paraphrase: users phrase things differently than your docs, and vectors bridge that gap. What you pay is precision on the literal: exact names, codes, and quoted error strings can drown among semantic neighbours, and negation ("cancel" against "renew") often lands closer than it should. When the answer must match a token exactly, like an ID or an SKU, pair vectors with keyword search rather than replacing it. That pairing is hybrid search.

The assumption underneath is that your text resembles the model's training text. Dense domain jargon, heavy misspellings, or a language it barely saw will place points almost at random. The fix is a model trained closer to your domain, which is the subject of embedding models, not a longer vector.

One vector per chunk, from one model

Two operating rules decide whether any of this survives production. First, don't embed whole documents: a single vector for a manual averages every topic in it into mush, so systems embed passage-sized chunks and retrieve those. How to split is chunking strategies. Second, every vector in one index must come from the same model at the same version, because each model builds its own coordinate system and a distance across two systems measures nothing. Upgrading the model means re-embedding the corpus, all of it.

Code

The point of this snippet is the ranking, not the formula: two cosines computed from scratch, deciding which passage a query retrieves.

import math

def dot(u: list[float], v: list[float]) -> float:    """Element-wise products, added up."""    return sum(x * y for x, y in zip(u, v))

def norm(u: list[float]) -> float:    """Straight-line length from the origin."""    return math.sqrt(sum(x * x for x in u))

def cosine(u: list[float], v: list[float]) -> float:    """Alignment divided by lengths, between -1 and 1."""    return dot(u, v) / (norm(u) * norm(v))

query = [2.0, 1.0]  # "reset my password"paraphrase = [1.0, 3.0]  # "forgot login credentials"unrelated = [-1.0, 2.0]  # "garden hose"
print(round(cosine(query, paraphrase), 4))  # -> 0.7071print(round(cosine(query, unrelated), 4))  # -> 0.0

The paraphrase outscores the unrelated passage 0.70710.7071 to 0.00.0, so it ranks first: the same three vectors as the worked example, and the same three arrows as the diagram.

Watch Out For

Searching across two embedding models at once

Upgrading the model without re-embedding leaves old and new vectors sharing one index. Nothing errors: the dimensions match, the distances compute, the results come back ranked. But the two models never agreed on what any direction means, so every cross-model distance is noise wearing a number.

Symptom: relevance quietly drops right after a model upgrade, with no error anywhere. Fix: version-pin the model, re-embed the whole corpus on upgrade, and refuse writes from any other model version.

Embedding whole documents as single vectors

One vector per long document averages every section into one point. A manual that answers your query in one paragraph then retrieves like a manual that mentions it in passing, because both collapsed to mush.

Symptom: the right document exists but the exact passage never surfaces, and scores bunch together. Fix: split into passage-sized chunks first (see chunking strategies), embed each chunk, and carry the document id along as metadata.

The Quick Version

  • An embedding is a fixed-length vector a trained model assigns to a piece of text, placed so that distance means relatedness.
  • Cosine similarity turns two embeddings into one score: 1 for the same direction, 0 for unrelated, negative past that.
  • In the worked example the paraphrase scores 0.7071 against the query and the unrelated text scores 0.0, so the paraphrase ranks first.
  • Every vector in one index must come from the same model version, or every distance in it is meaningless.
  • Embed passage-sized chunks, not whole documents, or one vector averages every topic into mush.
  • Pair vectors with keyword search when the answer must match an exact name, code, or quoted string.