Retrieval-augmented GPT-3.5-based Text-to-SQL Framework with Sample-aware Prompting and Dynamic Revision Chain

Chunxi Guo, Zhiliang Tian, Jintao Tang, Shasha Li, Zhihua Wen, Kaixuan Wang, Ting Wang

TL;DR

This paper presents a novel retrieval-augmented framework that enhances Large Language Model (LLM) performance on Text-to-SQL tasks. By combining sample-aware prompting—which uses simplified "question skeletons" to find relevant examples—with a dynamic revision chain that iteratively corrects SQL based on database feedback, the authors achieve state-of-the-art results on challenging benchmarks like Spider-DK.

What Problem Does This Paper Address?

Converting natural language questions (NLQ) into structured SQL queries is a cornerstone of making databases accessible to non-experts. While LLMs like GPT-3.5 have shown promise in this area, they face two major hurdles:

  1. Strict Syntax Requirements: SQL is unforgiving; a single misplaced comma or incorrect table name renders a query useless.
  2. Retrieval Bias: Current few-shot prompting methods often retrieve demonstration examples that are semantically distant from the user's actual intent. If the "hints" in the prompt don't match the structure of the problem, the LLM is likely to hallucinate an incorrect query.

The authors aim to bridge this "semantic gap" and provide a mechanism for the model to self-correct without human intervention.

Methods

The framework consists of two main modules designed to move beyond static, one-shot prompting:

1. Retrieval Repository & Sample-aware Prompting

Instead of using raw questions to find similar examples, the system processes the training data into a specialized repository:

  • Simplification: An LLM unifies various questioning styles into a clear, syntactically standard format.
  • Skeleton Extraction: The system strips away specific schema details (like table names) to focus solely on the query's logical structure and intent.
  • Adaptive Retrieval: When a new question arrives, the system extracts its skeleton and retrieves the most similar (Skeleton, SQL) pairs from the repository to build a custom, context-aware prompt.

2. Dynamic Revision Chain

Rather than accepting the first SQL query the LLM generates, the framework treats generation as an iterative dialogue between the LLM and the database engine.

Fig. 1. Framework overview: The left half shows retrieval repository construction in three steps. The top three sentences are three specific instances each. The Green dashed box presents the training set. The right half is a dynamic revision chain with SQL queries generated by LLM iterations as nodes (green boxes). The output of steps 2 and 4 are collectively referred to as fine-grained feedback.

As shown in Figure 1, the "Dynamic Revision Chain" uses three types of fine-grained feedback to refine the query:

  • Execution Error Feedback: If the SQL fails, the database engine (SQLite) returns specific error messages (e.g., "no such column") which are fed back into the next prompt.
  • Natural Language Explanation: The LLM is asked to explain its generated SQL in plain English and compare it to the original question to spot logical mismatches.
  • Related Database Contents: The prompt is enriched with actual values from the database to help the LLM understand the data distribution.

Datasets

The authors evaluated their framework on three major cross-domain Text-to-SQL benchmarks:

  • Spider: The standard large-scale benchmark covering 138 different domains.
  • Spider-Syn: A variant where questions use synonyms for schema terms, testing the model's robustness to linguistic variation.
  • Spider-DK: A challenging variant that incorporates external domain knowledge, designed to trip up models that rely too heavily on simple pattern matching.

Key Insights and Ideas

  • Skeletons over Sentences: Retrieving examples based on "question skeletons" is far more effective than using raw natural language. It forces the retriever to focus on the logic of the query (e.g., "Find the maximum of X grouped by Y") rather than just keyword overlap.
  • The Power of Iteration: Most of the performance gains occur in the first two rounds of the revision chain. This suggests that LLMs are highly capable of "self-debugging" if given the right environmental feedback (like compiler errors).
  • Complexity Scaling: The framework's superiority becomes more pronounced as the SQL queries get harder. On "Extra Hard" queries in the Spider dataset, this method significantly outperformed standard few-shot prompting.

Key Results

The framework, built on text-davinci-003 (GPT-3.5), delivered impressive performance across the board:

  • Spider: Achieved 85.0% Execution Accuracy (EX) and 83.2% Test-suite Accuracy (TS), outperforming both fine-tuned models like RESDSQL and prompting baselines like DIN-SQL.
  • Spider-DK: Showed a massive 13.6% improvement over previous state-of-the-art models, proving its effectiveness in handling domain-specific nuances.
  • Spider-Syn: Achieved 81.4% EX, a 4.5% lead over the next best model, demonstrating robust generalization.

Limitations and Future Work

While the paper demonstrates strong results, a few areas remain for exploration:

  • LLM Choice: The study primarily uses GPT-3.5. While it mentions GPT-4 in baselines, the core experiments focus on the slightly older text-davinci-003. Testing the full framework on GPT-4 or specialized open-source code models could yield even higher gains.
  • Computational Cost: Iterative revision chains require multiple calls to the LLM, which increases latency and API costs compared to one-shot methods.
  • Human-in-the-loop: The authors emphasize a zero-human-intervention approach, but future work could investigate how minimal human feedback might further stabilize the revision chain for mission-critical enterprise databases.

Summary updated

BibTeX
@inproceedings{guo2023retrieval,
  author = {Chunxi Guo and Zhiliang Tian and Jintao Tang and Shasha Li and Zhihua Wen and Kaixuan Wang and Ting Wang},
  title = {{Retrieval-augmented GPT-3.5-based Text-to-SQL Framework with Sample-aware Prompting and Dynamic Revision Chain}},
  booktitle = {ICONIP},
  year = {2023},
  url = {https://openreview.net/forum?id=WP3rkVxPDH},
}
Figure