WeightedRAG Setup Guide
Walks through setting up the WeightedRAG pipeline: data prep, chunking, embedding, indexing, and retrieval evaluation.
What this file does
Walks through setting up the WeightedRAG pipeline: data prep, chunking, embedding, indexing, and retrieval evaluation.
When to use it
- You want to reproduce the WeightedRAG benchmark on Natural Questions
- You need a step-by-step guide for multi-granular table chunking and BEIR evaluation
- You are setting up a retrieval pipeline with FAISS and Matryoshka embeddings
- You want to test the pipeline with sample data before scaling up
Assumes this stack
WeightedRAG Setup Guide
This guide walks you through setting up the WeightedRAG pipeline from scratch, including data preparation, chunking, and evaluation.
๐ Prerequisites
- Python 3.8+
- Git
- Google Cloud SDK (optional, for downloading NQ dataset)
- At least 8GB RAM for processing
- ~10GB disk space for full dataset
๐ Quick Start
1. Clone and Setup Environment
git clone https://github.com/Abhijit85/WeightedRAG.git
cd WeightedRAG
# Create virtual environment
python3 -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install dependencies
pip install --upgrade pip
pip install -e .
pip install -r requirements.txt
2. Install Package in Development Mode
# This enables imports from the weighted_rag module
pip install -e .
Note: The
pip install -e .command uses thesetup.pyfile in the root directory to install the WeightedRAG package in "editable" mode. This allows you to make changes to the code without reinstalling.
๐ Data Preparation
Option A: Download Natural Questions Dataset (Recommended)
# Create data directories
mkdir -p datasets/nq-table/raw
# Download NQ training files (requires gsutil)
gsutil -m cp gs://natural_questions/v1.0/train/nq-train-*.jsonl.gz datasets/nq-table/raw/
# Decompress files
for file in datasets/nq-table/raw/*.jsonl.gz; do
gunzip -k "$file"
done
Option B: Use Sample Data (For Testing)
# Extract sample from first shard only
python chunking/utils/corrected_nq_table_extractor.py \
--input datasets/nq-table/raw/nq-train-00.jsonl \
--output datasets/nq-table/nq_table_full_extraction.jsonl \
--sample 1000
๐ Pipeline Execution
Step 1: Extract Table QA Pairs
# Extract table-centric Q&A pairs from Natural Questions
python chunking/utils/corrected_nq_table_extractor.py \
--input datasets/nq-table/raw/nq-train-00.jsonl \
--output datasets/nq-table/nq_table_full_extraction.jsonl \
--sample 50000
Output: datasets/nq-table/nq_table_full_extraction.jsonl
Step 2: Generate Multi-Granular Chunks
# Create retrieval chunks with different granularities
cd chunking/core
python create_retrieval_tables.py --max-entries 50000
cd ../..
Outputs:
retrieval_tables/processed_tables.jsonlretrieval_tables/retrieval_chunks.jsonlretrieval_tables/chunks_*.jsonl(per chunk type)
Step 3: Convert to BEIR Format
# Transform to evaluation-ready format
python scripts/prepare_nq_tables_beir.py \
--tables retrieval_tables/processed_tables.jsonl \
--chunks retrieval_tables/retrieval_chunks.jsonl \
--output-dir datasets/nq-table/beir \
--chunk-types full_table,table_only,table_row,sliding_window,table_sample,pure_table
Outputs:
datasets/nq-table/beir/corpus.jsonldatasets/nq-table/beir/queries.jsonldatasets/nq-table/beir/qrels.tsv
Step 4: Run Retrieval Evaluation
# Evaluate retrieval performance
python scripts/evaluate_retrieval.py \
--dataset-root datasets/nq-table/beir \
--ks 1,3,5,10 \
--max-queries 500 \
--save-results outputs/nq_table_metrics.json
Outputs:
- Console metrics summary
outputs/nq_table_metrics.json(detailed results)
๐๏ธ Architecture Overview
Key Components
-
Document Chunking: Multi-granular table chunking
- Full tables
- Individual rows
- Sliding windows
- Table samples
-
Embedding: Matryoshka embeddings with multiple dimensions
- Model:
sentence-transformers/all-MiniLM-L6-v2 - Dimensions: 384 (configurable)
- Model:
-
Indexing: FAISS HNSW for efficient similarity search
- Index type:
HNSW32 - Approximate nearest neighbor search
- Index type:
-
Retrieval: Multi-stage weighted retrieval
- Dense vector similarity
- Metadata-based scoring
- Optional cross-encoder reranking
- Optional graph-based reranking
Pipeline Flow
Raw Data โ Extract Tables โ Generate Chunks โ Embed โ Index โ Retrieve โ Rank โ Generate
โ๏ธ Configuration
Basic Configuration
Create config.json for custom settings:
{
"embedding": {
"model_name": "sentence-transformers/all-MiniLM-L6-v2",
"batch_size": 16,
"device": "cpu"
},
"retrieval": {
"stages": [
{
"name": "dense",
"dimension": 384,
"top_k": 100,
"weight": 1.0,
"index_factory": "HNSW32"
}
]
},
"chunking": {
"max_tokens": 128,
"overlap_tokens": 16
}
}
Enable Optional Features
{
"use_graph_rerank": true,
"cross_encoder": {
"model_name": "cross-encoder/ms-marco-MiniLM-L-6-v2",
"batch_size": 32
}
}
๐งช Testing Your Setup
Quick Verification
# Test table extraction
python chunking/utils/corrected_nq_table_extractor.py \
--input datasets/nq-table/raw/nq-train-00.jsonl \
--output test_extraction.jsonl \
--sample 10
# Test chunking
cd chunking/core
python create_retrieval_tables.py --max-entries 10
cd ../..
# Test evaluation (small scale)
python scripts/evaluate_retrieval.py \
--dataset-root datasets/nq-table/beir \
--ks 1,3,5 \
--max-queries 5
๐ ๏ธ Troubleshooting
Common Issues
- ModuleNotFoundError: Run
pip install -e .from repo root - Memory issues: Reduce batch sizes or use smaller sample sizes
- FAISS errors: Install
faiss-cpuorfaiss-gpu - Slow processing: Use GPU if available, reduce dataset size
Performance Tips
- Use GPU for embedding computation
- Increase batch sizes if you have more RAM
- Use FAISS GPU index for large datasets
- Process in smaller chunks if memory constrained
๐ Project Structure
WeightedRAG/
โโโ src/weighted_rag/ # Main package
โ โโโ embeddings/ # Embedding models
โ โโโ index/ # Vector indexes
โ โโโ retrieval/ # Retrieval algorithms
โ โโโ evaluation/ # Metrics and evaluation
โโโ chunking/ # Table chunking logic
โ โโโ core/ # Main chunking algorithms
โ โโโ processors/ # Data processors
โ โโโ utils/ # Utility functions
โโโ scripts/ # Evaluation scripts
โโโ datasets/ # Data storage (gitignored)
โโโ retrieval_tables/ # Generated chunks (gitignored)
โโโ outputs/ # Results and metrics
โโโ setup.py # Package installation configuration
โโโ requirements.txt # Python dependencies
โโโ README.md # Project documentation
Key Files
- setup.py: Defines the package structure and dependencies for
pip install -e . - requirements.txt: Lists all Python dependencies needed for the project
- SETUP.md: This setup guide
- evaluation_log.txt: Detailed logs from evaluation runs
๐ Expected Results
Retrieval Metrics
After successful setup, you should see metrics like:
P@1: 0.25-0.35
P@5: 0.15-0.25
P@10: 0.10-0.20
Recall@10: 0.30-0.50
NDCG@10: 0.30-0.45
MRR: 0.30-0.40
Processing Times
- Table extraction: ~5-10 minutes (50k samples)
- Chunking: ~10-15 minutes (50k samples)
- BEIR conversion: ~2-3 minutes
- Evaluation: ~15-30 minutes (500 queries)
๐ Next Steps
- Scale Up: Process full dataset instead of samples
- Tune Parameters: Adjust chunking, embedding, and retrieval settings
- Add Features: Enable cross-encoder or graph reranking
- Custom Data: Adapt pipeline for your own table datasets
- Production: Deploy with proper error handling and monitoring
๐ Additional Resources
- Original README.md - Technical details
- Evaluation Log - Detailed logs
- Chunking README - Chunking specifics
๐ Getting Help
If you encounter issues:
- Check the troubleshooting section above
- Review the evaluation log for detailed error messages
- Ensure all dependencies are installed correctly
- Verify data paths and file permissions
- Open an issue with detailed error information
๐ฏ Success Criteria
Your setup is working correctly if:
- All scripts run without errors
- Generated files exist in expected locations
- Evaluation produces reasonable metric values
- Processing times are within expected ranges
- No import or dependency errors occur
What's inside
12 sections, 4 pipeline steps, 6 code blocks, 1 architecture diagram, 1 project tree
Change this for your project
- Replace
Abhijit85/WeightedRAGwith your own repository URL - Replace
gs://natural_questions/v1.0/train/nq-train-*.jsonl.gzwith your dataset path - Replace
sentence-transformers/all-MiniLM-L6-v2with your embedding model - Replace
cross-encoder/ms-marco-MiniLM-L-6-v2with your reranker model
Where it goes
Reference documentation for a retrieval pipeline. Keep with the ingestion or retrieval code it describes.
Worth borrowing
- Multi-granular chunking (full table, rows, sliding windows) for structured data retrieval
- Weighted multi-stage retrieval combining dense vectors, metadata, and optional reranking
Related Documents
SUMMARY
Proposes three on-prem AI architectures, modular, hybrid, and fully local RAG, with hardware specs and vendor lists.
Retrieval & Prompts
Explains how CharMemory's extraction prompt and Vector Storage settings determine memory retrieval quality in SillyTavern.
Advanced RAG Techniques
Surveys advanced RAG architectures, Agentic, Corrective, Self-RAG, Graph RAG, RAPTOR, Multimodal, Temporal, with design trade-offs and decision guidance.
RFC-BLite: High-Performance Embedded Document Database for .NET
Specifies an embedded document database for.NET with zero-allocation I/O, C-BSON format, and ACID transactions.