Algorithm Search⌘K

Techniqueresearch

Retrieval-augmented generation

Also called retrieval-augmented language model.

Retrieves passages for an input and conditions a text generator on them, so the output can use knowledge outside the model weights.

Retrieval-augmented generation (RAG) answers an input xx in two steps: a retriever finds the top kk passages zz for xx in a large text collection, and a generator produces the output yy conditioned on xx and those passages. In the model of Lewis et al. (2020), the retriever is a dense passage retrieval dual encoder with pη(z∣x)∝exp⁡(d(z)⊤q(x))p_\eta(z \mid x) \propto \exp(d(z)^\top q(x)), and the output probability is a marginal over the retrieved passages, so the retriever's query encoder can be trained through the generator's likelihood without labels for which passage is right. The DPR page trains the retriever with known positive passages; this page is about using retrieval inside a generator: the exact marginal objectives, the gradient that reaches the retriever, and the ways the method fails. The generator, the encoders, and the factual accuracy of the outputs are outside this page.

In plain words

A text generator only knows what it learned during training, and that knowledge goes out of date. Retrieval-augmented generation gives it a library. For each question, a search step finds a few passages that look useful. The generator reads the question together with each passage and writes the answer. Passages that the search step trusts more count more. When a passage helps the generator produce the right answer during training, the search step learns to trust passages like it more. To update what the system knows, we can change the library without retraining the generator.

When it applies

The technique applies when:

  • the task needs knowledge that a text collection holds, such as open-domain questions, fact checking or grounded dialogue;
  • there is a retriever whose score for a passage is a function of the input and the passage, so that the top kk passages can be found quickly, usually by maximum inner product search over precomputed passage vectors;
  • the generator can take the input and one passage, or several passages, as its conditioning text;
  • training pairs (x,y)(x, y) exist, but labels for which passage supports yy need not.

It does not make outputs correct: the generator can ignore the passages or misread them, and the retriever can return wrong ones.

The template

Let Zk(x)Z_k(x) be the top kk passages by retriever score s(z)=d(z)⊤q(x)s(z) = d(z)^\top q(x), and pη(z∣x)=es(z)/∑z′∈Zk(x)es(z′)p_\eta(z \mid x) = e^{s(z)} / \sum_{z' \in Z_k(x)} e^{s(z')}. The two models of Lewis et al. (2020) are

pseq(y∣x)=∑z∈Zk(x)pη(z∣x)∏ipθ(yi∣x,z,y<i),p_{\text{seq}}(y \mid x) = \sum_{z \in Z_k(x)} p_\eta(z \mid x) \prod_{i} p_\theta(y_i \mid x, z, y_{<i}),
ptok(y∣x)=∏i∑z∈Zk(x)pη(z∣x) pθ(yi∣x,z,y<i).p_{\text{tok}}(y \mid x) = \prod_{i} \sum_{z \in Z_k(x)} p_\eta(z \mid x)\, p_\theta(y_i \mid x, z, y_{<i}).

RAG-Sequence uses one passage for the whole output and mixes at the end; RAG-Token may draw each token from a different passage.

  1. RAG-TRAIN-STEP(x, y, k)
  2. 1u ← q(x) -- query encoder, trained
  3. 2Z ← the k passages with the largest d(z) · u -- passage vectors d(z) precomputed and frozen
  4. 3π(z) ← softmax over Z of d(z) · u
  5. 4for each z ∈ Z: a(z) ← Π_i p_θ(y_i | x, z, y_<i) -- one generator pass per passage
  6. 5loss ← -log Σ_z π(z) a(z) -- RAG-Sequence; RAG-Token mixes inside the product
  7. 6back-propagate into p_θ and into q, not into d

At inference, RAG-Token can use ordinary beam search over the mixed token distribution. RAG-Sequence does not factor over tokens; Lewis et al. (2020) decode with a beam for each passage and then score the hypotheses, either running extra passes for hypotheses missing from some beams (Thorough Decoding) or treating those as probability 0 (Fast Decoding).

The gradient that reaches the retriever. For RAG-Sequence, with a(z)=pθ(y∣x,z)a(z) = p_\theta(y \mid x, z) and p=∑zπ(z)a(z)p = \sum_z \pi(z) a(z),

∂log⁡p∂s(z)=π(z) a(z)p−π(z)=p(z∣x,y)−pη(z∣x).\frac{\partial \log p}{\partial s(z)} = \frac{\pi(z)\, a(z)}{p} - \pi(z) = p(z \mid x, y) - p_\eta(z \mid x).

Proof. ∂π(w)/∂s(z)=π(w)([w=z]−π(z))\partial \pi(w) / \partial s(z) = \pi(w)([w = z] - \pi(z)), so ∂p/∂s(z)=∑wa(w)π(w)([w=z]−π(z))=π(z)a(z)−π(z)p\partial p / \partial s(z) = \sum_w a(w) \pi(w)([w = z] - \pi(z)) = \pi(z) a(z) - \pi(z) p. Divide by pp. □\square

So the likelihood raises the score of a passage exactly when its posterior given the output, p(z∣x,y)∝π(z)a(z)p(z \mid x, y) \propto \pi(z) a(z), exceeds its prior from the retriever. This is how the retriever learns from (x,y)(x, y) pairs alone. The gradient reaches only the passages in Zk(x)Z_k(x); a passage outside the top kk gets nothing, however useful it would be.

Classic examples

Take k=2k = 2 retrieved passages with scores s=(2.0,1.0)s = (2.0, 1.0), so pη(z∣x)=(0.7311,0.2689)p_\eta(z \mid x) = (0.7311, 0.2689), and a two-token output whose generator probabilities are (0.9,0.2)(0.9, 0.2) given passage 1 and (0.3,0.8)(0.3, 0.8) given passage 2.

