Why: - Need implementation plan and task tracking documentation - Need strategy explanations for reference Changes: - phases.md: 5-phase implementation plan with status tracking - tasks.md: 25 tasks mapped to phases and steps - chunking_strategies.md: detailed explanations of all 5 strategies
4.0 KiB
4.0 KiB
Chunking Strategies
This document explains the 5 chunking strategies implemented in the RAG Chunking Strategy Benchmarking Framework.
1. FIXED_SIZE (Baseline)
How it works: Splits text into chunks of N tokens with M token overlap.
Algorithm
- Encode full markdown into tokens
- Take first
chunk_sizetokens as chunk 1 - Slide forward by
chunk_size - overlaptokens - Repeat until end of text
Pros & Cons
- Pros: Simple, predictable, fast, no dependencies
- Cons: Ignores meaning — can split mid-sentence, mid-word, or across topics
Configuration
Uses chunk_size and chunk_overlap from settings.
2. RECURSIVE (Cascade Splitting)
How it works: Tries to split on meaningful boundaries first, falling back to less meaningful ones.
Separator Cascade (in order)
- Markdown headers (
#,##,###) - Double newline (
\n\n) — paragraph breaks - Single newline (
\n) — line breaks - Sentence endings (
. ! ?+ space) - Space (word-level, last resort)
Algorithm
- Try splitting by highest-priority separator
- If parts are still too big, recurse with next separator
- Merge small parts back up to target size
Pros & Cons
- Pros: Respects document structure, produces natural chunks
- Cons: Still rule-based, no semantic understanding
3. SEMANTIC (Similarity-Based)
How it works: Groups sentences by meaning — when similarity drops, it starts a new chunk.
Algorithm
- Split markdown into sentences
- Embed each sentence via OpenAI (
text-embedding-3-small) - Compute cosine similarity between adjacent sentences
- When similarity <
semantic_threshold, create chunk boundary - Enforce minimum chunk size (
semantic_min_chunk_sizesentences)
Pros & Cons
- Pros: Respects topic changes, produces coherent chunks
- Cons: Requires embeddings at chunk-time (API calls), slower, costs money
Configuration
semantic_threshold(default 0.5)semantic_min_chunk_size(default 5)
4. CONTEXTUAL_RETRIEVAL (LLM-Enriched)
How it works: Based on Anthropic's research — prepends a short context summary to each chunk before embedding.
Algorithm
- Split text using fixed-size token splitting (same as #1)
- For each chunk, send surrounding text + chunk to LLM
- LLM generates a 1-2 sentence context prefix
- Enriched chunk = context prefix + original text
Example Output
This section discusses insurance claim deadlines for property damage...
[Original chunk text about specific deadlines...]
Pros & Cons
- Pros: Improved retrieval by 49% in Anthropic's benchmarks
- Cons: Expensive (1 LLM call per chunk), slowest strategy
Configuration
Uses llm_model (gpt-4o-mini) for context generation.
5. SEMANTIC_PARENT_CHILD (Hierarchical)
How it works: Groups paragraphs into semantic clusters. Each cluster is a parent; each paragraph is a child.
Algorithm
- Split markdown into paragraphs
- Embed each paragraph
- Cluster consecutive paragraphs by similarity (threshold-based)
- Each cluster = parent chunk (full cluster text)
- Each paragraph = child chunk (linked to parent)
Query-time Behavior
- Search finds child paragraph via vector match
- Return full parent cluster as context to LLM
Pros & Cons
- Pros: Rich context, no headings needed, works on flat documents
- Cons: More storage (both parent + child vectors), complex retrieval
Configuration
Uses semantic_threshold for clustering.
Summary Table
| Strategy | Split Method | Requires LLM | Requires Embeddings | Speed | Cost |
|---|---|---|---|---|---|
| fixed_size | Token count | No | No | Fast | Free |
| recursive | Separator cascade | No | No | Fast | Free |
| semantic | Similarity threshold | No | Yes (at chunk time) | Medium | Low |
| contextual_retrieval | Fixed-size + LLM context | Yes (per chunk) | No | Slow | High |
| semantic_parent_child | Similarity clustering | No | Yes (at chunk time) | Medium | Low |