nl2sql技术实现自动sql生成之Spring AI Alibaba Nl2sql

一、前言

上一节我们在nl2sql技术实现自动sql生成介绍了自然语言转sql的基本思路,今天我们通过实操来解决这一技术实现,通过Spring AI Alibaba开源的nl2sql框架进行这块技术的讲解。

二、环境要求

jdk:17+

spring boot:3.4.0以上

spring ai:1.0.0

spring ai alibaba:1.0.0.3

mysql:5.7+

三、技术实现

1、maven依赖

复制代码
<dependency>
   <groupId>com.alibaba.cloud.ai</groupId>
   <artifactId>spring-ai-alibaba-starter-nl2sql</artifactId>
   <exclusions>
      <exclusion>
         <groupId>com.alibaba.cloud.ai</groupId>
         <artifactId>spring-ai-alibaba-starter-dashscope</artifactId>
      </exclusion>
   </exclusions>
</dependency>
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
   <groupId>org.springframework.ai</groupId>
   <artifactId>spring-ai-advisors-vector-store</artifactId>
</dependency>

上面依赖引入了alibaba的nl2sql、spring boot webflux和向量数据库的组件

2、application配置

配置Mysql数据源、chatbi数据源和openai使用模型和向量库,LLM模型、文本嵌入模型以及向量库使用的都是阿里提供的模型,可根据实际情况进行调整,比如使用其他开源的模型或者向量库。

复制代码
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/orderdb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8
    driverClassName: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
  ai:
    openai:
      base-url: https://dashscope.aliyuncs.com/compatible-mode
      api-key: your-key
      model: qwen3-max
      embedding:
        model: text-embedding-v4
    vectorstore:
      analytic:
        enabled: false
        collectName: chatBi
        regionId: cn-hangzhou
        dbInstanceId: ''
        managerAccount: ''
        managerAccountPassword: ''
        namespace: chat
        namespacePassword: chat
        defaultTopK: 6
        defaultSimilarityThreshold: 0.75
        accessKeyId: ''
        accessKeySecret: ''
chatBi:
  dbConfig:
    url: jdbc:mysql://127.0.0.1:3306/orderdb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8
    username: root
    password: 123456
    schema: orderdb
    connection-type: jdbc
    dialect-type: mysql

3、测试验证

写一个Nl2sql的接口类,设置chatbi的数据源、表信息,将以上信息通过向量库服务通过文本嵌入模型存储到向量库中,便于生成sql时进行检索。代码如下:

java 复制代码
import com.alibaba.cloud.ai.connector.config.DbConfig;
import com.alibaba.cloud.ai.request.SchemaInitRequest;
import com.alibaba.cloud.ai.service.simple.SimpleNl2SqlService;
import com.alibaba.cloud.ai.service.simple.SimpleVectorStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

@RestController
public class SimpleChatController {


    @Autowired
    private SimpleNl2SqlService simpleNl2SqlService;

    @Autowired
    private SimpleVectorStoreService simpleVectorStoreService;

    @Autowired
    private DbConfig dbConfig;

    @PostMapping("/simpleChat")
    public String simpleNl2Sql(@RequestBody String input) throws Exception {
        SchemaInitRequest schemaInitRequest = new SchemaInitRequest();
        schemaInitRequest.setDbConfig(dbConfig);
        schemaInitRequest.setTables(Arrays.asList("orders"));
        simpleVectorStoreService.schema(schemaInitRequest);
        return simpleNl2SqlService.nl2sql(input);
    }
}

启动类配置下alibaba注入的package:

java 复制代码
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication(scanBasePackages = {"com.alibaba.cloud.ai", "com.llm.ai.spring.demo.nl2sql"})
public class Nl2SqlApplication {
	public static void main(String[] args) {
		new SpringApplicationBuilder(Nl2SqlApplication.class).run(args);
	}
}

在postman上发起请求,请求如下:

通过如上发起的请求,可以看到nl2sql根据用户的userMessage自动生成了统计的sql,具体的工作原理如上一篇的介绍的一样,下一篇我将通过spring ai框架实现自定义开发的nl2sql组件,敬请关注,今天就介绍到这里。

相关推荐
InfiSight智睿视界21 小时前
连锁店管理力不从心?让智能体接管30%重复工作
人工智能·智能巡检系统·ai巡检
围炉聊科技21 小时前
国内AI智能眼镜开放平台全景解析:从SDK到生态建设
人工智能
golang学习记21 小时前
Claude Code之父首次揭秘:13个CC独门AI编程使用技巧!
人工智能
狗狗学不会21 小时前
视觉检测的新范式:从“像素感知”到“时序语义推理”—— 基于 Qwen3-VL 与时序拼图策略的通用事件检测系统
人工智能·计算机视觉·视觉检测
zhglhy21 小时前
Spring Data Slice使用指南
java·spring
song1502653729821 小时前
如何选择适合的AI视觉检测设备?
人工智能
FE_C_P小麦21 小时前
AI Prompt 提示词模板【转载】
人工智能
桂花饼1 天前
量化双雄争霸:九坤 IQuest-Coder-V1 的技术突破
人工智能·aigc·nano banana 2·openai兼容接口·claude opus 4.5·sora2 pro
win x1 天前
Redis 主从复制
java·数据库·redis
undsky_1 天前
【n8n教程】:RSS Feed Trigger节点,玩转RSS订阅自动化
人工智能·ai·aigc·ai编程