Skip to content

πŸ”₯[ICML'25] Official repository for the paper "Alpha-SQL: Zero-Shot Text-to-SQL using Monte Carlo Tree Search"

License

Notifications You must be signed in to change notification settings

HKUSTDial/Alpha-SQL

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

10 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ Alpha-SQL: Zero-Shot Text-to-SQL using Monte Carlo Tree Search

Homepage ICML 2025 arXiv Slides Python License

✨ If you find our work helpful, please don't hesitate to give us a star ⭐ !

Introduction Figure

πŸ“– Introduction

Text-to-SQL, which enables natural language interaction with databases, serves as a pivotal method across diverse industries. With new, more powerful large language models (LLMs) emerging every few months, fine-tuning has become incredibly costly, labor-intensive, and error-prone. As an alternative, zero-shot Text-to-SQL, which leverages the growing knowledge and reasoning capabilities encoded in LLMs without task-specific fine-tuning, presents a promising and more challenging direction.

To address this challenge, we propose Alpha-SQL, a novel approach that leverages a Monte Carlo Tree Search (MCTS) framework to iteratively infer SQL construction actions based on partial SQL query states. To enhance the framework's reasoning capabilities, we introduce LLM-as-Action-Model to dynamically generate SQL construction actions during the MCTS process, steering the search toward more promising SQL queries. Moreover, Alpha-SQL employs a self-supervised reward function to evaluate the quality of candidate SQL queries, ensuring more accurate and efficient query generation.

Overview Figure

πŸ“ Project Structure

AlphaSQL/
β”œβ”€β”€ πŸ“‚ data/
β”‚   └── πŸ“‚ bird/
β”‚       └── πŸ“‚ dev/
β”‚           β”œβ”€β”€ πŸ“„ dev.json
β”‚           └── πŸ“‚ dev_databases/
β”œβ”€β”€ πŸ“‚ config/
β”‚   β”œβ”€β”€ πŸ“„ qwen7b_sds_exp.yaml
β”‚   └── πŸ“„ qwen32b_bird_dev.yaml
β”œβ”€β”€ πŸ“‚ results/
β”‚   └── πŸ“„ dev_pred_sqls.json
β”œβ”€β”€ πŸ“‚ script/
β”‚   β”œβ”€β”€ πŸ“„ preprocess.sh
β”‚   β”œβ”€β”€ πŸ“„ qwen32b_bird_dev_exp.sh
β”‚   β”œβ”€β”€ πŸ“„ qwen7b_sds_exp.sh
β”‚   └── πŸ“„ sql_selection.sh
β”œβ”€β”€ πŸ“‚ alphasql/
β”‚   β”œβ”€β”€ πŸ“‚ runner/
β”‚   β”‚   β”œβ”€β”€ πŸ“„ preprocessor.py
β”‚   β”‚   β”œβ”€β”€ πŸ“„ sql_selection.py
β”‚   β”‚   β”œβ”€β”€ πŸ“„ mcts_runner.py
β”‚   β”‚   β”œβ”€β”€ πŸ“„ selection_runner.py
β”‚   β”‚   └── πŸ“„ task.py
β”‚   β”œβ”€β”€ πŸ“‚ templates/
β”‚   β”‚   β”œβ”€β”€ πŸ“„ schema_selection.txt
β”‚   β”‚   β”œβ”€β”€ πŸ“„ sql_revision.txt
β”‚   β”‚   β”œβ”€β”€ πŸ“„ sql_generation.txt
β”‚   β”‚   β”œβ”€β”€ πŸ“„ raphrase_question.txt
β”‚   β”‚   β”œβ”€β”€ πŸ“„ identify_column_functions.txt
β”‚   β”‚   β”œβ”€β”€ πŸ“„ identify_column_values.txt
β”‚   β”‚   └── πŸ“„ keywords_extraction.txt
β”‚   β”œβ”€β”€ πŸ“‚ config/
β”‚   β”‚   └── πŸ“„ mcts_config.py
β”‚   β”œβ”€β”€ πŸ“‚ database/
β”‚   β”‚   β”œβ”€β”€ πŸ“„ sql_execution.py
β”‚   β”‚   β”œβ”€β”€ πŸ“„ utils.py
β”‚   β”‚   β”œβ”€β”€ πŸ“„ sql_parse.py
β”‚   β”‚   β”œβ”€β”€ πŸ“„ schema.py
β”‚   β”‚   β”œβ”€β”€ πŸ“„ database_manager.py
β”‚   β”‚   └── πŸ“„ lsh_index.py
β”‚   β”œβ”€β”€ πŸ“‚ llm_call/
β”‚   β”‚   β”œβ”€β”€ πŸ“„ cost_recoder.py
β”‚   β”‚   β”œβ”€β”€ πŸ“„ openai_llm.py
β”‚   β”‚   └── πŸ“„ prompt_factory.py
β”‚   └── πŸ“‚ algorithm/
β”‚       β”œβ”€β”€ πŸ“‚ selection/
β”‚       β”‚   └── πŸ“„ utils.py
β”‚       └── πŸ“‚ mcts/
β”‚           β”œβ”€β”€ πŸ“„ mcts_node.py
β”‚           β”œβ”€β”€ πŸ“„ mcts_action.py
β”‚           β”œβ”€β”€ πŸ“„ mcts.py
β”‚           └── πŸ“„ reward.py
β”œβ”€β”€ πŸ“„ README.md
β”œβ”€β”€ πŸ“„ requirements.txt
└── πŸ“„ .env

πŸ“₯ Dataset Preparation

  1. Download required resources:

  2. Unzip the dataset to data/bird directoty following the project structure above.

πŸ› οΈ Environment Setup

  1. AlphaSQL Env

    conda create -n alphasql python=3.11
    conda activate alphasql
    
    pip install -r requirements.txt
  2. VLLM Env

    conda create -n vllm python=3.12 -y
    conda activate vllm
    
    git clone https://github.com/vllm-project/vllm.git
    cd vllm
    pip install -e .

πŸš€ Deploy Local LLM Using VLLM

conda activate vllm

# For 4 GPUs
CUDA_VISIBLE_DEVICES=0,1,2,3 vllm serve Qwen/Qwen2.5-Coder-32B-Instruct --served-model-name Qwen/Qwen2.5-Coder-32B-Instruct --port 9999 -tp 4

# For 8 GPUs
CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 vllm serve Qwen/Qwen2.5-Coder-32B-Instruct --served-model-name Qwen/Qwen2.5-Coder-32B-Instruct --port 9999 -tp 8

πŸƒβ€β™‚οΈRun AlphaSQL

1. Switch AlphaSQL Conda Env

conda activate alphasql

2. Dataset Preprocessing

  1. Modify OPENAI_API_KEY and OPENAI_BASE_URL in .env file (we need to access text-embedding-3-large model of OpenAI in preprocessing stage)
    OPENAI_API_KEY = "your-api-key"
    OPENAI_BASE_URL = "your-custom-endopoint" # If you use non-OPENAI services
    
  2. Run the following:
    bash script/preprocess.sh

3. Generate SQL Candidates

  1. Modify OPENAI_API_KEY and OPENAI_BASE_URL in .env file (we need to access Qwen/Qwen2.5-Coder-32B-Instruct model of VLLM delopyment)

    OPENAI_API_KEY="EMPTY"
    OPENAI_BASE_URL="http://0.0.0.0:9999/v1"
  2. Run the following:

    bash script/qwen32b_bird_dev_exp.sh

4. Select Final SQL

  1. Run the following:

    bash script/sql_selection.sh
  2. The final pred_sqls.json will in the project root dir (defined in script/sql_selection.sh OUTPUT_PATH variable)

πŸ“ Citation

If you find our work useful or inspiring, please kindly cite:

@inproceedings{alpha-sql,
  author       = {Boyan Li and
                  Jiayi Zhang and
                  Ju Fan and
                  Yanwei Xu and
                  Chong Chen and
                  Nan Tang and
                  Yuyu Luo},
  title        = {Alpha-SQL: Zero-Shot Text-to-SQL using Monte Carlo Tree Search},
  booktitle    = {Forty-Second International Conference on Machine Learning, {ICML} 2025,
                  Vancouver, Canada, July 13-19, 2025},
  publisher    = {OpenReview.net},
  year         = {2025}
}

About

πŸ”₯[ICML'25] Official repository for the paper "Alpha-SQL: Zero-Shot Text-to-SQL using Monte Carlo Tree Search"

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published