ACT-SQL: In-Context Learning for Text-to-SQL with Automatically-Generated Chain-of-Thought

Hanchong Zhang, Ruisheng Cao, Lu Chen, Hongshen Xu, Kai Yu

Abstract

Translating natural language into SQL (Text-to-SQL) requires complex reasoning and deep schema understanding. While Large Language Models (LLMs) excel at this via In-Context Learning (ICL), the effectiveness of Chain-of-Thought (CoT) prompting is often hindered by the high cost of manual labeling. This paper presents ACT-SQL, a framework that automates CoT generation by mimicking human schema-linking processes. By combining this with a hybrid exemplar selection strategy and optimized database prompt styling, ACT-SQL achieves state-of-the-art performance on the Spider benchmark, reaching 74.5% Test-Suite accuracy with GPT-4 while requiring only a single API call per query.

Introduction

The Text-to-SQL task is a cornerstone of natural language interfaces to databases. Traditional approaches rely on fine-tuning specialized models, which, while effective, are often domain-specific and expensive to train. LLMs offer a more generalizable alternative through ICL. However, standard few-shot prompting frequently fails to capture the intricate reasoning required for complex queries. Chain-of-Thought (CoT) prompting has emerged as a solution to activate LLM reasoning, but its reliance on manually crafted examples limits scalability. ACT-SQL addresses this by automating the "thinking" process, specifically targeting the schema-linking phase essential for SQL generation.

Methodology

ACT-SQL introduces several key innovations to streamline the Text-to-SQL pipeline:

Automatic CoT Generation (Auto-CoT)

The core of ACT-SQL is the automated generation of reasoning steps. Instead of manual annotation, the system identifies links between natural language snippets and database components (tables, columns, values) using similarity scores from pretrained models. These links are then formatted into a step-by-step reasoning prompt that guides the LLM through the logic of the query.

Example of the Auto-CoT process identifying schema links and values to guide the LLM.

Hybrid Exemplar Selection and Prompt Styling

To provide optimal context, ACT-SQL employs a hybrid selection strategy, combining static exemplars (randomly chosen) and dynamic exemplars (chosen based on similarity to the user question). Furthermore, the researchers found that including a small number of actual database rows (e.g., 3 rows) in the prompt significantly aids the model in disambiguating values and understanding data types, leading to more accurate structural predictions.

Multi-turn Handling

For conversational contexts, ACT-SQL utilizes a "rewrite" stage where context-dependent questions are transformed into independent, single-turn queries before being processed by the main generation pipeline.

Results and Discussion

Evaluation on the Spider benchmark demonstrates that ACT-SQL sets a new standard for ICL-based Text-to-SQL methods.

Performance comparison of ACT-SQL against other LLM-based approaches and fine-tuned models on the Spider benchmark.

As shown in the results, ACT-SQL with GPT-4 achieves an Execution (EX) accuracy of 82.9%, outperforming previous SOTA ICL methods like DIN-SQL. Notably, ACT-SQL achieves these results using only a single API call per SQL query, whereas DIN-SQL requires four, representing a significant improvement in both latency and cost-efficiency.

The framework also exhibits strong robustness. On the Spider-DK (Domain Knowledge) variant, ACT-SQL's performance surpassed several highly-optimized fine-tuned models, highlighting the advantage of leveraging the vast internal knowledge of LLMs for cross-domain tasks.

Conclusion

ACT-SQL demonstrates that automating the reasoning process for Text-to-SQL is not only feasible but highly effective. By mimicking schema linking and optimizing prompt content, it bridges the gap between general-purpose LLMs and specialized fine-tuned models. Future work will focus on improving the multi-turn rewriting bottleneck and exploring the framework's performance on smaller, open-source language models.

Summary updated

BibTeX
@inproceedings{zhang2023act,
  author = {Hanchong Zhang and Ruisheng Cao and Lu Chen and Hongshen Xu and Kai Yu},
  title = {{ACT-SQL: In-Context Learning for Text-to-SQL with Automatically-Generated Chain-of-Thought}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2023},
  doi = {10.18653/v1/2023.findings-emnlp.227},
}
Figure