Back to .md Directory

WeightedRAG Setup Guide

Walks through setting up the WeightedRAG pipeline: data prep, chunking, embedding, indexing, and retrieval evaluation.

May 2, 2026
0 downloads
2 views
ai rag eval
View source

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

Python 3.8+FAISSsentence-transformersGoogle Cloud SDKBEIR

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 the setup.py file 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.jsonl
  • retrieval_tables/retrieval_chunks.jsonl
  • retrieval_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.jsonl
  • datasets/nq-table/beir/queries.jsonl
  • datasets/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

  1. Document Chunking: Multi-granular table chunking

    • Full tables
    • Individual rows
    • Sliding windows
    • Table samples
  2. Embedding: Matryoshka embeddings with multiple dimensions

    • Model: sentence-transformers/all-MiniLM-L6-v2
    • Dimensions: 384 (configurable)
  3. Indexing: FAISS HNSW for efficient similarity search

    • Index type: HNSW32
    • Approximate nearest neighbor search
  4. 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

  1. ModuleNotFoundError: Run pip install -e . from repo root
  2. Memory issues: Reduce batch sizes or use smaller sample sizes
  3. FAISS errors: Install faiss-cpu or faiss-gpu
  4. 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

  1. Scale Up: Process full dataset instead of samples
  2. Tune Parameters: Adjust chunking, embedding, and retrieval settings
  3. Add Features: Enable cross-encoder or graph reranking
  4. Custom Data: Adapt pipeline for your own table datasets
  5. Production: Deploy with proper error handling and monitoring

๐Ÿ“š Additional Resources

๐Ÿ› Getting Help

If you encounter issues:

  1. Check the troubleshooting section above
  2. Review the evaluation log for detailed error messages
  3. Ensure all dependencies are installed correctly
  4. Verify data paths and file permissions
  5. 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/WeightedRAG with your own repository URL
  • Replace gs://natural_questions/v1.0/train/nq-train-*.jsonl.gz with your dataset path
  • Replace sentence-transformers/all-MiniLM-L6-v2 with your embedding model
  • Replace cross-encoder/ms-marco-MiniLM-L-6-v2 with 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