A RAG Framework for Information Retrieval and Generation.
Project description
FlexRAG is an open-source framework designed for Retrieval-Augmented Generation (RAG), combining state-of-the-art information retrieval techniques with large language models (LLMs) for enhanced, context-aware responses.
:book: Table of Contents
- :book: Table of Contents
- :sparkles: Key Features
- :rocket: Getting Started
- :bar_chart: Benchmarks
- :label: License
- :pen: Citation
- :heart: Acknowledgements
:sparkles: Key Features
- Multiple Retriever Types: Supports a wide range of retrievers, including sparse, dense, network-based, and multimodal retrievers.
- Diverse Information Sources: Allows integration of multiple data types, such as pure text, images, documents, web snapshots, and more.
- Unified Configuration Management: Leveraging OmegaConf and dataclasses, FlexRAG simplifies configuration and management across your entire project.
- Out-of-the-Box Performance: Comes with carefully optimized default configurations for retrievers, delivering 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.
:rocket: 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/ZhuochengZhang98/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 your RAG Application
When the index is ready, you can run your RAG application. Here is an example of how to run a RAG application.
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 application on several knowledge intensive datasets with great ease. The following command let you evaluate the modular RAG 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 RAG 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 Application
To build your own RAG application, you can create a new Python file and import the necessary FlexRAG modules. Here is an example of how to build a RAG application:
from flexrag.retrievers import DenseRetriever, DenseRetrieverConfig
from flexrag.models import OpenAIGenerator, OpenAIGeneratorConfig
from flexrag.prompt import ChatPrompt, ChatTurn
def main():
# Initialize the retriever
retriever = DenseRetriever(
DenseRetrieverConfig(
database_path='path_to_database',
encode_fields=['text'],
top_k=1,
passage_encoder_config=HfEncoderConfig(
encoder_type='hf',
hf_config=HfConfig(
model_path='facebook/contriever',
device_id=[0],
)
),
)
)
# 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: ')
context = retriever.retrieve(query)[0]
prompt_str = f"Question: {query}\nContext: {context.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()
We also provide several detailed examples of how to build a RAG application in the flexrag examples repository.
:bar_chart: Benchmarks
We have conducted extensive benchmarks using the FlexRAG framework. For more details, please refer to the benchmarks page.
:label: License
This repository is licensed under the MIT License. See the LICENSE file for details.
:pen: Citation
If you find this project helpful, please consider citing it:
@software{FlexRAG,
author = {Zhang Zhuocheng},
doi = {10.5281/zenodo.14306984},
month = {12},
title = {{FlexRAG}},
url = {https://github.com/ZhuochengZhang98/flexrag},
version = {0.1.0},
year = {2024}
}
:heart: Acknowledgements
This project benefits from the following open-source projects:
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flexrag-0.1.1-cp313-cp313-musllinux_1_2_x86_64.whl.
File metadata
- Download URL: flexrag-0.1.1-cp313-cp313-musllinux_1_2_x86_64.whl
- Upload date:
- Size: 1.9 MB
- Tags: CPython 3.13, musllinux: musl 1.2+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cc5a623499a3f98d7bc6d6e9f19ea0ef32b222146482bd5ba3e4219bcd5f1c12
|
|
| MD5 |
61cf1452106da9cb9b51d92992d6a4aa
|
|
| BLAKE2b-256 |
4cf0e4470ee28b79105f9c3a857a69cad7d88743f6d4d151b6de5fe455f04030
|
File details
Details for the file flexrag-0.1.1-cp313-cp313-musllinux_1_2_i686.whl.
File metadata
- Download URL: flexrag-0.1.1-cp313-cp313-musllinux_1_2_i686.whl
- Upload date:
- Size: 2.0 MB
- Tags: CPython 3.13, musllinux: musl 1.2+ i686
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0b43d37dea129f6dfbf9072057ef1e29b81f531785f2be799dbefd78ba2c146a
|
|
| MD5 |
bc989d02628da267a40e72892588dfdc
|
|
| BLAKE2b-256 |
97ba288098ed33d553287339bef4facd08e8c2339563b9f17e3af83fa64780e7
|
File details
Details for the file flexrag-0.1.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: flexrag-0.1.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 929.7 kB
- Tags: CPython 3.13, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
023b0a4fe1f045223240309cf104b275a8aa615ba72b724a0fd7c27431dfef6e
|
|
| MD5 |
6ec746766396c5e3fa0fa896329b19e0
|
|
| BLAKE2b-256 |
dcdbc47803013fce13c5cd08e544e5b99bb0da537da05706af3f3f6df8e220ee
|
File details
Details for the file flexrag-0.1.1-cp313-cp313-manylinux_2_17_i686.manylinux2014_i686.whl.
File metadata
- Download URL: flexrag-0.1.1-cp313-cp313-manylinux_2_17_i686.manylinux2014_i686.whl
- Upload date:
- Size: 923.5 kB
- Tags: CPython 3.13, manylinux: glibc 2.17+ i686
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d7d2c2b090697ccd14503468ed5010375dd1a7843daf6485b6d82aa7a50f110d
|
|
| MD5 |
65a34b21be66bf548387f50ab78f8e4b
|
|
| BLAKE2b-256 |
a65cadd7307541a47771dff1c4163b4b88688d5e33f6cbd25e9e4286b58170b4
|
File details
Details for the file flexrag-0.1.1-cp312-cp312-musllinux_1_2_x86_64.whl.
File metadata
- Download URL: flexrag-0.1.1-cp312-cp312-musllinux_1_2_x86_64.whl
- Upload date:
- Size: 1.9 MB
- Tags: CPython 3.12, musllinux: musl 1.2+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
45e83d6fb0694a44998aa1ca494724004d84b652f34e85ed4758484a9cb9bb9e
|
|
| MD5 |
e2a9ac19a82c9f5631a2d7fd6dc8278c
|
|
| BLAKE2b-256 |
c9227384eaad70395530c4cb5ed1b26fdb0f4896adb119e945027c96ff2c17fe
|
File details
Details for the file flexrag-0.1.1-cp312-cp312-musllinux_1_2_i686.whl.
File metadata
- Download URL: flexrag-0.1.1-cp312-cp312-musllinux_1_2_i686.whl
- Upload date:
- Size: 2.0 MB
- Tags: CPython 3.12, musllinux: musl 1.2+ i686
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c9ee2dec50a58bf4e44c1af058f12acfc51101487f8124c84d06a6be86d54a01
|
|
| MD5 |
872fa2b49924e5f1b5c1b123574557e7
|
|
| BLAKE2b-256 |
07af58b99fb50eb1885f59b0a42eaa2a7a5ef1b052d2ae0f3c3dc1bb7b6d0211
|
File details
Details for the file flexrag-0.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: flexrag-0.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 929.6 kB
- Tags: CPython 3.12, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
775283bd98af341dbbf41c37c1118787895156d6542cb53b46424e4934e62947
|
|
| MD5 |
d3b74a95d56347cf83e3606c7d2bd524
|
|
| BLAKE2b-256 |
502e1d2218e0558ac99f29688a44c51899fd94686eed042b4577699b5ff02c92
|
File details
Details for the file flexrag-0.1.1-cp312-cp312-manylinux_2_17_i686.manylinux2014_i686.whl.
File metadata
- Download URL: flexrag-0.1.1-cp312-cp312-manylinux_2_17_i686.manylinux2014_i686.whl
- Upload date:
- Size: 924.1 kB
- Tags: CPython 3.12, manylinux: glibc 2.17+ i686
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4103e649b84691b2570de1339a527675780bc311eb11533606b6739ad5053c63
|
|
| MD5 |
0e0618d26602332cbd8b73762d4062b0
|
|
| BLAKE2b-256 |
ad0f5918f7b348ad85e4914235e6c9b444ac688fef0a0a0df53636809be33f4a
|
File details
Details for the file flexrag-0.1.1-cp311-cp311-musllinux_1_2_x86_64.whl.
File metadata
- Download URL: flexrag-0.1.1-cp311-cp311-musllinux_1_2_x86_64.whl
- Upload date:
- Size: 1.9 MB
- Tags: CPython 3.11, musllinux: musl 1.2+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aab5552ab9ed6d082463d59ab0933f41d7ae3a220b9f54ef185c070b7cc9c8be
|
|
| MD5 |
949375edbfb2620eecaeee087842bc2b
|
|
| BLAKE2b-256 |
0bbcaf1888763d0690220063cfee6c4b993cd0606439556d3a742bb3833e0720
|
File details
Details for the file flexrag-0.1.1-cp311-cp311-musllinux_1_2_i686.whl.
File metadata
- Download URL: flexrag-0.1.1-cp311-cp311-musllinux_1_2_i686.whl
- Upload date:
- Size: 2.0 MB
- Tags: CPython 3.11, musllinux: musl 1.2+ i686
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e257fe3dcc3d6b0c1113fedf12e15261349bb8e229a2519d5e1b9b29acf4ac57
|
|
| MD5 |
c708e5c13960c61e7a91bac7d3de0e3a
|
|
| BLAKE2b-256 |
788f872807b7ebc20e4ac7624969026d262d69f3d8dfdb8876da8ef36937b03d
|
File details
Details for the file flexrag-0.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: flexrag-0.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 931.2 kB
- Tags: CPython 3.11, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0e1502fabd9caed860246b186f5f7e69501e131df8fc8df6a3b7495ac6b223c8
|
|
| MD5 |
98a13a1e2346488153a2dcd9c9f75570
|
|
| BLAKE2b-256 |
5cdf1498ef785ec2757a1643f8f996ebac6578c6207947349b482f46cb00ceda
|
File details
Details for the file flexrag-0.1.1-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl.
File metadata
- Download URL: flexrag-0.1.1-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl
- Upload date:
- Size: 925.0 kB
- Tags: CPython 3.11, manylinux: glibc 2.17+ i686
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
92c295f98a2b059de4978be65ede013b5b5c62169186903238cd5cb5b58379dd
|
|
| MD5 |
76406ce73122240d1f241762d74b4916
|
|
| BLAKE2b-256 |
a29e1feeb525e99584290892c6accf9c2491cf820bdf30b0d6fb6e0bb76e81f0
|