Large Language Models have recently shown impressive capabilities in reasoning and code generation, making them promising tools for natural language interfaces to relational databases. However, existing approaches often fail to generalize in complex, real-world settings due to the highly database-specific nature of SQL reasoning, which requires deep familiarity with unique schemas, ambiguous semantics, and intricate join paths. To address this challenge, we introduce a novel two-stage LLM-based framework that decouples knowledge acquisition from query generation. In the Exploration Stage, the system autonomously constructs a database-specific knowledge base by navigating the schema with a Monte Carlo Tree Search-inspired strategy, generating triplets of schema fragments, executable queries, and natural language descriptions as usage examples. In the Deployment Stage, a dual-agent system leverages the collected knowledge as in-context examples to iteratively retrieve relevant information and generate accurate SQL queries in response to user questions. This design enables the agent to proactively familiarize itself with unseen databases and handle complex, multi-step reasoning. Extensive experiments on large-scale benchmarks demonstrate that our approach significantly improves accuracy over strong baselines, highlighting its effectiveness and generalizability.
翻译:大型语言模型近期在推理与代码生成方面展现出卓越能力,使其成为关系型数据库自然语言接口的有力工具。然而,现有方法在复杂的真实场景中往往难以泛化,这源于SQL推理高度依赖数据库特性的本质——需要对独特的模式结构、模糊的语义表达以及复杂的连接路径有深入理解。为解决这一挑战,我们提出一种新颖的两阶段基于大语言模型的框架,将知识获取与查询生成解耦。在探索阶段,系统通过受蒙特卡洛树搜索启发的策略自主导航数据库模式,构建特定于数据库的知识库,生成包含模式片段、可执行查询及自然语言描述的三元组作为使用示例。在部署阶段,双智能体系统利用已收集的知识作为上下文示例,迭代检索相关信息并根据用户问题生成准确的SQL查询。这一设计使智能体能够主动熟悉未见过的数据库,并处理复杂的多步推理问题。在大规模基准测试上的广泛实验表明,我们的方法相较于强基线模型显著提升了准确率,凸显了其有效性与泛化能力。