| | --- |
| | pipeline_tag: text-generation |
| | library_name: transformers |
| | license: apache-2.0 |
| | tags: |
| | - text-to-sql |
| | - sql |
| | - reinforcement-learning |
| | - qwen2 |
| | --- |
| | |
| | # CSC-SQL: Corrective Self-Consistency in Text-to-SQL via Reinforcement Learning |
| |
|
| | 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). |
| |
|
| | **Code Repository**: [https://github.com/CycloneBoy/csc_sql](https://github.com/CycloneBoy/csc_sql) |
| |
|
| | ## Introduction |
| |
|
| | 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%. |
| |
|
| |  |
| |
|
| | ## Main Results |
| |
|
| | Performance Comparison of different Text-to-SQL methods on BIRD dev and test dataset. |
| |  |
| |
|
| | ## Model Checkpoints |
| |
|
| | The models and datasets related to CSC-SQL are available on Hugging Face and ModelScope: |
| |
|
| | | **Model and Dataset** | Modelscope | HuggingFace | |
| | | :------------------------------------ | :---------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------ | |
| | | bird train and dev dataset | [π€ Modelscope](https://modelscope.cn/datasets/cycloneboy/bird_train) | [π€ HuggingFace](https://huggingface.co/datasets/cycloneboy/bird_train) | |
| | | 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) | |
| | | 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) | |
| | | 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) | |
| | | 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) | |
| | | 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) | |
| | | 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) | |
| |
|
| | ## Usage |
| |
|
| | 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). |
| |
|
| | ```python |
| | import torch |
| | from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig |
| | |
| | # The specific model identifier for this repository |
| | model_id = "cycloneboy/CscSQL-Grpo-Qwen2.5-Coder-7B-Instruct" # Replace with the actual model ID if different |
| | |
| | tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True) |
| | model = AutoModelForCausalLM.from_pretrained(model_id, device_map='auto', torch_dtype=torch.bfloat16, trust_remote_code=True) |
| | model.eval() |
| | |
| | # Example: Text-to-SQL inference using the Qwen2 chat template |
| | # For a real-world text-to-SQL task, you would typically need to provide the database schema or |
| | # context relevant to the query as part of the prompt. |
| | question = "What are the names of all employees who work in the 'Sales' department?" |
| | messages = [ |
| | {"role": "system", "content": "You are a helpful assistant trained to convert natural language questions into SQL queries."}, |
| | {"role": "user", "content": f"Translate the following natural language query into SQL: '{question}'"}, |
| | ] |
| | |
| | # Apply the chat template to format the input according to Qwen2's conventions |
| | text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) |
| | model_inputs = tokenizer([text], return_tensors="pt").to(model.device) |
| | |
| | # Define generation parameters |
| | generation_config = GenerationConfig( |
| | max_new_tokens=256, |
| | do_sample=False, # Use greedy decoding for reproducible results |
| | temperature=0.7, |
| | top_p=0.9, |
| | eos_token_id=tokenizer.eos_token_id, |
| | pad_token_id=tokenizer.pad_token_id, |
| | ) |
| | |
| | with torch.no_grad(): |
| | output_ids = model.generate(model_inputs.input_ids, generation_config=generation_config) |
| | |
| | # Decode the generated SQL query, skipping special tokens |
| | generated_sql = tokenizer.decode(output_ids[0][len(model_inputs.input_ids[0]):], skip_special_tokens=True) |
| | print(f"Generated SQL: {generated_sql}") |
| | ``` |
| |
|
| | ## Citation |
| |
|
| | If you find our work helpful or inspiring, please feel free to cite it: |
| |
|
| | ```bibtex |
| | @misc{sheng2025cscsqlcorrectiveselfconsistencytexttosql, |
| | title={CSC-SQL: Corrective Self-Consistency in Text-to-SQL via Reinforcement Learning}, |
| | author={Lei Sheng and Shuai-Shuai Xu}, |
| | year={2025}, |
| | eprint={2505.13271}, |
| | archivePrefix={arXiv}, |
| | primaryClass={cs.CL}, |
| | url={https://arxiv.org/abs/2505.13271}, |
| | } |
| | ``` |