R3-RAG: Learning Step-by-Step Reasoning and Retrieval for LLMs via Reinforcement Learning

October 5, 2025 ยท View on GitHub

arXiv Python 3.10+ License

๐Ÿ“– Overview

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.

R3-RAG Training Pipeline

Key Features

  • ๐Ÿ”ฅ Autonomous Learning: Uses RL to learn reasoning-retrieval strategies instead of relying on fixed human-designed workflows
  • ๐ŸŽฏ Dual Reward System: Combines outcome rewards (answer correctness) with process rewards (document relevance)
  • ๐Ÿš€ Strong Performance: Achieves significant improvements over state-of-the-art iterative RAG methods
  • ๐Ÿ”„ Transferable: Works across different retrievers (E5, BGE, BM25) with consistent performance
  • ๐Ÿ“Š Comprehensive: Evaluated on multiple multi-hop QA datasets (HotpotQA, 2WikiMultiHopQA, MuSiQue)

๐Ÿ“Š Main Results

Our method significantly outperforms existing baselines across three multi-hop QA datasets:

MethodsRetrieverHotpotQA2WikiMultiHopQAMuSiQueAverage
Llama-3.1-8B
CoT-39.228.814.027.3
RAG with CoTE553.332.916.334.2
IRCoTE552.840.616.736.7
R3-RAGE564.461.032.252.6
R3-RAGBGE65.362.133.853.8
Qwen2.5-7B
CoT-34.031.112.725.9
RAG with CoTE552.433.516.934.3
IRCoTE548.435.813.532.6
R3-RAGE565.562.333.653.8
R3-RAGBGE66.463.034.854.8

๐Ÿš€ Quick Start

Environment Setup

We recommend setting up three separate conda environments to avoid dependency conflicts:

  1. FlashRAG Environment (for retrieval tools): Please refer to FlashRAG to set up the environment.

  2. LLaMA-Factory Environment (for cold start training): Please refer to LLaMA-Factory to set up the environment.

  3. OpenRLHF Environment (for RL training): Please refer to OpenRLHF to set up the environment, then install our modified openrlhf code in this repository.

Model Download

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

Quick Demo

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.

๐Ÿ“ Repository Structure

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

๐Ÿค— Available Models and Data

Models

Datasets

All models and datasets are available on Hugging Face.

๐Ÿ“„ Citation

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}, 
}

๐Ÿค Contributing

We welcome contributions! Please feel free to submit issues and pull requests.

๐Ÿ“ License

This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.

๐Ÿ™ Acknowledgments

๐Ÿ“ž Contact

For questions or collaborations, please contact:


Made with โค๏ธ by the Fudan NLP Group