Skip to main content
GW
HomeWorkAI appsPapersAboutNotesStackLabPilotPlan↗ (opens in a new tab)INVST↗ (opens in a new tab)
Govind Waghmare

Finance systems and AI. Building in public from NYC.

Resume (PDF)Book 20 minEmail
HomeNotesWorkAboutStackPapersLabFor founders
XLinkedInGitHubllms.txt
© 2026 · New York City

Keyboard shortcuts

?
Open this list
g then h
Go to Home
g then w
Go to Work
g then n
Go to Notes
g then a
Go to About
c
Ask the AI guide
Esc
Close this list
Original paperSection A, The abstract
Open in new tab (opens in a new tab)
All papers
FoundationsClassic

RAG: let the model search before it answers

Paper: Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks

Patrick Lewis, Ethan Perez, Aleksandra Piktus, Fabio Petroni, Vladimir Karpukhin, Naman Goyal, Heinrich Küttler, Mike Lewis, Wen-tau Yih, Tim Rocktäschel, Sebastian Riedel, Douwe Kiela · arXiv preprint (Facebook AI Research, University College London, New York University) · Posted 22 May 2020 · 14 min read · arXiv 2005.11401

Share this paper

LinkedIn (opens in a new tab)X (opens in a new tab)WhatsApp (opens in a new tab)EmailDownload PDFExplainer (PDF), 605 KB

  • Language models store facts in their parameters.
  • They cannot easily update or show where an answer came from.
  • They sometimes hallucinate, which means they make things up.
44.5

exact match on Natural Questions for RAG-Sequence, a new best.

  • They joined a retriever with a text generator, and trained both together.
  • The memory was a search index of 21M Wikipedia passages.
  • They tested question answering, question writing and fact checking.
  • They also tested swapping the index and removing parts.

  • RAG set new bests on three open-domain QA tasks.
  • RAG text was more factual, specific and diverse than BART.
  • Learned retrieval beat keyword search on most tasks.
  • Swapping the index updated the model's facts.

RAG beats closed-book and open-book models on Natural QuestionsFrom Table 1
015304560Exact match (EM) on Natural Questions testT5-11B (closed-book)34.5 EMT5-11B+SSM (closed-book)36.6 EMREALM40.4 EMDPR41.5 EMRAG-Token44.1 EMRAG-Sequence44.5 EM

Hover, tap or focus a bar to read its exact value.

ModelExact match
T5-11B (closed-book)34.5
T5-11B+SSM (closed-book)36.6
REALM40.4
DPR41.5
RAG-Token44.1
RAG-Sequence44.5

  • Wikipedia is never fully factual and carries bias.
  • Retrieval can collapse and return the same passages every time.
  • RAG still produced wrong text sometimes.
  • Results use one December 2018 Wikipedia dump.

Jump to

  1. A The abstract
  2. 1 Introduction
  3. 2 Methods
  4. 2.1 Models: RAG-Sequence and RAG-Token
  5. 2.2 Retriever: DPR
  6. 2.3 Generator: BART
  7. 2.4 Training
  8. 2.5 Decoding
  9. 3 Experiments
  10. 4 Results
  11. 4.5 Additional results
  12. 5 Related work
  13. 6 Discussion
  14. B Broader impact
  15. A-I Appendices A to I (summary)
  16. W Words to know

Every detail from the paper

Cards follow the paper's own order. Dotted words link to Words to know. Numbers match the paper.

The problem

  • Large pre-trained models store facts in their parameters.
  • They do well when fine-tuned on tasks.
  • But they are limited at reaching and using knowledge precisely.
  • On knowledge-heavy tasks they lag task-specific systems.
  • Giving the source of a decision and updating world knowledge are open problems.
  • Earlier models with a search memory only did extractive tasks.

The method

  • They explore a general fine-tuning recipe: retrieval-augmented generation (RAG).
  • RAG joins parametric memory with non-parametric memory.
  • The parametric memory is a pre-trained seq2seq model.
  • The non-parametric memory is a dense vector index of Wikipedia.
  • A pre-trained neural retriever reads the index.
  • They compare two forms. One uses the same passages for the whole output.
  • The other can use different passages for each token.

