Gradient LabStart the lesson

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 lesson

Step by step

Story

Finding the right page

Pip needs to find the right page among 3,000 policy documents, by meaning.

Concept

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.

Build

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.

Key idea

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.

Insight

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.

Real life

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…

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 project

In the news

Recent models and papers that build on this idea: