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

相关推荐
爱敲代码的小鱼13 小时前
springsecurity:
java·开发语言·数据库
豆沙小饼干13 小时前
MySQL 小白循序渐进实操路线(纯动手,跟着敲就能练,全程生活化案例:外卖平台)
java
歪歪歪比巴卜13 小时前
医疗金融千级KOS矩阵短视频违禁词批量检测方案与合规工具测评
大数据·人工智能·物联网·社媒合规管控
凌杰13 小时前
AI 学习笔记:由 Palantir CEO 的观点所展开的行业观察
人工智能
AI 小老六13 小时前
Agent 工程化新底座:用 CLI 契约层打通 HTTP 接口与业务能力
网络·人工智能·http·ai·架构
Wang's Blog13 小时前
Java框架快速入门: Spring Bean核心配置与实例化详解
java·开发语言·spring
promising_xxx13 小时前
深度学习个人开源知识库 深度筑基 | DeepBase
人工智能·python·深度学习·计算机视觉·ai·语言模型·nlp
SSO_Crown13 小时前
AI 招聘管理系统深度评测与选型指南
大数据·运维·人工智能
zhangxingchao14 小时前
AI应用开发九:从 Hermes Agent 学长期记忆
前端·人工智能·后端
爱敲代码的小鱼14 小时前
springboot原理篇:
java·spring boot·spring