Source code for "TeaRAG: A Token-Efficient Agentic Retrieval-Augmented Generation Framework"

July 2, 2026 ยท View on GitHub

TeaRAG

Source code for "TeaRAG: A Token-Efficient Agentic Retrieval-Augmented Generation Framework"

๐ŸŽ‰ News: Our paper has been accepted by ACM TOIS (Transactions on Information Systems)!

๐Ÿ“œ Overview

TeaRAG is a tokenโ€‘efficient, agentic Retrievalโ€‘Augmented Generation framework that solves complex queries with fewer tokens and faster reasoning. By compressing both retrieval content and reasoning steps, TeaRAG delivers +4% / +2% EM gains on Llama3โ€‘8Bโ€‘Instruct and Qwen2.5โ€‘14Bโ€‘Instruct while cutting token usage by ~60%. Built on FlashRAG, it integrates graphโ€‘based knowledge retrieval and a novel Iterative Processโ€‘aware DPO to achieve better results and higher efficiency in agentic RAG.

Demo

Overview

๐Ÿ“š Model & Dataset

Our models and the Wiki corpusโ€“based knowledge graph are available on Hugging Face: ๐Ÿค— zclfe/tearag

โš™๏ธ Installation

# Clone TeaRAG (built on top of FlashRAG)
git clone https://github.com/Applied-Machine-Learning-Lab/TeaRAG.git
cd TeaRAG
pip install -e .

# FlashRAG dependencies
pip install vllm>=0.4.1
conda install -c pytorch -c nvidia faiss-gpu=1.8.0

# Training dependencies
pip3 install flash-attn --no-build-isolation
pip install accelerate==0.34.2
pip install trl==0.17.0
pip install torch==2.6.0 torchvision==0.21.0 torchaudio==2.6.0 \
    --index-url https://download.pytorch.org/whl/cu126

# Redis setup
pip install redis
sudo apt install redis-server -y
redis-server --dir path/redis \
             --appendonly yes \
             --appendfilename appendonly.aof \
             --daemonize yes \
             --port 6379

# Test Redis (note: dataset loading may take some time)
redis-cli GET t:77899428

๐Ÿ“‚ Repository Structure

TeaRAG's file organization should be constructed as follows, to minimize modifications to the code.

โ”œโ”€โ”€ Root Path
    โ”œโ”€โ”€ TeaRAG        # Source code for TeaRAG
    โ”œโ”€โ”€ model         # Saved pre-trained models
    โ”œโ”€โ”€ data          # Datasets and corpora
    โ”œโ”€โ”€ train_log     # Intermediate outputs and trained models
    โ”œโ”€โ”€ log           # Inference logs (config, intermediate results, final results)
    โ”œโ”€โ”€ redis         # Redis-based knowledge graph storage
    โ”œโ”€โ”€ index         # Pre-built retrieval indexes

This repository builds on FlashRAG and uses a similar structure except for the alg directory, which contains our customized scripts:

โ”œโ”€โ”€ alg
    โ”œโ”€โ”€ config        # Fixed inference hyper-parameter configs
    โ”œโ”€โ”€ data          # Dataset preparation scripts
    โ”œโ”€โ”€ download      # Model + dataset download scripts
    โ”œโ”€โ”€ ds_config     # DeepSpeed config
    โ”œโ”€โ”€ index         # Index construction scripts
    โ”œโ”€โ”€ infer_script  # Inference scripts
    โ”œโ”€โ”€ method        # Method entry points
    โ”œโ”€โ”€ train         # Training code
    โ”œโ”€โ”€ train_script  # Training scripts
    โ”œโ”€โ”€ prepare.sh    # Full preparation pipeline
    โ”œโ”€โ”€ run_pipeline_medium.sh  # Full pipeline for Llama3-8B-Instruct
    โ”œโ”€โ”€ run_pipeline_qwen.sh    # Full pipeline for Qwen2.5-14B-Instruct

โ–ถ๏ธ Quick Start

Prepare (download data, build KG + index, prepare training set)

cd alg
bash prepare.sh

Train & Evaluate (Llama-3-8B-Instruct)

cd alg
bash run_pipeline_medium.sh

Train & Evaluate (Qwen2.5-14B-Instruct)

cd alg
bash run_pipeline_qwen.sh

Run inference for all datasets/models/baselines

cd alg/infer_script
bash run_all.sh

๐Ÿ“ฌ Contact

For questions, suggestions, or bug reports, please reach out:
๐Ÿ“ง zclfe00@gmail.com

We welcome contributions and feedback to make TeaRAG even better!


๐Ÿ™ Acknowledgements

  • FlashRAG โ€“ TeaRAG is built upon the overall framework of FlashRAG.
  • Xiaohongshu โ€“ This research was supported by computational resources from Xiaohongshuโ€™s Search group, which greatly facilitated this research.

Citation

If TeaRAG is helpful in your research or applications, please consider citing our work:

@article{zhang2025tearag,
  title={TeaRAG: A Token-Efficient Agentic Retrieval-Augmented Generation Framework},
  author={Zhang, Chao and Wang, Yuhao and Xu, Derong and Zhang, Haoxin and Lyu, Yuanjie and Chen, Yuhao and Liu, Shuochen and Xu, Tong and Zhao, Xiangyu and Gao, Yan and others},
  journal={ACM Transactions on Information Systems},
  year={2025},
  publisher={ACM New York, NY}
}