Overview
Multi-Query Rewriter RAG is an advanced retrieval strategy that:- Generates multiple rewritten versions of the original query
- Retrieves documents for each query variation
- Combines and re-ranks results with weighted scoring
- Returns diverse, high-quality documents from the merged pool
How It Works
Pipeline Steps
- Query Analysis: Receive user’s original question
- Multi-Query Generation: Generate 3 query variations using different rewriting strategies:
- Standalone rewrite: Make the query self-contained and specific
- Synonym expansion: Rephrase using alternative medical terminology
- Context expansion: Expand to include related aspects and complications
- Multi-Retrieval: Retrieve top-5 documents for each of the 3 rewritten queries (15 candidates total)
- Weighted Re-ranking: Combine results with query-position weighting to reduce redundancy
- Deduplication: Remove duplicate documents using content-based identification
- Final Selection: Select top 8 diverse documents for answer generation
- Answer Generation: Generate final answer from merged, diverse context
Query position weighting penalizes later queries (more speculative rewrites) to balance precision and recall: Query 1 weight = 1.0, Query 2 = 0.95, Query 3 = 0.90.
Key Features
- Three rewriting strategies: Covers different aspects of query reformulation
- Multi-perspective retrieval: Each query variant surfaces different documents
- Weighted fusion: Earlier (more faithful) queries have higher influence
- Automatic deduplication: Prevents redundant documents in final context
- Larger context window: Returns 8 documents vs. 5 in simpler methods
- Detailed query tracking: Returns all rewritten queries for analysis
Implementation Details
Query Rewriting Templates
Core Processing Function
Example Query Rewrites
For the original query: “¿Qué debo hacer si tengo contracciones?” The system might generate:Usage with query_for_evaluation()
Return Structure
When to Use This Approach
Best For
- Ambiguous queries: Questions that could be interpreted multiple ways
- Incomplete information: Vague or underspecified questions
- Maximum recall: When you need to find all relevant documents
- Exploratory search: When users might not know exact terminology
- Complex topics: Multi-faceted questions that span different aspects
- Synonym-rich domains: Medical/technical fields with multiple terms for same concepts
Advantages Over Other Methods
- Highest recall: Multiple queries cast a wider net for relevant documents
- Handles ambiguity: Different rewrites explore different interpretations
- Vocabulary robustness: Synonym expansion catches different terminologies
- Comprehensive coverage: Expansion strategy includes related aspects
- Explicit query diversity: Each rewrite targets different retrieval angles
Trade-offs
- Highest cost: 3 rewrite LLM calls + 1 answer call (~$0.005-0.008 per query)
- Highest latency: Multiple LLM calls + multiple retrievals (~5-7 seconds)
- Potential noise: More retrievals may include less relevant documents
- Complex metrics tracking: Must track costs across multiple LLM invocations
- May over-expand: Expansion can drift from original intent
Performance Characteristics
Speed
- Query rewriting: ~2-3 seconds (3 × gpt-3.5-turbo calls)
- Multi-retrieval: ~1-2 seconds (3 × semantic search)
- Answer generation: ~1-2 seconds (1 × gpt-4o call)
- Total: ~5-8 seconds end-to-end
Cost
- Query rewrites: ~$0.0003-0.0006 (3 × gpt-3.5-turbo, ~50 tokens each)
- Embeddings: ~$0.00003 (3 × query embeddings)
- Answer generation: ~$0.003-0.006 (gpt-4o with larger context)
- Total: ~$0.004-0.008 per query (highest among all architectures)
Quality
- Excellent recall: Best at finding all relevant documents
- Good for ambiguity: Multiple interpretations increase coverage
- Variable precision: More documents may include some less relevant ones
- Context richness: 8 documents provide comprehensive information
- Query-dependent: Quality depends on rewrite quality
Configuration and Tuning
Rewriter Model Temperature
Number of Final Documents
Query Weighting Strategy
The current implementation uses linear decay:Comparison with Other Architectures
Advanced: Custom Rewriting Strategies
You can define custom rewriting prompts for your domain:Error Handling and Robustness
The system gracefully handles edge cases:
- If two rewrites are identical, deduplication removes the duplicate
- If a rewrite fails, the system can continue with successful rewrites
- If no documents match a rewrite, it’s skipped without affecting other queries
Metrics and Observability
The implementation provides detailed cost breakdowns:Source Files
- Implementation:
~/workspace/source/src/rag/rewriter.py:163-244 - Rewriting prompts:
~/workspace/source/src/rag/rewriter.py:64-104 - Retrieval and fusion:
~/workspace/source/src/rag/rewriter.py:184-212 - Evaluation interface:
~/workspace/source/src/rag/rewriter.py:247-323
