Skip to main content

A RAG Framework for Information Retrieval and Generation.

Project description

Language github license Read the Docs PyPI - Version DOI

[ English | 中文 ]

FlexRAG is a flexible and high-performance framework designed for Retrieval-Augmented Generation (RAG) tasks, offering support for multimodal data, seamless configuration management, and out-of-the-box performance for both research and prototyping.

https://github.com/user-attachments/assets/4dfc0ec9-686b-40e2-b1f0-daa2b918e093

📖 Table of Contents

✨ Key Features

  • Multimodal RAG Support: FlexRAG isn't limited to just text-based Retrieval-Augmented Generation (RAG). It also supports multimodal RAG, opening up a wide range of application possibilities across different media types.
  • Diverse Data Types: FlexRAG enables seamless integration of multiple data formats, including text (e.g., CSV, JSONL), images, documents, web snapshots, and more, giving you flexibility in working with varied data sources.
  • Unified Configuration Management: Leveraging python dataclass and hydra-core, FlexRAG simplifies configuration management, making it easier to handle complex setups and customize your workflow.
  • Out-of-the-Box: With carefully optimized default configurations, FlexRAG delivers solid performance without the need for extensive parameter tuning.
  • High Performance: Built with persistent cache system and asynchronous methods to significantly improve speed and reduce latency in RAG workflows.
  • Research & Development Friendly: Supports multiple development modes and includes a companion repository, flexrag_examples, to help you reproduce various RAG algorithms with ease.
  • Lightweight: Designed with minimal overhead, FlexRAG is efficient and easy to integrate into your project.

📢 News

  • 2025-01-05: Documentation for FlexRAG is now available. Check out the documentation for more details.

🚀 Getting Started

Step 0. Installation

Install from pip

To install FlexRAG via pip:

pip install flexrag

Install from source

Alternatively, to install from the source:

pip install pybind11

git clone https://github.com/ictnlp/flexrag.git
cd flexrag
pip install ./

You can also install the FlexRAG in editable mode with the -e flag.

Step 1. Prepare the Retriever

Download the Corpus

Before starting you RAG application, you need to download the corpus. In this example, we will use the wikipedia corpus provided by DPR as the corpus. You can download the corpus by running the following command:

# Download the corpus
wget https://dl.fbaipublicfiles.com/dpr/wikipedia_split/psgs_w100.tsv.gz
# Unzip the corpus
gzip -d psgs_w100.tsv.gz

Prepare the Index

After downloading the corpus, you need to build the index for the retriever. If you want to employ the dense retriever, you can simply run the following command to build the index:

CORPUS_PATH='[psgs_w100.tsv]'
CORPUS_FIELDS='[title,text]'
DB_PATH=<path_to_database>

python -m flexrag.entrypoints.prepare_index \
    corpus_path=$CORPUS_PATH \
    saving_fields=$CORPUS_FIELDS \
    retriever_type=dense \
    dense_config.database_path=$DB_PATH \
    dense_config.encode_fields='[text]' \
    dense_config.passage_encoder_config.encoder_type=hf \
    dense_config.passage_encoder_config.hf_config.model_path='facebook/contriever' \
    dense_config.passage_encoder_config.hf_config.device_id=[0,1,2,3] \
    dense_config.index_type=faiss \
    dense_config.faiss_config.batch_size=4096 \
    dense_config.faiss_config.log_interval=100000 \
    dense_config.batch_size=4096 \
    dense_config.log_interval=100000 \
    reinit=True

If you want to employ the sparse retriever, you can run the following command to build the index:

CORPUS_PATH='[psgs_w100.tsv]'
CORPUS_FIELDS='[title,text]'
DB_PATH=<path_to_database>

python -m flexrag.entrypoints.prepare_index \
    corpus_path=$CORPUS_PATH \
    saving_fields=$CORPUS_FIELDS \
    retriever_type=bm25s \
    bm25s_config.database_path=$DB_PATH \
    bm25s_config.indexed_fields='[title,text]' \
    bm25s_config.method=lucene \
    bm25s_config.batch_size=512 \
    bm25s_config.log_interval=100000 \
    reinit=True

Step 2. Run FlexRAG Assistant

When the index is ready, you can run RAG Assistant provided by FlexRAG. Here is an example of how to run a Modular Assistant.

Run the FlexRAG Example RAG Application with GUI

python -m flexrag.entrypoints.run_interactive \
    assistant_type=modular \
    modular_config.used_fields=[title,text] \
    modular_config.retriever_type=dense \
    modular_config.dense_config.top_k=5 \
    modular_config.dense_config.database_path=${DB_PATH} \
    modular_config.dense_config.query_encoder_config.encoder_type=hf \
    modular_config.dense_config.query_encoder_config.hf_config.model_path='facebook/contriever' \
    modular_config.dense_config.query_encoder_config.hf_config.device_id=[0] \
    modular_config.response_type=short \
    modular_config.generator_type=openai \
    modular_config.openai_config.model_name='gpt-4o-mini' \
    modular_config.openai_config.api_key=$OPENAI_KEY \
    modular_config.do_sample=False

Run the FlexRAG Example Assistants for Knowledge Intensive Tasks

You can evaluate your RAG assistant on several knowledge intensive datasets with great ease. The following command let you evaluate the Modular Assistant with dense retriever on the Natural Questions (NQ) dataset:

OUTPUT_PATH=<path_to_output>
DB_PATH=<path_to_database>
OPENAI_KEY=<your_openai_key>

python -m flexrag.entrypoints.run_assistant \
    data_path=flash_rag/nq/test.jsonl \
    output_path=${OUTPUT_PATH} \
    assistant_type=modular \
    modular_config.used_fields=[title,text] \
    modular_config.retriever_type=dense \
    modular_config.dense_config.top_k=10 \
    modular_config.dense_config.database_path=${DB_PATH} \
    modular_config.dense_config.query_encoder_config.encoder_type=hf \
    modular_config.dense_config.query_encoder_config.hf_config.model_path='facebook/contriever' \
    modular_config.dense_config.query_encoder_config.hf_config.device_id=[0] \
    modular_config.response_type=short \
    modular_config.generator_type=openai \
    modular_config.openai_config.model_name='gpt-4o-mini' \
    modular_config.openai_config.api_key=$OPENAI_KEY \
    modular_config.do_sample=False \
    eval_config.metrics_type=[retrieval_success_rate,generation_f1,generation_em] \
    eval_config.retrieval_success_rate_config.context_preprocess.processor_type=[simplify_answer] \
    eval_config.retrieval_success_rate_config.eval_field=text \
    eval_config.response_preprocess.processor_type=[simplify_answer] \
    log_interval=10

Similarly, you can evaluate the Modular Assistant with sparse retriever on the Natural Questions dataset:

OUTPUT_PATH=<path_to_output>
DB_PATH=<path_to_database>
OPENAI_KEY=<your_openai_key>

python -m flexrag.entrypoints.run_assistant \
    data_path=flash_rag/nq/test.jsonl \
    output_path=${OUTPUT_PATH} \
    assistant_type=modular \
    modular_config.used_fields=[title,text] \
    modular_config.retriever_type=bm25s \
    modular_config.bm25s_config.top_k=10 \
    modular_config.bm25s_config.database_path=${DB_PATH} \
    modular_config.response_type=short \
    modular_config.generator_type=openai \
    modular_config.openai_config.model_name='gpt-4o-mini' \
    modular_config.openai_config.api_key=$OPENAI_KEY \
    modular_config.do_sample=False \
    eval_config.metrics_type=[retrieval_success_rate,generation_f1,generation_em] \
    eval_config.retrieval_success_rate_config.context_preprocess.processor_type=[simplify_answer] \
    eval_config.retrieval_success_rate_config.eval_field=text \
    eval_config.response_preprocess.processor_type=[simplify_answer] \
    log_interval=10

You can also evaluate your own assistant by adding the user_module=<your_module_path> argument to the command.

Build your own RAG Assistant

To build your own RAG assistant, you can create a new Python file and import the necessary FlexRAG modules. Here is an example of how to build a RAG assistant:

from dataclasses import dataclass

from flexrag.assistant import ASSISTANTS, AssistantBase
from flexrag.models import OpenAIGenerator, OpenAIGeneratorConfig
from flexrag.prompt import ChatPrompt, ChatTurn
from flexrag.retriever import DenseRetriever, DenseRetrieverConfig


@dataclass
class SimpleAssistantConfig(DenseRetrieverConfig, OpenAIGeneratorConfig): ...


@ASSISTANTS("simple", config_class=SimpleAssistantConfig)
class SimpleAssistant(AssistantBase):
    def __init__(self, config: SimpleAssistantConfig):
        self.retriever = DenseRetriever(config)
        self.generator = OpenAIGenerator(config)
        return

    def answer(self, question: str) -> str:
        prompt = ChatPrompt()
        context = self.retriever.search(question)[0]
        prompt_str = ""
        for ctx in context:
            prompt_str += f"Question: {question}\nContext: {ctx.data['text']}"
        prompt.update(ChatTurn(role="user", content=prompt_str))
        response = self.generator.chat([prompt])[0][0]
        prompt.update(ChatTurn(role="assistant", content=response))
        return response

After defining the SimpleAssistant class and registering it with the ASSISTANTS decorator, you can run the assistant with the following command:

DB_PATH=<path_to_database>
OPENAI_KEY=<your_openai_key>
DATA_PATH=<path_to_data>
MODULE_PATH=<path_to_simple_assistant_module>

python -m flexrag.entrypoints.run_assistant \
    user_module=${MODULE_PATH} \
    data_path=${DATA_PATH} \
    assistant_type=simple \
    simple_config.model_name='gpt-4o-mini' \
    simple_config.api_key=${OPENAI_KEY} \
    simple_config.database_path=${DB_PATH} \
    simple_config.index_type=faiss \
    simple_config.query_encoder_config.encoder_type=hf \
    simple_config.query_encoder_config.hf_config.model_path='facebook/contriever' \
    simple_config.query_encoder_config.hf_config.device_id=[0] \
    eval_config.metrics_type=[retrieval_success_rate,generation_f1,generation_em] \
    eval_config.retrieval_success_rate_config.eval_field=text \
    eval_config.response_preprocess.processor_type=[simplify_answer] \
    log_interval=10

In flexrag_examples repository, we provide several detailed examples of how to build a RAG assistant.

Run your own RAG Application

In addition to using FlexRAG's built-in Entrypoints to run your RAG Assistant, you can also use FlexRAG to build your own RAG application. The following is an example of how to build a RAG application.

from flexrag.models import HFEncoderConfig, OpenAIGenerator, OpenAIGeneratorConfig
from flexrag.prompt import ChatPrompt, ChatTurn
from flexrag.retriever import DenseRetriever, DenseRetrieverConfig


def main():
    # Initialize the retriever
    retriever_cfg = DenseRetrieverConfig(database_path="path_to_database", top_k=1)
    retriever_cfg.query_encoder_config.encoder_type = "hf"
    retriever_cfg.query_encoder_config.hf_config = HFEncoderConfig(
        model_path="facebook/contriever"
    )
    retriever = DenseRetriever(retriever_cfg)

    # Initialize the generator
    generator = OpenAIGenerator(
        OpenAIGeneratorConfig(
            model_name="gpt-4o-mini", api_key="your_openai_key", do_sample=False
        )
    )

    # Run your RAG application
    prompt = ChatPrompt()
    while True:
        query = input("Please input your query (type `exit` to exit): ")
        if query == "exit":
            break
        context = retriever.search(query)[0]
        prompt_str = ""
        for ctx in context:
            prompt_str += f"Question: {query}\nContext: {ctx.data['text']}"
        prompt.update(ChatTurn(role="user", content=prompt_str))
        response = generator.chat(prompt)
        prompt.update(ChatTurn(role="assistant", content=response))
        print(response)
    return


if __name__ == "__main__":
    main()

For more details on how to build your own RAG application, please refer to the flexrag_examples repository.

🏗️ Architecture

FlexRAG is designed with a modular architecture, allowing you to easily customize and extend the framework to meet your specific needs. The following diagram illustrates the architecture of FlexRAG:

📊 Benchmarks

We have conducted extensive benchmarks using the FlexRAG framework. For more details, please refer to the benchmarks page.

🏷️ License

This repository is licensed under the MIT License. See the LICENSE file for details.

❤️ Acknowledgements

This project benefits from the following open-source projects:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

flexrag-0.1.4-cp313-cp313-musllinux_1_2_x86_64.whl (1.9 MB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

flexrag-0.1.4-cp313-cp313-musllinux_1_2_i686.whl (2.0 MB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ i686

flexrag-0.1.4-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (931.7 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

flexrag-0.1.4-cp313-cp313-manylinux_2_17_i686.manylinux2014_i686.whl (925.6 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ i686

flexrag-0.1.4-cp312-cp312-musllinux_1_2_x86_64.whl (1.9 MB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

flexrag-0.1.4-cp312-cp312-musllinux_1_2_i686.whl (2.0 MB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ i686

flexrag-0.1.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (931.7 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

flexrag-0.1.4-cp312-cp312-manylinux_2_17_i686.manylinux2014_i686.whl (926.2 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ i686

flexrag-0.1.4-cp311-cp311-musllinux_1_2_x86_64.whl (1.9 MB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

flexrag-0.1.4-cp311-cp311-musllinux_1_2_i686.whl (2.0 MB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ i686

flexrag-0.1.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (933.3 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

flexrag-0.1.4-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl (927.1 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ i686

File details

Details for the file flexrag-0.1.4-cp313-cp313-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for flexrag-0.1.4-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 ae38a2f7d8a3e68304bce54336dabe78d6b64b0caa84ba565bf7f8c687cf3936
MD5 5eddb1d491355e21989f465f45cff41b
BLAKE2b-256 ba1c4f9c97db206371a30eb4d3dcdd5a63d78b893a4bbb57cc076ccdc5795499

See more details on using hashes here.

File details

Details for the file flexrag-0.1.4-cp313-cp313-musllinux_1_2_i686.whl.

File metadata

File hashes

Hashes for flexrag-0.1.4-cp313-cp313-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 0e320e33bb1d6bc7532d7d2700f23339ca1f63ba74c73bc5391c9617d5a338dc
MD5 382ee17e4154cfd733f959b63a197c8d
BLAKE2b-256 3027e9328edc477a30aa8837bcaad2f351fcb264839cb1960de3c865e29f013d

See more details on using hashes here.

File details

Details for the file flexrag-0.1.4-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for flexrag-0.1.4-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 fa0189ec1dc4cc40d396d848e4b1f6b96d49ecbb3b85bdd89c6248eb98d0226c
MD5 7f0b1a318c3deb50ed7f3a5e2be65c19
BLAKE2b-256 4c3e4f8a6b2360698861d1d123664eda2b00013b2dd9edde38bd5893fb12fd78

See more details on using hashes here.

File details

Details for the file flexrag-0.1.4-cp313-cp313-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for flexrag-0.1.4-cp313-cp313-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 584904b33c54235c1b6c91abbd4b0ccb5ea863811402eb7b0b17d49f27d3477f
MD5 56bd18479bab147aef85d54ce7781cd7
BLAKE2b-256 ff15f538e3a67003e5e105c6c3e7b476367871b80219b500ecb1ad2051a7f851

See more details on using hashes here.

File details

Details for the file flexrag-0.1.4-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for flexrag-0.1.4-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 9205d2d7cde6f864649fdcc8d98f26577800d3a996f8e999e13fd663eb85ea02
MD5 b230058d2cfdf9771fea49f8e55e987b
BLAKE2b-256 657c3a9592f686211786c9c0f3f72aa21a43d791851dc344fd5b099acc2110c8

See more details on using hashes here.

File details

Details for the file flexrag-0.1.4-cp312-cp312-musllinux_1_2_i686.whl.

File metadata

File hashes

Hashes for flexrag-0.1.4-cp312-cp312-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 87daae90802e48405106c0319aae14c34bba730e8ae3fb8917612f5510dbf3ff
MD5 36af7ed8546558794d7abf2c99867196
BLAKE2b-256 36e2a0f31d4363ece1f3d4f163661e01a55ee1b4d8c4fb955ddeb4f5e03ec015

See more details on using hashes here.

File details

Details for the file flexrag-0.1.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for flexrag-0.1.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1af809d22e574b377bd397de2ead2aaf0e7fd87e0a8ab43ffb8cb670898482fc
MD5 29b80052eb64021cdf964bdcfd4cfc5c
BLAKE2b-256 efd153ae60753fbb9fd99c188f9ff0f755f880662f7ca93246846209867d833c

See more details on using hashes here.

File details

Details for the file flexrag-0.1.4-cp312-cp312-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for flexrag-0.1.4-cp312-cp312-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 10885ceaff71615d79df16d832f91bc6cd3139d830efd41e715f4e53748008ac
MD5 4aa59c14a30497e2b0357cec2648976a
BLAKE2b-256 2692d8d80feac0afb8c4ad95256a29e374e85c783ba115d0be1b8d26606d0dce

See more details on using hashes here.

File details

Details for the file flexrag-0.1.4-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for flexrag-0.1.4-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 d1695165bc8bdd1debe9a67d16853fcaf74137b22dc01dc09ea19d9539ad3c24
MD5 1178b5f5e62fddedb6e75bfbe0b3104e
BLAKE2b-256 d7c4c5d9e50754914cc1c903cb70be71c6ee2c2dcd8916180d066e70fbf5f9ac

See more details on using hashes here.

File details

Details for the file flexrag-0.1.4-cp311-cp311-musllinux_1_2_i686.whl.

File metadata

File hashes

Hashes for flexrag-0.1.4-cp311-cp311-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 853e4d52e4d3c47cc0b29dce622d2cbb8cef61812d655f34f9b383c2cd8c208c
MD5 d38cc6eb6a72049f95e5e0a58248e9de
BLAKE2b-256 a5798c0b9cfacf8d7eaaab8e246b49d7ab039f18b044f7f8a10ef560a8bc7542

See more details on using hashes here.

File details

Details for the file flexrag-0.1.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for flexrag-0.1.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3c756f88897b4c75565c628e1a4095e00a35338339f0b00e7001e9e0790252e9
MD5 b5f7857ccf392ec41c0a4f893364031b
BLAKE2b-256 bf5b8534d2e42003e2d0ce7ced152e31b68887a0b1da900f5ceac33a99500907

See more details on using hashes here.

File details

Details for the file flexrag-0.1.4-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl.

File metadata

File hashes

Hashes for flexrag-0.1.4-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 26bebc910816650678d5a306c713dc1b37377f7a7fade3459b3a1a534b29fede
MD5 b968257c7d92c315ebc409ffb880439a
BLAKE2b-256 cc8b679621bb3077f473d8df5479ccbfb5b7d5ac73e5e3fc155416d50ef8eec5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page