We present SQL Query Engine, an open-source, self-hosted service that translates natural language questions into validated PostgreSQL queries through a two-stage LLM pipeline. The first stage performs automatic schema introspection and SQL generation; a multi-strategy response parser extracts SQL from any LLM output format (JSON, code blocks, or raw text) without requiring structured output APIs. The second stage executes the query against PostgreSQL and, upon failure or empty results, enters an iterative self-healing loop in which the LLM diagnoses the error using full SQLSTATE codes and PostgreSQL diagnostic messages. Two mechanisms prevent regressions: early-accept returns successful queries immediately without LLM re-evaluation, and best-result tracking preserves the best partial result across retries. Schema context is cached per session in Redis, progress events stream via Redis Pub/Sub and SSE, and an OpenAI-compatible /v1/chat/completions endpoint lets existing tools work without modification. All database connections are read-only at the driver level. We evaluate across five LLM backends on a synthetic benchmark (75 questions, three databases) where the self-healing loop yields up to +9.3pp accuracy gains with zero regressions on the best model (Llama 4 Scout 17B, 57.3%), and on BIRD (437 questions, 11 databases migrated from SQLite to PostgreSQL) where the full pipeline reaches 49.0% execution accuracy (GPT-OSS-120B, +4.6pp). Source code: https://github.com/codeadeel/sqlqueryengine.


翻译:我们提出SQL Query Engine,一个开源、自托管服务,通过两阶段LLM流水线将自然语言问题转化为经过验证的PostgreSQL查询。第一阶段执行自动模式自省与SQL生成;多策略响应解析器能从任意LLM输出格式(JSON、代码块或纯文本)中提取SQL,无需结构化输出API。第二阶段对PostgreSQL执行查询,在遇到失败或空结果时进入迭代自愈循环,该循环中LLM利用完整的SQLSTATE码和PostgreSQL诊断消息诊断错误。两种机制防止性能退化:早期接受机制无需LLM重新评估即直接返回成功查询,最佳结果追踪机制在重试过程中保留最佳部分结果。模式上下文按会话缓存在Redis中,进度事件通过Redis Pub/Sub和SSE流式传输,兼容OpenAI的/v1/chat/completions端点使现有工具无需修改即可运行。所有数据库连接在驱动层均为只读。我们在五个LLM后端上通过合成基准测试(75个问题、三个数据库)进行评估,自愈循环使最佳模型(Llama 4 Scout 17B,57.3%)获得高达+9.3个百分点的准确率提升且零退化;同时BIRD数据集(437个问题、11个从SQLite迁移至PostgreSQL的数据库)上,完整流水线达到49.0%的执行准确率(GPT-OSS-120B,+4.6个百分点)。源代码:https://github.com/codeadeel/sqlqueryengine。

0
下载
关闭预览

相关内容

PostgreSQL 是自由的对象-关系数据库服务器(数据库管理系统),在灵活的 BSD 风格许可证下发行。
大型语言模型对齐技术综述:RLHF、RLAIF、PPO、DPO 等
专知会员服务
55+阅读 · 2024年7月24日
专知会员服务
33+阅读 · 2021年9月27日
基于MySQL Binlog的Elasticsearch数据同步实践
DBAplus社群
15+阅读 · 2019年9月3日
中文自然语言处理数据集:ChineseNLPCorpus
AINLP
35+阅读 · 2019年6月21日
工行基于MySQL构建分布式架构的转型之路
炼数成金订阅号
15+阅读 · 2019年5月16日
NLP-Progress记录NLP最新数据集、论文和代码: 助你紧跟NLP前沿
中国人工智能学会
12+阅读 · 2018年11月15日
NLG ≠ 机器写作 | 专家专栏
量子位
13+阅读 · 2018年9月10日
TextInfoExp:自然语言处理相关实验(基于sougou数据集)
全球人工智能
12+阅读 · 2017年11月12日
国家自然科学基金
18+阅读 · 2017年12月31日
国家自然科学基金
7+阅读 · 2015年12月31日
国家自然科学基金
3+阅读 · 2015年12月31日
国家自然科学基金
5+阅读 · 2015年12月31日
国家自然科学基金
1+阅读 · 2015年12月31日
国家自然科学基金
3+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
2+阅读 · 2014年12月31日
VIP会员
最新内容
《通过小型无人机系统将情报能力“作战化”》
消耗优势:美军的“精确规模化”概念
专知会员服务
8+阅读 · 6月15日
《离线语言支持系统:面向空战战术决策》
专知会员服务
10+阅读 · 6月15日
相关资讯
基于MySQL Binlog的Elasticsearch数据同步实践
DBAplus社群
15+阅读 · 2019年9月3日
中文自然语言处理数据集:ChineseNLPCorpus
AINLP
35+阅读 · 2019年6月21日
工行基于MySQL构建分布式架构的转型之路
炼数成金订阅号
15+阅读 · 2019年5月16日
NLP-Progress记录NLP最新数据集、论文和代码: 助你紧跟NLP前沿
中国人工智能学会
12+阅读 · 2018年11月15日
NLG ≠ 机器写作 | 专家专栏
量子位
13+阅读 · 2018年9月10日
TextInfoExp:自然语言处理相关实验(基于sougou数据集)
全球人工智能
12+阅读 · 2017年11月12日
相关基金
国家自然科学基金
18+阅读 · 2017年12月31日
国家自然科学基金
7+阅读 · 2015年12月31日
国家自然科学基金
3+阅读 · 2015年12月31日
国家自然科学基金
5+阅读 · 2015年12月31日
国家自然科学基金
1+阅读 · 2015年12月31日
国家自然科学基金
3+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
2+阅读 · 2014年12月31日
Top
微信扫码咨询专知VIP会员