A comprehensive demonstration and comparison of different information retrieval methods for RAG (Retrieval-Augmented Generation) systems. This project implements and compares TF-IDF, BM25, and hybrid search algorithms using sample corporate documentation.
This project explores various search and ranking algorithms commonly used in RAG systems. It provides implementations of:
- TF-IDF (Term Frequency-Inverse Document Frequency): A classic information retrieval method that balances term frequency with document rarity
- BM25 (Best Matching 25): An advanced probabilistic ranking function with document length normalization
- Hybrid Search: A combination of TF-IDF and BM25 with configurable weights
- Method Comparison: Side-by-side comparison of different search approaches
- Multiple Search Algorithms: Implementations of TF-IDF, BM25, and hybrid search
- Real-World Documents: Sample corporate documentation (TechCorp) for testing
- Performance Comparison: Compare different search methods on the same queries
- Configurable Hybrid Search: Adjust weights between TF-IDF and BM25
- Cross-Platform Support: Works on Windows, macOS, and Linux
rag-project/
βββ techcorp-docs/ # Sample documents for search
β βββ employee-handbook.md
β βββ health-insurance-benefits.md
β βββ onboarding-guide.md
β βββ pet-policy.md
β βββ remote-work-policy.md
β βββ vacation-policy.md
βββ tfidf_search.py # TF-IDF search implementation
βββ bm25_search.py # BM25 search implementation
βββ hybrid_search.py # Hybrid search (TF-IDF + BM25)
βββ compare_methods.py # Comparison of all methods
βββ utils.py # Common utilities
βββ requirements.txt # Python dependencies
βββ README.md # This file
- Python 3.8 or higher
- pip (Python package manager)
-
Clone the repository (or navigate to the project directory):
cd rag-project -
Create a virtual environment (recommended):
python -m venv venv
-
Activate the virtual environment:
- Windows:
venv\Scripts\activate
- macOS/Linux:
source venv/bin/activate
- Windows:
-
Install dependencies:
pip install -r requirements.txt
Run the TF-IDF search demo:
python tfidf_search.pyThis will:
- Load documents from
techcorp-docs/ - Perform searches for sample queries
- Display top results with similarity scores
Run the BM25 search demo:
python bm25_search.pyRun the hybrid search demo with different weight combinations:
python hybrid_search.pyThis demonstrates how different weightings between TF-IDF and BM25 affect search results.
Compare all search methods side-by-side:
python compare_methods.pyThis shows how different algorithms rank the same documents for a given query.
The scripts include example queries such as:
- "remote work policy"
- "health insurance benefits"
- "pet policy dogs"
You can modify the queries in each script to test different search scenarios.
TF-IDF measures how important a word is to a document in a collection of documents. It:
- Increases with the number of times a word appears in a document
- Decreases with the frequency of the word in the corpus
- Helps identify documents that are most relevant to a query
BM25 is a ranking function used by search engines to estimate the relevance of documents. It:
- Uses probabilistic information retrieval
- Normalizes for document length
- Handles term saturation (repeated terms have diminishing returns)
- Generally performs better than TF-IDF for information retrieval
Combines TF-IDF and BM25 scores with configurable weights:
- Allows fine-tuning based on your specific use case
- Can leverage strengths of both methods
- Default weights: 30% TF-IDF, 70% BM25
- Add markdown files to the
techcorp-docs/directory - The scripts will automatically load all
.mdfiles
Edit the queries list in any of the search scripts:
queries = ["your query here", "another query"]In hybrid_search.py, modify the weight combinations:
weight_combinations = [
(0.5, 0.5, "Equal weights"),
(0.3, 0.7, "BM25 favored"),
(0.7, 0.3, "TF-IDF favored")
]- scikit-learn (>=1.4.0): TF-IDF vectorization and cosine similarity
- rank-bm25 (==0.2.2): BM25 ranking implementation
- pandas (>=2.2.0): Data manipulation (if needed)
- numpy (>=1.26.0,<2): Numerical operations
- matplotlib (>=3.8.0): Visualization (if needed)
If you encounter Unicode encoding errors, the scripts include a safe_print() function that handles Windows console encoding automatically.
If documents aren't loading:
- Ensure markdown files are in the
techcorp-docs/directory - Check that files have
.mdextension - Verify files are not empty
If you get import errors:
- Ensure the virtual environment is activated
- Run
pip install -r requirements.txtagain - Check Python version:
python --version(should be 3.8+)
Feel free to:
- Add more search algorithms
- Improve document processing
- Add visualization capabilities
- Enhance the comparison functionality
This project is provided as-is for educational and demonstration purposes.
- Grep: Simple exact matching, good for specific terms
- TF-IDF: Balances term frequency with document rarity
- BM25: Advanced ranking with document length normalization, generally better for information retrieval
- Hybrid: Combines strengths of multiple methods for optimal results
Note: This project is designed for educational purposes to understand different search and ranking algorithms in the context of RAG systems.