Retrieval-Augmented Generation: Giving a Language Model Documents to Read Before It Answers

What a Language Model Does Not Know

A large language model stores what it knows in its weights, and those weights stop changing when training ends. That has three consequences. The model knows nothing about events after its training cutoff. It knows nothing about private information, such as a company’s internal manuals or a hospital’s protocols. And when asked about something it has little information on, it often produces a fluent, confident and wrong answer, the behavior usually called hallucination.

Retraining a model every time a document changes is too expensive to be practical. Retrieval-augmented generation (RAG) takes a different route. When a question arrives, the system searches a collection of documents for relevant passages, places them in the prompt together with the question, and asks the model to answer using that material. The knowledge sits in a database that can be updated at any time, and the model’s job shifts from recalling facts to reading and summarizing them.

The 2020 Paper

The name comes from «Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks,» by Patrick Lewis and colleagues at Facebook AI Research, University College London and New York University, presented at NeurIPS 2020. Their system combined two trained components: a retriever based on Dense Passage Retrieval (DPR), which searched an index of about 21 million Wikipedia passages, and BART, a sequence-to-sequence model that generated the answer conditioned on the retrieved passages. The two were fine-tuned together.

The paper described the combination as parametric memory (the weights of the generator) plus non-parametric memory (the document index). It set state-of-the-art results on several open-domain question answering benchmarks and generated text that was more specific and factual than BART alone. It also pointed out a property that later became the main selling point: the index can be swapped to update what the system knows without retraining anything.

Modern RAG systems rarely train the retriever and generator jointly. They connect an off-the-shelf embedding model, a vector database and a general-purpose language model. The principle is the same.

Turning Documents Into a Searchable Index

Before any question can be answered, the documents have to be prepared. They are first split into chunks, pieces of a few hundred words each. Chunking matters more than it seems. Chunks that are too large dilute the relevant sentence among unrelated text. Chunks that are too small lose the context needed to understand them (a paragraph that says «this method» without saying which one).

Each chunk is then passed through an embedding model, which turns it into a vector of a few hundred to a few thousand numbers. The model is trained so that texts with similar meaning get nearby vectors. The vectors are stored in a vector database along with the original text. This step is done once, and repeated only for documents that change.

Sparse and Dense Retrieval

There are two families of search methods. Sparse retrieval matches words. Its standard algorithm is BM25, developed by Stephen Robertson and colleagues in the 1990s, which scores a document by how often the query’s terms appear in it, weighted by how rare each term is across the collection. It is fast, needs no training, and is very good with exact terms like product codes, names and error messages.

Dense retrieval compares embeddings. Karpukhin and colleagues introduced DPR in 2020, training two BERT encoders, one for questions and one for passages, so that a question and the passage that answers it end up close together. On open-domain question answering datasets, DPR beat a strong BM25 system by 9 to 19 percentage points in top-20 retrieval accuracy. Dense retrieval can match a question to a passage that shares no words with it, which sparse methods cannot do.

Each approach fails where the other works, so production systems often run both and merge the results, a setup called hybrid search. Searching millions of vectors exactly would be slow, so vector databases use approximate nearest neighbor indexes such as HNSW (Malkov and Yashunin), which trade a small loss in accuracy for a large gain in speed.

Reranking and Building the Prompt

The first retrieval step is tuned for speed and returns maybe 50 to 100 candidates. A second model, a reranker, then scores each candidate against the question more carefully. Rerankers are usually cross-encoders: they read the question and the passage together, which is more accurate than comparing two separately computed vectors but too slow to run on the whole collection.

The top few passages are inserted into the prompt with an instruction along the lines of «answer using only the following sources and cite them.» The order matters. In «Lost in the Middle» (2023), Nelson Liu and colleagues showed that language models use information at the beginning and end of a long context much better than information in the middle, with accuracy dropping noticeably when the relevant passage was placed in the center.

Ways RAG Fails

RAG reduces hallucinations, but it does not eliminate them. Failures happen at each stage. The retriever can miss the relevant passage, and then the model answers from memory or invents something. The retriever can return a passage that looks related but is about something else, and the model follows it. The correct passage can be retrieved and then misread or ignored. And some questions need information scattered across many documents («what are the main themes in these 500 reports?»), which retrieving five chunks will not answer.

A study from Stanford’s RegLab, by Varun Magesh and colleagues, tested commercial legal research tools built on RAG, whose vendors had advertised them as avoiding hallucinations. On about 200 legal queries scored by hand, the tools still gave incorrect or misgrounded answers a large share of the time: about 17% for Lexis+ AI and about 33% for Westlaw AI-Assisted Research. Those numbers were better than GPT-4 without retrieval, at 43%, but far from zero. In many cases the tools cited real documents that did not support the claim attributed to them.

Refinements

Much recent work targets those failure points. Query rewriting has a language model reformulate the user’s question, or break it into sub-questions, before searching. Self-RAG, from Akari Asai and colleagues (ICLR 2024), trains the model to decide when retrieval is needed and to critique whether the retrieved passages are relevant and whether its own answer is supported by them. GraphRAG, from Microsoft Research in 2024, uses a language model to extract entities and relationships from the documents into a knowledge graph and writes summaries of clusters in that graph, which helps with questions about the collection as a whole.

Agentic approaches let the model run several searches in sequence, read the results, and decide what to look for next, closer to how a person researches a topic.

RAG, Fine-Tuning and Long Context

RAG is not the only way to adapt a model to new knowledge. Fine-tuning changes the weights, which works well for teaching a style, a format or a skill, but poorly for facts that change often, and it gives no source to cite. Context windows have grown to hundreds of thousands of tokens, so small collections can be placed directly in the prompt, at a higher cost per query and with the middle-of-context problem described above. For large or frequently updated collections, and wherever answers need to point to their sources, retrieval is still the practical option. Many systems combine the three.

Evaluation

A RAG system has to be evaluated in two parts. Retrieval is measured with metrics like recall at k: of the questions in a test set, how often is the passage with the answer among the top k results. Generation is measured on faithfulness (is every claim in the answer supported by the retrieved passages) and on whether the answer addresses the question. Looking only at the final answer hides which part is failing, and a wrong answer caused by bad retrieval needs a different fix than one caused by the model misreading a good passage.

A Final Observation

RAG changes where the difficulty of the problem is. The language model no longer has to know the answer, but the system has to find the right paragraph among millions, and most of the errors come from that search step. Search has been studied for decades in information retrieval, long before language models existed. A team building a RAG system will usually get more out of improving chunking, indexing and ranking than out of switching to a larger model.

By: Max Johnson B.

Comentarios

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *