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组件,敬请关注,今天就介绍到这里。

相关推荐
编码忘我3 分钟前
java多线程安全集合
java
悟空码字8 分钟前
滑块拼图验证:SpringBoot完整实现+轨迹验证+Redis分布式方案
java·spring boot·后端
编码忘我9 分钟前
java类加载器及tomcat为什么不用双亲委派
java
lcj092466611 分钟前
机房U位资产智能化管理解决方案:破解传统运维痛点
人工智能
正宗咸豆花11 分钟前
端到端AI决策架构如何重塑实时协作体验?
人工智能·架构
lally.12 分钟前
Claude code agent由哪些东西组成
人工智能
NOCSAH13 分钟前
统好AI数智平台SRM:重塑采购管理新范式
大数据·人工智能·数智化一体平台·统好ai
superior tigre21 分钟前
LLM/HPC常见术语汇总
人工智能·llm·hpc
乱世刀疤24 分钟前
openclaw更换模型操作步骤
人工智能
高德开放平台26 分钟前
Skill 上新|高德开放平台上线 Amap SDK Skills!
人工智能·信息可视化·开发者·高德地图