The results

  • They set a new best on three open-domain QA tasks.
  • They beat parametric seq2seq models and task-specific retrieve-and-extract systems.
  • For language generation, RAG output is more specific, diverse and factual than a parametric-only baseline.

The setting

  • Pre-trained language models learn a lot of knowledge from data.
  • They act as a parameterized implicit knowledge base.
  • They cannot easily expand or revise their memory.
  • They cannot easily explain their predictions.
  • They may produce 'hallucinations'.

Hybrid models

  • Hybrids join parametric memory with retrieval-based memory.
  • Knowledge can be revised and expanded directly.
  • Accessed knowledge can be inspected.
  • REALM and ORQA joined masked language models with a trainable retriever.
  • They tested only open-domain extractive question answering.

This paper

  • The authors bring hybrid memory to seq2seq models, the 'workhorse of NLP'.
  • The parametric memory is a pre-trained seq2seq transformer.
  • The non-parametric memory is a dense vector index of Wikipedia.
  • The retriever is Dense Passage Retriever (DPR). The generator is BART.
  • They train everything end to end as one probabilistic model.
  • The retrieved passages are a latent variable, summed out over the top K.

Two ways to sum

  • Per output: the same passage is responsible for all tokens.
  • Per token: different passages are responsible for different tokens.
  • Like T5 or BART, RAG fine-tunes on any seq2seq task.
  • Both the generator and the retriever are learned together.

Earlier work

  • Earlier systems added memory but trained from scratch for one task.
  • Examples: memory networks, stack-augmented networks and memory layers.
  • Here both memories are pre-trained and pre-loaded with knowledge.
  • So the access ability is already there, and it needs no new training for access.

Findings

  • Knowledge-intensive tasks are ones people could not do without an outside source.
  • RAG sets bests on open Natural Questions, WebQuestions and CuratedTrec.
  • It strongly beats models with special pre-training on TriviaQA.
  • Free generation beats earlier extractive models, even on extractive tasks.
  • On MS-MARCO and Jeopardy question generation, RAG is more factual, specific and diverse than BART.
  • On FEVER fact checking it is within 4.3% of the best pipeline models.
  • The non-parametric memory can be replaced to update the model's knowledge.

Code

  • Code is open-sourced in the HuggingFace Transformers library.
  • An interactive demo is at huggingface.co/rag.

Overview

  • RAG uses an input x to fetch text passages z.
  • It uses them as extra context to write the target y.
  • It has two parts.
  • A retriever p_η(z|x) with parameters η gives top-K distributions over passages.
  • A generator p_θ(y_i | x, z, earlier tokens) writes the next token.
How RAG answers a questionFrom Figure 1
  1. Query encoder reads the question
  2. Search the index (MIPS)
  3. Take the top K passages
  4. BART reads question and passage
  5. Sum answers over passages

The passage is a latent variable. No one labels which passage is right.

Training idea

  • To train the retriever and generator together, passages are a latent variable.
  • They propose two ways to marginalize, meaning to sum over passages.

RAG-Sequence

  • It uses the same passage to write the whole sequence.
  • The passage is one latent variable, summed out by the top-K estimate.
  • The retriever finds the top K passages.
  • The generator gives a sequence probability for each passage.
  • These are then combined.

RAG-Token

  • It can draw a different passage for each target token.
  • So the generator can use several passages in one answer.
  • For each token, it gets a next-token distribution per passage.
  • It then combines them and repeats for the next token.

Classification

  • RAG can classify too. The class is a target sequence of length one.
  • Then RAG-Sequence and RAG-Token are the same.

Design

  • The retriever is based on DPR, a bi-encoder.
  • A BERT BASE document encoder makes d(z), a vector for each passage.
  • A query encoder, also BERT BASE, makes q(x).
  • Finding the top-k passages is a Maximum Inner Product Search problem.
  • It can be solved approximately in sub-linear time.

Setup

  • A pre-trained DPR bi-encoder starts the retriever and builds the index.
  • It was trained to find passages that hold answers to TriviaQA and Natural Questions.
  • The document index is the non-parametric memory.

