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

相关推荐
霖大侠4 分钟前
【无标题】
人工智能·深度学习·机器学习
callJJ13 分钟前
Spring AI 文本聊天模型完全指南:ChatModel 与 ChatClient
java·大数据·人工智能·spring·spring ai·聊天模型
CBeann13 分钟前
企业级规则引擎落地实战:动态脚本引擎 QLExpress ,真香!
java·ai·大模型·规则引擎·qlexpress·大厂实战项目
懈尘14 分钟前
从 Java 1.7 到 Java 21:逐版本深入解析新特性与平台演进
java·开发语言
亓才孓14 分钟前
[Maven]Maven基础
java·maven
hello 早上好18 分钟前
05_Java 类加载过程
java·开发语言
是店小二呀28 分钟前
CANN 异构计算的极限扩展:从算子融合到多卡通信的统一优化策略
人工智能·深度学习·transformer
冻感糕人~31 分钟前
收藏备用|小白&程序员必看!AI Agent入门详解(附工业落地实操关联)
大数据·人工智能·架构·大模型·agent·ai大模型·大模型学习
echoVic31 分钟前
多模型支持的架构设计:如何集成 10+ AI 模型
java·javascript
橙露33 分钟前
Java并发编程进阶:线程池原理、参数配置与死锁避免实战
java·开发语言