Retrieve-Augmented Generation Workflow
RAG, or Retrieval-Augmented Generation, combines two capabilities:- Retrieval: Find relevant information from a knowledge source.
- Generation: Use an LLM to generate an answer based on the retrieved information.
1. What is RAG?
A traditional LLM workflow is:- Not know recent information.
- Not know private or company-specific data.
- Generate incorrect information.
- Hallucinate an answer.
2. Important Components of a RAG System
A complete RAG pipeline usually contains the following components:3. Document Loading
The first step is collecting data. RAG can retrieve information from:- PDF files
- Text files
- CSV files
- Databases
- Websites
- APIs
- Documentation
- Knowledge bases
- Company documents
4. Document Preprocessing
Raw documents may contain unnecessary information. Preprocessing can include:- Removing unwanted spaces.
- Removing duplicate content.
- Cleaning HTML.
- Removing irrelevant sections.
- Fixing encoding issues.
- Normalizing text.
5. Text Chunking
Large documents are usually divided into smaller pieces called chunks. Example:Why Chunking is Important
Large chunks may:- Contain too much unrelated information.
- Reduce retrieval precision.
- Use more LLM context.
- Lose important context.
- Split related information.
6. Chunk Size and Chunk Overlap
Two important RAG parameters are:Chunk Size
The amount of text stored in each chunk. Example:Chunk Overlap
Some text is shared between consecutive chunks. Example:7. Embeddings
Embeddings convert text into numerical vectors.- Sentence Transformers
- BGE models
- E5 models
- OpenAI embedding models
8. Vector Store
A vector store stores embeddings and allows similarity search.- FAISS
- Chroma
- Pinecone
- Qdrant
- Weaviate
- Milvus
9. Vector Indexing
The vector index organizes embeddings for efficient retrieval.- Search speed
- Memory usage
- Accuracy
- Scalability
10. Metadata
Metadata is additional information stored with a chunk. Example:- Show document sources.
- Filter search results.
- Retrieve specific document types.
- Apply access controls.
- Improve traceability.
11. Query Processing
When a user asks a question:- Query cleaning
- Query rewriting
- Query expansion
- Multi-query retrieval
- HyDE
- Intent detection
12. Query Embedding
The processed query is converted into an embedding using the embedding model.13. Similarity Search
The vector store searches for the closest vectors.k=3 means:
- Cosine similarity
- Dot product / inner product
- Euclidean distance
14. Dense Retrieval
Dense retrieval uses embeddings to search based on semantic meaning. Example:15. Sparse Retrieval
Sparse retrieval is based mainly on keywords. A common algorithm is:- Exact keywords
- Product names
- IDs
- Error codes
- Technical terms
16. Hybrid Search
Hybrid search combines:17. Reranking
Initial vector search may return relevant but poorly ordered results. A reranker takes the retrieved chunks and reorders them.18. Context Construction
The retrieved chunks are combined before sending them to the LLM. Example:19. Prompt Construction
A typical RAG prompt looks like:20. Generation
The LLM receives:- GPT models
- Llama models
- Mistral models
- Gemma models
- Other language models
21. Grounded Generation
A good RAG answer should be grounded in the retrieved documents. Example:22. Citations and Source Attribution
A production RAG system should ideally provide sources. Example:- Trust
- Transparency
- Debugging
- Verification
23. Context Window Management
LLMs have a limited context window. You cannot always send every retrieved document. Therefore, a RAG system must select:- Top-K retrieval
- Reranking
- Context compression
- Summarization
- Token limits
24. Context Compression
Sometimes retrieved chunks contain too much irrelevant information. Context compression reduces them to only the important information.- Lower token usage
- Faster generation
- More focused context
25. Parent-Child Retrieval
A useful advanced technique is parent-child retrieval. Example:- Retrieval precision
- Context completeness
26. Multi-Query Retrieval
A single user query may not retrieve all relevant information. Example:27. HyDE
HyDE stands for Hypothetical Document Embeddings. The workflow is:28. Retrieval Failure
Sometimes the retriever does not find relevant information. Example:- Similarity score thresholds
- Reranking
- Query rewriting
- Fallback search
- Asking the user for clarification
- Returning “I could not find relevant information”
29. RAG Evaluation
A RAG system should be evaluated at multiple levels.Retrieval Evaluation
Ask:- Precision@K
- Recall@K
- MRR
- NDCG
Generation Evaluation
Ask:- Correctness
- Relevance
- Faithfulness
- Completeness
End-to-End Evaluation
Ask:30. Important RAG Problems
Hallucination
The LLM generates unsupported information. Solution:- Ground answers in retrieved context.
- Use clear prompts.
- Use source citations.
- Add refusal behavior when evidence is missing.
Poor Retrieval
Relevant documents are not retrieved. Solution:- Improve chunking.
- Use better embedding models.
- Add hybrid search.
- Add reranking.
- Improve queries.
Lost Context
Important information is split between chunks. Solution:- Use chunk overlap.
- Adjust chunk size.
- Use parent-child retrieval.
Too Much Context
Too many chunks may confuse the LLM. Solution:- Use Top-K retrieval.
- Add reranking.
- Use context compression.