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/birddirectoty 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 8conda activate alphasql-
Configure your
.envfile based onenv.example:# Required: OpenAI API Configuration (for LLM) OPENAI_API_KEY=your-api-key OPENAI_BASE_URL=https://api.openai.com/v1 # Or your custom endpoint # Required: Embedding Model Configuration EMBEDDING_MODEL=text-embedding-3-large # Or text-embedding-3-small, text-embedding-ada-002 # Optional: Separate embedding service (if using different base URL or API key) # EMBEDDING_API_KEY=your-embedding-api-key # EMBEDDING_BASE_URL=https://api.openai.com/v1 # or http://localhost:8080/v1
Important Note:
- If your embedding model uses a different base URL than your LLM, configure
EMBEDDING_BASE_URLandEMBEDDING_API_KEYseparately - If not set, embedding will automatically use
OPENAI_BASE_URLandOPENAI_API_KEY - Example: LLM uses local VLLM (
http://localhost:9999/v1), embedding uses OpenAI API (https://api.openai.com/v1) - See
EMBEDDING_CONFIG.mdfor detailed configuration examples
- If your embedding model uses a different base URL than your LLM, configure
-
Run the following:
bash script/preprocess.sh
-
Modify
OPENAI_API_KEYandOPENAI_BASE_URLin.envfile (we need to accessQwen/Qwen2.5-Coder-32B-Instructmodel 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.jsonwill in the project root dir (defined inscript/sql_selection.shOUTPUT_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}
}
