Retrieval-Augmented Generation (RAG)
Retrieval-Augmented Generation (RAG) is a hybrid approach that enhances large pre-trained language models by combining:
- Parametric memory: A pre-trained sequence-to-sequence (seq2seq) transformer (e.g., BART or T5) that generates responses.
- Non-parametric memory: A retriever that fetches relevant documents from an external knowledge base (e.g., Wikipedia).
RAG retrieves relevant passages at inference time and conditions generation on this external knowledge, leading to more accurate, interpretable, and factually grounded outputs.
Key Features of RAG
- Retrieves external knowledge: Instead of relying only on model parameters, RAG fetches top-K relevant documents dynamically.
- Supports end-to-end learning: The retriever and generator are jointly fine-tuned to improve performance.
- Enhances factual accuracy: Reduces hallucinations by conditioning responses on retrieved evidence.
- Allows knowledge updating: By swapping the document index, RAG can update its knowledge without retraining the model.
- Works across multiple NLP tasks: Used for question answering, fact verification, and text generation.
How Does RAG Differ from Existing Methods
Unlike static LLMs, which rely on pre-trained knowledge alone, RAG dynamically retrieves relevant facts during inference. Unlike DPR (Retriever-Reader), which extracts answers from retrieved documents, RAG generates fluent, contextual responses.
How Does RAG Work?
RAG follows a retrieval-generation pipeline that can be fine-tuned end-to-end.
Step-by-Step Process
- Retrieve documents: A query encoder (Dense Passage Retriever, DPR) converts the input query into an embedding and retrieves top-K relevant documents from a pre-indexed knowledge base.
- Condition on retrieved knowledge:
- The seq2seq generator (BART) takes both the query and retrieved documents as input.
- It conditions its output on the retrieved information to generate an answer.
- Marginalize over retrieved passages:
- Two retrieval strategies:
- RAG-Sequence: Uses one retrieved document for the entire generation.
- RAG-Token: Allows the generator to use different documents per token (more flexible).
- Two retrieval strategies:
- Generate the final output: The model integrates retrieved knowledge and generates a well-informed response.
RAG is available as part of Hugging Face Transformers: GitHub Repository
Advantages of RAG
Feature | Description |
---|---|
Better factual accuracy | · Reduces hallucinations by grounding responses in retrieved documents. · More trustworthy for knowledge-based tasks. |
Easy knowledge updates | · Unlike GPT-3/T5, which require retraining, RAG can update its knowledge base simply by changing the document index. |
End-to-end fine-tuning | · Both retriever and generator can be trained together, optimizing retrieval relevance and answer quality. |
Strong results across tasks | · Excels in open-domain QA, knowledge verification, and knowledge generation. |
Conclusion
RAG represents a major step forward in Retrieval-Augmented Generation, improving factual accuracy, adaptability, and task performance. By combining pre-trained language models with real-time document retrieval, RAG enables NLP models to be more knowledgeable, reliable, and versatile.
Valeriia Kuka
Valeriia Kuka, Head of Content at Learn Prompting, is passionate about making AI and ML accessible. Valeriia previously grew a 60K+ follower AI-focused social media account, earning reposts from Stanford NLP, Amazon Research, Hugging Face, and AI researchers. She has also worked with AI/ML newsletters and global communities with 100K+ members and authored clear and concise explainers and historical articles.
Footnotes
-
Lewis, P., Perez, E., Piktus, A., Petroni, F., Karpukhin, V., Goyal, N., Küttler, H., Lewis, M., tau Wen-Yih, Rocktäschel, T., Riedel, S., & Kiela, D. (2021). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. https://arxiv.org/abs/2005.11401 ↩