MCPcopy Index your code
hub / github.com/Yuan-Li-FNLP/R3-RAG

github.com/Yuan-Li-FNLP/R3-RAG @main

Chat with this repo
repository ↗ · DeepWiki ↗ · + Follow
2,042 symbols 9,120 edges 297 files 502 documented · 25%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

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

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:

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

🚀 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: - Yuan Li: liyuan24@m.fudan.edu.cn - Xipeng Qiu: xpqiu@fudan.edu.cn


Made with ❤️ by the Fudan NLP Group

Core symbols most depended-on inside this repo

append
called by 367
tool/FlashRAG/webui/chatter.py
append
called by 170
train/R3RAG_OpenRLHF/openrlhf/trainer/ppo_utils/replay_buffer.py
append
called by 163
train/LLaMA-Factory/src/llamafactory/webui/chatter.py
format
called by 107
train/R3RAG_OpenRLHF/openrlhf/utils/logging_utils.py
update
called by 93
train/LLaMA-Factory/src/llamafactory/extras/misc.py
info_rank0
called by 90
train/LLaMA-Factory/src/llamafactory/extras/logging.py
register_model_group
called by 82
train/LLaMA-Factory/src/llamafactory/extras/constants.py
is_rank_0
called by 80
train/R3RAG_OpenRLHF/openrlhf/utils/deepspeed/deepspeed.py

Shape

Method 1,015
Function 722
Class 293
Route 12

Languages

Python100%
TypeScript1%

Modules by API surface

tool/FlashRAG/flashrag/evaluator/metrics.py65 symbols
tool/FlashRAG/flashrag/refiner/llmlingua_compressor.py60 symbols
tool/FlashRAG/flashrag/retriever/retriever.py52 symbols
train/LLaMA-Factory/src/llamafactory/data/mm_plugin.py46 symbols
tool/FlashRAG/flashrag/pipeline/active_pipeline.py39 symbols
train/R3RAG_OpenRLHF/openrlhf/trainer/ppo_utils/experience_maker_prm_orm.py38 symbols
train/R3RAG_OpenRLHF/openrlhf/trainer/ppo_utils/experience_maker_orm.py38 symbols
train/R3RAG_OpenRLHF/openrlhf/trainer/ppo_utils/experience_maker_only_prm.py38 symbols
train/R3RAG_OpenRLHF/openrlhf/trainer/ppo_utils/experience_maker.py38 symbols
train/LLaMA-Factory/src/llamafactory/train/callbacks.py30 symbols
train/R3RAG_OpenRLHF/openrlhf/models/loss.py28 symbols
tool/FlashRAG/flashrag/generator/generator.py27 symbols

For agents

$ claude mcp add R3-RAG \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact