Prompt
I’m building a RAG system and need help with chunking, embeddings, and retrieval
Latest observation
Absolutely — for a solid RAG system, the quality of chunking, embeddings, and retrieval matters a lot more than the generator model in many cases.
Here’s a practical framework you can use.
1) Chunking: split text in a retrieval-friendly way
Goals
You want chunks that are:
- Self-contained enough to be meaningful
- Small enough to retrieve precisely
- Large enough to preserve context
Good chunking strategies
A. Semantic / structure-aware chunking
Prefer splitting by:
- headings / sections
- paragraphs
- bullet lists
- code blocks
- sentence boundaries
This is usually better than fixed-size splitting alone.
B. Recursive chunking
A common approach:
- Split by document structure (e.g. headings)
- If a section is too large, split by paragraphs
- If still too large, split by sentences
- If still too large, split by tokens/characters
This preserves semantic boundaries as much as possible.
C. Sliding window overlap
Add overlap between chunks to avoid cutting off important context:
- Typical overlap: 10–20%
- Example: chunk size 300–800 tokens, overlap 50–150 tokens
Practical chunk size recommendations
These depend on your content:
- FAQs / policy docs / manuals: 200–500 tokens
- General docs / knowledge base: 300–800 tokens
- Technical docs / legal / academic: 500–1200 tokens
- Code: chunk by function/class, not arbitrary size
Rules of thumb
- Don’t chunk across semantic boundaries if you can avoid it.
- Avoid chunks that are too tiny: they lose context.
- Avoid chunks that are too large: retrieval gets noisy.
Metadata is critical
Store metadata with each chunk:
- document ID
- section title
- source URL
- page number
- timestamps
- chunk index
- parent section ID
This helps filtering, ranking, and citation.
2) Embeddings: choose a model and represent chunks well
What embeddings do
Embeddings map text into vectors so semantically similar text is nearby.
Model choice
Pick based on:
- language support
- domain specificity
- vector dimension / cost
- latency
- whether you need query/doc-specific embeddings
Common options:
- OpenAI embedding models
- Cohere embeddings
- bge / e5 family
- Instructor-style models for domain-tuned retrieval
Best practices for embeddings
A. Embed chunks, not whole documents
Use one vector per chunk. Large docs should be chunked first.
B. Embed queries separately
At retrieval time, embed the user query and compare it with chunk vectors.
C. Normalize if your vector DB expects cosine similarity
Most systems use cosine similarity or dot product with normalized vectors.
D. Consider domain adaptation
If your domain is specialized:
- legal
- medical
- finance
- internal enterprise docs
you may benefit from a domain-tuned embedding model.
Query/document mismatch
Some embedding models work better when they are trained for:
- query → passage retrieval
- document clustering
- semantic similarity
If possible, pick a model explicitly suited for retrieval.
3) Retrieval: get the right chunks back
Common retrieval methods
A. Dense vector retrieval
Use embedding similarity to find semantically relevant chunks.
Pros:
- semantic matching
- handles paraphrases well
Cons:
- may miss exact keyword matches
B. Sparse keyword retrieval
Examples:
- BM25
- Elasticsearch keyword search
Pros:
- great for exact terms, IDs, names, rare words
Cons:
- weaker semantic matching
C. Hybrid retrieval
Combine dense + sparse retrieval.
This is often the best practical solution.
Example:
- Retrieve top 20 by BM25
- Retrieve top 20 by embeddings
- Merge and rerank
Reranking
After initial retrieval, rerank candidates with a stronger model:
- cross-encoder reranker
- LLM-based reranking
- rule-based heuristics
This often improves answer quality a lot.
Retrieval parameters to tune
top_k: number of candidates returned- similarity threshold
- chunk overlap
- hybrid weighting
- reranker cutoff
Good defaults
A solid starting point:
- retrieve top 20–50 chunks
- rerank down to 3–8 chunks for the generator
- use hybrid retrieval if precision matters
4) A practical RAG pipeline
Ingestion
- Load documents
- Clean text
- Split into chunks
- Attach metadata
- Create embeddings
- Store in vector DB + optional keyword index
Query time
- User asks question
- Embed query
- Retrieve candidate chunks
- Apply hybrid merge / reranking
- Build context window
- Generate answer with citations
5) Common mistakes
Chunking mistakes
- Chunking by fixed length only
- No overlap
- Breaking tables/code/headers
- Losing document structure
- Chunks too large to fit in context
Embedding mistakes
- Using a generic embedding model for a specialized domain without testing
- Mixing different embedding models in the same index
- Not normalizing vectors when needed
Retrieval mistakes
- Using only vector search when exact terms matter
- Returning too many chunks to the generator
- Not reranking
- No metadata filtering
- No evaluation loop
6) How to evaluate your RAG system
Measure retrieval quality separately from answer quality.
Retrieval metrics
- Recall@k
- Precision@k
- MRR
- nDCG
Answer metrics
- factual correctness
- citation accuracy
- completeness
- hallucination rate
Human evaluation
For a small test set, manually check:
- did the retriever fetch the right evidence?
- did the generator use it correctly?
7) Recommended starting setup
If you want a strong baseline:
- Chunking: recursive, structure-aware, 400–800 tokens, 10–20% overlap
- Embeddings: a retrieval-oriented embedding model
- Retrieval: hybrid dense + BM25
- Reranking: cross-encoder reranker
- Top-k: retrieve 20–50, pass top 3–8 to generator
- Metadata: store source, section, page, and chunk IDs
8) If you want, I can help with one of these next:
- Designing a chunking strategy for your document type
- Choosing an embedding model
- Setting up hybrid retrieval
- Writing code for a RAG pipeline
- Evaluating retrieval quality
If you want, tell me:
- your document type,
- approximate doc length,
- language(s),
- vector DB / framework you’re using,
and I’ll suggest a concrete configuration.