What is retrieval-augmented generation?
8 min read
The problem RAG solves
A language model learns from text collected before a fixed date. After that date it learns nothing new, and the date is called its Knowledge cutoffThe date at which a model's training text ends. The model knows nothing that happened after it, unless the prompt supplies that information.. The model has also never seen your private files: your returns policy, your contracts, your support tickets.
Ask about either and a model has two bad options. It can say it does not know, or it can write an answer that sounds right and is not. That second failure is called a AI hallucinationAn answer that sounds confident but is false. The model wrote text that fits the pattern instead of text that is true.. The Lesson Why models make things up explains why it happens.
RAG changes the question. Instead of asking the model to remember a fact, the program finds the fact first and puts it in front of the model. Reading a passage and answering from it is something a model does well.
The three steps
- RetrieveSearch the documents and pick the passages that fit the question.
- AugmentAdd those passages to the prompt, next to the question.
- GenerateThe model writes an answer from the passages.
The name follows the steps. Retrieval is the search. Augmented means the prompt is made larger with the passages. Generation is the model writing the answer.
The model is not changed. Its weights stay exactly as they were, and the passages exist only for this one request. When a document changes, you update the search collection. You do not train anything.
A whole RAG program
The program below has a tiny document collection, a search that counts shared words, and a prompt builder. It stops before the model call, so you can see exactly what the model would receive.
documents = {
"returns": "You can return an unused bike within 30 days. The refund goes to the original payment method within 5 business days.",
"shipping": "Standard shipping takes 3 to 5 business days. Express shipping takes 1 to 2 business days.",
"warranty": "The frame has a 5 year warranty. The battery has a 2 year warranty.",
}
def words(text):
return set(text.lower().replace(".", "").replace("?", "").split())
def retrieve(question, top=1):
scored = [(len(words(question) & words(text)), name) for name, text in documents.items()]
scored.sort(reverse=True)
return [name for score, name in scored[:top] if score > 0]
question = "How many days do I have to return a bike?"
sources = "\n".join(f"[{name}] {documents[name]}" for name in retrieve(question))
print(f"Answer using only the sources below. If they do not contain the answer, say so.\n\nSources:\n{sources}\n\nQuestion: {question}")It prints:
Answer using only the sources below. If they do not contain the answer, say so.
Sources:
[returns] You can return an unused bike within 30 days. The refund goes to the original payment method within 5 business days.
Question: How many days do I have to return a bike?Send that text to any language model and the answer, 30 days, is in the prompt. A real system replaces the word count with a better search, and the next Lessons build one. The three steps stay the same.
The instruction matters
The first line of the prompt tells the model to use only the sources, and to say so when they lack the answer. Without it, the model may fill a gap from memory and mix it with your document. The Lesson Citations and honest refusals covers this in detail.
What RAG gives you and what it does not
| Model alone | RAG | |
|---|---|---|
| Where the facts come from | Training data, up to the cutoff | Your documents, at question time |
| Updating a fact | Train again | Change the document |
| Showing a source | Not possible by itself | Show the passage that was used |
| Main risk | An answer that is made up | Search that misses the right passage |
RAG does not make a model correct. If the search returns the wrong passage, the model answers from the wrong passage, and it can sound just as sure. The quality of a RAG system is the quality of its search plus the quality of the answer built from it. Later Lessons measure both.
RAG is also not the only way to teach a model something. If you want to change how a model writes or behaves, not what it knows, fine-tuning fits better. The Lesson RAG or fine-tuning? compares them.
What to read next
The next Lesson, How retrieval works, looks inside step one. After that the path follows the data: preparing documents, searching them, building the prompt, then measuring the result.
What does RAG do before the model answers?
A support bot must answer from this month's returns policy, which the model never saw in training.
RAG finds the facts first and puts them in front of the model. The model's weights do not change.
Which problem does RAG help with most directly?
A model trained last year is asked about a rule that changed last week.
A model knows nothing after its cutoff. Retrieval can supply newer text at question time.
What can still go wrong with RAG?
The retrieved passages are on topic, yet the answer states a fee that is in none of them.
RAG makes grounding possible but does not guarantee it. The answer still needs checking against the sources.
Your reading position is saved
0 of 15 complete
Lesson complete
What is retrieval-augmented generation?
Up next