R3-RAG is a novel framework that uses Reinforcement learning to teach LLMs how to Reason and Retrieve step by step. Unlike traditional RAG methods that rely on human-designed workflows, R3-RAG enables models to autonomously learn optimal reasoning-retrieval strategies through reinforcement learning with both outcome and process rewards.

Our method significantly outperforms existing baselines across three multi-hop QA datasets:
| Methods | Retriever | HotpotQA | 2WikiMultiHopQA | MuSiQue | Average |
|---|---|---|---|---|---|
| Llama-3.1-8B | |||||
| CoT | - | 39.2 | 28.8 | 14.0 | 27.3 |
| RAG with CoT | E5 | 53.3 | 32.9 | 16.3 | 34.2 |
| IRCoT | E5 | 52.8 | 40.6 | 16.7 | 36.7 |
| R3-RAG | E5 | 64.4 | 61.0 | 32.2 | 52.6 |
| R3-RAG | BGE | 65.3 | 62.1 | 33.8 | 53.8 |
| Qwen2.5-7B | |||||
| CoT | - | 34.0 | 31.1 | 12.7 | 25.9 |
| RAG with CoT | E5 | 52.4 | 33.5 | 16.9 | 34.3 |
| IRCoT | E5 | 48.4 | 35.8 | 13.5 | 32.6 |
| R3-RAG | E5 | 65.5 | 62.3 | 33.6 | 53.8 |
| R3-RAG | BGE | 66.4 | 63.0 | 34.8 | 54.8 |
We recommend setting up three separate conda environments to avoid dependency conflicts:
FlashRAG Environment (for retrieval tools): Please refer to FlashRAG to set up the environment.
LLaMA-Factory Environment (for cold start training): Please refer to LLaMA-Factory to set up the environment.
OpenRLHF Environment (for RL training): Please refer to OpenRLHF to set up the environment, then install our modified openrlhf code in this repository.
Download our pre-trained models from Hugging Face:
# Cold start models
git clone https://huggingface.co/Yuan-Li-FNLP/R3-RAG-CS-Llama
git clone https://huggingface.co/Yuan-Li-FNLP/R3-RAG-CS-Qwen
# Full R3-RAG models
git clone https://huggingface.co/Yuan-Li-FNLP/R3-RAG-Llama
git clone https://huggingface.co/Yuan-Li-FNLP/R3-RAG-Qwen
Experience R3-RAG with our visualization interface:
# First, start the server
cd startup
bash server.sh
# Then, start the visualization interface
bash startup_visualize.sh
Make sure to configure the model paths and parameters in the startup scripts before running.
R3-RAG/
├── benchmark/ # Evaluation scripts and benchmarks
│ ├── evaluate.py # Main evaluation script
│ ├── metrics/ # Evaluation metrics implementation
│ └── datasets/ # Dataset loading and processing
├── data/ # Cold start data construction
│ ├── build_coldstart_data.py # Generate high-quality cold start trajectories
│ ├── data_processing/ # Data preprocessing utilities
│ └── templates/ # Prompt templates for data generation
├── startup/ # Demo and visualization scripts
│ ├── server.sh # Start the server
│ ├── startup_visualize.sh # Start visualization interface
│ └── demo_config.py # Configuration for demo
├── tool/ # Retrieval tools and services
│ ├── retrieval/ # Retrieval tool implementations
│ ├── vllm_service/ # VLLM service code
│ └── utils/ # Utility functions
├── train/ # Training frameworks
│ ├── llamafactory/ # SFT training with LLaMA-Factory
│ │ ├── sft_training.py # Cold start SFT training script
│ │ └── configs/ # Training configurations
│ └── openrlhf/ # RLHF training with OpenRLHF
│ ├── rl_training.py # Reinforcement learning training
│ ├── reward_models/ # Reward model implementations
│ └── configs/ # RL training configurations
├── README.md # This file
└── LICENSE # License file
All models and datasets are available on Hugging Face.
If you find our work helpful, please consider citing:
@misc{li2025r3raglearningstepbystepreasoning,
title={R3-RAG: Learning Step-by-Step Reasoning and Retrieval for LLMs via Reinforcement Learning},
author={Yuan Li and Qi Luo and Xiaonan Li and Bufan Li and Qinyuan Cheng and Bo Wang and Yining Zheng and Yuxin Wang and Zhangyue Yin and Xipeng Qiu},
year={2025},
eprint={2505.23794},
archivePrefix={arXiv},
primaryClass={cs.CL},
url={https://arxiv.org/abs/2505.23794},
}
We welcome contributions! Please feel free to submit issues and pull requests.
This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.
For questions or collaborations, please contact: - Yuan Li: liyuan24@m.fudan.edu.cn - Xipeng Qiu: xpqiu@fudan.edu.cn
Made with ❤️ by the Fudan NLP Group
$ claude mcp add R3-RAG \
-- python -m otcore.mcp_server <graph>