File size: 8,951 Bytes
bc764fe
 
c0a712d
 
bc764fe
 
 
 
 
 
db409e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc764fe
c0a712d
 
 
 
 
 
 
bc764fe
c0a712d
bc764fe
c0a712d
 
 
 
 
bc764fe
 
 
 
 
c0a712d
 
 
 
 
 
 
 
 
bc764fe
 
 
 
 
 
 
 
 
c0a712d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
---
license: apache-2.0
pipeline_tag: text-generation
library_name: transformers
---

<div align="center">
<h1>SQL-R1: Training Natural Language to SQL Reasoning Model By Reinforcement Learning</h1>
</div>

<div align="center">
<p>
    <a href="https://github.com/MPX0222">Peixian Ma</a><sup>1,2</sup>&nbsp;&nbsp;
    <a href="https://idea-finai.github.io/SQL-R1/">Xialie Zhuang</a><sup>1,3</sup>&nbsp;&nbsp;
    <a href="https://idea-finai.github.io/SQL-R1/">Chengjin Xu</a><sup>1,4</sup>&nbsp;&nbsp;
    <a href="https://idea-finai.github.io/SQL-R1/">Xuhui Jiang</a><sup>1,4</sup>&nbsp;&nbsp;
    <a href="https://idea-finai.github.io/SQL-R1/">Ran Chen</a><sup>1</sup>&nbsp;&nbsp;
    <a href="https://idea-finai.github.io/SQL-R1/">Jian Guo</a><sup>1</sup>
</p>

<p>
    <sup>1</sup>IDEA Research, International Digital Economy Academy
    <sup>2</sup>The Hong Kong University of Science and Technology (Guangzhou)
    <sup>3</sup>University of Chinese Academy of Science
    <sup>4</sup>DataArc Tech Ltd.
</p>
</div>

<div align="center" style="display: flex; gap: 5px; justify-content: center;">
<a href="https://dataarctech.github.io/SQL-R1/"><img src="https://img.shields.io/badge/🏠_Homepage-blue?style=for-the-badge"/></a>
<a href="https://arxiv.org/abs/2504.08600"><img src="https://img.shields.io/badge/arXiv-red?style=for-the-badge&logo=arxiv"/></a>
<a href="https://huggingface.co/papers/2504.08600"><img src="https://img.shields.io/badge/Paper-HuggingFace-ffbd45.svg?style=for-the-badge&logo=huggingface"/></a>
<a href="https://github.com/IDEA-FinAI/SQL-R1"><img src="https://img.shields.io/badge/GitHub-black?style=for-the-badge&logo=github"/></a>
<a href="https://huggingface.co/MPX0222forHF/SQL-R1-7B"><img src="https://img.shields.io/badge/HuggingFace-FF9D00?style=for-the-badge&logo=huggingface"/></a>
<a href="https://www.modelscope.cn/models/MPX0222/SQL-R1-7B"><img src="https://img.shields.io/badge/πŸ€–_ModelScope-946CE6?style=for-the-badge"/></a>
<a href="https://github.com/IDEA-FinAI/SQL-R1/stargazers"><img src="https://img.shields.io/github/stars/IDEA-FinAI/SQL-R1?style=for-the-badge&color=white"/></a>
</div>
<br>

<div align="center" style="display: flex; gap: 5px; justify-content: center;">
  <p>
  <b>πŸ”₯  Our work is accepted by NeurIPS 2025. Welcome to star and cite our work! ✨</b> 
  </p>
</div>

## πŸ“– Overview

Natural Language to SQL (NL2SQL) enables intuitive interactions with databases by transforming natural language queries into structured SQL statements.  Despite recent advancements in enhancing human-computer interaction within database applications, significant challenges persist, particularly regarding the inference performance in complex scenarios involving multi-table joins and nested queries. Current methodologies primarily utilize supervised fine-tuning (SFT) to train the NL2SQL model, which may limit adaptability and interpretability in new environments (e.g., finance and healthcare). In order to enhance the reasoning performance of the NL2SQL model in the above complex situations, we introduce SQL-R1, a novel NL2SQL reasoning model trained by the reinforcement learning (RL) algorithms. We design a specialized RL-based reward function tailored for NL2SQL tasks and discussed the impact of cold start on the effectiveness of intensive training. In addition, we achieve competitive accuracy using only a tiny amount of synthetic NL2SQL data for augmented training and further explore data engineering for RL. In existing experiments, SQL-R1 achieves execution accuracy of 88.6\% and 67.1\% on the benchmark Spider and BIRD, respectively.

<div align="center">
<img src="images/overview.png" alt="SQL-R1 Overview" width="800"/>
<p align="center">
Figure 1: Demonstration of our work.
</p>

</div>


## πŸ“š Citations

```bibtex
@article{ma2025sql,
  title={SQL-R1: Training Natural Language to SQL Reasoning Model By Reinforcement Learning},
  author={Ma, Peixian and Zhuang, Xialie and Xu, Chengjin and Jiang, Xuhui and Chen, Ran and Guo, Jian},
  journal={arXiv preprint arXiv:2504.08600},
  year={2025}
}
```

## πŸ“° News

- **[2025.09.18]** πŸŽ‰ SQL-R1 is accept by NeurIPS 2025! We will soon update the full version of the paper and poster. Welcome to star and cite our work!
- **[2025.05.27]** πŸŽ‰ We have released the full version of SQL-R1.
- **[2025.05.21]** πŸŽ‰ We have released our model weights on HuggingFace! Check out the [Model Weights](#-model-weights) section below.
- **[2025.04.11]** πŸ“‘ Our paper is now available on [arXiv](https://arxiv.org/abs/2504.08600).


## πŸš€ Coming Soon Checklist

- [x] πŸ“ Update the camera-ready version of the paper, homepage and poster. coming sooooon!
- [x] πŸ“Š Release model weights on HuggingFace and ModelScope
- [x] πŸ”§ Open source training code and RL dataset
- [x] πŸ“ Detailed documentation
- [x] πŸ› οΈ Environment setup guide


## πŸ€– Model Weights

We are excited to release our SQL-R1 model weights! You can find them on HuggingFace:

| Model  | Size | HuggingFace Link | ModelScope Link |
|-------------|-------------|------|------|
| SQL-R1 (3B) | 3B | [πŸ€— Download](https://huggingface.co/MPX0222forHF/SQL-R1-3B) | - |
| SQL-R1 (7B) | 7B | [πŸ€— Download](https://huggingface.co/MPX0222forHF/SQL-R1-7B) | [πŸ€– Download](https://www.modelscope.cn/models/MPX0222/SQL-R1-7B) |
| SQL-R1 (14B) | 14B | [πŸ€— Download](https://huggingface.co/MPX0222forHF/SQL-R1-14B) | - |


## πŸ“‘ Documentation Structure

This repository is organized as follows:

```
SQL-R1/
β”œβ”€β”€ data/                             # Datasets
β”‚   β”œβ”€β”€ Spider/      
β”‚   └── BIRD/        
β”œβ”€β”€ models/                           # Base models or checkpoints
β”‚   β”œβ”€β”€ Qwen2.5-Coder-3B-Instruct/   
β”‚   └── Qwen2.5-Coder-7B-Instruct/   
......
β”œβ”€β”€ sh/                               # Scripts for data processing, training, inference and evaluation
β”œβ”€β”€ src/                              # Source code
└── verl/                             # Verl framework
```


## πŸ› οΈ Environment Setup

> [!NOTE]
> Before getting started, make sure your computing environment supports the following settings:
> - Environment: Python 3.9+
> - CUDA Version: 12.0+ (for verl and vllm integration)
> - GPU Prerequisites: 8 x 80GB+ GPU (for training) / 2 x 40GB GPU (for inference)

### Installation

1. Clone the repository:
```bash
git clone https://github.com/IDEA-FinAI/SQL-R1.git
cd SQL-R1
```

2. Create and activate a virtual environment (recommended):
```bash
conda create -n sqlr1 python=3.9
```

3. Install dependencies:
```bash
pip install torch==2.4.0 --index-url https://download.pytorch.org/whl/cu121
pip install vllm==0.6.3 ray
pip install flash-attn --no-build-isolation
pip install -e .  # For verl integration
pip install wandb IPython matplotlib sqlparse func_timeout nltk ijson
```

4. Download the model weights from HuggingFace and put them in the `models/` directory. 

5. For training, copy the training dataset in the `example_data` directory to the `data` directory. For inference, copy the database information in the `db_info` directory (including files forSpider-dev, Spider-test and BIRD-dev) to the related dataset (`data/Spider`, `data/BIRD`) directory.

## πŸš€ Quick Start

1. Run training:
```bash
sh sh/train.sh
```

2. Run inference:
```bash
sh sh/inference.sh
```

3. Run evaluation:
```bash
# evaluate spider
sh sh/eval_spider.sh
# evaluate bird
sh sh/eval_bird.sh
```

## 🌟 Applications

SQL-R1 can be effectively utilized in the following key areas:

1.  **Foundation Model for Workflow Systems**
    - Serves as a base model for complex database operations
    - Enables seamless integration with existing workflow automation systems
    - Supports customization and fine-tuning for specific business processes
    - Provides robust API endpoints for system integration

2.  **Enhanced NL2SQL Interpretability**
    - Generates detailed explanations for SQL query construction
    - Provides step-by-step reasoning for query transformation
    - Helps users understand the relationship between natural language and SQL syntax
    - Offers visualization of query execution plans

3.  **Self-Evolving NL2SQL Agent Checkpoint**
    - Acts as a foundation checkpoint for continuous learning
    - Enables iterative improvement through feedback loops
    - Supports transfer learning for domain-specific adaptations
    - Facilitates model versioning and performance tracking

This may be added in the future work. If you have any ideas, please feel free to contact us.

## Thanks for

We thank [OmniSQL](https://github.com/RUCKBReasoning/OmniSQL) and follow their evaluation code and database information retrieval code. We have adapted and modified their evaluation scripts for our project.

## Star History

[![Star History Chart](https://api.star-history.com/svg?repos=IDEA-FinAI/SQL-R1&type=Date)](https://www.star-history.com/#IDEA-FinAI/SQL-R1&Date)