Area 02 of 08
RAG
Retrieval-augmented generation: give a model the right documents at the right moment, from parsing and chunking to hybrid search, reranking and measuring retrieval.
Printable cheat sheet for this area
- 032
What RAG is: the whole pipeline
Retrieval-augmented generation (RAG) means finding the most relevant pieces of your own documents for each question and putting them into the prompt, so the model answers from your data instead of from memory.
CoversWhat RAG is and the full pipeline end to end · Long context vs RAG
- 033
Embeddings and cosine similarity
An embedding model turns a piece of text into a vector of numbers that captures its meaning, so texts that mean similar things end up close together, and cosine similarity measures how close two of them are; this is how RAG finds relevant chunks even when the words differ.
CoversEmbeddings · Cosine Similarity · Choosing an embedding model
- 034
Document parsing and ingestion pipelines
Before documents can be searched, they must be turned into clean, structured text (parsing), and a repeatable ingestion pipeline must keep the search index in sync as documents are added, changed and deleted.
CoversDocument Parsing · Ingestion pipelines and keeping the index fresh
- 035
Chunking strategies
Chunking splits documents into pieces small enough to retrieve precisely but complete enough to make sense on their own, and parent-child chunking gets both by searching small pieces while handing the model the larger section around them.
CoversChunking Strategies · Parent-Child Chunking
- 036
Vector databases, HNSW and pgvector
A vector database stores embeddings with their text and metadata and finds the nearest ones to a query in milliseconds, using approximate indexes like HNSW; pgvector adds the same ability to ordinary Postgres, and metadata filters narrow results by things like date, customer or permissions.
CoversVector Databases · HNSW Indexes · pgvector · Metadata Filtering
- 037
BM25 keyword search and hybrid search
BM25 is the classic keyword-ranking formula that finds documents sharing the query's exact words (great for codes, names and rare terms), and hybrid search combines it with vector search so you catch both exact matches and matches by meaning.
CoversBM25 Keyword Search · Hybrid Search
- 038
Reranking
Reranking is a second, more careful pass over the candidates your search returned, using a model that reads the question and each candidate together, so the truly most relevant chunks end up at the top before you send them to the LLM.
- 039
Query rewriting, HyDE and query routing
Users' questions are often vague, follow-ups or phrased very differently from your documents, so you improve retrieval by rewriting the question into a good search query, by searching with a hypothetical answer (HyDE), and by routing each question to the right index or tool.
CoversQuery Rewriting · HyDE · Query routing
- 040
Contextual retrieval
Contextual retrieval fixes chunks that make no sense on their own by having an LLM write a short sentence of context for each chunk (which document, which company, which period) and adding it before embedding and keyword indexing.
- 041
Citations
Citations link each claim in an answer to the exact source passage that supports it, so users can check the answer and your system can automatically catch claims that have no real support.
- 042
GraphRAG
GraphRAG builds a knowledge graph of the entities in your documents (people, teams, products) and how they relate, so the system can answer questions that need connecting facts spread across many documents, or summarising a whole collection, which plain chunk search struggles with.
- 043
Agentic RAG
Agentic RAG lets the model run retrieval itself as a tool, in a loop, deciding what to search for, reading the results, searching again or using other tools until it has enough to answer, instead of doing one fixed search before answering.
- 044
Multimodal RAG: images, tables and PDFs
Multimodal RAG makes charts, tables, diagrams and scanned pages searchable, usually by turning each into good text (markdown tables, captions written by a vision model) or by embedding page images directly, while keeping a link to the original so the answering model and the user can see it.
- 045
Measuring retrieval: Recall@k, MRR and nDCG
Retrieval metrics tell you, for a set of test questions with known relevant documents, how often the right documents are found (Recall@k), how high the first right one appears (MRR), and how good the whole ranking is (nDCG), so you can improve search with numbers instead of guesses.
CoversRecall@k · MRR and nDCG
- 046
Capstone: build a small RAG app from scratch
You build a complete, testable RAG app (ingestion, hybrid retrieval, grounded answers with citations, an HTTP API, tests and an eval gate) that answers questions about a set of company policies, and that later lessons containerise and deploy.