1. The Goal
We have a chunk of text, "the river bank". We want one vector for it. Who turns the chunk into a vector?
2. Inside the Embedding Model: Four Steps
The embedding model is already made by a company. We are not building it. We only look inside to see how it works. It does four steps, one after another.
318, 12456, 61903. Step 1: The Tokenizer Cuts the Text
The first step cuts the chunk into small pieces, and gives each piece an ID.
- The tokenizer is a normal program. It is not a neural network. It has a fixed list of pieces and cuts the text using it.
- Pieces are not always whole words. A rare word can be split into several pieces. That is how the model can handle any word.
- The ID is a row number. In the next step we use it to find a row in the embedding table.
- A model needs its own tokenizer. Its IDs match the rows of its own embedding table.
4. Step 2: The Embedding Table Gives the Rows
The second step looks up one row of numbers for each ID in the embedding table.
| col 1 | col 2 | col 3 | |
|---|---|---|---|
| the | 0.50 | 0.40 | 1.60 |
| river | 2.80 | 0.40 | 0.50 |
| bank | 1.60 | 1.40 | 0.30 |
5. Step 3: Attention Uses the Context
Our rows are generic. Attention changes them using the other words in the sentence.
Where we are. After steps 1 and 2 we have three generic rows. "bank" has the same row in every sentence, so it does not know that this one is about a river.
| col 1 | col 2 | col 3 | |
|---|---|---|---|
| the | 0.50 | 0.40 | 1.60 |
| river | 2.80 | 0.40 | 0.50 |
| bank | 1.60 | 1.40 | 0.30 |
DefinitionAttention lets each word look at the other words in the sentence and take a share of their meaning, so that its row fits the sentence.
We do it for "bank" in three steps:
Step A: compare. Multiply the matching numbers of two rows and add them up (a dot product). A bigger score means the rows are more alike.
| "bank" with | Multiply the matching numbers, and add | Score |
|---|---|---|
| the | 1.6×0.5 + 1.4×0.4 + 0.3×1.6 | 1.84 |
| river | 1.6×2.8 + 1.4×0.4 + 0.3×0.5 | 5.19 |
| bank | 1.6×1.6 + 1.4×1.4 + 0.3×0.3 | 4.61 |
Step B: make shares. Softmax: raise e (about 2.718) to the power of each score, then divide each by the total. The shares add up to 1.
| Word | Score | e to the power of the score | Share = that number ÷ 286.2 |
|---|---|---|---|
| the | 1.84 | 6.3 | 0.022 (2.2%) |
| river | 5.19 | 179.5 | 0.627 (62.7%) |
| bank | 4.61 | 100.5 | 0.351 (35.1%) |
| Total | 286.2 | 1.000 (100%) |
Step C: mix. Multiply each word's row by its share, then add the three results.
| Word | Share | Its row | Share × its row |
|---|---|---|---|
| the | 0.022 | [0.5, 0.4, 1.6] | [0.011, 0.009, 0.035] |
| river | 0.627 | [2.8, 0.4, 0.5] | [1.756, 0.251, 0.313] |
| bank | 0.351 | [1.6, 1.4, 0.3] | [0.562, 0.491, 0.105] |
| Add them up | [2.328, 0.751, 0.454] |
In "the money bank" the same steps use the "money" row, [0.3, 2.7, 0.6], instead of "river". The scores become 1.84, 4.44 and 4.61, and the new "bank" row is [0.989, 1.942, 0.475]: it moves toward "money" (closeness 0.74 to 0.94). The embedding table does not change. Only the copies change, for this sentence.
It is called attention because each word pays attention to the other words and decides how much each one matters. A real model does this for every word at once, and repeats it in many layers.
6. Step 4: Average and Normalize
The last step turns many rows into one vector.
Where we are. After attention we have three rows, one for each word. We need one vector for the whole chunk, so we average them and then normalize. Three steps:
Step A: average. Add the three rows column by column, then divide by 3.
| col 1 | col 2 | col 3 | |
|---|---|---|---|
| the | 1.360 | 0.573 | 1.055 |
| river | 2.740 | 0.445 | 0.494 |
| bank | 2.328 | 0.751 | 0.454 |
| Add the three rows | 6.428 | 1.769 | 2.003 |
| Divide by 3 (the average) | 2.143 | 0.590 | 0.668 |
The last line is one row of numbers for the whole chunk.
Step B and Step C: normalize. Normalizing means scaling the row so its magnitude becomes 1, without changing its direction. The magnitude is the length of the row as an arrow: square each number, add the squares, take the square root (Step B). Then divide every number by it (Step C).
| What we do | col 1 | col 2 | col 3 | All three |
|---|---|---|---|---|
| The average row | 2.143 | 0.590 | 0.668 | |
| Square each number | 4.592 | 0.348 | 0.446 | add them: 5.385 |
| Square root of that sum = the magnitude | 2.321 | |||
| Divide each number by 2.321 = the embedding | 0.923 | 0.254 | 0.288 |
This final row, [0.923, 0.254, 0.288], is the embedding of "the river bank". Its magnitude is 1.
Why divide by the magnitude? A long chunk and a short chunk about the same topic give arrows of different lengths, but the same direction. The direction carries the meaning. Dividing by the magnitude keeps only the direction, so comparisons between vectors are fair. Here is the idea with a small example.
The red arrow is [3, 4], the green arrow is [0.6, 0.8]. They point the same way. Only the length changed, from 5 to 1.
7. Does the Length Carry Meaning?
We divided by the magnitude and kept only the direction. Is the length really worth nothing?
Why do we throw the length away? It depends on how the model was trained. It is not a law of maths that length means nothing. Most text embedding models are trained so that texts with the same meaning point the same way. Only the direction is rewarded, so the model has no reason to put anything useful into the length. The length is what is left over.
What is the difference between [3, 4] and [6, 8]? They point the same way, and the second is twice as long. It depends on the model:
| A model trained on direction (most text embedding models) | A model trained for the dot product (a few models) | |
|---|---|---|
| What the length means | Nothing useful. It can come from things like the length of the text, or how the words were averaged. | It can carry a signal, such as how important or popular something is. |
| Are [3, 4] and [6, 8] the same? | Yes, the same meaning. | No. The longer one can score higher for every question. |
| What we do | Normalize. Compare by direction (cosine). | Do not normalize. Use the metric the docs say, the dot product. |
The exception is the second column. The dot product uses the angle and the length together, so a model trained for it can use the length. If you used cosine on such a model, or normalized its vectors, you would throw that signal away.
What this means for us. For the models in this course, the direction is the meaning. Many text embedding models already return vectors of length 1, or normalize inside, so this step costs nothing. Check your model's docs for which metric it was trained for. If it says dot product, follow it.
8. In Code
One call does all four steps for you. Which model you call depends on your app.
Which embedding model do I use? It depends on the provider of your app. Every provider has its own LangChain class. You change only the import and the model name, because all of them have the same two methods: embed_query for one text and embed_documents for a list of chunks. The code below is the one from your file.
pip install langchain-mistralailangchain-openailangchain-google-genailangchain-huggingface sentence-transformersfrom langchain_mistralai import MistralAIEmbeddings embedding_model = MistralAIEmbeddings(model="mistral-embed") query = "What is the capital of France?" query_vec = embedding_model.embed_query(query) print(f"Vector Dimensions: {len(query_vec)}") print(f"Sample First 5 Floats: {query_vec[:5]}")from langchain_openai import OpenAIEmbeddings embedding_model = OpenAIEmbeddings(model="text-embedding-3-small") query = "What is the capital of France?" query_vec = embedding_model.embed_query(query) print(f"Vector Dimensions: {len(query_vec)}") print(f"Sample First 5 Floats: {query_vec[:5]}")from langchain_google_genai import GoogleGenerativeAIEmbeddings embedding_model = GoogleGenerativeAIEmbeddings(model="gemini-embedding-001") query = "What is the capital of France?" query_vec = embedding_model.embed_query(query) print(f"Vector Dimensions: {len(query_vec)}") print(f"Sample First 5 Floats: {query_vec[:5]}")from langchain_huggingface import HuggingFaceEmbeddings embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2") query = "What is the capital of France?" query_vec = embedding_model.embed_query(query) print(f"Vector Dimensions: {len(query_vec)}") print(f"Sample First 5 Floats: {query_vec[:5]}")
Vector Dimensions: 1024 Sample First 5 Floats: [-0.0069, -0.053, 0.040, 0.218, -0.031]Vector Dimensions: 1536 Sample First 5 Floats: [0.012, -0.034, 0.057, -0.008, 0.021]Vector Dimensions: 3072 Sample First 5 Floats: [0.004, -0.021, 0.018, 0.033, -0.009]Vector Dimensions: 384 Sample First 5 Floats: [-0.045, 0.012, 0.087, -0.003, 0.031]
- Only two lines change from tab to tab: the import and the model line. The query and the two prints stay the same.
- Import the embedding class from the provider's own package:
langchain_mistralailangchain_openailangchain_google_genailangchain_huggingface. Create the model with the class MistralAIEmbeddingsOpenAIEmbeddingsGoogleGenerativeAIEmbeddingsHuggingFaceEmbeddings, and put the model name inside the brackets. - The size of the vector depends on the model: 1,0241,5363,072384 numbers here.
- Put its API key in your
.envfile. This is the one used in this course.Put its API key in your.envfile. Put its API key in your.envfile. No API key is needed. The model is downloaded the first time you run it.
- The embedding model and the chat model are separate choices. You can embed with Mistral and answer with Gemini. Pick the embedding model by quality, cost and size, and the chat model by what you want to say.
- Use the same embedding model for the chunks and the question. The vectors of two models do not match.
- The vector database must match the size. A collection fixed at 1,024 numbers cannot take a 1,536-number vector. If you change the model, embed all the chunks again into a new collection.
- Some models let you shorten the vector. OpenAI has a
dimensionssetting, and Google hasoutput_dimensionality. A shorter vector saves space but keeps less detail. - Model names and sizes change. These are examples, so check the provider's docs for the current names.
The output numbers are examples. Yours will differ, but the size is fixed for each model.