Working through this module?
Module 07 · Page 8 of 20 · 9 sections
RAG System Design
Retrieval Augmented Generation end to end: ingestion, chunking, embeddings, retrieval, prompt construction and generation.
What is RAG?
- Retrieval Augmented Generation is a technique that combines retrieval of relevant information from external knowledge sources with generative LLMs to produce accurate, up-to-date and context-aware responses.
- It helps reduce hallucination and allows LLMs to use domain-specific knowledge.
Why RAG is Important?
- 1Provides access to latest and domain-specific data.
- 2Improves accuracy and reduces hallucination.
- 3Useful for enterprise and private data.
- 4Can be updated without retraining the model.
- 5Cost effective compared to fine-tuning.
- 6Works well for long and specific documents.
High Level Architecture
User Query
Query Processing (optional)Rewriting, intent detection, expansion
RetrieverVector, keyword, hybrid search
Relevant DocumentsTop-k chunks, re-ranking, filtering
LLM (Generator)Answer, use context, show citations
Response to UserAccurate, relevant, cited source
1. Document Ingestion Pipeline
- 1Load documents from different sources (PDF, DOCX, web, database, APIs).
- 2Extract text and metadata.
- 3Split text into smaller chunks (chunking).
- 4Generate embeddings for each chunk.
- 5Store embeddings and metadata in a vector database.
2. Text Chunking
- Split large documents into smaller meaningful chunks to improve retrieval accuracy.
- Fixed size chunking
- For example 500 tokens per chunk.
- Recursive chunking
- Based on headings and paragraphs.
- Semantic chunking
- Using embeddings to find meaning boundaries.
1
2
3
3. Embedding and Vector Database
- Convert text chunks into vector embeddings using embedding models (e.g. OpenAI, SentenceTransformer).
- Store embeddings in a vector database for similarity search.
- Popular vector databases: Pinecone, Qdrant, Weaviate, FAISS, Chroma, Milvus.
DocumentsPDF, DOCX, etc.
Text Chunking
Embedding Modeltext-embedding-3-small, all-MiniLM-L6-v2
Vector DatabasePinecone, Qdrant, etc.
4. Retrieval Techniques
- Vector Search
- Find similar chunks using embeddings.
- Keyword Search
- Traditional BM25 search.
- Hybrid Search
- Combine vector and keyword search.
- Re-ranking
- Re-rank top results using a cross-encoder or LLM.
- Metadata Filtering
- Filter by source, date or category.
1
2
3
4
5
5. Prompt Construction
- Combine the user query and retrieved context to create a well-structured prompt.
- Include clear instructions and citation format.
prompt
You are a helpful assistant. Use the following context to answer the question. If the answer is not in the context, say you don't know. Context: {retrieved_chunks} Question: {user_question} Answer:
6. Generation and Response
- 1Send the prompt to an LLM (e.g. GPT-4, Claude, Llama).
- 2Generate a clear and concise answer.
- 3Include citations (source of information).
- 4Support follow-up questions using conversation history.
- 5Optionally, implement guardrails to prevent unsafe responses.