Design

  • The generator can be any encoder-decoder.
  • They use BART-large, a pre-trained seq2seq transformer with 400M parameters.
  • To mix the input x with passage z, they simply join them.
  • BART was pre-trained with a denoising goal and many kinds of noise.
  • It beat T5 models of similar size on many generation tasks.
  • The BART parameters θ are the parametric memory.

How RAG is trainedFrom Section 2.4
  1. Pair inputs with target outputs
  2. No labels for which passage
  3. Keep document encoder fixed
  4. Fine-tune query encoder and BART
  5. Minimize negative log-likelihood

Training uses stochastic gradient descent with Adam.

Details

  • They train the retriever and generator together.
  • There is no direct supervision on which passage to retrieve.
  • The loss is the negative marginal log-likelihood of each target.
  • Updating the document encoder is costly. It forces periodic re-indexing, as REALM does.
  • They found this unnecessary for strong results.
  • So the document encoder and index stay fixed.
  • They fine-tune only the query encoder BERT_q and the BART generator.

RAG-Token

  • It is a standard autoregressive seq2seq generator.
  • Its next-token chance sums over the top-k passages.
  • You can plug it into a standard beam decoder.

RAG-Sequence

  • The likelihood does not split by token. So one beam search will not do.
  • They run a beam search for each passage.
  • Each guess is scored with the generator.
  • To rate a guess missing from some beams, they run extra forward passes.
  • They multiply by the retriever chance and sum across beams.
  • This is 'Thorough Decoding'. It can be costly for long outputs.
  • 'Fast Decoding' assumes zero chance for guesses not seen in beam search.
  • This avoids the extra forward passes.

The knowledge source

  • One Wikipedia dump: December 2018.
  • Each article is cut into disjoint 100-word chunks.
  • That gives 21M documents.
  • The document encoder embeds each one.
  • They build one MIPS index with FAISS and a Hierarchical Navigable Small World approximation.
  • In training they fetch the top k documents. They try k of 5 and 10.
  • They set k at test time using dev data.
3.1 Open-domain question answering
  • Questions and answers are input-output text pairs.
  • They train RAG by minimizing the negative log-likelihood of answers.
  • They compare with extractive QA, which pulls answer spans from passages.
  • They also compare with closed-book QA, which generates answers without retrieval.
  • Data sets: Natural Questions, TriviaQA, WebQuestions and CuratedTrec.
  • CT and WQ are small.
  • They start from the trained NQ RAG model, as DPR does.
  • They use the same splits as prior work and report Exact Match.
  • For TriviaQA, they also use the TQA Wiki test set, to compare with T5.
3.2 Abstractive question answering
  • RAG can answer with free-form text.
  • They test on MSMARCO NLG v2.1.
  • It has questions, ten gold passages each, and a full sentence answer.
  • They do not use the gold passages. They treat it as open-domain abstractive QA.
  • Some questions cannot be answered without the gold passages.
  • Example: 'What is the weather in Volcano, CA?'
  • So scores will be lower.
  • Some questions cannot be answered from Wikipedia alone.
  • RAG then relies on its parametric knowledge.
3.3 Jeopardy question generation

The task

  • Jeopardy asks you to guess an entity from a fact about it.
  • Here the model writes the question from the answer entity.
  • These are precise, factual statements. That makes a harder test.
  • Example answer: 'The World Cup'.

The setup

  • They use SearchQA splits: 100K train, 14K dev and 27K test.
  • They train a BART model as a baseline.
  • The metric is SQuAD-tuned Q-BLEU-1, which weights entity matches more.
  • They also run two human tests, for factuality and for specificity.
  • Factuality: can trusted outside sources support a statement.
  • Specificity: a strong link between input and output.
  • Judges see an answer and two questions, one from BART and one from RAG.
  • They pick A, B, both good or neither good.
3.4 Fact verification (FEVER)
  • FEVER asks if Wikipedia supports, refutes or lacks info on a claim.
  • It joins retrieval with a hard reasoning task.
  • They map the 3 labels to single tokens and train on claim-label pairs.
  • They use no supervision on retrieved evidence.
  • Many real uses lack such signals.
  • They test 3-way (supports, refutes, not enough info) and 2-way (supports, refutes).
  • They report label accuracy.

4.1 Open-domain question answering

What they found

  • On all four tasks RAG sets a new best.
  • For TriviaQA the best applies only on the T5-comparable split.
  • RAG mixes the freedom of closed-book models with the strength of open-book models.
  • It needs no costly 'salient span masking' pre-training, unlike REALM and T5+SSM.
  • Its retriever starts from DPR, which had retrieval supervision on NQ and TriviaQA.
  • It matches the DPR QA system without a re-ranker or an extractive reader.

Why generating helps

  • Documents with clues but no verbatim answer can still help.
  • Extractive models cannot use them.
  • RAG can answer correctly when no retrieved passage has the answer.
  • It gets 11.8% accuracy in such cases on NQ. An extractive model gets 0%.
RAG leads on Natural QuestionsFrom Table 1
015304560Exact match (EM) on Natural Questions testT5-11B (closed-book)34.5 EMT5-11B+SSM (closed-book)36.6 EMREALM40.4 EMDPR41.5 EMRAG-Token44.1 EMRAG-Sequence44.5 EM

Hover, tap or focus a bar to read its exact value.

ModelExact match
T5-11B (closed-book)34.5
T5-11B+SSM (closed-book)36.6
REALM40.4
DPR41.5
RAG-Token44.1
RAG-Sequence44.5
ModelNQTQA (std / Wiki)WQ / CT
T5-11B (closed-book)34.5- / 50.137.4 / -
T5-11B+SSM (closed-book)36.6- / 60.544.7 / -
REALM (open-book)40.4- / -40.7 / 46.8
DPR (open-book)41.557.9 / -41.1 / 50.6
RAG-Token44.155.2 / 66.145.5 / 50.0
RAG-Sequence44.556.8 / 68.045.2 / 52.2

Table 1: open-domain QA test scores (exact match)

WQ is WebQuestions. CT is CuratedTrec. For TQA, the first number is the standard test set and the second is the TQA-Wiki test set.

4.2 Abstractive question answering
  • RAG-Sequence beats BART on Open MS-MARCO NLG by 2.6 Bleu and 2.6 Rouge-L.
  • It nears the best models, which use gold passages.
  • Those models also face questions that are unanswerable without those passages.
  • RAG hallucinates less and gives factual text more often than BART.
  • Table 3 in the paper shows example answers.
4.3 Jeopardy question generation

Scores

  • RAG-Token does better than RAG-Sequence.
  • Both beat BART on Q-BLEU-1.

Human tests

  • They compared 452 pairs of BART and RAG-Token output.
  • BART was more factual in only 7.1% of cases.
  • RAG was more factual in 42.7%.
  • Both were factual in a further 17%.
  • RAG was also found more specific by a large margin.
JudgmentFactualitySpecificity
BART better7.1%16.8%
RAG better42.7%37.4%
Both good11.7%11.8%
Both poor17.7%6.9%
No majority20.8%20.1%

Table 4: human assessment on Jeopardy

4.4 Fact verification
  • For 3-way classification, RAG is within 4.3% of the best.
  • That best model uses a complex pipeline with strong retrieval supervision.
  • For 2-way classification, it is within 2.7% of a RoBERTa model.
  • That model gets the gold evidence sentence. RAG got only the claim.
  • The top retrieved document comes from a gold article in 71% of cases.
  • A gold article is in the top 10 in 90% of cases.
ModelJeopardy B-1 / QB-1MSMARCO R-L / B-1FEVER-3 / FEVER-2
SotA- / -49.8* / 49.9*76.8 / 92.2*
BART15.1 / 19.738.2 / 41.664.0 / 81.1
RAG-Token17.3 / 22.240.1 / 41.572.5 / 89.5
RAG-Sequence14.7 / 21.440.8 / 44.272.5 / 89.5

Table 2: generation and classification test scores

A star means the model uses gold context or evidence. FEVER scores are the same for both RAG models, as it is a classification task.