quantity passage 1 passage 2 total
pη(z∣x)p_\eta(z \mid x) 0.7311 0.2689 1
pθ(y∣x,z)p_\theta(y \mid x, z) 0.18 0.24
pη(z∣x) pθ(y∣x,z)p_\eta(z \mid x)\, p_\theta(y \mid x, z) 0.1316 0.0645 pseq=0.1961p_{\text{seq}} = 0.1961
posterior p(z∣x,y)p(z \mid x, y) 0.6709 0.3291 1
∂log⁡pseq/∂s(z)\partial \log p_{\text{seq}} / \partial s(z) -0.0601 0.0601 0

RAG-Token mixes per token: (0.7311⋅0.9+0.2689⋅0.3)(0.7311⋅0.2+0.2689⋅0.8)=0.7386⋅0.3614=0.2669(0.7311 \cdot 0.9 + 0.2689 \cdot 0.3)(0.7311 \cdot 0.2 + 0.2689 \cdot 0.8) = 0.7386 \cdot 0.3614 = 0.2669, higher than pseqp_{\text{seq}} here, because each token can come from the passage that supports it best. Passage 2 explains the output better than the retriever thought, so the gradient raises its score; a finite-difference check of log⁡pseq\log p_{\text{seq}} gives the same ±0.0601\pm 0.0601.

Systems built on the template:

  • RAG with DPR and BART. Lewis et al. (2020) use the DPR retriever over 21 million Wikipedia passages of 100 words, a FAISS HNSW index, kk of 5 or 10, and a BART-large generator of about 400 million parameters, and train the query encoder and the generator while the passage encoder and index stay fixed. Table 1 of the paper reports exact match on Natural Questions of 44.1 for RAG-Token and 44.5 for RAG-Sequence, as measurements.
  • Sparse or hybrid retrieval. Replace the dense retriever by BM25, or fuse both lists with reciprocal rank fusion. The marginal still needs a score per passage to form pη(z∣x)p_\eta(z \mid x); a fixed retriever with no gradient also works, as plain retrieve-then-read.
  • Reranked context. Retrieve many passages, rerank them with a cross-encoder, and give the generator the top few. The reranker is usually trained apart from the generator.
  • Late interaction retrieval. Use ColBERT as the retriever for better recall on long or complex inputs, at the cost of a larger index.

Variations

  • Concatenated context. Put all kk passages into one long input instead of one generator pass per passage. This is common with large language models; the output no longer is an explicit marginal over passages, and the gradient formula above does not apply.
  • Joint pretraining of the retriever. Train the retriever and a language model together from the start, with a periodically refreshed index, instead of starting from a DPR retriever.
  • Retrieval at every chunk. Retrieve again during generation, conditioned on what has been written so far.
  • Updating the passage encoder. Training d(z)d(z) as well means re-encoding and re-indexing the collection, which Lewis et al. (2020) avoid by freezing it.
  • Index swaps. Replacing the collection, for example with a newer snapshot, changes the knowledge available without retraining, as long as the passage encoder is the same.

Pitfalls

  • Training the frozen side. Line L6 must not update d(z)d(z) unless the index is rebuilt; otherwise the stored vectors of line L2 no longer match the encoder, and retrieval uses stale scores.
  • Recall caps everything. If the useful passage is not in the top kk of line L2, neither the generator nor the gradient can use it. Measure the recall of the retriever separately.
  • Softmax over the wrong set. Line L3 normalizes over the kk retrieved passages. Normalizing over the whole collection is the true pη(z∣x)p_\eta(z \mid x), but it cannot be computed cheaply, and mixing the two changes the gradient.
  • Grounding is not guaranteed. A high likelihood can come from the generator's own knowledge rather than from the passage. Check whether the cited passage supports the output.
  • Costs grow with kk. Line L4 runs one generator pass per passage, so time and memory per example grow linearly in kk.
  • Leaked test answers. If the collection contains the test questions with their answers, measured accuracy reflects lookup, not reasoning.

Exercises

  1. For RAG-Token with two tokens, derive ∂log⁡ptok/∂s(z)\partial \log p_{\text{tok}} / \partial s(z) and check it in the example by finite differences.
  2. Show that pseq=ptokp_{\text{seq}} = p_{\text{tok}} when every passage gives the same generator distribution, and also when k=1k = 1.
  3. Suppose a(z)a(z) is the same for all retrieved passages. Show that the gradient of log⁡pseq\log p_{\text{seq}} in the retriever scores is 0, and explain what this means for learning a retriever from outputs that do not depend on the passage.

References

  1. 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 and Douwe Kiela, Retrieval-augmented generation for knowledge-intensive NLP tasks, Advances in Neural Information Processing Systems 33 (NeurIPS 2020), arXiv:2005.11401, pp. 9459-9474; the RAG-Sequence and RAG-Token models (the marginal over the top k retrieved documents), the retriever p(z|x) proportional to exp(d(z) . q(x)) with a DPR bi-encoder and a frozen document encoder, the BART-large generator, the Wikipedia index of 21M passages of 100 words with FAISS HNSW, Thorough and Fast decoding, Table 1 (measured exact match on open-domain question answering)
  2. Vladimir Karpukhin, Barlas Oguz, Sewon Min, Patrick Lewis, Ledell Wu, Sergey Edunov, Danqi Chen and Wen-tau Yih, Dense passage retrieval for open-domain question answering, Proceedings of EMNLP 2020, pp. 6769-6781 (the dual encoder retriever that RAG starts from)

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • five exercises