Bookmarks

RAG — retrieval-augmented generation

A way of working with a language model: before answering, the system searches a database for suitable fragments of documents and passes them to the model together with the question.

Also: retrieval-augmented generation, RAG approach, RAG pipeline

checked 9 Nov

All terms

RAG addresses the main weakness of a language model: its knowledge stops at its training date, and on narrow or fresh topics the model makes things up. Instead of retraining it, the model is supplied with text retrieved from a database — corporate documents, handbooks, instructions — and answers on the basis of that text.

How the pipeline works

The knowledge base is prepared in advance: documents are cut into chunks of a few hundred tokens each, embeddings are computed for them, and the chunks are stored in a vector database. Then, for each question:

  1. the question is turned into an embedding and the nearest chunks are retrieved — by meaning, not by word matches;
  2. the retrieved chunks are inserted into the prompt together with the question: the context grows by hundreds and thousands of tokens;
  3. the model generates an answer from the supplied text — in more advanced schemes it also marks which chunks it relied on.

What it provides

A model’s problem without retrievalWhat RAG changes
Knowledge stops at the training dateThe documents in the database can be updated at any time; the model is not retrained
Hallucinations on narrow questionsThe answer rests on retrieved text rather than the model’s “memory”
The answer has nothing to show in supportIt is visible which documents the answer was assembled from — the answer can be verified
A separate model for each topicOne model plus a replaceable knowledge base: swap the documents and the expertise changes

This is how corporate assistants, support bots, documentation search and answers on fresh data that was not in the model’s training are built.

Where the term comes from

The term was proposed by Patrick Lewis and colleagues in the 2020 paper “Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks”: a generator was combined with a dense vector index of Wikipedia articles, and on question-answering tasks such models outperformed the same models without retrieval.

Sources

the term is written from these pages

Verified 9 Nov

Not to be confused with

what it is mixed up with and how it differs

  • fine-tuning

    Fine-tuning changes the model’s weights and teaches it skills; RAG does not change the model, it merely supplies the needed documents in the context.

  • vector database

    A vector database is one component of the RAG pipeline, a store of embeddings for retrieval, not the whole approach.

In depth in the references

where the topic is covered in detail