All 29 lessons
Foundations: what RAG is and a working pipeline
Better retrieval: chunking and search
Answers you can trust, measure and debug
Production in a company
Agentic RAG
Frameworks: LangChain, LangGraph and LangSmith
- 24. The RAG toolbox: what each library does and why you import it
- 25. The framework landscape: LangChain, LangGraph, LlamaIndex, Haystack
- 26. Lab: Ask Brightline in LangChain
- 27. Lab: agentic RAG as a LangGraph graph
- 28. Lab: tracing, evaluating and monitoring with LangSmith
- 29. Wrap-up: your production agentic RAG checklist
15 min
How embeddings and vector search find meaning
Rebuild vector search from its parts and explain why similar meaning lands near in vector space.
By the end of this lesson you can
- Compute cosine similarity on small vectors by hand
- Explain what an approximate nearest neighbour index trades for speed
- Say what a vector database adds over a NumPy array
From earlier lessons · 1 of 2
In the lab, why did k=1 miss the alcohol rule?
Pick one answer.
From earlier lessons · 2 of 2
What should you print before reading an answer?
Pick one answer.
An employee types "PTO rollover". The policy never uses the word PTO; it says "paid time off" and "carry over". Vector search still ranks it first. Keyword search would return nothing.
The one line that does it all
In lab 1, one call did all the searching: retrieve("PTO rollover"). It returned hr-pto first. Let's take that call apart and look at each piece's output.
Part 1: an embedding is a position
An embedding model turns text into a fixed-length list of numbers, a vector. It was trained so that texts with similar meaning get vectors that point in similar directions. The space those vectors live in is called vector space.
Our model, all-MiniLM-L6-v2, outputs 384 numbers. Nobody can read them, but you can measure the angle between two of them.
from rag import embedv = embed(["paid time off carries over"])[0]print(len(v), v[:4].round(3))
Part 2: cosine similarity, by hand
Cosine similarity measures the angle between two vectors: 1 means the same direction, 0 means unrelated. Here it is on 3-number toy vectors so you can follow every step.
- "paid time off" is [0.9, 0.1, 0.2].
- "PTO" is [0.85, 0.15, 0.25].
- "laptop" is [0.1, 0.9, 0.3].
The dot product of the first two is 0.9×0.85 + 0.1×0.15 + 0.2×0.25 = 0.83. Their lengths are 0.93 and 0.90, so the cosine is 0.83 ÷ (0.93 × 0.90) ≈ 0.99. For "paid time off" against "laptop", the dot product is 0.24 and the cosine is about 0.27. Same idea, far apart in meaning.
Part 3: finding the nearest vectors
Nearest neighbour search finds the vectors closest to the question's vector. The simplest way scores every one, as the trace shows.
Step through
Brute-force search for the best match
- hotel0i
- meals1
- vpn2
- pto3
- remote4
- device5
Question: a hotel in Chicago. Score every snippet. Start at hotel: 0.66, the best so far.
Brute force is exact, and fine for tens of thousands of chunks. At 10 million chunks of 384 numbers, each question costs about 4 billion multiplications.
An approximate nearest neighbour (ANN) index avoids reading everything. The most common one, HNSW, links each vector to a few close neighbours in layers. A query starts at the top layer and hops greedily toward closer vectors, usually reaching the answer in milliseconds. The trade-off is recall: it can occasionally miss a true nearest neighbour. Most indexes let you raise a search-effort setting to miss less, at some cost in speed.
Part 4: what a vector database adds
A vector database is an ANN index plus the things a company needs around it:
- Persistence and updates. Add, change and delete chunks without rebuilding everything.
- Metadata and filters. Store the source, date and allowed groups on each chunk, and filter by them during search. Lesson 16 depends on this.
- Scale and operations. Replication, backups and access control.
Many teams start with a vector index inside a database they already run, such as Postgres with a vector extension, or with a managed search service.
Match
Match each part to what it does
Pick an item on the left, then its match on the right.
Predict, then reveal
What was the one-line retrieve() hiding? Name one rule you'd break by accident without knowing these parts.
Worth keeping
- An embedding places text by meaning; only comparisons between vectors mean anything.
- Cosine similarity is the angle between vectors; on normalised vectors it's a dot product.
- Brute force is exact; ANN indexes like HNSW trade a little recall for a lot of speed.
- Same model for queries and documents, and re-embed everything when you change it.
Embeddings work on clean text. Real company documents arrive as two-column PDFs, scanned contracts, spreadsheets and slide decks. Next lesson shows what each one breaks, before a single chunk is cut.
Leaderboard
Loading…
50 XP for each lesson you finish, 10 for each right answer. Names show only for learners who turn on Show me on leaderboards.