摘要
针对大语言模型(large language models,LLMs)在 Text-to-SQL 任务中生成的查询语句仍存在语义偏差,且现有自纠错方法因过度依赖执行反馈或人工规则而难以定位错误根源的问题,本文提出一种基于推理对比机制的结构化查询语言(structured query language,SQL)生成自纠错框架 SCR-SQL.该框架通过显式对比预测 SQL 与标准 SQL 背后的思维链(chains-of-thought,CoT)差异,构建出可复用的面向纠错的推理知识库.在推理阶段,模型通过检索相关的推理引导信息,实现对预测 SQL 的针对性语义修正.该方法不依赖预定义的错误类别或硬性规则,具有更强的鲁棒性与泛化能力.在 BIRD 与 Spider 基准测试上的实验结果表明,SCR-SQL在上下文学习与监督微调 2 种设置下均能稳定提升执行准确率,其中以 CodeS-15B 为基座模型时,在 BIRD 开发集上取得了60.82%的执行准确率,较基线方法提升了2.35%.
Large language models(LLMs)still suffer from semantic deviations in the SQL queries generated for Text-to-SQL tasks,while existing self-correction methods often struggle to identify the root causes of errors because they rely heavily on execution feedback or handcrafted rules.To alleviate this issue,we propose SCR-SQL,a self-correction framework for SQL generation based on a reasoning comparison mechanism.Specifically,the framework explicitly compares the differences between the chains-of-thought(CoT)underlying the predicted SQL and the gold SQL,thereby constructing a reusable reasoning knowledge base for error correction.During inference,the model retrieves relevant reasoning guidance from this knowledge base to perform targeted semantic correction on the predicted SQL.Since SCR-SQL does not rely on predefined error categories or rigid rules,it exhibits stronger robustness and generalization ability.Experimental results on the BIRD and Spider benchmarks show that SCR-SQL consistently improves execution accuracy under both in-context learning and supervised fine-tuning settings.In particular,when CodeS-15B is used as the backbone model,SCR-SQL achieves an execution accuracy of 60.82%on the BIRD development set,outperforming the baseline by 2.35%.