Lesson 11: Retrieval-Augmented Generation (RAG)
A language model on its own has two problems: it cannot know anything after its training data ends, and it cannot show you where its answers came from. Retrieval-augmented generation — RAG — fixes both, by giving the model something to look at before it answers. Instead of asking the model a question from memory alone, a RAG system first searches a collection of documents for the passages most relevant to the question, then hands those passages to the model along with the question, and asks it to answer using them. The model still generates the answer, but now it is grounded in real, checkable sources.
The idea was introduced in a 2020 paper by Patrick Lewis and colleagues, who combined a language model with a searchable index of Wikipedia. The system worked in two phases. First, at index time, documents are split into short chunks and each chunk is turned into an embedding and stored, ready to search — exactly the embedding idea from Lesson 6. Then, at question time, the question is turned into an embedding too, the closest chunks are retrieved, and the model receives the question plus those chunks, with an instruction such as 'answer using only the passages below'. The name describes the recipe: retrieve, then augment the prompt, then generate.
Why RAG helps — and where it still fails
RAG helps in three ways. It lets a model answer about information it was never trained on — a company's own manuals, a school's own rules, today's news — without retraining. It reduces hallucination, because the model has real text in front of it instead of guessing from memory. And it makes answers checkable: the system can show the passages it used, so a person can verify the answer against the source. For most real applications — a support chatbot that answers from a company's documents, a study helper that answers from a textbook — RAG is the single most useful technique in this course.
But RAG is not a guarantee. The retrieval step can find the wrong passages, especially if the question uses different words from the documents, or if the answer is spread across several chunks. The model can also ignore the passages and answer from memory anyway, or mix a real fact from a passage with an invented one. And if the documents themselves are wrong or out of date, the model will faithfully repeat their errors. RAG moves the problem from 'the model invents facts' to 'did we retrieve the right facts, and did the model use them?' — which is progress, but still needs a human checking the result.
Hybrid search: the best of both kinds
Semantic search is powerful, but it has a weakness that keyword search does not: it is bad at exact matches. If you search for a part number, a person's name, or a specific error code, the words matter exactly, and a semantic search may return documents that are similar in meaning but contain none of those exact terms. Keyword search has the opposite weakness: it finds exact words but misses different words that mean the same thing. The practical answer is hybrid search — run both, and combine the results. A question about 'resetting your login credentials' is answered by meaning; a question about 'error code 0x80070005' is answered by exact match. Most serious retrieval systems do this, and some add a third step called reranking: take the best candidates from both searches and score them again, more carefully, before handing them to the model. The lesson is a general one for this course: the best systems are rarely one clever technique, but several simple ones combined, each covering the others' weaknesses.
The practical lesson is to treat RAG as a system with three parts to get right: the documents, the retrieval, and the generation. Keep the documents current and clean. Split them into chunks that each hold one idea. Test the retrieval separately — ask questions and check whether the right passages come back. And always show the sources to the user, so the answer can be checked. A RAG system that hides its sources is barely better than a model answering from memory.
How RAG answers a question
Retrieval finds the most relevant chunks; the model then answers using those passages, and the answer is shown together with its sources.
Key Terms
- Retrieval-augmented generation (RAG)
- A technique that searches documents for passages relevant to a question, then gives those passages to a language model so it answers from them.
- Retrieval
- The search step in RAG: finding the document chunks whose embeddings are closest to the question.
- Index time
- The preparation phase of RAG, when documents are split into chunks, embedded and stored for searching.
- Question time
- The moment a user asks a question, when retrieval runs and the model generates an answer from the retrieved passages.
- Grounding
- Basing a model's answer on real, checkable sources rather than on its memory alone.
- Chunk
- A short piece cut from a document before embedding, so each embedding captures one clear topic.
Discussion Questions
- A company's support chatbot answers from memory and invents policies. How would RAG change what it does, and what would still need checking?
- Why is showing the sources to the user so important in a RAG system?
- If the company's documents are out of date, what will the RAG system do — and whose fault is the wrong answer?
Practice Questions
- What problem does RAG solve?
- What are the two phases of RAG?
- How does RAG reduce hallucination?
- Why is it important to test retrieval separately from generation?
- What happens if the documents in a RAG system are wrong?
Show answers
- A model cannot know things after its training data ends, and cannot show where its answers came from. RAG gives the model passages to answer from, so it can use current, checkable information.
- Index time and question time. At index time documents are chunked, embedded and stored; at question time the closest chunks are retrieved and the model answers from them.
- The model has real text in front of it instead of guessing from memory. It still can ignore the passages or mix in invented facts, so it reduces hallucination rather than removing it.
- Because the model can only answer well if the right passages were retrieved. If retrieval finds the wrong chunks, even a perfect model will answer from the wrong information.
- The model will faithfully repeat their errors. RAG grounds answers in the documents, so the documents themselves must be current and correct.