Skip to main content

A RAG Framework for Information Retrieval and Generation.

Project description

Language github license

[ 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.

🚀 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.gz
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.gz
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.3-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.3-cp313-cp313-musllinux_1_2_i686.whl (2.0 MB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ i686

flexrag-0.1.3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (931.5 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

flexrag-0.1.3-cp313-cp313-manylinux_2_17_i686.manylinux2014_i686.whl (925.4 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ i686

flexrag-0.1.3-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.3-cp312-cp312-musllinux_1_2_i686.whl (2.0 MB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ i686

flexrag-0.1.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (931.5 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

flexrag-0.1.3-cp312-cp312-manylinux_2_17_i686.manylinux2014_i686.whl (926.0 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ i686

flexrag-0.1.3-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.3-cp311-cp311-musllinux_1_2_i686.whl (2.0 MB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ i686

flexrag-0.1.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (933.1 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

flexrag-0.1.3-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl (926.9 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ i686

File details

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

File metadata

File hashes

Hashes for flexrag-0.1.3-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 f376f8726b6b8aa776fad0aadc64fefb88b53d03a34c1dba64c1e27f110fc446
MD5 0324bfdce0c8e923d1a4620b40f032c9
BLAKE2b-256 24c39355321e4937cf1feef0b07081f46b0d15a3889d3054587f5b752e613c72

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flexrag-0.1.3-cp313-cp313-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 806c2d70dad872775e86ec9356e1bd8b6b3785fd47dc2597e7545b664b117a8b
MD5 b54b5d5418225b76c8b3b6dc4dee3d35
BLAKE2b-256 82a6d1c8c108f7c0ddd0540a484ee5f0b8a45d6ff8430691451d294cf1a2af34

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flexrag-0.1.3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 697ae3a7ba35330adf48d2048d5ed5bde45ba4bfec3eaa669f124bcd3ae638d8
MD5 4916c717b3a8318aa319f85f2a66dc4f
BLAKE2b-256 4ad2510623d73e897f04200f666896a41926385debdc82f058226820afd8f763

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flexrag-0.1.3-cp313-cp313-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 19de1a04aeb5294c60f72dd13ce9a30f8d5435762247257459262ce1e85479c4
MD5 59b19646c34be02b6d7f46750243e152
BLAKE2b-256 cfd38d5854025ef31c03439c8d5291562941b43042e567daca6462567acb2306

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flexrag-0.1.3-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 8dc6dc5df9b85ce62e4c647e3e6ff119c02e9942ed21462987da974259ccaf4f
MD5 ceef69c388c90306202c737078beaba6
BLAKE2b-256 8872a52f908a83ac8cf2fb305217b6b78f8d077cefd83f4db6e94df4cfd6a3ba

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flexrag-0.1.3-cp312-cp312-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 4699426de23bdc057216c1608338756bf4e001738804ccf792a2dc9d01269f57
MD5 594ba9de41ac28179e0416365d3a42f9
BLAKE2b-256 275b360c31b0586b8619dc9d28f274a06e76839dfdf82cd29f10236cb4973aa7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flexrag-0.1.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7b86a1567b5428c541b73d237a1bd210ef885a32942df7d23702e4a19728fb2d
MD5 916e7f60cdc37a571a93b77cc9e8dee2
BLAKE2b-256 daf03d956b039dad874f3afff3b8ee0d4af556d206d070c6b08c9e31dfa3773e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flexrag-0.1.3-cp312-cp312-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 e54cbb2a909beedeb99f674cf9f0e513cdc948612d822e53fe4cd3e538caabce
MD5 bfdf053e66b3a28e2f4dd48e3064a8fb
BLAKE2b-256 3362f290abf8ed53d83aa5d1bdb2f0e4578da3506c7072bc2dae82b396e02626

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flexrag-0.1.3-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 fafc9b2e4a9defdc154dd63eaaa3877ce7769c08bffa0c0e46647a3d205d6e1d
MD5 1c02f68df177881d07511b0b209e90b2
BLAKE2b-256 553a84b90df6a71d80867846622811c4873783e14fae63af80bec42a13b36771

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flexrag-0.1.3-cp311-cp311-musllinux_1_2_i686.whl
Algorithm Hash digest
SHA256 7ebfcc9ed93e3faad8c33885d2216b29807cde2b5f358bef8edf1482e84f78c0
MD5 08c6d4d1b282d77969b70e8d2992f566
BLAKE2b-256 615f9c4f9b55ac7300ff615a701bd29c164c1f6e504f4360a8cbab5389abefee

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flexrag-0.1.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f381cce58900767557c89236a5704a15eedb92f203e681ba3e5e5ee844c5d835
MD5 b3428d82d4787688156a79224d49a75c
BLAKE2b-256 c915d60f32c53f30ce1cb72cdcbf06a0241a5ef606024612a5cfb2c5b2529d77

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flexrag-0.1.3-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm Hash digest
SHA256 9a3b62a49b349175154cb3be76771dc983722851ec04254dfc99943b03e41e32
MD5 dbc86627e86473cbc656e7dc19204761
BLAKE2b-256 506f3022ad46b82eee91006efb9cbd93f0ff3e26bb864d88043bbe33f720b3cb

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