Papa Programmer
Lesson 01 of 15

What is retrieval-augmented generation?

8 min read

Optional

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

  1. RetrieveSearch the documents and pick the passages that fit the question.
  2. AugmentAdd those passages to the prompt, next to the question.
  3. GenerateThe model writes an answer from the passages.
One RAG request, from question to answer

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.

python
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:

text
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 aloneRAG
Where the facts come fromTraining data, up to the cutoffYour documents, at question time
Updating a factTrain againChange the document
Showing a sourceNot possible by itselfShow the passage that was used
Main riskAn answer that is made upSearch that misses the right passage
The same question, answered with and without RAG

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.

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.

Look it up firstRAG finds the facts, then asks the model to write from them.
Change a document, not the modelUpdating the collection updates later answers, with no training.
Check the search tooA right answer needs the right passage. Test both halves.

Your reading position is saved

0%

0 of 15 complete

Contents

Learning tools