↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
Loading the recording…

Retrieve, then generate

A language model answers from what its weights hold, its parametric memory, and a small model holds little: asked when the UK formally subscribed to the Agreement on Social Policy, Qwen2.5-0.5B-Instruct on its own says 1999. Retrieval-augmented generation (RAG) gives it a non-parametric memory as well: retrieve passages that bear on the question from a collection, put them in front of the model, and let it answer from them. The collection can be updated without retraining, and the answer can point to its sources.

This tab runs the whole pipeline on the eight questions every Retrieval tab features, from the same 2,067 SQuAD paragraphs: bge-small-en-v1.5 embeds the question and exact search returns the 20 nearest paragraphs; bge-reranker-base rescores those 20; the best 3 go into Qwen2.5-0.5B-Instruct's prompt, and the model answers greedily in at most 48 tokens. Each question is also answered with bge-small's own top 3 and with no paragraphs at all.

Retrieve, then rerank

The Retrieve, then rerank panel draws the 20 retrieved paragraphs in their vector order and in the cross-encoder's order, with a line joining each paragraph's two places; "its ¶" marks the paragraph the question was written on. Only what reaches the prompt matters, so the question for the retriever is whether that paragraph is in the top 3.

On SQuAD the reranker often ranks the question's own paragraph lower than bge-small does, as the Reranker tab measures on all 300 questions, which is why the page also answers with bge-small's top 3 as they came. On question #4 the paragraph is bge-small's first and the reranker's fourth, so the reranked prompt lacks it and the model answers 1992; with bge-small's top 3 it answers 1997, exactly right. On #13 it goes the other way: fifth by vector, second after reranking, and only the reranked prompt yields the right emperor.

The prompt and the answer

The panel The prompt shows the chat-template text the model reads: Qwen's default system message, then "Answer the question using these passages.", the three passages numbered with their article titles, the question, and "Answer with a short phrase." With passages a prompt runs to between 384 and 765 tokens; without them, under 60. Order inside the prompt matters too: Liu et al. found that models use information at the start or end of a long context more reliably than in the middle, and here the best-ranked passage comes first.

The model then decodes greedily, taking the most likely token at every step,

until its end-of-turn token or 48 tokens. The Three answers panel shades each answer token by , how unsure the model was of the token it chose, and lists the five most likely candidates at any step. Lewis et al.'s original RAG instead marginalised over the retrieved passages, , training the retriever and the generator together; putting the passages into one prompt of an instruction-tuned model, with nothing trained, is the common practice today.

Scoring against SQuAD

Each answer is compared with SQuAD's reference answers after the official normalisation: lower case, punctuation and the articles a, an and the removed. Exact match asks whether the normalised strings are equal. F1 compares them as bags of words: with words in common, a prediction of words and a reference of ,

taking the best over the reference answers. The page adds contains: whether a normalised reference appears inside the answer, since a chat model often answers in a sentence.

Over the eight questions the mean F1 is 0.29 with the reranked top 3, 0.36 with bge-small's top 3 and 0.04 with no retrieval; two answers of each retrieval run contain a reference answer, none without. Retrieval is what makes the small model answer at all, but having the right paragraph is not enough: on #3 its paragraph is first in both prompts and the model says "Two" once and "Won 6 Grammy Awards" once, against the reference "Six". The second is right and scores 0, because the normalisation does not turn digits into words. Eight questions show how the pipeline behaves; they are far too few to rank its variants.

Where it came from

DrQA (Chen et al., 2017) answered open-domain questions by retrieving Wikipedia articles and reading them with a span extractor, on SQuAD among others. In 2020 REALM trained a retriever jointly with a masked language model, RAG did the same with a sequence-to-sequence generator, and Fusion-in-Decoder then showed that a generator gets better as it reads more retrieved passages. With instruction-tuned models such as Qwen2.5, retrieval became something added to a prompt rather than trained in. The other Retrieval tabs take the pipeline apart: Embeddings for the vectors, HNSW and IVF-PQ for searching them at scale, Reranker for the second stage.

Papers

  1. Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks Patrick Lewis et al. · NeurIPS 2020 Introduces RAG: a dense retriever as non-parametric memory for a generator, marginalising over the retrieved passages.
  2. Reading Wikipedia to Answer Open-Domain Questions Danqi Chen, Adam Fisch, Jason Weston, Antoine Bordes · ACL 2017 Retrieve, then read: open-domain question answering over Wikipedia with a retriever and a reader.
  3. REALM: Retrieval-Augmented Language Model Pre-Training Kelvin Guu, Kenton Lee, Zora Tung, Panupong Pasupat, Ming-Wei Chang · ICML 2020 A retriever trained jointly with a language model, by backpropagating through the choice of passage.
  4. Leveraging Passage Retrieval with Generative Models for Open Domain Question Answering Gautier Izacard, Edouard Grave · EACL 2021 Fusion-in-Decoder: a generator that reads many retrieved passages at once and improves as it reads more.
  5. Qwen2.5 Technical Report An Yang et al. · arXiv 2024 The family of Qwen2.5-0.5B-Instruct, the model that answers here.
  6. Lost in the Middle: How Language Models Use Long Contexts Nelson F. Liu et al. · TACL 2024 Models use relevant passages at the start or end of their context better than in the middle.
  7. SQuAD: 100,000+ Questions for Machine Comprehension of Text Pranav Rajpurkar, Jian Zhang, Konstantin Lopyrev, Percy Liang · EMNLP 2016 The questions, paragraphs and exact-match and F1 scoring used here.

See also