Retrieval-Augmented Generation
Look it up, then answer: the three passages nearest the question are taken from an index and handed to the model with it, which answers from them and marks its sources.
Scroll
What a Model Cannot Know
What a language model knows is stored in its parameters, and it stops changing when training ends. It has not seen what happened since, and it cannot point to where it read what it says. The 2020 paper that proposed retrieval-augmented generation puts it this way: such models cannot easily expand or revise their memory, cannot say where their predictions come from, and may produce “hallucinations”.
Putting the right text in front of a model makes a large difference. In a 2023 test, GPT-3.5-Turbo answered the same set of questions twice: given no documents, it got 56.1% right; given only the one document that holds the answer, 88.3%.
GPT-3.5-Turbo, one set of questions · no documents: 56.1% · only the answering document: 88.3%
Look It Up, Then Answer
Retrieval-augmented generation answers in two steps. Look it up: the question becomes a list of numbers, and the passages nearest to it are found in an index computed beforehand. Then answer: those passages and the question go to a language model together, and it writes from them. On the plinth, the card catalogue on the left is the index, the stand in the middle is the prompt, and the teleprinter on the right is the model.
The name comes from a 2020 paper. Its index was the English Wikipedia of December 2018, cut into passages of 100 words, about 21 million of them; in training, each question took the nearest 5 or 10; the answers were written by BART-large, a model with 400 million parameters.
The 2020 index: about 21 million passages of 100 words · 5 or 10 taken each time · the writer: BART-large, 400 million parameters
The Question Becomes Numbers Too
How do we know which passage is near the question? An encoder model first turns each passage into a list of numbers, a direction; texts that mean alike point in similar directions. The question is turned into a direction by the same model. How alike two texts are is the cosine of the angle between them: cos θ = a·b / (|a||b|), and the closer to 1, the more alike. Vectors and angles are told at length in “Embeddings: The Geometry of Meaning”.
An example with only three numbers: a = (3, 1, 0), b = (2, 2, 1), c = (0, 1, 3). The cosine of a and b is 8 / (3.16 × 3) = 0.84, an angle of 33°; a and c have only 0.10, an angle of 84°. The model in this page’s lab gives each text 384 numbers: “Why is the sky blue?” and the passage on the colour of the sky have a cosine of 0.75, an angle of 41°.
cos θ = a·b / (|a||b|) · a, b: 0.84, 33° · a, c: 0.10, 84° · this page’s model: 384 numbers a text
Finding the Nearest
Every passage’s vector in the index is computed beforehand and stored; a question needs only its own vector, compared against every passage, and the top k most alike are taken. Dense passage retrieval (DPR) in 2020 used two BERT encoders, 768 numbers a passage: the vectors for 21 million passages took about 8.8 hours on 8 GPUs, and building the FAISS index took 8.5 hours on one server; once built, it fetched 100 passages for each of 995 questions a second.
Comparing tens of millions of passages one by one is too slow, so in practice the search is approximate (the 2020 retrieval-augmented model used a layered graph of neighbours in FAISS): it compares only the most promising few, far faster, but with no promise that what it finds are exactly the nearest.
DPR’s index: 21 million passages, 768 numbers each · about 8.8 hours to embed (8 GPUs) · 8.5 hours to index · 995 questions a second
Into the Prompt
The passages found and the question are joined and handed to the model that writes the answer; the paper’s words are “we simply concatenate them”. The model has not memorised these texts: for every question it reads the passages put before it again. With each passage numbered in the prompt, the answer can be asked to say which passage it rests on, so a reader can go back and check. The answer typed on the plinth is written by this page, to show the form.
In the paper, training adjusts only the question’s encoder and the writer; the passages’ vectors and the index stay fixed, and 626 million parameters are trained in all. On Natural Questions it got 44.5% exactly right; T5-11B, which looks nothing up and has 11 billion parameters, got 34.5%.
Natural Questions, exact answers · T5-11B (no lookup) 34.5% · REALM 40.4% · DPR 41.5% · RAG 44.5%
A New Set of Cards
To make it know new things, there is no need to train it again: swap the index. The paper tested 82 world leaders, asking who holds each post: with an index made from the 2016 Wikipedia, asked about the leaders of 2016, it got 70% right; with the 2018 index and the leaders of 2018, 68%.
With the index and the question out of step, it fails: the 2018 index asked about 2016 leaders, 12%; the 2016 index asked about 2018 leaders, 4%. The answers are as new as the index: given an old index, the model answers from the old cards just the same.
82 leaders · matching years: 70% (2016), 68% (2018) · mismatched: 12%, 4%
By Meaning, Not Just by Words
Before vector search, the usual way was to score keywords, as BM25 does: the more often a passage holds the question’s words, and the rarer those words, the higher it scores. DPR in 2020 ran both ways over the same 21 million passages, took 20 from each, and checked whether any held the answer: on Natural Questions, BM25 59.1%, vector search 78.4%.
Of five data sets, vector search did better on four; on SQuAD it was the other way round, 68.8% against 63.2%. The authors conjecture that SQuAD’s questions were written by people looking at the passage, so they share many words with it, which is where keywords are strong. The paper adds that keyword matching is sensitive to rare names and phrases, while vector search better catches the same thing said in other words.
Top 20, at least one holds the answer · NQ: BM25 59.1%, DPR 78.4% · SQuAD: 68.8% against 63.2%
An Index of Two Trillion Tokens
Retrieval can also go into every step of writing. RETRO in 2022 cut text into chunks of 64 tokens and, for each chunk it wrote, looked up that chunk’s neighbours in a database; at evaluation the database held 1.75 trillion tokens, which the paper rounds to “2 trillion”, and a query took about 10 ms.
On the Pile, a set of language-modelling tests, the 7.5-billion-parameter RETRO did about as well as GPT-3 and Jurassic-1 with 25 times fewer parameters, and beat the 178-billion-parameter Jurassic-1 and the 280-billion-parameter Gopher on a majority of the subsets. What is compared is how well it predicts the text that follows, not how well it answers questions; on the subsets of maths problems and Ubuntu chat logs it did worse than a model of its own size that looks nothing up. Some of the test text is in the database itself, and the paper says RETRO makes more use of that overlap.
RETRO · 7.5 billion parameters · evaluation database of 1.75 trillion tokens · the Pile: ahead of Jurassic-1 (178 billion) and Gopher (280 billion) on most subsets
Wrong Cards, an Answer Anyway
An index always hands back k passages, even when none is to the point. Taking 20 passages for each question on Natural Questions, DPR still had 21.6% of questions where none of the 20 held the answer. In this page’s lab, “Who won the World Cup in 2022?” finds as its nearest of 16 passages one with a cosine of only 0.08, about a compass.
Near is not the same as right. For “How far away is the Moon?” the nearest is the passage on the tides (0.43): it has the Moon in it, but no distance. For the first picture of a black hole, second comes the passage on gravitational waves: also a “first”, also black holes, but the year is 2015. A cosine measures whether the wording and meaning are alike; it does not check what is true. When no passage holds the answer, the model may still answer from the memory in its parameters: in the paper, 11.8% of such questions were answered right, but such an answer has no source.
DPR, NQ, top 20: 21.6% of questions with no passage holding the answer · this page: nearest passage for the World Cup question 0.08
Lost in the Middle
Put into the prompt, a passage may still go unused. A 2023 test gave GPT-3.5-Turbo 20 documents (about 4,000 tokens), only one of which held the answer, and moved that one around: as the 1st document, 75.8% right; the 10th, 53.8%; the 20th, 63.2%. Good at both ends and worst in the middle: a U.
The 53.8% at the 10th place is lower than the 56.1% with no documents at all: with the answer in the prompt, the model did worse. Taking more passages helps little: beyond 20 documents, GPT-3.5-Turbo got only about 1.5% more right, while the prompt grew much longer, slower and dearer.
GPT-3.5-Turbo, 20 documents · answer 1st, 5th, 10th, 15th, 20th: 75.8%, 57.2%, 53.8%, 55.4%, 63.2% · no documents: 56.1%
Taken Apart
On the left, the index: an oak card catalogue of 8 × 12 drawers, each with a brass label frame, a cup pull and a rod knob below; sixteen of the drawers hold the lab’s sixteen passages. The brass letterbox on its top takes the question card. In the middle, the prompt stand, a typist’s copy holder leaning back: the question clipped at its top, the cards taken out standing on its ledge, under each a small gauge whose needle shows its cosine with the question. On the right, the teleprinter that writes the answer: its keyboard in front, its paper roll and platen behind, the answer coming out of its top. A person stands behind, for scale.
Index · 16 drawers · letterbox · prompt stand · five gauges · teleprinter · answer
The Bench
The bench holds 16 short passages this page wrote about some of the site’s plates, and 7 questions, two of which have no answer in the 16. For the question chosen, the browser works out its cosine with each passage from vectors computed beforehand (a small multilingual encoder model, 384 numbers a text), ranks them, and puts the top k on the prompt stand.
No language model is called here; only the lookup is done, and what shows is what would be handed to a model. The English page uses the English texts’ vectors and the Chinese page the Chinese ones; the same question can rank the passages differently in the two languages.
This page’s model: 384 numbers a text · 16 passages · 7 questions · every cosine worked out in the browser
Try thisIn the lab, ask “How far away is the Moon?” with k set to 3: which three passages go on the stand? What is the nearest one’s cosine? Does any of the three hold the answer?
What the model leaves out
The card catalogue, drawers, prompt stand and teleprinter are this page’s picture of the method, not parts of any system: a real index is a table of vectors, and the writer is a language model. The passages in sixteen of the drawers and their cosines are real, worked out with the lab’s small model; the other 80 drawers are there to make the index look large. The answer on the teleprinter is written by this page, to show the form; no language model ran. The dark disc is a diagram: each passage sits at a distance from the middle equal to its angle to the question, and its direction only spreads the points out.
Easy to get wrong
Retrieval-augmented generation does not make a model learn the texts: for every answer it reads again the passages put into the prompt. Vectors being near means the wording or meaning is alike, not that the content is right. An index always returns k passages, even when none is relevant. DPR’s 78.4% is the share of questions with at least one answering passage in the top 20, not the share answered right. RETRO’s comparison is language modelling on the Pile, not question answering; “2 trillion” is the paper’s round figure, and the evaluation database held 1.75 trillion tokens.
Common questions · 5
- What is retrieval-augmented generation?
Look it up: the question becomes a list of numbers, and the passages nearest to it are found in an index computed beforehand. Then answer: those passages and the question go to a language model together, and it writes from them.
02Look It Up, Then Answer ↑- Why have a language model look things up first?
Putting the right text in front of a model makes a large difference. In a 2023 test, GPT-3.5-Turbo answered the same set of questions twice: given no documents, it got 56.1% right; given only the one document that holds the answer, 88.3%.
01What a Model Cannot Know ↑- Does retrieval-augmented generation make the model learn the documents?
The model has not memorised these texts: for every question it reads the passages put before it again.
05Into the Prompt ↑- Is vector search always better than keyword search?
Of five data sets, vector search did better on four; on SQuAD it was the other way round, 68.8% against 63.2%.
07By Meaning, Not Just by Words ↑- Does a model use every passage put into its prompt?
A 2023 test gave GPT-3.5-Turbo 20 documents (about 4,000 tokens), only one of which held the answer, and moved that one around: as the 1st document, 75.8% right; the 10th, 53.8%; the 20th, 63.2%.
10Lost in the Middle ↑
Sources
- P. Lewis et al. (2020): Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks, NeurIPS 2020, arXiv:2005.11401 (parametric and non-parametric memory; the December 2018 Wikipedia in 100-word chunks, about 21 million; FAISS with HNSW; k of 5 or 10; BART-large, 400 million parameters; “we simply concatenate them”; 626 million trainable parameters against T5-11B’s 11 billion; Table 1: 34.5, 40.4, 41.5, 44.5; swapping the index: 70%, 68%, 12%, 4%; 11.8% when no retrieved passage holds the answer)
- V. Karpukhin et al. (2020): Dense Passage Retrieval for Open-Domain Question Answering, EMNLP 2020, arXiv:2004.04906 (two BERT encoders, d = 768; 21,015,324 passages; Table 2: top-20 retrieval accuracy, BM25 against DPR; the SQuAD exception; 8.8 hours to embed, 8.5 hours to build the index, about 30 minutes for an inverted index; 995 questions a second)
- K. Guu et al. (2020): REALM: Retrieval-Augmented Language Model Pre-Training, ICML 2020, arXiv:2002.08909 (retrieval during pre-training; just over 13 million chunks; NQ 40.4)
- S. Borgeaud et al. (2022): Improving language models by retrieving from trillions of tokens (RETRO), ICML 2022, arXiv:2112.04426 (a 2-trillion-token database, 1.75 trillion at evaluation; 64-token chunks; SCaNN, about 10 ms; 7.5 billion parameters; comparable to GPT-3 and Jurassic-1 on the Pile with 25× fewer parameters; ahead of Jurassic-1 and Gopher on a majority of subsets; dm_mathematics and ubuntu_irc the exceptions; test-set leakage)
- N. F. Liu et al. (2024): Lost in the Middle: How Language Models Use Long Contexts, TACL, arXiv:2307.03172 (20 documents, the answer at position 1, 5, 10, 15, 20: 75.8%, 57.2%, 53.8%, 55.4%, 63.2%; closed book 56.1%, the one answering document alone 88.3%; beyond 20 documents only about 1.5% more)
- N. Reimers and I. Gurevych: paraphrase-multilingual-MiniLM-L12-v2, sentence-transformers, Apache-2.0 (384 numbers a text, the mean of its token vectors)
Data
Retrieval-augmented generation after P. Lewis, E. Perez, A. Piktus, F. Petroni, V. Karpukhin, N. Goyal, H. Küttler, M. Lewis, W. Yih, T. Rocktäschel, S. Riedel and D. Kiela, “Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks” (NeurIPS 2020, arXiv:2005.11401): §1–§4, Tables 1 and 6, App. G. Dense passage retrieval after V. Karpukhin, B. Oğuz, S. Min, P. Lewis, L. Wu, S. Edunov, D. Chen and W. Yih (EMNLP 2020, arXiv:2004.04906): §3, Table 2, §5. REALM after K. Guu, K. Lee, Z. Tung, P. Pasupat and M. Chang (ICML 2020, arXiv:2002.08909). RETRO after S. Borgeaud, A. Mensch, J. Hoffmann et al. (ICML 2022, arXiv:2112.04426): §2, §4. “Lost in the middle” after N. F. Liu, K. Lin, J. Hewitt, A. Paranjape, M. Bevilacqua, F. Petroni and P. Liang (TACL 2024, arXiv:2307.03172): Tables 1 and 6, §5. The lab’s sixteen passages and seven questions are this page’s own text about the site’s plates. Their vectors are from paraphrase-multilingual-MiniLM-L12-v2, by N. Reimers and I. Gurevych (sentence-transformers, Apache-2.0), computed by this page’s script (docs/plates/rag/computed.py) and stored as 8-bit numbers; every cosine is computed in the browser. The catalogue, the copy holder and the teleprinter are this page’s picture of the method, not parts of any system; the typed answer is written by this page, not by a model. Oak from the site’s CC0 wood scan; material presets and the model kit as on the site’s other model plates (public/looks/CREDITS.md).
The text, pictures and models on this page are licensed CC BY-NC 4.0: free to use and adapt for teaching and other non-commercial purposes, with credit to world.lapla.si. Third-party material listed above keeps its own licence.
Cite this plate
Laplace’s Mini World, Plate F78, “Retrieval-Augmented Generation”, https://world.lapla.si/en/rag/, CC BY-NC 4.0, updated 2026-11-06.

