Generative AIby Dhruv Narang

1. What Is an Embedding Vector?

An embedding is a list of numbers that stands for the meaning of a piece of text. It is also called a vector.

It is made by an embedding model. This is a ready-made program: you give it a chunk of text, and it gives you back one list of numbers. The same text always gives the same list, and texts with a similar meaning give similar lists.

We make these lists for all our chunks, store them, and later use them to find the chunks that match a question.

A chunk of text
"A young farmer discovers a hidden map and sets out to find a lost city."
→embedding model
Its embedding vector
[0.02-0.340.120.50-0.070.81…]
1,024 numbers
Example numbers.
  • Same size every time. A short line and a long chunk both give a list of the same length, for example 1,024. The length depends on the model.
  • Each number holds a little meaning. More numbers means more room for detail.
  • Similar meaning gives a similar list. Texts about different things give lists that are far apart.
  • Reality check. The meaning is spread across all the numbers together. You cannot point at one number and say "this is adventure".

2. Example: Movies as Vectors

Let us see what "every number holds some meaning" looks like. Take three movie plots and give each a vector.

Movie plotactionromancecomedysci-fi
A superhero team fights an alien army0.900.100.300.80
Astronauts fly through a wormhole to save humanity0.500.200.000.95
Two strangers fall in love in Paris0.000.950.300.00
  • Each number says a little about the plot. The first movie scores high on action and sci-fi. The third scores high on romance. That is why an embedding is also called a meaning vector.
  • Similar plots get similar vectors. The superhero movie and the wormhole movie are both sci-fi action, so their lists are close (closeness 0.90). The Paris love story is far from them (closeness 0.15).
  • More numbers means more detail. With 4 numbers we can only say "action, romance, comedy, sci-fi". With 1,024 numbers a model can also capture mood, setting, themes and more.
  • Reality check. I named the 4 columns only so you can see the idea. In a real embedding the numbers have no names, and the meaning is spread across all of them together.

3. Why Do We Need It?

In RAG we have many chunks, and for every question we must find the chunk that answers it. The first idea is to match words. But people say the same thing in different words, so matching words picks the wrong chunk. An embedding lets the computer compare meaning instead.

The question
"How much money did we earn?"
Chunk in the PDFWords in common
with the question
Close in meaning?
"The total income was 4.2 lakh."noneyes, it answers the question
"The money transfer was done on Monday."“money”no, a different topic
"Paris is the capital of France."noneno
  • Matching words picks the wrong chunk. The best answer shares no word with the question. The wrong chunk shares the word "money".
  • Matching meaning picks the right chunk. "earn" and "income" are about the same thing, even though the letters are different.
  • An embedding makes this possible. Every chunk and the question become lists of numbers, and the lists with a similar meaning are close to each other. The computer can measure that.

4. Who Makes It?

You do not make embeddings yourself. Companies and open-source projects build embedding models, and you use one of them.

Embedding modelMade byHow you use itSize of its vector
(fixed for the model)
all-MiniLM-L6-v2open sourceon your computer384
mistral-embedMistral AIAPI1,024
text-embedding-3-smallOpenAIAPI1,536
text-embedding-3-largeOpenAIAPI3,072
Through an API
OpenAI, Mistral, Google
You send the text over the internet and get the vector back. You need an API key, and you pay per use.
On your own computer
Open-source models
You download the model and run it yourself. It is free to use, and your text stays on your machine.
  • Every model has its own embedding table, with its own fixed size. That size is the number of columns in the table, and the usual length of the vectors the model gives. You cannot change the table.
  • You choose a model and use its default size. Some models let you ask for a shorter vector at the end. That saves space and search time but keeps less detail.
  • Use the same embedding model for the chunks and for the question. Numbers from two different models do not match. This is only for retrieving. After we get the chunk text back, the LLM that writes the answer can be a different model, because it reads text, not vectors.