Study guide · Language models & AI · 2020

Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks

Patrick Lewis et al. · NeurIPS 2020

Language models store facts inside their weights, where they are hard to check, hard to update and easy to garble. This paper gives a text generator a search engine: for each question it first retrieves a few relevant Wikipedia passages, then writes its answer while reading them. The result was more accurate on open-domain questions, more specific and factual in its writing, and could be updated simply by swapping the document index.

The paper in four sentences

Big language models remember a lot of facts, but only implicitly, in their billions of internal numbers: you cannot easily see where an answer came from, correct it, or add new knowledge without retraining.

RAG (retrieval-augmented generation) pairs a text generator with a retriever that searches 21 million short Wikipedia passages and hands the best few to the generator.

The two parts are trained together, end to end, using only question–answer pairs: nobody tells the retriever which passage is "right"; it learns which passages help the generator answer.

RAG set new records on three open-domain question-answering benchmarks, wrote more factual and specific text than a generator alone, and changed its answers when given a newer copy of Wikipedia.

Idea · §1

Two kinds of memory

A language model such as GPT-2 learns facts during training and keeps them in its parameters, the millions or billions of numbers inside the network. The paper calls this parametric memory. It is powerful but opaque: when the model says something false (a "hallucination"), there is no source to check, and teaching it a new fact means more training.

The alternative is non-parametric memory: an external collection of documents the system can look things up in, like a library. Facts there are readable, citable and replaceable.

RAG combines the two. A retriever finds passages related to the input. A generator, a pre-trained model that writes text (here BART), then produces the answer while looking at both the question and the retrieved passages.

Everyday pictureA closed-book exam versus an open-book exam. In a closed-book exam you rely on what you memorised and might misremember. In an open-book exam you still need understanding to write a good answer, but you can look up the exact date, and someone marking your work can see which page you used.

TakeawayRAG keeps the language skill in the model's weights but moves much of the factual knowledge into a searchable document collection.

Idea · §2.2 · Fig. 1

How the retriever finds passages

The retriever turns every passage into a list of numbers, a vector, that captures roughly what it is about. It does the same for the question. Passages whose vectors point in a similar direction to the question's get a high score, and the top few are passed on. Because all passage vectors are computed once in advance, searching millions of passages takes a fraction of a second.

The real system learns its vectors with neural networks. The toy below uses a much cruder version (it simply counts shared meaningful words), but the pipeline is the same: score every passage, convert scores into probabilities, keep the top k, and let the generator read them.

An illustrative toy with eight made-up "Wikipedia" passages. Each passage's score is the number of meaningful words it shares with the question; scores become probabilities with a softmax (higher scores take a bigger share). Amber passages are the top k handed to the generator. The answer shown is the one supported by the highest-probability retrieved passage. In real RAG the scores come from learned vectors (so "author" can match "wrote" even with no shared word).
Check yourself
Why can RAG search 21 million passages quickly?
The passage vectors are computed once and stored in an index; at question time only the question needs encoding, and a fast nearest-neighbour search finds the highest-scoring passages.

Idea · §2.1 · §2.4 · Fig. 2

Learning what to retrieve, without being told

Training data is only question–answer pairs, with no note of which passage contains the answer. So how does the retriever improve?

RAG treats the retrieved passage as an unknown it averages over. For each candidate passage, the generator says how likely the correct answer is given that passage. Passages that make the correct answer more likely get their retrieval score pushed up; unhelpful ones get pushed down. Over many examples the retriever learns which kinds of passages actually help.

The paper offers two ways to do the averaging. RAG-Sequence picks one passage and writes the whole answer from it, then averages over passages. RAG-Token may lean on a different passage for each word. That matters when an answer combines facts from several places: the paper shows the generator drawing on one passage for "The Sun Also Rises" and another for "A Farewell to Arms" when writing about Hemingway (Fig. 2).

Everyday pictureA student and a librarian work together on homework. The librarian fetches books; the student writes answers. When the teacher marks an answer right, the librarian remembers which books were on the desk that time, and fetches books like those more often.

TakeawayThe retriever learns from the generator's success, not from labelled "correct passages", which makes RAG easy to train on any input–output task.

Check yourself
What is the practical difference between RAG-Sequence and RAG-Token?
RAG-Sequence commits to one passage per whole answer; RAG-Token can switch passages word by word, so it can combine facts from different documents in one answer.

Idea · §4.1–4.4 · Tables 1–4

More accurate, more specific, more factual

On four open-domain question benchmarks (questions with no document provided), RAG beat both a 11-billion-parameter model answering from memory (T5) and earlier "retrieve, then extract a span" systems on three of them, while having far fewer trainable parameters than T5-11B. Because it generates rather than copies, it could even answer correctly when no retrieved passage contained the exact answer: 11.8% of such Natural Questions cases, where a copying system would score zero.

The authors also had it write quiz-show (Jeopardy-style) clues. Human judges compared RAG's clues with those of the same generator without retrieval (BART): RAG was judged more factual in 42.7% of comparisons and BART in only 7.1%; for specificity it was 37.4% against 16.8%.

TakeawayAdding retrieval made a mid-sized generator more accurate than a far larger memory-only model, and made its writing measurably more factual.

Idea · §4.5

Update the knowledge by swapping the library

Because facts live in the index, you can change what RAG knows without retraining it. The authors built a test of 82 world leaders ("Who is the President of Peru?" and so on) whose holders changed between 2016 and 2018, and swapped a 2016 Wikipedia index for a 2018 one.

Index:Ask about leaders in:

Real numbers from §4.5. Accuracy is high only when the index matches the year being asked about: 70% (2016 index, 2016 leaders) and 68% (2018 index, 2018 leaders), but 12% and 4% when mismatched. The model itself was not retrained.
Later refinement"RAG" became the general name for any system that retrieves documents and feeds them to a language model, usually today by pasting retrieved text into the prompt of a large chat model rather than training retriever and generator jointly. Retrieval also became a step inside agents such as ReAct. It brought a new risk too: retrieved text can carry hidden instructions, the problem of indirect prompt injection.

TakeawaySwap the documents and the answers change: knowledge in an index can be updated, inspected and cited in a way knowledge in weights cannot.

Check yourself
What does the 70% versus 4% result show?
That RAG's answers really come from the index, not from memorised facts: with the old index it gives the old leader, and switching to a newer index updates its answers without any training.

Follow the idea

Led to: ReAct (2022)