Skip to content
AI Engineering
Embeddings & Retrieval

What are embeddings?

An embedding turns a piece of text, an image or a sound into a list of numbers so that similar meanings end up close together.

Chapter 8: Embeddings & RetrievalLesson 1 of 13Level: StarterAbout 15 min

Dot product and cosine similarity

ab

a · b = 3.86  |  cosine = 0.64

Partly aligned. Lengths: a = 3, b = 2.

Words on this page (10)
embedding
: A list of numbers that represents the meaning of a piece of text or an image, so similar things sit close together.
vector
: An ordered list of numbers. Embeddings and model activations are vectors.
retrieval
: Finding the most relevant pieces of text for a query.
RAG
: Retrieval-augmented generation: look up relevant text first, then give it to the model to answer from.
cosine similarity
: A score from -1 to 1 for how much two vectors point the same way, ignoring their length.
weights
: The learned numbers that define a trained model. A model whose weights are published is called open-weight.
batch
: A group of examples processed together in one step.
training
: Adjusting a model's parameters on data so its predictions improve.
reranker
: A second model that rescores a short list of search results by reading query and passage together.
dot product
: Multiply matching entries of two vectors and add them up. Large when they point the same way.

The idea

Computers cannot compare meaning directly, but they can compare numbers. An model reads a sentence and outputs a , which is a list of numbers, often 384 to 3,072 of them. The model is trained so that sentences with similar meaning get vectors that land close together. "How do I reset my password?" and "I forgot my login" end up near each other. "Best pizza in Rome" ends up far away.

A tiny example

Imagine a 2-number embedding where the first number means "about animals" and the second "about vehicles". "cat" might be [0.9, 0.0], "dog" [0.8, 0.1], "truck" [0.0, 0.9]. The cat and dog vectors are close. Cat and truck are far apart. Real embeddings have hundreds of dimensions that are not human-readable like this, but the geometry works the same way.

Plot the three as points on a graph, with "animals" along one axis and "vehicles" along the other. Cat and dog land in the same corner. An arrow from the middle of the graph to each point is the vector's direction, so "pointing the same way" just means "landing in the same corner".

Where embeddings are used

Semantic search (find documents by meaning), for , clustering similar items, recommendations, duplicate detection and classification with few labels.

Try it

Use the demo above to drag two arrows and see how the angle between them sets the similarity score.

A worked example: three sentences

A: "How do I reset my password?" B: "I forgot my login details." C: "What is the weather in Rome?" Scored with (a closeness score where 1 means the same direction and 0 means unrelated), A to B might get 0.82 and A to C 0.08. No shared keyword between A and B, yet the model knows they are about the same thing. That is why a help center search can answer "can't sign in" with the password-reset article.

How a model learns where to put text

Embedding models are usually trained on pairs of texts that belong together, such as a question and the passage that answers it. Each round nudges the model's so the pair moves closer. The same round pushes the pair away from unrelated texts in the same (a batch is a group of examples handled together). After millions of rounds, topics form clusters.

Here is one round with invented numbers:

  1. The pair is "reset password" and "forgot login". The model scores them 0.20.
  2. It scores "reset password" against "pizza in Rome" at 0.35, which is wrong.
  3. shifts the weights a little so the first score rises and the second falls, say to 0.25 and 0.30. The order is still wrong, but the gap has narrowed.
  4. Millions more rounds repeat this until the related pair clearly wins.

A second example: where embeddings blur

Take "The hotel was clean" and "The hotel was not clean". The words are almost identical and the meanings are opposite. Embeddings often rate such a pair as very close, because both are about a hotel and its cleanliness. The same happens with "refund within 30 days" and "refund within 3 days": the number changes and the vector barely moves.

Embeddings capture topic well and fine detail poorly. Negations, numbers, rare names and error codes are the weak spots. Later lessons cover the fixes: keyword search catches exact strings, and a reads question and passage together.

How to tell it is working

Write 10 pairs of sentences: five that should match and five that should not. Embed all of them and compute the cosine similarity for each pair. Every matching pair should score above every non-matching pair. If one does not, note what kind of pair failed (negation, number, name) so you know the model's blind spot.

Compare scores only within one model. A score of 0.82 from one model has no fixed meaning in another, because each model spreads its scores differently.

Common mistake

Expecting an embedding to answer a question. It only finds related text, and a model still has to read that text and respond.

The math, when you are ready

Closeness between two embeddings is measured with a or cosine similarity. Vectors and similarity works through both with small numbers. The next lessons here explain what you need, so you can skip it for now.

Check yourself

What do embeddings of similar sentences have in common?

Which is a common use of embeddings?

"I forgot my login" and "How do I reset my password?" share no keyword. How can search still match them?

Go deeper

Spotted a mistake or something unclear? Report it on GitHub.

On this page