Lessons › Chapter 11: Transformers and LLMs
Embeddings and vector search
Run every document, photo or product description through an embedding model. Each becomes a point. Similar things cluster: beach photos here, city photos there, food over there.
Watch it move. This lesson is interactive on Gradient Lab: every step is an animated scene you can replay.
Start the interactive lessonStep by step
Finding the right page
Pip needs to find the right page among 3,000 policy documents, by meaning.
Turn every document into a point
Run every document, photo or product description through an embedding model. Each becomes a point. Similar things cluster: beach photos here, city photos there, food over there.
Turn the question into a point too
Embed the search query with the same model. “beach sunset photos” lands right among the beach pictures, even if none of them were tagged with those words.
Find the nearest neighbors
vector search returns the stored points closest to the query. That’s search by meaning rather than by matching exact words.
Fast, even with millions
Comparing against every stored item would be slow. Special indexes organize the points so the nearest ones are found in milliseconds, even among hundreds of millions.
In real life: “show me photos of the beach”
Type “beach” into your phone’s photo search and it finds sand and waves, even though you never tagged a single picture. Each photo has an embedding, and your words are matched to them by meaning.
Quick check: Vector search returns documents that…
- Are closest in meaning to the query (correct)
- Contain the exact same words only
- Were added most recently
It compares embeddings, so it finds the nearest meanings, not just word matches.
Pseudocode
for each document:
vector = embed(document) # meaning → numbers
store the vector in an index
query_vector = embed("beach sunset photos")
results = the stored vectors closest to query_vector
# smart indexes find neighbors among millions in milliseconds
Key terms
- vector search
- Finding stored items whose embeddings are closest to a query’s embedding.
Build it: Build a mini search engine (the R in RAG) (30 min, runs in your browser)
See the projectIn the news
Recent models and papers that build on this idea: