RAG Optimization · Module 01
Advanced Indexing
Better chunks and a faster index: semantic chunking against recursive chunking, how an HNSW index works and how to tune it, and newer chunking strategies.
Học xong bạn làm được
- Apply semantic chunking and compare it with recursive chunking
- Explain how an HNSW index finds neighbours
- Tune M, ef_construction and ef_search for recall and speed
- Choose a chunking strategy for a document type
Bài học
- Advanced Indexing: Semantic Chunking
Fixed-size chunking and flat indexing work for a few hundred documents but break down at scale, with loss of semantics and high latency. Semantic chunking fixes the first problem - it embeds sentences, compares each with the next, and starts a new chunk where similarity drops below a threshold, at the boundary between two topics.
- Comparison: Recursive vs. Semantic Chunking
On a passage about the NVIDIA H100 followed by Llama-3, recursive chunking cuts by punctuation into four chunks and separates a claim from its subject, while semantic chunking returns two complete chunks, one per topic. Recursive is fast, cheap and suits structured documents; semantic preserves ideas but costs compute and suits narrative text.
- Optimizing Vector Database: HNSW Index
Brute-force search is too slow at scale, so vector databases use approximate nearest neighbour algorithms; HNSW is among the best balanced. It builds a multi-layer graph, searches from a sparse top layer down to the dense layer 0, and is tuned with M, ef_construction and ef_search.
- Advanced Chunking Strategies (2025-2026)
Three newer techniques go beyond recursive and semantic chunking. Late chunking embeds the whole document first and pools token embeddings per chunk; contextual chunking prepends an LLM-written context to each chunk; proposition-based chunking breaks text into atomic, self-contained facts. Each trades indexing cost for retrieval quality.
- Assignment: Advanced Indexing
Advanced Indexing for RAG Systems