Embeddings
Computers cannot compare meanings. They can only compare numbers.
An embedding turns a piece of text into a long list of numbers that captures its meaning.
Think of a map. Every sentence gets coordinates. Sentences with similar meaning land close together on the map.
Make an embedding model
An embedding model is different from a chat model. It does not talk. It only turns text into numbers.
text-embedding-3-small is cheap and good. It is the usual pick for learning and for most apps.
Embed one sentence
embed_query takes one string and returns one vector. A vector is just a list of floats.
1536 numbers for one short sentence. Each number is one coordinate on the meaning map.
The numbers alone mean nothing to you. They only make sense when compared to other vectors.
Embed many texts at once
embed_documents takes a list of strings and returns a list of vectors. Use it for your chunks.
One call, three vectors. This is faster and cheaper than calling embed_query three times.
Close vectors, close meanings
Cosine similarity measures how close two vectors point. 1 means the same direction. 0 means unrelated.
The two fox sentences use different words, but their vectors are close. The Python sentence is far away.
This is the magic. Search by meaning, not by matching words.
A free local option
No API key? Ollama can run an embedding model on your own computer.
Same two methods, embed_query and embed_documents. Only the vector length is different.
Rules to remember
Always use the same embedding model for storing and for searching. Vectors from different models do not mix.
Vectors from different models have different lengths. 1536 for OpenAI small, 768 for nomic.
Embeddings cost money per token, but much less than chat models.
Remember: an embedding is coordinates for meaning. Similar text, nearby points. That is what lets us search by meaning in the next lesson.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.