RAG
A technique for enriching the model's responses with information retrieved from an external knowledge base
What is RAG (Retrieval-Augmented Generation)?
RAG (Retrieval-Augmented Generation) is an optimization technique that combines the power of language models (LLMs) with external, private data sources. Instead of relying solely on the knowledge frozen in the model's parameters during training, a RAG system dynamically "reads up" on the information it needs before giving an answer.
Put simply: it's like giving the model an open book during an exam, instead of forcing it to recite answers from memory.
How does RAG work?
The process consists of three key stages:
- Retrieval: When the user asks a question, the system first searches an external knowledge base (most often a vector database containing embeddings). There it finds the text fragments that are semantically closest to the query.
- Augmentation: The fragments found are attached to the original prompt as "context".
- Generation: The prompt enriched in this way goes to the LLM. The model generates an answer based on the facts provided, not on its own (often outdated) training data.
Why do we use RAG?
When building AI-based apps, RAG solves two main problems:
- Hallucinations: It forces the model to stick to the facts contained in the documents provided.
- Freshness and privacy: It lets the model "see" data that was created after it was trained or that is confidential company data, without the need for an expensive Fine-tuning process.
Key components of the RAG stack
- Vector Store: E.g. Pinecone, Weaviate or Qdrant – this is where your knowledge is stored in numerical form.
- Orchestrator: A framework such as LangChain or LlamaIndex, which ties together the retrieval process and communication with the model.