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

相关推荐
晚霞的不甘17 小时前
CANN 支持多模态大模型:Qwen-VL 与 LLaVA 的端侧部署实战
人工智能·神经网络·架构·开源·音视频
草履虫建模21 小时前
力扣算法 1768. 交替合并字符串
java·开发语言·算法·leetcode·职场和发展·idea·基础
华玥作者1 天前
[特殊字符] VitePress 对接 Algolia AI 问答(DocSearch + AI Search)完整实战(下)
前端·人工智能·ai
AAD555888991 天前
YOLO11-EfficientRepBiPAN载重汽车轮胎热成像检测与分类_3
人工智能·分类·数据挖掘
王建文go1 天前
RAG(宠物健康AI)
人工智能·宠物·rag
ALINX技术博客1 天前
【202601芯动态】全球 FPGA 异构热潮,ALINX 高性能异构新品预告
人工智能·fpga开发·gpu算力·fpga
qq_297574671 天前
【实战教程】SpringBoot 实现多文件批量下载并打包为 ZIP 压缩包
java·spring boot·后端
易营宝1 天前
多语言网站建设避坑指南:既要“数据同步”,又能“按市场个性化”,别踩这 5 个坑
大数据·人工智能
老毛肚1 天前
MyBatis插件原理及Spring集成
java·spring·mybatis
学嵌入式的小杨同学1 天前
【Linux 封神之路】信号编程全解析:从信号基础到 MP3 播放器实战(含核心 API 与避坑指南)
java·linux·c语言·开发语言·vscode·vim·ux