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.
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
-
Download required resources:
- Bird dataset: Bird Official Website
-
Unzip the dataset to
data/bird
directoty following the project structure above.
-
AlphaSQL Env
conda create -n alphasql python=3.11 conda activate alphasql pip install -r requirements.txt
-
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 .
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
conda activate alphasql
- Modify
OPENAI_API_KEY
andOPENAI_BASE_URL
in.env
file (we need to accesstext-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
- Run the following:
bash script/preprocess.sh
-
Modify
OPENAI_API_KEY
andOPENAI_BASE_URL
in.env
file (we need to accessQwen/Qwen2.5-Coder-32B-Instruct
model of VLLM delopyment)OPENAI_API_KEY="EMPTY" OPENAI_BASE_URL="http://0.0.0.0:9999/v1"
-
Run the following:
bash script/qwen32b_bird_dev_exp.sh
-
Run the following:
bash script/sql_selection.sh
-
The final
pred_sqls.json
will in the project root dir (defined inscript/sql_selection.sh
OUTPUT_PATH variable)
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}
}