9. RAG Systems#
Retrieval-augmented generation (RAG) lets a model answer from an organization’s own documents instead of its training data. This chapter builds the RAG pipeline step by step, shows how to assemble good context, and explains how to keep answers grounded in sources.
RAG Pipeline: Indexing, retrieval, and generation work together to answer from organizational knowledge.
Context Assembly: Chunk size, reranking, and the context window decide what the model actually sees.
Grounding: Citations, faithfulness checks, and refusals keep answers tied to sources.
Chapter Video
This explainer walks through how retrieval-augmented generation grounds a model’s answers in an organization’s own documents.
RAG Explained For Beginners (10:09) by KodeKloud
Hosted on YouTube; property of its original creator, embedded here for educational reference only.
Learning Objectives
By the end of this chapter, you will be able to:
Describe the indexing, retrieval, and generation stages of a RAG system.
Choose chunk sizes and overlap for a document collection.
Assemble retrieved chunks into a prompt within a context budget.
Require citations and detect answers that are not supported by sources.
Build a help-desk question-answering tool with citations in Python.
Chapter Flow
Glossary
# |
Term |
Explanation |
|---|---|---|
1 |
Retrieval-augmented generation (RAG) |
A pattern that retrieves relevant documents and gives them to a model to answer from. |
2 |
Knowledge base |
The collection of documents a RAG system answers from. |
3 |
Indexing |
Processing documents into chunks and embeddings ready for retrieval. |
4 |
Retriever |
The component that finds the chunks most relevant to a query. |
5 |
Chunk overlap |
Repeating text at chunk boundaries so ideas are not cut in half. |
6 |
Top-k retrieval |
Returning the k chunks with the highest relevance scores. |
7 |
Reranking |
Reordering retrieved chunks with a more precise relevance model. |
8 |
Context budget |
The share of the context window reserved for retrieved text. |
9 |
Grounding |
Tying a model’s answer to provided sources. |
10 |
Citation |
A reference in an answer that points to its source chunk. |
11 |
Faithfulness |
The degree to which an answer is supported by its sources. |
12 |
Refusal |
A response that declines to answer when sources do not support one. |
13 |
Evidence inventory |
The exact authorized source set supplied for generation. |
14 |
Citation membership |
Whether a cited source was included in that inventory. |
15 |
Answerability |
Whether available evidence resolves the question. |
16 |
Extractive answer |
An answer returning source text rather than newly generated wording. |
17 |
Prefix packing |
Selecting a ranked prefix that fits the available budget. |
18 |
No-answer outcome |
An explicit result for insufficient evidence. |
19 |
Context assembly |
Preparing selected evidence for a model request. |
20 |
Abstention |
Declining to answer when evidence or permission is insufficient. |