RAG (Retrieval Augmented Generation)
Retrieves information from external knowledge base
What Is This Pattern?
RAG (Retrieval Augmented Generation) is a pattern that combines information retrieval with text generation. Instead of relying solely on the AI's training data, RAG first retrieves relevant documents or information from a knowledge base (database, vector store, documentation), then uses that retrieved context to generate accurate, up-to-date responses. This pattern is essential for applications that need to reference specific documents, codebases, or knowledge bases that change frequently or contain domain-specific information not in the AI's training data.
How It Works
The RAG process involves: (1) **Query**: User asks a question, (2) **Retrieval**: System searches knowledge base (often using semantic search/embeddings) to find relevant documents, (3) **Augmentation**: Retrieved documents are added to the prompt as context, (4) **Generation**: AI generates response using both its training knowledge and the retrieved context. This ensures answers are grounded in your specific data.
When To Use This Pattern
- You have a large knowledge base or documentation that changes frequently
- You need to answer questions about specific codebases or projects
- The AI needs access to information not in its training data
- You want to cite sources or provide references
- Building chatbots that answer questions about your product/service
- Creating AI assistants that reference internal documentation
- You need to combine multiple sources of information
Example
Answer this question using our product documentation.
**Question:** What are the key features of our product?
**Step 1: Retrieve Relevant Information**
Search our product documentation for:
- Feature descriptions
- Product specifications
- Release notes
- User guides
**Step 2: Extract Key Information**
From the retrieved documents, identify:
- Core features
- Feature descriptions
- Use cases
- Technical specifications
**Step 3: Generate Response**
Using the retrieved information, provide a comprehensive answer about our product features. Cite specific documents or sections where information came from.
**Available Documentation:**
[Your knowledge base or documentation source]
Generate the answer based on the retrieved context.Best Practices
- Use semantic search or embeddings for better retrieval
- Retrieve top 3-5 most relevant documents
- Include source citations in the response
- Instruct the AI to use only information from retrieved context
- Handle cases where no relevant information is found
- Combine retrieved information with AI's general knowledge appropriately
- Use chunking strategies for large documents
Common Mistakes to Avoid
- Retrieving too many documents (overwhelming context)
- Not instructing the AI to prioritize retrieved information
- Using simple keyword search instead of semantic search
- Not handling cases where retrieval finds nothing
- Not citing sources (hard to verify accuracy)
- Mixing retrieved context with hallucinated information