Contextual Enrichment
Enhance your RAG system chunks with rich contextual information
In modern Retrieval-Augmented Generation (RAG) systems, like R2R, documents are systematically broken down into smaller, manageable pieces called chunks. While chunking is essential for efficient vector search operations, these individual chunks sometimes lack the broader context needed for comprehensive question answering or analysis tasks.
The Challenge of Context Loss
Let’s examine a real-world example using Lyft’s 2021 annual report (Form 10-K) from their public filing.
During ingestion, this 200+ page document is broken into 1,223 distinct chunks. Consider this isolated chunk:
Reading this chunk in isolation raises several questions:
- What specific impacts are being discussed?
- Which rental programs are affected?
- What’s the broader context of these business challenges?
This is where contextual enrichment becomes invaluable.
Introducing Contextual Enrichment
Contextual enrichment an ingestiont echnique that enhances chunks with relevant information from surrounding or semantically related content. Think of it as giving each chunk its own “memory” of related information.
Enabling Enrichment
To activate this feature, configure your r2r.toml
file with the following settings:
Enrichment Strategies Explained
R2R implements two sophisticated strategies for chunk enrichment:
1. Neighborhood Strategy
This approach looks at the document’s natural flow by examining chunks that come before and after the target chunk:
- Forward Looking: Captures upcoming context (configurable, default: 3 chunks)
- Backward Looking: Incorporates previous context (configurable, default: 3 chunks)
- Use Case: Particularly effective for narrative documents where context flows linearly
2. Semantic Strategy
This method uses advanced embedding similarity to find related content throughout the document:
- Vector Similarity: Identifies chunks with similar meaning regardless of location
- Configurable Neighbors: Customizable number of similar chunks to consider
- Similarity Threshold: Set minimum similarity scores to ensure relevance
- Use Case: Excellent for documents with themes repeated across different sections
The Enrichment Process
When enriching chunks, R2R uses a carefully crafted prompt to guide the LLM:
Implementation and Results
To process your documents with enrichment:
Viewing Enriched Results
Access your enriched chunks through the API:
Let’s compare the before and after of our example chunk:
Before Enrichment:
After Enrichment:
Notice how the enriched version:
- Specifies the cause (COVID-19 pandemic)
- Names specific programs (Lyft Rentals, Express Drive)
- Provides clearer context about the business impact
- Maintains professional, third-person tone
Metadata and Storage
The system maintains both enriched and original versions:
This dual storage ensures transparency and allows for version comparison when needed.
Best Practices
- Tune Your Parameters: Adjust
forward_chunks
,backward_chunks
, andsemantic_neighbors
based on your document structure - Monitor Enrichment Quality: Regularly review enriched chunks to ensure they maintain accuracy
- Consider Document Type: Different documents may benefit from different enrichment strategies
- Balance Context Size: More context isn’t always better - find the sweet spot for your use case