Abstract
While In-Context Learning (ICL) has proven to be an effective technique to improve the performance of Large Language Models (LLMs) in translating natural language questions into Structured Query Language (NL2SQL), selecting the most beneficial demonstration examples remains a challenge. Prior works often use off-the-shelf encoders, leading to a representational discrepancy between retrievers and LLMs. We propose DeTriever, a novel demonstration retrieval framework that learns a weighted combination of LLM hidden states. By using a proxy score based on output query similarities, DeTriever aligns the retriever's embedding space with the LLM's generative logic. Experiments on Spider and BIRD benchmarks show that DeTriever significantly outperforms state-of-the-art baselines.
Introduction
Translating natural language into SQL (NL2SQL) is a complex task requiring deep understanding of both linguistic nuances and database structures. Large Language Models (LLMs) have shown promise through In-Context Learning (ICL), where few-shot examples guide the generation process. However, the effectiveness of ICL is highly sensitive to the choice of demonstration examples.
Existing methods typically rely on external embedding models (e.g., Sentence-BERT) to retrieve similar examples. This approach suffers from two main issues: a "representational gap" where the retriever and generator operate in different latent spaces, and the difficulty of evaluating example quality without exhaustive pairwise inference.
Methodology: The DeTriever Framework
DeTriever addresses these limitations by utilizing the LLM's internal representations for retrieval.
- Decoder-Based Representations: Instead of external models, DeTriever extracts hidden states directly from the LLM's decoder layers.
- Weighted Layer Combination: Since different layers capture varying levels of semantic and structural information, DeTriever learns a weighted combination of these states via a 3-layer Multi-Layer Perceptron (MLP).
- Proxy-Based Training: To optimize the retriever efficiently, we introduce a proxy score that measures the similarity between the target SQL queries of the examples. This ensures the retriever prioritizes examples that are logically and structurally relevant to the desired output.
Experimental Evaluation
We evaluated DeTriever on two prominent NL2SQL benchmarks:
- Spider: A cross-domain dataset featuring complex queries across 200 databases.
- BIRD: A large-scale benchmark focusing on real-world database complexity and massive data volumes.
Our experiments focused on Out-of-Domain (OOD) scenarios, where demonstration databases differ from the target, testing the model's generalization capabilities.
Key Insights
Our analysis revealed several critical findings:
- Optimal Layer Selection: Contrary to common intuition, the final layer of the LLM is not the most effective for retrieval. Middle layers (e.g., layers 10-20 in CodeLlama-13b) provide richer semantic representations for identifying relevant demonstrations.
- Internal Alignment: Using the LLM's own hidden states consistently outperforms high-end external embeddings like OpenAI's. This underscores the importance of alignment between the retrieval and generation components.
- Efficiency: DeTriever is highly parameter-efficient, with learnable components representing only 0.084% of the total model size.
Results
DeTriever achieves state-of-the-art performance across both benchmarks, significantly reducing error rates and improving execution accuracy.
On the Spider dataset, DeTriever reached an execution accuracy of 68.38%, an 11.58-point improvement over random 1-shot selection. On BIRD, it outperformed the best LLM-representation baseline, demonstrating its robustness on large-scale, complex tasks. On Spider, DeTriever reduced the error rate from 16.3% (zero-shot) down to 9.77%.
Conclusion and Future Work
DeTriever demonstrates that bridging the representational gap between retrievers and generators is key to maximizing ICL performance in NL2SQL. Future work will explore the application of DeTriever to a broader range of open-ended generation tasks and investigate more advanced metric learning techniques to further refine example selection.
Summary updated