Large language models (LLMs) have revolutionized Text-to-SQL generation, allowing users to query structured data using natural language with growing ease. Yet, real-world deployment remains challenging, especially in complex or unseen schemas, due to inconsistent accuracy and the risk of generating invalid SQL. We introduce Template Constrained Decoding (TeCoD), a system that addresses these limitations by harnessing the recurrence of query patterns in labeled workloads. TeCoD converts historical NL-SQL pairs into reusable templates and introduces a robust template selection module that uses a fine-tuned natural language inference model to match or reject queries efficiently. Once the template is selected, TeCoD enforces it during SQL generation through grammar-constrained decoding, implemented via a novel partitioned strategy that ensures both syntactic validity and efficiency. Together, these components yield up to 36% higher execution accuracy than in-context learning (ICL) and 2.2x lower latency on matched queries.
翻译:大型语言模型(LLMs)革新了Text-to-SQL生成技术,使用户能够以日益便捷的方式通过自然语言查询结构化数据。然而,在实际部署中,尤其在复杂或未见过的模式(schema)中,由于准确性不一致以及生成无效SQL的风险,仍面临挑战。我们提出模板约束解码(TeCoD)系统,通过利用标注工作负载中查询模式的重复性来应对这些局限。TeCoD将历史自然语言-SQL对转化为可复用模板,并引入一个鲁棒的模板选择模块——该模块使用微调后的自然语言推理模型来高效匹配或拒绝查询。选定模板后,TeCoD通过语法约束解码在SQL生成过程中强制实施该模板,该解码过程采用一种新颖的分区策略实现,确保语法有效性和高效性。这些组件共同在匹配查询上实现了相比上下文学习(ICL)最高36%的执行准确率提升,以及2.2倍的延迟降低。