RAG from the ground up
Retrieval that actually finds the answer
- Intermediate
- 10 chapters
- 161 min read
Retrieval-augmented generation built from its parts: chunking, embeddings, cosine similarity, a vector store, hybrid search, reranking, a grounded prompt and the metrics that tell you which stage is failing. Every concept comes with a runnable example written in plain Python.
What You'll Learn
- Explain what retrieval changes about a model request
- Split documents into chunks that can be found and cited
- Compute and compare embeddings with cosine similarity
- Build a vector store with top-k search and metadata filters
- Combine lexical and semantic retrieval, then rerank the shortlist
- Write a grounded prompt that cites and refuses
- Measure recall, MRR and nDCG over a labelled set
- Assemble an end-to-end pipeline you can evaluate
Prerequisites
- Comfortable with Python functions, lists and dictionaries
- The LangChain course is helpful but not required
- No API key is needed — the examples use hand-built vectors
Vocabulary
The terms the chapters assume, in plain words.
- BM25
- A word-matching score that ranks a document higher when it contains rare query words, with a cap on repetition.
- chunk
- A short passage cut from a longer document, small enough to retrieve and cite on its own.
- citation
- A pointer from an answer back to the exact passage it came from, so a reader can check it.
- context window
- The fixed amount of text a model can read in one request, counting the question and everything retrieved.
- corpus
- The whole collection of documents a system searches.
- cosine similarity
- A score from -1 to 1 for how closely two arrows point in the same direction, ignoring their lengths.
- dot product
- Multiply matching numbers in two lists and add the results; a larger total means closer agreement.
- embedding
- A list of numbers that places a piece of text in a space where similar meanings sit close together.
- grounded answer
- A reply built only from the passages that were retrieved, so its claims can be traced to them.
- index
- A structure built ahead of time so the system can find relevant passages without reading everything.
- lexical search
- Finding passages by the words they contain, matching the query's terms rather than its meaning.
- magnitude
- The length of a vector, found by squaring its numbers, adding them and taking the square root.
- reranking
- Scoring a short list of results again with a slower, more careful method and putting the best first.
- retrieval
- The step that selects passages from the corpus to put in front of the model.
- top-k
- The shortlist a search returns: its best few results, cut off at a number chosen in advance.
- vector
- An ordered list of numbers, where each position is one measured property.
Course Content
- 01Why retrievalNot started
A model knows what it was trained on. Retrieval is how it answers about your documents, your data and this morning — without pretending it remembers.
- 02Documents and chunksNot started
Retrieval returns chunks, not documents. How you cut the text decides what can be found, what a citation points at, and whether an answer is missing its first half.
- 03EmbeddingsNot started
An embedding turns text into a list of numbers that places it in a space where “similar meaning” means “nearby”. Everything about vector search follows from that one idea.
- 04A vector storeNot started
A vector store is three operations: add vectors with metadata, find the nearest ones to a query, and filter before you rank. Write one in thirty lines and the hosted ones stop being mysterious.
- 05Better retrievalNot started
Vectors miss exact terms; keywords miss synonyms. Combine them, then reorder the shortlist with a model that reads the query and the passage together.
- 06The RAG promptNot started
Four rules turn retrieved passages into a trustworthy answer: answer only from the context, cite what you used, refuse when it is missing, and treat every passage as data.
- 07Evaluating retrievalNot started
You cannot improve what you do not measure. Recall finds out whether the right passage was even fetched; MRR tells you how high it ranked. Together they explain most bad answers.
- 08Tables and multimodal documentsNot started
A table is not prose and a scan is not text. The extraction step decides whether a cell keeps its column, whether a figure keeps its caption, and whether a number can be found at all.
- 09Keeping the index freshNot started
An index is a snapshot, and a snapshot goes stale. Incremental sync, stable ids and an atomic swap are what keep answers current without rebuilding everything every night.
- 10Capstone: RAG end to endNot started
One corpus, from splitting to a cited answer, with no external services. Every stage the course covered appears once, in the order a real pipeline runs them.
