RAG Toolkit
The RAG Toolkit is a library designed to streamline the creation of Retrieval-Augmented Generation (RAG) pipelines. It provides utilities for document processing, vector-based retrieval, query routing, and integration with large language models (LLMs). This toolkit simplifies the development of RAG-based systems, enabling developers to focus on solving real-world problems.
Features
- Data Loading: Extract and process data from PDF files.
- Base Retrieval: Efficient retrieval setup using embeddings for document search.
- Retrieval Strategies: Support for various retrieval strategies, including StepBack, Fusion, and more.
- Generation Strategies: Flexible response generation methods, including Recursive and HyDE.
- Query Routing: Route user queries dynamically based on routing logic or templates.
- Customizable Templates: Predefined or user-defined templates for task-specific use cases.
- Pipeline Integration: Combines retrieval and generation into a single, streamlined pipeline.
Installation
To install the RAG Toolkit, clone the repository or install it directly from PyPI.
Clone the Repository
git clone https://github.com/youssef-yasser-ali/rag-toolkit.git
cd rag-toolkit
pip install .
Install via PyPI
pip install rag-toolkit
Configuration
use config/config.py ( optional ) to manage model names and API keys.
# Example config
GENRATIVE_MODEL = "gpt-3.5-turbo"
EMBEDDING_MODEL = "text-embedding-ada-002"
def get_generator_api_key():
return "your-generator-api-key"
def get_query_gen_api_key():
return "your-query-generator-api-key"
def get_embedding_api_key():
return "your-embedding-api-key"
Quickstart Guide
1. Initialize Models
from rag_toolkit.google_models import initialize_llm, initialize_embedding
# your configration
from config.config import get_generator_api_key, get_query_gen_api_key, get_embedding_api_key, GENRATIVE_MODEL, EMBEDDING_MODEL
# Initialize models
retrieval_llm = initialize_llm(model_name=GENRATIVE_MODEL, api_key=get_query_gen_api_key())
generation_llm = initialize_llm(model_name=GENRATIVE_MODEL, api_key=get_generator_api_key())
embedding_llm = initialize_embedding(model_name=EMBEDDING_MODEL, api_key=get_embedding_api_key())
2. Load and Process Documents
from rag_toolkit.data_loader import load_pdf_pages
file_path = './data/raw/your_data.pdf'
documents = load_pdf_pages(file_path=file_path, start_page=1, end_page=20)
3. Create a Vector Store Retriever
from rag_toolkit.vector_store import create_vector_store_retriever
retriever = create_vector_store_retriever(documents=documents, embeddings_model=embedding_llm)
4. Retrieval Strategy Setup
After setting up the base retrieval, you can define more advanced retrieval strategies. The Retrieval Strategy setup defines how to enhance document retrieval accuracy and optimize the search process based on the user's needs.
Available Retrieval Strategies
- Simple Retrieval: Basic retrieval using embeddings to find the most relevant documents based on similarity to the input query.
- Multi-Query Generation Retrieval: Generates multiple variations of the original query to improve coverage and ensure more diverse results.
- Fusion Generation Retrieval: Combines results from different retrieval methods to increase accuracy by fusing different document retrieval outputs.
- Decomposition Retrieval: Breaks down a complex query into smaller, simpler sub-queries to improve the effectiveness of document retrieval.
- Step-Back Retrieval: Refines the query iteratively by paraphrasing it into a more general form to improve accuracy and retrieve relevant documents.
- HyDE Retrieval: Adapts dynamically to the complexity of the query by generating a more detailed, scientifically-based answer using a relevant passage from research.
from rag_toolkit.retriever import StepBackRetriever
retrieval_strategy = StepBackRetriever(model=retrieval_llm, base_retriever=retriever, template=None)
5. Generation Strategy Setup
Once the retrieval strategy is set up, configure the Generation Strategy. This step defines how the system generates context-aware responses based on the retrieved documents.
Available Generation Strategies
Here’s a concise summary of each generator:
- SimpleGenerator: Generates answers using a basic retrieval approach and a single context.
- MultiQueryGenerator: Uses multiple queries to retrieve diverse contexts, improving answer depth.
- FusionGenerator: Combines multiple retrieval strategies, such as reciprocal rank fusion, to enhance answer quality.
- RecursiveGenerator: Iteratively generates sub-questions and refines answers by using previous question-answer pairs and additional context.
- IndividualGenerator: Synthesizes answers from individual question-answer pairs for more comprehensive responses.
- StepBackGenerator: Considers both normal and "step-back" contexts to provide comprehensive, contextually relevant answers.
- HyDEGenerator: Uses detailed context to improve the generation of answers, ensuring relevance and accuracy.
from rag_toolkit.generator import StepBackGenerator
generation_strategy = StepBackGenerator(model=generation_llm, template=None)
6. Define RAG Pipeline
build your pipeline :
from rag_toolkit.pipeline import RagPipeline
rag_pipeline = RagPipeline(retrieval=retrieval_strategy, generator=generation_strategy)
7. Process Queries
query = "What's ML?"
result = rag_pipeline.process(query=query)
print(result)
Examples
The examples/ directory contains sample scripts to help you get started with the toolkit:
- example_pipeline: A basic example of a RAG pipeline for question-answering.
- routing_example: Example of routing queries based on the context.
- customize_template: How to use custom templates for retrieval and generation.
Run these examples using:
python -m examples.example_pipeline
Dependencies
The RAG Toolkit requires the following Python libraries:
langchainlangsmithchromadbpydantic
Install the required dependencies with:
pip install -r requirements.txt
Contributing
We welcome contributions to the RAG Toolkit! To contribute:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Commit your changes.
- Submit a pull request.
License
This project is licensed under the Apache License 2.0. See the LICENSE file for more information.
Contact
For questions or support, feel free to reach out:
- Email: yyasser849@gemail.com
- GitHub: youssef-yasser-ali
Happy Coding!
Metadata
Release files for rag-toolkit 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| rag-toolkit-0.1.1.tar.gz | 15.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| rag_toolkit-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 31.1 kB
Release files / rag-toolkit-0.1.1.tar.gz
| Download URL | rag-toolkit-0.1.1.tar.gz |
|---|---|
| Size | 15.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
00a0003586d5e7a5f6388be77cc4b2c0d53609401e4b7892dcf2b746e3bcfafe
|
|
BLAKE2b-256 checksum How to use checksums |
3336f0a4872f8ffba2288b10cf4b0daac019a8535f1d6323c14c4967529910e2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.9.16
|
Release files / rag_toolkit-0.1.1-py3-none-any.whl
| Download URL | rag_toolkit-0.1.1-py3-none-any.whl |
|---|---|
| Size | 15.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a73a08b53c83c56efb14e787bc16ce97bfa050a68b4d378622e567c8a00a4195
|
|
BLAKE2b-256 checksum How to use checksums |
a78f5b754962a686c7faf6b69c27ae8835e6d20f04c1e5a81022586597e15431
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.9.16
|
Security for rag-toolkit 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.