Add comprehensive model card for CSC-SQL model (#1)
Browse files- Add comprehensive model card for CSC-SQL model (0a0d80548f8f5c165ac902f2ad76447c69679e8a)
Co-authored-by: Niels Rogge <[email protected]>
README.md
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
pipeline_tag: text-generation
|
| 3 |
+
library_name: transformers
|
| 4 |
+
license: apache-2.0
|
| 5 |
+
tags:
|
| 6 |
+
- text-to-sql
|
| 7 |
+
- sql
|
| 8 |
+
- reinforcement-learning
|
| 9 |
+
- qwen2
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# CSC-SQL: Corrective Self-Consistency in Text-to-SQL via Reinforcement Learning
|
| 13 |
+
|
| 14 |
+
This repository contains the model presented in the paper [CSC-SQL: Corrective Self-Consistency in Text-to-SQL via Reinforcement Learning](https://huggingface.co/papers/2505.13271).
|
| 15 |
+
|
| 16 |
+
**Code Repository**: [https://github.com/CycloneBoy/csc_sql](https://github.com/CycloneBoy/csc_sql)
|
| 17 |
+
|
| 18 |
+
## Introduction
|
| 19 |
+
|
| 20 |
+
Large language models (LLMs) have demonstrated strong capabilities in translating natural language questions about relational databases into SQL queries. In particular, test-time scaling techniques such as Self-Consistency and Self-Correction can enhance SQL generation accuracy by increasing computational effort during inference. However, these methods have notable limitations: Self-Consistency may select suboptimal outputs despite majority votes, while Self-Correction typically addresses only syntactic errors. To leverage the strengths of both approaches, we propose CSC-SQL, a novel method that integrates Self-Consistency and Self-Correction. CSC-SQL selects the two most frequently occurring outputs from parallel sampling and feeds them into a merge revision model for correction. Additionally, we employ the Group Relative Policy Optimization (GRPO) algorithm to fine-tune both the SQL generation and revision models via reinforcement learning, significantly enhancing output quality. Experimental results confirm the effectiveness and generalizability of CSC-SQL. On the BIRD private test set, our 7B model achieves 71.72% execution accuracy, while the 32B model achieves 73.67%.
|
| 21 |
+
|
| 22 |
+

|
| 23 |
+
|
| 24 |
+
## Main Results
|
| 25 |
+
|
| 26 |
+
Performance Comparison of different Text-to-SQL methods on BIRD dev and test dataset.
|
| 27 |
+

|
| 28 |
+
|
| 29 |
+
## Model Checkpoints
|
| 30 |
+
|
| 31 |
+
The models and datasets related to CSC-SQL are available on Hugging Face and ModelScope:
|
| 32 |
+
|
| 33 |
+
| **Model and Dataset** | Modelscope | HuggingFace |
|
| 34 |
+
| :------------------------------------ | :---------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------ |
|
| 35 |
+
| bird train and dev dataset | [π€ Modelscope](https://modelscope.cn/datasets/cycloneboy/bird_train) | [π€ HuggingFace](https://huggingface.co/datasets/cycloneboy/bird_train) |
|
| 36 |
+
| CscSQL-Merge-Qwen2.5-Coder-3B-Instruct | [π€ Modelscope](https://modelscope.cn/models/cycloneboy/CscSQL-Merge-Qwen2.5-Coder-3B-Instruct) | [π€ HuggingFace](https://huggingface.co/cycloneboy/CscSQL-Merge-Qwen2.5-Coder-3B-Instruct) |
|
| 37 |
+
| CscSQL-Merge-Qwen2.5-Coder-7B-Instruct | [π€ Modelscope](https://modelscope.cn/models/cycloneboy/CscSQL-Merge-Qwen2.5-Coder-7B-Instruct) | [π€ HuggingFace](https://huggingface.co/cycloneboy/CscSQL-Merge-Qwen2.5-Coder-7B-Instruct) |
|
| 38 |
+
| CscSQL-Grpo-Qwen2.5-Coder-3B-Instruct | [π€ Modelscope](https://modelscope.cn/models/cycloneboy/CscSQL-Grpo-Qwen2.5-Coder-3B-Instruct) | [π€ HuggingFace](https://huggingface.co/cycloneboy/CscSQL-Grpo-Qwen2.5-Coder-3B-Instruct) |
|
| 39 |
+
| CscSQL-Grpo-XiYanSQL-QwenCoder-3B-2502 | [π€ Modelscope](https://modelscope.cn/models/cycloneboy/CscSQL-Grpo-XiYanSQL-QwenCoder-3B-2502) | [π€ HuggingFace](https://huggingface.co/cycloneboy/CscSQL-Grpo-XiYanSQL-QwenCoder-3B-2502) |
|
| 40 |
+
| CscSQL-Grpo-Qwen2.5-Coder-7B-Instruct | [π€ Modelscope](https://modelscope.cn/models/cycloneboy/CscSQL-Grpo-Qwen2.5-Coder-7B-Instruct) | [π€ HuggingFace](https://huggingface.co/cycloneboy/CscSQL-Grpo-Qwen2.5-Coder-7B-Instruct) |
|
| 41 |
+
| CscSQL-Grpo-XiYanSQL-QwenCoder-7B-2502 | [π€ Modelscope](https://modelscope.cn/models/cycloneboy/CscSQL-Grpo-XiYanSQL-QwenCoder-7B-2502) | [π€ HuggingFace](https://huggingface.co/cycloneboy/CscSQL-Grpo-XiYanSQL-QwenCoder-7B-2502) |
|
| 42 |
+
|
| 43 |
+
## Usage
|
| 44 |
+
|
| 45 |
+
This model can be loaded and used with the Hugging Face `transformers` library. Below is a simple example for text-to-SQL inference. For more advanced usage, including data processing, training, and evaluation scripts, please refer to the [official GitHub repository](https://github.com/CycloneBoy/csc_sql).
|
| 46 |
+
|
| 47 |
+
```python
|
| 48 |
+
import torch
|
| 49 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
|
| 50 |
+
|
| 51 |
+
# The specific model identifier for this repository
|
| 52 |
+
model_id = "cycloneboy/CscSQL-Grpo-Qwen2.5-Coder-7B-Instruct" # Replace with the actual model ID if different
|
| 53 |
+
|
| 54 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 55 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, device_map='auto', torch_dtype=torch.bfloat16, trust_remote_code=True)
|
| 56 |
+
model.eval()
|
| 57 |
+
|
| 58 |
+
# Example: Text-to-SQL inference using the Qwen2 chat template
|
| 59 |
+
# For a real-world text-to-SQL task, you would typically need to provide the database schema or
|
| 60 |
+
# context relevant to the query as part of the prompt.
|
| 61 |
+
question = "What are the names of all employees who work in the 'Sales' department?"
|
| 62 |
+
messages = [
|
| 63 |
+
{"role": "system", "content": "You are a helpful assistant trained to convert natural language questions into SQL queries."},
|
| 64 |
+
{"role": "user", "content": f"Translate the following natural language query into SQL: '{question}'"},
|
| 65 |
+
]
|
| 66 |
+
|
| 67 |
+
# Apply the chat template to format the input according to Qwen2's conventions
|
| 68 |
+
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 69 |
+
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
| 70 |
+
|
| 71 |
+
# Define generation parameters
|
| 72 |
+
generation_config = GenerationConfig(
|
| 73 |
+
max_new_tokens=256,
|
| 74 |
+
do_sample=False, # Use greedy decoding for reproducible results
|
| 75 |
+
temperature=0.7,
|
| 76 |
+
top_p=0.9,
|
| 77 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 78 |
+
pad_token_id=tokenizer.pad_token_id,
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
with torch.no_grad():
|
| 82 |
+
output_ids = model.generate(model_inputs.input_ids, generation_config=generation_config)
|
| 83 |
+
|
| 84 |
+
# Decode the generated SQL query, skipping special tokens
|
| 85 |
+
generated_sql = tokenizer.decode(output_ids[0][len(model_inputs.input_ids[0]):], skip_special_tokens=True)
|
| 86 |
+
print(f"Generated SQL: {generated_sql}")
|
| 87 |
+
```
|
| 88 |
+
|
| 89 |
+
## Citation
|
| 90 |
+
|
| 91 |
+
If you find our work helpful or inspiring, please feel free to cite it:
|
| 92 |
+
|
| 93 |
+
```bibtex
|
| 94 |
+
@misc{sheng2025cscsqlcorrectiveselfconsistencytexttosql,
|
| 95 |
+
title={CSC-SQL: Corrective Self-Consistency in Text-to-SQL via Reinforcement Learning},
|
| 96 |
+
author={Lei Sheng and Shuai-Shuai Xu},
|
| 97 |
+
year={2025},
|
| 98 |
+
eprint={2505.13271},
|
| 99 |
+
archivePrefix={arXiv},
|
| 100 |
+
primaryClass={cs.CL},
|
| 101 |
+
url={https://arxiv.org/abs/2505.13271},
|
| 102 |
+
}
|
| 103 |
+
```
|