Skip to main content

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

  1. Simple Retrieval: Basic retrieval using embeddings to find the most relevant documents based on similarity to the input query.
  2. Multi-Query Generation Retrieval: Generates multiple variations of the original query to improve coverage and ensure more diverse results.
  3. Fusion Generation Retrieval: Combines results from different retrieval methods to increase accuracy by fusing different document retrieval outputs.
  4. Decomposition Retrieval: Breaks down a complex query into smaller, simpler sub-queries to improve the effectiveness of document retrieval.
  5. Step-Back Retrieval: Refines the query iteratively by paraphrasing it into a more general form to improve accuracy and retrieve relevant documents.
  6. 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:

  1. SimpleGenerator: Generates answers using a basic retrieval approach and a single context.
  2. MultiQueryGenerator: Uses multiple queries to retrieve diverse contexts, improving answer depth.
  3. FusionGenerator: Combines multiple retrieval strategies, such as reciprocal rank fusion, to enhance answer quality.
  4. RecursiveGenerator: Iteratively generates sub-questions and refines answers by using previous question-answer pairs and additional context.
  5. IndividualGenerator: Synthesizes answers from individual question-answer pairs for more comprehensive responses.
  6. StepBackGenerator: Considers both normal and "step-back" contexts to provide comprehensive, contextually relevant answers.
  7. 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:

  • langchain
  • langsmith
  • chromadb
  • pydantic

Install the required dependencies with:

pip install -r requirements.txt

Contributing

We welcome contributions to the RAG Toolkit! To contribute:

  1. Fork the repository.
  2. Create a new branch for your feature or bug fix.
  3. Commit your changes.
  4. 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:


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)

Source distribution for rag-toolkit 0.1.1
File Size Uploaded Provenance
rag-toolkit-0.1.1.tar.gz 15.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for rag-toolkit 0.1.1
File Interpreter ABI Platform
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.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page