Back to Tutorials
LLM Application Development
Intermediate

Embeddings: Turning Meaning into Numbers

Explore 32 sentences through a real embedding model, measure “close” three ways, and find out what similarity scores miss.

10 minInteractive: drag, click and try things as you read


An embedding turns a piece of text into a list of numbers. That sounds dull until you see what the numbers do: texts that mean similar things get similar numbers, so “find something relevant” becomes “find something nearby”. It’s the engine under semantic search, RAG, recommendations and duplicate detection. Here you’ll explore 32 sentences through a real embedding model, measure “nearby” three ways, and find out what the numbers miss.

A list of numbers that means something#

An embedding model reads a text and returns a fixed-length list of numbers, called a vector. MiniLM, a small free model you can run on a laptop, returns 384 numbers for any text up to about 200 words (it cuts off the rest). OpenAI’s text-embedding-3-small returns 1,536. No single number means anything you could name. What matters is how two vectors compare.

The model learned that by example. MiniLM was trained on over a billion pairs of texts that belong together, such as questions and their answers, or posts and their titles, and rewarded each time it put a pair closer together than unrelated texts. Nobody told it what “password” means; it learned which texts tend to go together.

Predict first

Which of these will the model place closest to “How do I reset my password?”

Try it
32 made-up help-desk questions and remarks in six topics. Their embeddings come from the real all-MiniLM-L6-v2 model, computed in advance; the scores are worked out live from the 384 numbers. The map squeezes 384 numbers into two (with t-SNE), so use it to spot groups and trust the scores for how close things are.

The score is cosine similarity: multiply the two lists position by position and add up. Where both vectors are high, or both low, the product is positive and the texts agree; where one is high and the other low, they disagree. 1 would mean pointing exactly the same way, 0 means nothing in common. For short texts and this model, related usually means 0.3 to 0.6, not 0.9.

Look at what the model gets right. “The bank approved my loan” sits with the money questions, not with “A picnic on the river bank”. “Apple’s new phone has a better camera” is closest to the phone battery, with the apple pie third at 0.12. The model matches meaning it learned from examples, not letters. But words still pull: “How do I open a savings account?” lands closest to “I can’t get into my account” (0.39), a login problem, because both mention an account. And some neighbours are a puzzle: the lost suitcase is closest to the password reset, perhaps because both are someone asking for help. Small models blur; bigger ones blur less.

Three ways to measure “close”#

Picture each vector as an arrow from a shared starting point. There are three common ways to say how close two arrows are:

  • Cosine similarity looks only at the angle between them: 1 for the same direction, 0 at right angles, −1 for opposite.
  • Dot product multiplies position by position and adds up. It’s the cosine multiplied by both arrows’ lengths, so longer arrows score higher.
  • Euclidean distance is the straight-line distance between the two tips. Smaller is closer.

Predict first

Document A points exactly the same way as your question but is a short arrow. Document C points 45° away but is twice as long as the question. Which one does the dot product call closer?

Try it
Vectors with two numbers instead of 384, so you can see the angles. Drag the question and the three documents. The maths is the same in any number of dimensions.

In the starting picture the three measures pick three different documents: the dot product picks the long arrow C, cosine picks A because it points exactly the question’s way, and distance picks B, whose tip is nearest. Now switch to Every arrow length 1. They agree, and they always will: when every vector has length 1, the dot product is the cosine, and the distance squared is 2 − 2 × cosine, so all three put documents in the same order.

That’s why the choice usually doesn’t matter in practice. MiniLM and BGE small hand back vectors of length 1 already, and OpenAI says its embeddings are normalised to length 1 and recommends cosine. Not every model does it: the multilingual model in the next figure returns lengths between 4.8 and 6.2 for our 32 sentences, and a few models are trained to be used with the dot product, length and all. Use the measure your model was trained for; if you don’t know, normalise to length 1 and use cosine. A common trick is to normalise once when you store the vectors, then use the dot product, the cheapest of the three to compute.

What a score can’t tell you#

Embeddings are good at “what is this about?”. Whether two texts say the same thing about it is a different question.

Predict first

How will MiniLM score “This phone has a great battery” against “This phone does not have a great battery”?

Try it
Ten pairs we wrote, scored by three free models from Hugging Face (computed in advance with sentence-transformers). Drag the line to choose what counts as similar, then switch models.

Three lessons hide in that list. First, embeddings record topic more than stance: the opposite scores 0.88, “The man bit the dog” scores 0.98 against “The dog bit the man”, and a flight that left on time scores 0.70 against one that was badly delayed. For search that’s often what you want, since both battery sentences are relevant to a battery question. To check whether two texts agree, use a model that reads both together, such as a reranker or an LLM.

Second, languages. MiniLM was trained on English and gives the Spanish and French translations 0.16 and 0.11. The multilingual model, trained on the same sentences in over 50 languages, gives them 0.99 and 0.97. If your users write in more than one language, you need a model built for it.

Third, scores from different models aren’t on the same scale. BGE small gives even the unrelated pairs about 0.4; across the 32 sentences in the first figure its lowest score is 0.25 and its average 0.43, against −0.10 and 0.07 for MiniLM. A cut-off of 0.4 that works for one model is meaningless for the other. A similarity score means something only next to other scores from the same model. Pick thresholds by testing on your own examples, and never compare vectors from two different models: they live in different spaces.

What you can build#

Once text is a vector, many problems become “find the nearby vectors”:

  • Semantic search and RAG. Embed your documents once and store the vectors. Embed each question as it arrives and return the nearest documents.
  • Clustering. Group thousands of support tickets or reviews by topic without reading them all.
  • Duplicates. Two texts scoring very close are probably saying the same thing in different words.
  • Recommendations. “More like this” is the nearest neighbours of what someone just read.
  • Classification with few labels. Label a handful of examples, then give each new text the label of its nearest labelled neighbours.

In code#

The figures’ model, free and local, with pip install sentence-transformers:

from sentence_transformers import SentenceTransformer

model = SentenceTransformer("all-MiniLM-L6-v2")  # downloads about 90 MB once
texts = [
    "How do I reset my password?",
    "I can’t get into my account",
    "Easy banana bread recipe",
]
vectors = model.encode(texts)  # one row of 384 numbers per text
print(vectors.shape)
print(model.similarity(vectors, vectors))  # cosine similarity, every pair
# (3, 384)
# tensor([[1.0000, 0.5088, 0.0884],
#         [0.5088, 1.0000, 0.0279],
#         [0.0884, 0.0279, 1.0000]])

Hosted models work the same way: text in, vector out. Whichever you use, embed your documents and your questions with the same model, and store the vectors so you only pay for each document once.

What to remember

  • An embedding model turns text into a vector so that texts with similar meaning land close together, even with no words in common.
  • Cosine looks at the angle, the dot product adds length, distance compares tips; for vectors of length 1 they all rank the same.
  • Embeddings capture topic more than stance or word order, and their scores only compare within one model: test thresholds on your own data.

Keep going

Sign in to save your place and keep a learning streak. Free, with Google.