Generation diversity
  • They measure diversity as distinct n-grams over total n-grams.
  • RAG-Sequence is more diverse than RAG-Token.
  • Both are much more diverse than BART, with no diversity-boosting decoding.
RAG writes more varied Jeopardy questions than BARTFrom Table 5
0255075100Ratio of distinct to total tri-grams (%)BART32.4%RAG-Token46.8%RAG-Sequence53.8%Gold (human)90.0%

Hover, tap or focus a bar to read its exact value.

ModelDistinct tri-grams (%)
BART32.4
RAG-Token46.8
RAG-Sequence53.8
Gold (human)90.0
ModelMSMARCOJeopardy QGen
Gold89.6%90.0%
BART70.7%32.4%
RAG-Token77.8%46.8%
RAG-Sequence83.5%53.8%

Table 5: ratio of distinct to total tri-grams

Retrieval ablations
  • They freeze the retriever during training to test learned retrieval.
  • Learned retrieval improves results on all tasks.
  • They also swap in BM25, a word-overlap retriever.
  • BM25 scores are used as logits for p(z|x).
  • On FEVER, BM25 does best. FEVER claims are heavy on entities, which suits word overlap.
  • On other tasks, trainable retrieval helps. It is crucial for open-domain QA.
Learned retrieval beats BM25 and frozen retrievalFrom Table 6
BM25FrozenLearned
015304560Exact match, dev set (RAG-Sequence)NQ31.8 EM41.2 EM44.0 EMTQA44.1 EM52.1 EM55.8 EMWQ36.6 EM41.8 EM44.9 EMCT33.8 EM52.6 EM53.4 EM

Hover, tap or focus a bar to read its exact value.

TaskBM25 (EM)Frozen (EM)Learned (EM)
NQ31.841.244.0
TQA44.152.155.8
WQ36.641.844.9
CT33.852.653.4
ModelNQ / TQAWQ / CTFVR-3 / FVR-2
RAG-Token-BM2529.7 / 41.532.1 / 33.175.1 / 91.6
RAG-Sequence-BM2531.8 / 44.136.6 / 33.875.1 / 91.6
RAG-Token-Frozen37.8 / 50.137.1 / 51.172.9 / 89.4
RAG-Sequence-Frozen41.2 / 52.141.8 / 52.672.9 / 89.4
RAG-Token43.5 / 54.846.5 / 51.974.5 / 90.6
RAG-Sequence44.0 / 55.844.9 / 53.474.5 / 90.6

Table 6: ablations on the dev set (exact match)

Both RAG forms share one FEVER score, as it is a classification task. Jeopardy and MSMarco columns are in the paper.

Index hot-swapping
  • Parametric-only models need more training to update their facts.
  • They built an index from the DrQA Wikipedia dump of December 2016.
  • They compared it to the December 2018 index.
  • They listed 82 world leaders who changed between these dates.
  • They asked 'Who is {position}?' to an NQ RAG model.
  • With the 2016 index, 70% were right for 2016 leaders.
  • With the 2018 index, 68% were right for 2018 leaders.
  • Mismatched indices scored low: 12% and 4%.
  • So replacing the non-parametric memory updates the model's knowledge.
Effect of retrieving more documents
  • Models trained with 5 or 10 passages show no big difference.
  • At test time you can change how many passages you fetch.
  • More passages always help RAG-Sequence on open-domain QA.
  • RAG-Token peaks at 10 retrieved passages.
  • More passages raise Rouge-L for RAG-Token, at a cost to Bleu-1.
  • The effect is smaller for RAG-Sequence.

Single-task retrieval

  • Retrieval helped many tasks when used alone.
  • Examples: open-domain QA, fact checking, long-form QA, dialogue, translation and language modeling.
  • This work unifies them in one retrieval-based design for several tasks.

General-purpose architectures

  • One pre-trained model can do many tasks, such as with GLUE or GPT-2.
  • BART and T5 use one encoder-decoder for many tasks.
  • RAG adds a learned retrieval module to these generative models.

