Ask a language model who won a prize last year, or what a company’s policy says, and it answers from what it absorbed while it was trained. That has three familiar costs:
- What it absorbed stops at the date its training data was collected.
- It cannot point to where an answer came from.
- It can produce fluent statements that are wrong.
Lewis et al. [Reference 1, §1][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article list the same three problems for models that keep all their knowledge in their parameters. Such models, they write, “cannot easily expand or revise their memory”, “can’t straightforwardly provide insight into their predictions”, and may produce what they call “hallucinations”.
Retrieval-augmented generation, usually shortened to RAG, is a response to those costs. Before the model writes an answer, a separate component searches a collection of documents for the passages most relevant to the question, and the model writes its answer with those passages in front of it. The collection can be updated without retraining the model, and the passages it used can be shown to the reader.
This article explains how that works. It starts with the intuition, then follows a question through a RAG system step by step, then writes down the few equations that define the method, as Lewis et al. introduced it in 2020 [Reference 1][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article. It then traces where the idea came from, what the original experiments showed, and where the method falls short. By the end you should be able to say what a RAG system is made of, why it is trained the way it is, and what the evidence does and does not support.
Two kinds of memory
A useful picture is the difference between a closed-book and an open-book exam. In a closed-book exam, everything you use must already be in your head. In an open-book exam, you still need to read and write well, but you can look facts up, and you can show where you found them.
A language model on its own is in the closed-book situation. Whatever it has picked up about the world is stored, indirectly, in its parameters: the millions or billions of numbers that training adjusts. There is evidence that this store is substantial. Petroni et al. turned facts such as a person’s place of birth into fill-in-the-blank sentences and asked models to fill in the blank, without any extra training for the task. They found that “without fine-tuning, BERT contains relational knowledge competitive with traditional NLP methods that have some access to oracle knowledge” [Reference 2, Abstract][2] Fabio Petroni, Tim Rocktäschel, Sebastian Riedel et al. (2019). Language Models as Knowledge Bases? Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing and the 9th International Joint Conference on Natural Language Processing (EMNLP-IJCNLP). doi:10.18653/v1/D19-1250. That comparison was with a system that extracts facts from text. It was handed the sentences known to state each test fact and, in its stronger version, told which real person, place or thing each name in them referred to: that help is the “oracle” [Reference 2, §4.3][2] Fabio Petroni, Tim Rocktäschel, Sebastian Riedel et al. (2019). Language Models as Knowledge Bases? Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing and the 9th International Joint Conference on Natural Language Processing (EMNLP-IJCNLP). doi:10.18653/v1/D19-1250. The same study shows the limits of that store: accuracy was high for some kinds of fact and very low for others, it covered only answers that were a single token, a whole word or a common part of one, and it was higher for answers that appear often in the training text [Reference 2, §4.5, §5][2] Fabio Petroni, Tim Rocktäschel, Sebastian Riedel et al. (2019). Language Models as Knowledge Bases? Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing and the 9th International Joint Conference on Natural Language Processing (EMNLP-IJCNLP). doi:10.18653/v1/D19-1250.
Lewis et al. name the two kinds of memory directly. They call the generator’s parameters the parametric memory, and the searchable collection of documents the non-parametric memory [Reference 1, §2.2, §2.3][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article. RAG is an open-book design: it keeps the parametric memory, which supplies language and general knowledge, and adds a non-parametric memory that can be searched, read, inspected and replaced.
Following one question through a RAG system
A RAG system has two trained parts, a retriever and a generator, and one prepared resource, an index of passages. Here is what happens to a question, in the order it happens. The details follow Lewis et al.; later systems differ in many of them.
1. Prepare the collection, once, before any question arrives. Lewis et al. used a snapshot of English Wikipedia from December 2018. “Each Wikipedia article is split into disjoint 100-word chunks, to make a total of 21M documents” [Reference 1, §3][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article. Each chunk is a passage (the paper calls them documents). Each passage is turned into a list of numbers, a vector, by a neural network called the document encoder, and all 21 million vectors are stored in an index built for fast search.
2. Turn the question into a vector. A second network, the query encoder, turns the question into a vector of the same length. The two encoders are trained so that a question and a passage that answers it get a high score when their vectors are compared, using a calculation called the dot product that the next section explains.
3. Retrieve the closest passages. The retriever scores every passage by how well its vector matches the question’s vector, and keeps the few with the highest scores. Lewis et al. kept 5 or 10 in training, and chose the number for testing on held-out questions [Reference 1, §3][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article. Searching 21 million vectors exactly for every question would be slow, so the index uses an approximate search that finds high-scoring passages without comparing against every one [Reference 1, §2.2, §3][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article.
4. Generate an answer from the question and each passage. The generator is a sequence-to-sequence model, BART-large, with 400 million parameters [Reference 1, §2.3][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article. It reads text and writes text. To give it a passage, the system does something simple: “we simply concatenate them,” meaning the question and the passage are joined into one input [Reference 1, §2.3][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article.
5. Combine what the passages contribute. Each passage leads the generator to its own probabilities for the answer. RAG does not pick one passage and ignore the rest. It weighs each passage’s contribution by the probability the retriever gives that passage, computed from its score, and adds them up. The two ways of doing this are the two RAG models, described below.
Consider the question “Which river flows through Vienna?” The retriever might return a passage from the article on Vienna that mentions the Danube, a passage from the article on the Danube that lists the capitals it passes, and a passage about a smaller river in the city. The generator, reading each one alongside the question, would produce “the Danube” with high probability from the Vienna and Danube passages, and something else from the third. Because those two passages also received higher retrieval scores, the combined answer is “the Danube.” Because the answer is generated rather than copied out of a passage, it can be right even when no single passage states it: passages with clues can still help, and so can what the generator’s parameters hold [Reference 1, §4.1, §4.3][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article. In the Natural Questions cases where the correct answer was in none of the retrieved passages, RAG still answered 11.8% correctly, where a system that copies answers out of passages would score 0% [Reference 1, §4.1][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article.
Describe this figure
The question “Which river flows through Vienna?” goes into a query encoder, which produces a vector q(x). The vector is compared with an index of 21 million passage vectors, each passage scored by the dot product d(z) · q(x), and the top three passages are kept: “… Vienna lies on the Danube …” with retrieval probability 0.61, “The Danube passes four capitals …” with 0.30, and “The Wien river crosses the city …” with 0.09. A generator reads the question together with each passage and gives the answer “the Danube” probability 0.92, 0.85 and 0.10 respectively. The weighted sum, 0.61 × 0.92 + 0.30 × 0.85 + 0.09 × 0.10, gives 0.83 for “the Danube”.
Try: Choose the question about the “waterway”, then switch from vector similarity to shared words: passage 1, which states the answer most directly, shares no words with the question and is not retrieved, and a passage about Vienna with no answer takes its place.
This figure could not run, so a static version is shown.
For the question “Which river flows through Vienna?”, scored by vector similarity with k = 2, the retriever keeps the passage saying that Vienna lies on the Danube (retrieval probability 0.65) and the passage about the Wien river (0.35). The combined answer is “the Danube” with probability 0.58 and “the Wien” with 0.25. With k = 1 only the Danube passage is kept and “the Danube” rises to 0.90. For the question “Which waterway runs past the Austrian capital?”, scoring by shared words misses the passage saying that Vienna lies on the Danube, which shares no words with the question, and keeps a passage about Vienna that does not answer it; the answer “the Danube” falls from 0.86 to 0.40.
Finding passages: words or vectors
How does the retriever decide which passages match? There are two broad approaches, and RAG uses the second.
The older approach compares words. A sparse retriever represents a question and a passage by which words they contain, giving more weight to words that are rare in the collection, so that a match on “Danube” counts for more than a match on “the”. BM25 is a widely used ranking function of this kind [Reference 3][3] Stephen Robertson and Hugo Zaragoza (2009). The Probabilistic Relevance Framework: BM25 and Beyond. Foundations and Trends in Information Retrieval, 4(1–2). doi:10.1561/1500000019. Chen et al.’s DrQA system, discussed below, used a similar method: questions and articles “are compared as TF-IDF weighted bag-of-word vectors” [Reference 4, §3.1][4] Danqi Chen, Adam Fisch, Jason Weston and Antoine Bordes (2017). Reading Wikipedia to Answer Open-Domain Questions. Proceedings of the 55th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers). doi:10.18653/v1/P17-1171. Word matching is fast and needs no trained network, but it misses passages that answer a question in different words.
The newer approach compares meanings, as far as a trained network captures them. A dense retriever uses neural networks to map questions and passages to vectors of a few hundred numbers. Karpukhin et al.’s Dense Passage Retriever (DPR) uses two separate BERT networks, one for questions and one for passages, and defines the similarity of a question and a passage as the dot product of their vectors [Reference 5, §3.1, Eq. 1][5] Vladimir Karpukhin, Barlas Oguz, Sewon Min et al. (2020). Dense Passage Retrieval for Open-Domain Question Answering. Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP). doi:10.18653/v1/2020.emnlp-main.550:
Here is the question encoder and the passage encoder, each producing a vector of 768 numbers. A dot product multiplies the two vectors entry by entry and adds the results; it is large when the vectors point in similar directions, and larger the longer they are. The authors chose this form because “the similarity function needs to be decomposable so that the representations of the collection of passages can be pre-computed” [Reference 5, §3.1][5] Vladimir Karpukhin, Barlas Oguz, Sewon Min et al. (2020). Dense Passage Retrieval for Open-Domain Question Answering. Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP). doi:10.18653/v1/2020.emnlp-main.550: every passage vector can be computed once, in advance, and only the question has to be encoded when it arrives.
Describe this figure
The question is “Which river flows through Vienna?” A word-matching retriever ranks “Vienna sausages are smoked and served hot …” first because it shares the word “Vienna”, and ranks “The Danube passes four capitals …” low because it shares no words with the question. A retriever that matches meanings places each text as a point; the Danube passage is close to the question and ranked first, and the sausage passage is far away and ranked low. The map is drawn in two dimensions; DPR’s vectors have 768.
On the Natural Questions benchmark, DPR found a passage containing the answer among its top 20 results for 78.4% of test questions, compared with 59.1% for BM25 [Reference 5, §5.1, Table 2][5] Vladimir Karpukhin, Barlas Oguz, Sewon Min et al. (2020). Dense Passage Retrieval for Open-Domain Question Answering. Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP). doi:10.18653/v1/2020.emnlp-main.550. A DPR retriever “trained using only 1,000 examples” already did better than BM25 on the development set [Reference 5, §5.2, Figure 1][5] Vladimir Karpukhin, Barlas Oguz, Sewon Min et al. (2020). Dense Passage Retrieval for Open-Domain Question Answering. Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP). doi:10.18653/v1/2020.emnlp-main.550. Word matching kept an advantage in some cases, though. On SQuAD, BM25 did better; the authors suggest one reason is that “the annotators wrote questions after seeing the passage”, so questions and passages share many words [Reference 5, §5.1][5] Vladimir Karpukhin, Barlas Oguz, Sewon Min et al. (2020). Dense Passage Retrieval for Open-Domain Question Answering. Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP). doi:10.18653/v1/2020.emnlp-main.550. They also note that “Term-matching methods like BM25 are sensitive to highly selective keywords and phrases, while DPR captures lexical variations or semantic relationships better” [Reference 5, §5.3][5] Vladimir Karpukhin, Barlas Oguz, Sewon Min et al. (2020). Dense Passage Retrieval for Open-Domain Question Answering. Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP). doi:10.18653/v1/2020.emnlp-main.550.
RAG uses DPR’s design for its retriever and starts from DPR’s trained encoders [Reference 1, §2.2][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article.
The formal picture
This section writes down what the steps above compute. It needs only probabilities, sums and products; the notation follows Lewis et al., with small changes noted where they occur.
The retriever as a probability. Write the input, such as a question, as , and a passage as . The retriever assigns each passage a probability of being the relevant one, , where stands for the retriever’s parameters. In RAG it follows DPR [Reference 1, §2.2][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article:
where is the passage’s vector from the document encoder, is the input’s vector from the query encoder, and means “proportional to”: the scores are exponentiated and then divided by their total, so that the probabilities over all passages add up to 1. A higher dot product means a higher probability. In practice only the passages with the highest probability are kept; call that set . Their probabilities are then rescaled to add up to 1 over those passages, which the paper calls “top-K truncated” distributions [Reference 1, §2][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article; the figures in this article do the same.
The generator. Write the output, such as an answer, as a sequence of tokens , where a token is a word or a piece of a word. The generator, with parameters , gives the probability of each next token given the input, a passage and the tokens written so far: , where means tokens 1 to of the output.
RAG-Sequence. This model uses one passage for the whole answer, computes the answer’s probability under each retrieved passage, and averages those probabilities, weighted by the retriever [Reference 1, §2.1][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article:
Read from the inside out: the product is the probability of the whole answer if the generator reads passage ; the sum weights that by how likely is to be the relevant passage. This is the step-5 combination from earlier, written out. The equation says “approximately equal” () rather than “equal” because the sum runs over the top passages rather than all 21 million, which the paper calls a “top-K approximation” [Reference 1, §2.1][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article; it assumes the passages left out have small probability.
RAG-Token. The other model lets each token of the answer draw on a different passage. It moves the sum inside the product, so the combination happens at every token [Reference 1, §2.1][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article:
In words: to choose each next token, ask the generator how likely each possible next token is with each passage, and average those probabilities, weighted by the retriever. An answer can then take one fact from one passage and another fact from a second. When the output is a single token, as in classifying a claim as true or false, the two models are the same [Reference 1, §2.1][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article. (In the generator’s term the paper writes the passage as , to show that it may change from token to token; here it is written , since the sum already ranges over all the retrieved passages for each token.)
Describe this figure
Passage A reads “… Vienna lies on the Danube …”; passage B reads “The Danube ends in the Black Sea …”. With RAG-Sequence, the answer written with A is “the Danube; it ends in … ?” and the answer written with B is “… ? It ends in the Black Sea”; they are weighted and added up, and neither has both facts. With RAG-Token, the single answer “the Danube, ending in the Black Sea” takes “Danube” from passage A and “Black Sea” from passage B.
Training without being told which passage is right. Lewis et al. train the retriever and the generator together on pairs of inputs and correct outputs, “without any direct supervision on what document should be retrieved” [Reference 1, §2.4][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article. Training minimizes over the training pairs , where is one of the two probabilities above [Reference 1, §2.4][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article. Because the passages appear in that probability only through a weighted sum, the learning signal reaches the retriever too: a passage that makes the correct answer more likely than the others do gets its weight raised, and one that makes it less likely gets it lowered. The passage that matters is treated as a hidden, or latent, variable, and the model learns which passages help by finding out which ones lead to correct answers.
One part is kept fixed. Updating the document encoder would change every passage vector, so all 21 million would have to be recomputed and re-indexed again and again. Lewis et al. “do not find this step necessary for strong performance”, and they “keep the document encoder (and index) fixed, only fine-tuning the query encoder BERTq and the BART generator” [Reference 1, §2.4][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article. Training adjusts which passages a question is matched to, and how the generator uses them, but not how the passages themselves are represented.
Where the idea came from
The idea of answering questions from a collection of documents is older than neural networks. Chen et al. note that open-domain question answering “was originally defined as finding answers in collections of unstructured documents”, following the annual TREC competitions [Reference 4, §2][4] Danqi Chen, Adam Fisch, Jason Weston and Antoine Bordes (2017). Reading Wikipedia to Answer Open-Domain Questions. Proceedings of the 55th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers). doi:10.18653/v1/P17-1171. This section covers only the steps that lead most directly to RAG, from 2017 to 2021.
Retrieve, then read (2017). Chen et al. set out to answer questions “using Wikipedia as the unique knowledge source”, which they called “machine reading at scale” [Reference 4, Abstract, §1][4] Danqi Chen, Adam Fisch, Jason Weston and Antoine Bordes (2017). Reading Wikipedia to Answer Open-Domain Questions. Proceedings of the 55th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers). doi:10.18653/v1/P17-1171. Their system, DrQA, had a word-matching retriever that returned 5 Wikipedia articles for each question, and a neural reader that picked out the span of text answering it [Reference 4, §3.1, §3.2][4] Danqi Chen, Adam Fisch, Jason Weston and Antoine Bordes (2017). Reading Wikipedia to Answer Open-Domain Questions. Proceedings of the 55th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers). doi:10.18653/v1/P17-1171. The paper showed how much harder the search makes the task: the reader alone, given the right paragraph, scored 69.5% exact match on SQuAD, but the full system, searching all of Wikipedia, scored 27.1% [Reference 4, §5.3, Tables 4 and 6][4] Danqi Chen, Adam Fisch, Jason Weston and Antoine Bordes (2017). Reading Wikipedia to Answer Open-Domain Questions. Proceedings of the 55th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers). doi:10.18653/v1/P17-1171.
Learn the retriever during pre-training (2020). Guu et al.’s REALM made retrieval part of how a language model is pre-trained. Pre-training here means masked language modeling: hiding words in a sentence and training the model to predict them. REALM retrieves documents to help with that prediction, and trains its retriever “using masked language modeling as the learning signal and backpropagating through a retrieval step” [Reference 6, Abstract][6] Kelvin Guu, Kenton Lee, Zora Tung, Panupong Pasupat and Mingwei Chang (2020). Retrieval Augmented Language Model Pre-Training. International Conference on Machine Learning, PMLR 119. arXiv:2002.08909. Its retriever defines a relevance score as an inner product of two embeddings, much as DPR’s does [Reference 6, §3.2][6] Kelvin Guu, Kenton Lee, Zora Tung, Panupong Pasupat and Mingwei Chang (2020). Retrieval Augmented Language Model Pre-Training. International Conference on Machine Learning, PMLR 119. arXiv:2002.08909. Because the document vectors change as the retriever learns, REALM has to recompute them; its authors “refresh” the index “by asynchronously re-embedding and re-indexing all documents every several hundred training steps” [Reference 6, §3.3][6] Kelvin Guu, Kenton Lee, Zora Tung, Panupong Pasupat and Mingwei Chang (2020). Retrieval Augmented Language Model Pre-Training. International Conference on Machine Learning, PMLR 119. arXiv:2002.08909. On three open-domain question-answering benchmarks, REALM reported gains of “4-16% absolute accuracy” over the previous methods it compared against [Reference 6, Abstract, Table 1][6] Kelvin Guu, Kenton Lee, Zora Tung, Panupong Pasupat and Mingwei Chang (2020). Retrieval Augmented Language Model Pre-Training. International Conference on Machine Learning, PMLR 119. arXiv:2002.08909. For question answering, REALM assumed that the answer “can be found as a contiguous sequence of tokens in some document”: it extracted answers, as DrQA did, rather than generating them [Reference 6, §3.2][6] Kelvin Guu, Kenton Lee, Zora Tung, Panupong Pasupat and Mingwei Chang (2020). Retrieval Augmented Language Model Pre-Training. International Conference on Machine Learning, PMLR 119. arXiv:2002.08909.
Learn a dense retriever directly (2020). DPR, described above, showed that a dense retriever trained on question and passage pairs could retrieve better than BM25 on most of the benchmarks it tested, without REALM’s pre-training [Reference 5, §5.1][5] Vladimir Karpukhin, Barlas Oguz, Sewon Min et al. (2020). Dense Passage Retrieval for Open-Domain Question Answering. Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP). doi:10.18653/v1/2020.emnlp-main.550.
Retrieve, then generate (2020). Lewis et al. combined a DPR retriever with a pre-trained generator and fine-tuned them together, calling the result “a general-purpose fine-tuning recipe” [Reference 1, Abstract][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article. Unlike the question-answering systems before it, it generated the answer instead of extracting it, so the same recipe could be fine-tuned to write questions, answer in full sentences or classify claims. And unlike REALM, it needed no retrieval-specific pre-training: it started from a separately trained retriever and generator and kept the document index fixed throughout, avoiding the periodic re-indexing REALM does while pre-training [Reference 1, §2.4][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article.
Read many passages at once (2020, published 2021). At about the same time, Izacard and Grave proposed Fusion-in-Decoder, or FiD. Each retrieved passage is “concatenated with the question, and processed independently from other passages by the encoder”, and the decoder then reads all the encoded passages together [Reference 7, §3][7] Gautier Izacard and Edouard Grave (2021). Leveraging Passage Retrieval with Generative Models for Open Domain Question Answering. Proceedings of the 16th Conference of the European Chapter of the Association for Computational Linguistics: Main Volume. doi:10.18653/v1/2021.eacl-main.74. Because the passages are encoded separately, the cost grows in proportion to the number of passages rather than faster, so FiD could use 100 passages per question [Reference 7, §3, §4][7] Gautier Izacard and Edouard Grave (2021). Leveraging Passage Retrieval with Generative Models for Open Domain Question Answering. Proceedings of the 16th Conference of the European Chapter of the Association for Computational Linguistics: Main Volume. doi:10.18653/v1/2021.eacl-main.74. Unlike RAG, it trained the reader but not the retriever [Reference 7, §3, §5][7] Gautier Izacard and Edouard Grave (2021). Leveraging Passage Retrieval with Generative Models for Open Domain Question Answering. Proceedings of the 16th Conference of the European Chapter of the Association for Computational Linguistics: Main Volume. doi:10.18653/v1/2021.eacl-main.74.
The table below sets the five systems side by side. Its rows are in order of each paper’s first arXiv posting, and the years are publication years.
| Published | System | Retriever compares | Retriever during training | Answer |
|---|---|---|---|---|
| 2017 | DrQA [Reference 4][4] Danqi Chen, Adam Fisch, Jason Weston and Antoine Bordes (2017). Reading Wikipedia to Answer Open-Domain Questions. Proceedings of the 55th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers). doi:10.18653/v1/P17-1171 | Words | Fixed | Extracted |
| 2020 | REALM [Reference 6][6] Kelvin Guu, Kenton Lee, Zora Tung, Panupong Pasupat and Mingwei Chang (2020). Retrieval Augmented Language Model Pre-Training. International Conference on Machine Learning, PMLR 119. arXiv:2002.08909 | Vectors | Trained in pre-training; query side only in fine-tuning | Extracted |
| 2020 | DPR [Reference 5][5] Vladimir Karpukhin, Barlas Oguz, Sewon Min et al. (2020). Dense Passage Retrieval for Open-Domain Question Answering. Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP). doi:10.18653/v1/2020.emnlp-main.550 | Vectors | Trained | Extracted |
| 2020 | RAG [Reference 1][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article | Vectors | Query side trained, document side fixed | Generated |
| 2021 | FiD [Reference 7][7] Gautier Izacard and Edouard Grave (2021). Leveraging Passage Retrieval with Generative Models for Open Domain Question Answering. Proceedings of the 16th Conference of the European Chapter of the Association for Computational Linguistics: Main Volume. doi:10.18653/v1/2021.eacl-main.74 | Vectors; words (BM25) on SQuAD | Fixed | Generated |
Source: the paper cited in each row.
What the evidence shows
The experiments below come from the papers that introduced the methods. Each is a single study, on public benchmarks, with models of 2020 and 2021. They show what the methods did under those conditions; how well the results carry over to other tasks, data and models is a separate question, taken up in the next section.
Open-domain question answering. On four question-answering benchmarks, Lewis et al. compared RAG with models that answer from their parameters alone (“closed book”) and with retrieve-then-read systems (“open book”) [Reference 1, §4.1, Table 1][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article. On Natural Questions, measured by exact match:
| System | Kind | Exact match (%) |
|---|---|---|
| T5-11B + SSM | Closed book | 36.6 |
| REALM | Open book, extracts answers | 40.4 |
| DPR | Open book, extracts answers | 41.5 |
| RAG-Token | Open book, generates answers | 44.1 |
| RAG-Sequence | Open book, generates answers | 44.5 |
Source: [Reference 1, Table 1][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article.
The closed-book model in this comparison has 11 billion parameters; RAG’s generator has 400 million, plus its retriever and index [Reference 1, §2.3, Table 1][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article. The paper notes that RAG’s retriever starts from DPR’s, which had been trained with supervision on which passages answer Natural Questions and TriviaQA questions [Reference 1, §4.1][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article. FiD’s larger version, with 100 passages, later reported 51.4% on the same test set [Reference 7, Table 1][7] Gautier Izacard and Edouard Grave (2021). Leveraging Passage Retrieval with Generative Models for Open Domain Question Answering. Proceedings of the 16th Conference of the European Chapter of the Association for Computational Linguistics: Main Volume. doi:10.18653/v1/2021.eacl-main.74.
Training the retriever helped on most tasks. Lewis et al. also ran RAG with its retriever frozen (left as DPR trained it, not updated while RAG trains), and with BM25 in its place [Reference 1, §4.5, Table 6][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article. On the Natural Questions development set, RAG-Sequence scored 44.0 with its trained retriever, 41.2 with the retriever frozen, and 31.8 with BM25. On FEVER, a task of checking claims against Wikipedia, BM25 did slightly better (75.1% against 74.5% label accuracy for RAG-Token on the three-way version), “perhaps since FEVER claims are heavily entity-centric” [Reference 1, §4.5][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article, which echoes DPR’s finding that word matching is strong when exact names matter.
More factual generated text, in one human evaluation. RAG was also tested on writing text, not just answering. In one task it generated Jeopardy-style clues for a given answer. Human evaluators compared 452 pairs of outputs from RAG-Token and from BART without retrieval, judging factuality, defined as “whether a statement can be corroborated by trusted external sources” [Reference 1, §3.3][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article. BART was judged more factual in 7.1% of pairs and RAG in 42.7%; in the other pairs, both were judged good (11.7%) or poor (17.7%), or the evaluators did not agree (20.8%) [Reference 1, §4.3, Table 4][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article.
Updating knowledge by swapping the index. Lewis et al. built a second index from a Wikipedia snapshot of December 2016 and asked RAG “Who is” questions about 82 world leaders who had changed between 2016 and 2018 [Reference 1, §4.5][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article. With the 2016 index, RAG answered 70% of the 2016 leaders correctly; with the 2018 index, 68% of the 2018 leaders. With the mismatched index, accuracy fell to 12% and 4%. The authors conclude: “This shows we can update RAG’s world knowledge by simply replacing its non-parametric memory” [Reference 1, §4.5][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article. The test is small, and it covers one kind of fact that changes, but it shows directly the property that motivated the design: the facts a RAG system draws on can be changed without retraining it.
Fewer false statements in conversation. Shuster et al. tested retrieval in dialogue systems that talk about Wikipedia topics, with human evaluation by annotators from the authors’ lab [Reference 8, §4.2.1][8] Kurt Shuster, Spencer Poff, Moya Chen, Douwe Kiela and Jason Weston (2021). Retrieval Augmentation Reduces Hallucination in Conversation. Findings of the Association for Computational Linguistics: EMNLP 2021. doi:10.18653/v1/2021.findings-emnlp.320. They describe “the well known “hallucination” problem” as models that “generate plausible looking statements that are factually incorrect” [Reference 8, §1][8] Kurt Shuster, Spencer Poff, Moya Chen, Douwe Kiela and Jason Weston (2021). Retrieval Augmentation Reduces Hallucination in Conversation. Findings of the Association for Computational Linguistics: EMNLP 2021. doi:10.18653/v1/2021.findings-emnlp.320, and their annotators judged 100 responses from each model on whether “some of the model output [is] factually incorrect”, or mixes up ideas [Reference 8, §4.2.1][8] Kurt Shuster, Spencer Poff, Moya Chen, Douwe Kiela and Jason Weston (2021). Retrieval Augmentation Reduces Hallucination in Conversation. Findings of the Association for Computational Linguistics: EMNLP 2021. doi:10.18653/v1/2021.findings-emnlp.320. Without retrieval, 68.2% of the BART model’s responses were judged to contain something incorrect. With retrieval, the share ranged from 7.9% to 21.5% across the retrieval models tested [Reference 8, Table 4][8] Kurt Shuster, Spencer Poff, Moya Chen, Douwe Kiela and Jason Weston (2021). Retrieval Augmentation Reduces Hallucination in Conversation. Findings of the Association for Computational Linguistics: EMNLP 2021. doi:10.18653/v1/2021.findings-emnlp.320. Retrieval reduced the share of incorrect responses in this test; it did not remove it.
More passages can help a reader that uses them well. FiD’s accuracy rose as it read more passages: going from 10 to 100 passages gave “6% improvement on TriviaQA and 3.5% improvement on NaturalQuestions” for its base model on the development sets, while, citing earlier work, the authors note that the performance of most extractive models “seems to peak around 10 to 20 passages” [Reference 7, §4, Figure 3][7] Gautier Izacard and Edouard Grave (2021). Leveraging Passage Retrieval with Generative Models for Open Domain Question Answering. Proceedings of the 16th Conference of the European Chapter of the Association for Computational Linguistics: Main Volume. doi:10.18653/v1/2021.eacl-main.74. For RAG, retrieving more passages at test time improved question answering for RAG-Sequence, but RAG-Token’s performance “peaks … at 10 retrieved documents” [Reference 1, §4.5, Figure 3][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article. In these two studies, whether more passages helped depended on the model reading them.
Limits and open questions
RAG moves part of a model’s knowledge into a collection that can be searched. That changes the problems; it does not remove them.
Retrieval can miss. If the passage that answers the question is not retrieved, the generator is left with its parametric memory, or worse, with passages that are related but wrong. DrQA shows both problems. Its retriever found an article containing the answer for 77.8% of SQuAD questions, yet given the correct article rather than the paragraph, its reader’s exact match fell from 69.5% to 49.4%; the authors put many of its errors down to “highly topical sentences”, and much of the remaining drop to SQuAD’s questions, which were written with one paragraph in mind [Reference 4, §5.3, Tables 3 and 4][4] Danqi Chen, Adam Fisch, Jason Weston and Antoine Bordes (2017). Reading Wikipedia to Answer Open-Domain Questions. Proceedings of the 55th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers). doi:10.18653/v1/P17-1171. DPR improved retrieval considerably, but its top 20 passages still contained no answer for the remaining 21.6% of Natural Questions test questions (100% minus the 78.4% above) [Reference 5, Table 2][5] Vladimir Karpukhin, Barlas Oguz, Sewon Min et al. (2020). Dense Passage Retrieval for Open-Domain Question Answering. Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP). doi:10.18653/v1/2020.emnlp-main.550. The authors also found that higher retrieval accuracy “typically leads to better final QA results” [Reference 5, §6.2][5] Vladimir Karpukhin, Barlas Oguz, Sewon Min et al. (2020). Dense Passage Retrieval for Open-Domain Question Answering. Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP). doi:10.18653/v1/2020.emnlp-main.550.
The collection can be wrong, or out of date. A RAG system depends on what it retrieves. Lewis et al. note that “Wikipedia, or any potential external knowledge source, will probably never be entirely factual and completely devoid of bias” [Reference 1, Broader Impact][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article. A collection also has its own date. In one of Shuster et al.’s examples, a retrieval-augmented model calls Thierry Henry “the current manager of French club Monaco”; the authors note that he no longer was, but “he was at the time our Wikipedia dump was collected” [Reference 8, Table 3][8] Kurt Shuster, Spencer Poff, Moya Chen, Douwe Kiela and Jason Weston (2021). Retrieval Augmentation Reduces Hallucination in Conversation. Findings of the Association for Computational Linguistics: EMNLP 2021. doi:10.18653/v1/2021.findings-emnlp.320. Swapping the index can fix this, but only if someone does it.
Models do not use everything they are given. Liu et al. studied how language models use long inputs. They gave models a question with 10, 20 or 30 retrieved passages, exactly one of which contained the answer, and moved that passage around [Reference 9, §2.1, §2.3][9] Nelson F. Liu, Kevin Lin, John Hewitt et al. (2024). Lost in the Middle: How Language Models Use Long Contexts. Transactions of the Association for Computational Linguistics, 12. doi:10.1162/tacl_a_00638. Performance was “often highest when relevant information occurs at the beginning or end of the input context, and significantly degrades when models must access relevant information in the middle of long contexts” [Reference 9, Abstract][9] Nelson F. Liu, Kevin Lin, John Hewitt et al. (2024). Lost in the Middle: How Language Models Use Long Contexts. Transactions of the Association for Computational Linguistics, 12. doi:10.1162/tacl_a_00638. For one model, accuracy in the worst position with 20 or 30 passages was lower than its accuracy with no passages at all, 56.1% [Reference 9, §2.3, Figure 5][9] Nelson F. Liu, Kevin Lin, John Hewitt et al. (2024). Lost in the Middle: How Language Models Use Long Contexts. Transactions of the Association for Computational Linguistics, 12. doi:10.1162/tacl_a_00638. The arXiv version of the paper also gives these accuracies as tables [Reference 10, Table 1; Appendix G, Table 6][10] Nelson F. Liu, Kevin Lin, John Hewitt et al. (2023). Lost in the Middle: How Language Models Use Long Contexts. arXiv. Version v3. arXiv:2307.03172, and the chart below plots its values for 20 passages. In a second experiment, accuracy hardly improved beyond 20 retrieved passages: using 50 instead of 20 added about 1.5 points for GPT-3.5-Turbo and about 1 for Claude-1.3, even though more passages kept increasing the chance that the answer was among them [Reference 9, §5, Figure 11][9] Nelson F. Liu, Kevin Lin, John Hewitt et al. (2024). Lost in the Middle: How Language Models Use Long Contexts. Transactions of the Association for Computational Linguistics, 12. doi:10.1162/tacl_a_00638. These results are for models available in 2023, and the authors call their analysis of why it happens “preliminary” [Reference 9, §4][9] Nelson F. Liu, Kevin Lin, John Hewitt et al. (2024). Lost in the Middle: How Language Models Use Long Contexts. Transactions of the Association for Computational Linguistics, 12. doi:10.1162/tacl_a_00638.
Describe this figure
Accuracy by position of the answer-bearing passage among 20: position 1, 75.8%; position 5, 57.2%; position 10, 53.8%; position 15, 55.4%; position 20, 63.2%. With no passages at all, the model reached 56.1%, drawn as a dashed line; the band below it, labelled “below this line: worse than no retrieval”, holds positions 10 and 15.
More passages can mean more errors. In Shuster et al.’s conversations, “all models suffer from more hallucination when we condition on more documents for each generation” [Reference 8, §5][8] Kurt Shuster, Spencer Poff, Moya Chen, Douwe Kiela and Jason Weston (2021). Retrieval Augmentation Reduces Hallucination in Conversation. Findings of the Association for Computational Linguistics: EMNLP 2021. doi:10.18653/v1/2021.findings-emnlp.320. In their human evaluation, going from 5 to 25 passages raised the share of responses judged incorrect from 17.0% to 21.5% for RAG-Token and from 7.9% to 19.8% for FiD-RAG [Reference 8, Table 4][8] Kurt Shuster, Spencer Poff, Moya Chen, Douwe Kiela and Jason Weston (2021). Retrieval Augmentation Reduces Hallucination in Conversation. Findings of the Association for Computational Linguistics: EMNLP 2021. doi:10.18653/v1/2021.findings-emnlp.320. The authors also observe that models built on RAG-Token, which are designed to combine information across passages, were more prone to incorrect statements than those that are not [Reference 8, §4.2.1][8] Kurt Shuster, Spencer Poff, Moya Chen, Douwe Kiela and Jason Weston (2021). Retrieval Augmentation Reduces Hallucination in Conversation. Findings of the Association for Computational Linguistics: EMNLP 2021. doi:10.18653/v1/2021.findings-emnlp.320.
Retrieval is not attribution. A RAG system can show the passages it retrieved, but that does not establish that its answer follows from them. The generator can ignore a passage, combine passages wrongly, or add something from its parameters. The evaluations above measure whether outputs are correct; none of them measures whether each statement is supported by the passage shown next to it.
Cost. A RAG system has to store and search an index, and its generator reads several passages for every answer. FiD’s authors report 425 GPU hours to train their model with 100 passages [Reference 7, §4][7] Gautier Izacard and Edouard Grave (2021). Leveraging Passage Retrieval with Generative Models for Open Domain Question Answering. Proceedings of the 16th Conference of the European Chapter of the Association for Computational Linguistics: Main Volume. doi:10.18653/v1/2021.eacl-main.74, and Liu et al. point out that longer inputs increase “latency and cost” [Reference 9, §5][9] Nelson F. Liu, Kevin Lin, John Hewitt et al. (2024). Lost in the Middle: How Language Models Use Long Contexts. Transactions of the Association for Computational Linguistics, 12. doi:10.1162/tacl_a_00638.
Open questions. Shuster et al. end with one: “understanding the interplay between retrieved knowledge and knowledge stored in the model’s weights” [Reference 8, §6][8] Kurt Shuster, Spencer Poff, Moya Chen, Douwe Kiela and Jason Weston (2021). Retrieval Augmentation Reduces Hallucination in Conversation. Findings of the Association for Computational Linguistics: EMNLP 2021. doi:10.18653/v1/2021.findings-emnlp.320. When the passages and the parameters disagree, which wins, and can a system be built to prefer the right one?
What this article leaves out
This is an introduction, and it simplifies.
- One version of the method. It describes RAG as Lewis et al. defined it in 2020. Many systems now called RAG instead give retrieved passages to a large language model that is not fine-tuned for the task, by placing them in its prompt, as in Liu et al.’s experiments above [Reference 9, §2.1][9] Nelson F. Liu, Kevin Lin, John Hewitt et al. (2024). Lost in the Middle: How Language Models Use Long Contexts. Transactions of the Association for Computational Linguistics, 12. doi:10.1162/tacl_a_00638. That is a different design, with a different training story, even though it shares the name and the basic idea.
- Decoding. It does not explain how RAG-Sequence turns probabilities into a single answer, which needs a beam search for each passage, then either extra computation to score answers that some passages’ searches missed, or an approximation that skips it [Reference 1, §2.5][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article.
- Later variants. Methods that retrieve repeatedly while writing, rerank passages, retrieve during pre-training at larger scale, or retrieve from sources other than Wikipedia are beyond this introduction.
- The evidence is from benchmarks. Every result here is on public research benchmarks with models from 2017 to 2023. Numbers on these benchmarks are not predictions for other tasks or newer models.
- The analogy has limits. The open-book picture suggests a model reads a passage the way a person does. The equations say only that the generator computes its probabilities with the passage as part of its input; they say nothing about how the model uses it.
Data and code
- Code: the toy retriever runs in your browser, as part of this page. It scores the passages against the question, turns the scores into retrieval probabilities and adds up the weighted answers, as described above. Its ten passages, their vectors and the generator’s answers were written by hand for the figure, so it trains no model and uses no dataset. The code that runs is the version published with this article. Its source code, and the code that drew the article’s other figures, is not published.
- Data: this article creates no data and runs no experiments. The exact-match values in the Natural Questions table are copied from Lewis et al. [Reference 1, Table 1][1] Patrick Lewis, Ethan Perez, Aleksandra Piktus et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. Advances in Neural Information Processing Systems 33. arXiv:2005.11401 Focus of this article, and the accuracies in the chart of answer position from version 3 of Liu et al. on arXiv [Reference 10, Tables 1 and 6][10] Nelson F. Liu, Kevin Lin, John Hewitt et al. (2023). Lost in the Middle: How Language Models Use Long Contexts. arXiv. Version v3. arXiv:2307.03172.