Module 2 · AI engineering foundations · Chapter 9 · Free
RAG end to end
What RAG is
Retrieval-augmented generation means: before the model answers, look up relevant passages from your own data and put them in the prompt. The model then writes an answer grounded in those passages. It lets a general model answer from private, current information without retraining it, and it lets you show sources.
- The same pieces from Chapter 8 are already in place: your documents were chunked, embedded and stored with metadata.
- Search by meaning (embeddings) and by exact words (keyword search such as BM25). Combining them is called hybrid search. Meaning finds paraphrases; keywords find exact names, codes and numbers.
- Filter the candidates against the asker's permissions before anything goes near the model. A model cannot leak what it never received.
- A second, more careful model reorders a short list of candidates by how well each answers this exact question. Slower than the first pass, so it runs on only a handful of chunks.
- Rules, the top few passages with their sources, and the question share the context budget from Chapter 7.
- The model answers using the passages and cites them. Instruct it to say "I don't know" when the passages do not contain the answer.
Retrieval lab: break it, then fix it
The question is "How long do Enterprise customers have to request a refund?" The corpus has the current policy, a retired policy, a vague marketing FAQ, and a Legal-only contract excerpt. Start with everything off, then switch stages on one at a time. Try asking as a Support agent and as Legal.
Diagnose the symptom
When a RAG answer is bad, find the stage that failed before you change anything. Match each symptom to the stage to fix.
Check your understanding
Recap
- RAG = retrieve relevant passages, then generate an answer grounded in them, with citations.
- Pipeline: chunk and embed (offline), then retrieve, filter by permission, re-rank, build the prompt, generate.
- Hybrid search fixes missed exact terms. Re-ranking fixes ordering. Permission filtering is the only thing that prevents leaks.
- Debug stage by stage: chunking, retrieval, ranking, permissions, grounding.
Chapter quiz
Four questions. Score 70% or more to mark this chapter complete.