Learned retrieval and memory

  • Past work tuned retrievers for one task using search, reinforcement learning or latent variables.
  • Here one design fits many tasks.
  • The document index is like a large external memory, as in memory networks.
  • Its memory is raw text, so people can read and write it.
  • That gives interpretability and lets you edit the index to update the model.

Retrieve-and-edit

  • Retrieve-and-edit finds a similar training pair and edits it.
  • RAG instead combines content from several passages.
  • It also learns retrieval and fetches evidence documents.

What they showed

  • They built hybrid models with parametric and non-parametric memory.
  • RAG reaches the best results on open-domain QA.
  • People prefer its output to parametric-only BART, as more factual and specific.
  • They studied the learned retriever in depth.
  • They showed the index can be hot-swapped with no retraining.

Future work

  • Maybe both parts could be pre-trained together from scratch.
  • The work opens questions on how the two memories interact.

Benefits

  • RAG is grounded in real facts from Wikipedia. It hallucinates less.
  • It offers more control and interpretability.
  • Examples: a medical index for medical questions, or help for people to be more informed.

Risks

  • No outside knowledge source is fully factual or free of bias.
  • RAG can act as a language model.
  • So GPT-2 type concerns apply, to a lesser extent.
  • It could make fake news, impersonate people or automate spam.
  • It may also help automate jobs.
  • AI systems could be used to fight such misuse.

Implementation details

  • QA test numbers: 15 passages for RAG-Token, 50 for RAG-Sequence.
  • QA uses Thorough Decoding and greedy decoding.
  • MS-MARCO and Jeopardy use ten passages, beam size four and Fast Decoding.
  • They train a BART-large baseline for these two tasks.

Human evaluation and training setup

  • Judges were shown two answers in random order, plus some gold checks.
  • Two annotators did poorly on gold checks and were removed.
  • Training used Fairseq on 8 NVIDIA V100 GPUs with 32GB each.
  • FAISS search runs on CPU. The full index needs about 100 GB of CPU memory.
  • A compressed index needs 36GB.

Open-domain QA details

  • NQ and WQ use each (question, answer) pair separately in training.
  • TriviaQA answers are filtered if they do not appear in the passages.
  • CuratedTrec answers are regular expressions.
  • They fetch the top 1000 documents and use the most common match.
  • TQA scores are much higher on the official Wiki test set.
  • The open-domain test set gives lower scores.

FEVER and null document

  • FEVER: they re-generate the claim, then classify from the final hidden state.
  • They did not tackle the evidence extraction sub-task.
  • They tried a 'null document' idea, to cover cases with no useful passage.

Parameters

  • RAG has 110M parameters for each BERT-base encoder, though the document encoder is not trained.
  • BART-large has 406M parameters. The trainable total is 626M.
  • T5-large has 770M parameters and scores 28.9 EM on NQ, far below 44.5 for RAG-Sequence.
  • The index holds 21M vectors of 728 dimensions, which is 15.3B values.

Retrieval collapse

  • In story generation, retrieval 'collapsed' and always gave the same passages.
  • The generator then ignored them, and RAG matched BART.
  • Causes may be a weak need for facts, or long targets with weak gradients.

Data set sizes

  • Table 7 lists train, dev and test counts.
  • Examples: Natural Questions 79169, 8758 and 3611.
  • MS-MARCO has 153726 train and 12468 dev instances.

Words to know

  • Maximum Inner Product Search: Finding the stored vectors that best match a query vector.
  • Non-parametric memory: Facts stored outside the model, such as in a search index.
  • Parametric memory: Facts stored inside the weights of a model.
  • RAG: A model that searches a text index, then writes using what it found.

Share this paper

LinkedIn (opens in a new tab)X (opens in a new tab)WhatsApp (opens in a new tab)EmailDownload PDFExplainer (PDF), 605 KB

Ask AI about this paper

Stuck on a word or an idea? Ask in plain words. The answer comes from the write-up on this page.

Read the original paper on arXiv: Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks (opens in a new tab)

Jump to

One paper a week, in plain English

Every Monday: the new AI papers worth your time. Short lines, the key number, and what to do with it.

One email a week. Unsubscribe with one click. No spam.