Text as a list of numbers
An embedding is a vector: a fixed-length list of numbers. An embedding model gives every text its own vector, and the model is trained so that texts with related meanings get vectors that lie close together. “Cheap car” and “affordable used automobile” do not share a single word, yet their vectors can still be very similar.
In the demo every number has a name (vehicle, animal, food, money …), so you can see why two texts are similar. Real embeddings usually have between a few hundred and a few thousand numbers, and the individual numbers have no fixed meaning you can read off directly.
Cosine similarity: compare the direction
Two vectors are most often compared with cosine similarity: the dot product divided by the product of the two lengths, cos θ = (a · b) / (|a| |b|). The result lies between −1 and 1. Close to 1 means the same direction, 0 means no shared direction. Length does not count, only direction, so a long and a short text about the same thing can still be similar. Many embedding models normalise their vectors to length 1, and then cosine similarity and the dot product are the same.
From word vectors to text vectors
The demo makes a text vector by averaging its words’ vectors. That is an older but real method from the days of word vectors such as word2vec. It has two clear weaknesses: words outside the vocabulary disappear, and every word has one vector whatever the context. A word like “bank” (river or money) gets a single vector that mixes both meanings.
Modern embedding models are transformers. They split the text into tokens, let all the tokens influence each other through attention, and finally pool the result into one vector, for example the average over the token positions. So “bank” gets a different representation in “the bank of the river” and “the bank raised its rates”. The models are usually trained contrastively: pairs of texts that belong together, such as a question and its answer, are pulled towards each other, while the other texts in the same batch are pushed away.
Semantic search or keywords?
Keyword search finds documents that contain the same words. “Cheap car” finds only the expensive sports car, because it contains the word “car”. Embedding search instead puts the affordable used automobile at the top. Try “something to eat in the morning”: no document contains those words, but the breakfast and the bakery still come out on top.
Keywords still have advantages. Names, product codes and rare technical terms are matched exactly by keywords, while an embedding can blur them. Many systems therefore combine the two (hybrid search), for example a classic ranking such as BM25 together with an embedding search.
RAG: retrieve first, then answer
RAG stands for retrieval-augmented generation. The documents are split into smaller chunks, and each chunk gets its embedding, which is stored, often in a vector database. When a user asks something, the system embeds the question, retrieves the most similar chunks and puts them into the prompt before the question. That is the prompt you see at the bottom of the demo. The language model then answers with that text in front of it.
RAG gives the model access to knowledge it was not trained on and makes it possible to cite sources. But the answer cannot be better than what was retrieved. If the right chunk is missing, or the chunks were cut in unlucky places, the model can still answer wrongly or guess.
Pitfalls
Similarity is not the same as relevance or truth. Try “expensive car” in the demo: the affordable car comes out on top, just ahead of the expensive one, because both texts are about the same topic (cars and price) even though they say the opposite. Real embedding models show the same tendency to a lesser degree: opposites and negations (“not cheap”) can lie close together because they appear in the same contexts. A model trained on one kind of text can also rank poorly in another field or language. So test the search on real questions before you rely on it.
Embeddings inside a language model
A language model such as GPT or Pythia also has embeddings, but at a different level: a table with one vector for every token ID. Pythia-70M’s vectors have 512 numbers. The first thing the model does is look up each token in that table; the transformer layers then work on those vectors. An embedding model for search, by contrast, gives one vector for a whole text.
See real embeddings from a language model
In the free Tokenization lab you look up tokens in Pythia-70M’s embedding table, compare ␠cat and ␠dog with cosine similarity, and see how the same token gets different activations when the context changes. No installation needed.