基于Spring AI集成AI大模型快速入门

基于Spring AI集成AI大模型快速入门

精选文章moguli202025-06-05 6:30:095A+A-

本文重点介绍,基于Spring AI框架,并使用阿里百炼大模型服务平台的AI服务,快速搭建一个springboot工程,并进行简单的AI问答,初步验证Spring AI框架的易用性,以及与阿里巴巴AI框架spring-ai-alibaba-starter的友好集成性。

1、使用Spring AI前提条件

  • JDK为17以上版本,本人使用的jdk21版本;
  • SpringBoot版本为3.x以上,本项目使用的是SpringBoot 3.3.3版本;
  • 开通阿里大模型服务,获取 API-KEY,后面代码里要使用。具体操作,请参考阿里云大模型服务平台百炼。

2、创建SpringBoot工程

通过IDEA开发工具,创建一个普通的Java工程,注意JDK版本至少是17以上,我这里使用的是jdk21版本。

3、配置pom.xml文件

工程创建完成后,在pom.xml里添加依赖。

首先,需要在项目中添加 spring-boot-starter-parent声明,指定springboot框架的版本号:

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>3.3.3</version>

<relativePath/>

</parent>

在dependencies标签中引入spring-boot-starter-web依赖:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

然后,需要在项目中添加 spring-ai-alibaba-starter依赖:

<dependency>

<groupId>com.alibaba.cloud.ai</groupId>

<artifactId>spring-ai-alibaba-starter</artifactId>

<version>1.0.0-M3.2</version>

</dependency>

由于 spring-ai 相关依赖包还没有发布到中央仓库,如出现 spring-ai-core 等相关依赖解析问题,请在您项目的 pom.xml 依赖中加入如下仓库配置。

<repositories>

<repository>

<id>spring-milestones</id>

<name>Spring Milestones</name>

<url>https://repo.spring.io/milestone</url>

<snapshots>

<enabled>false</enabled>

</snapshots>

</repository>

</repositories>

最后pom.xml文件完整内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.yuncheng</groupId>
<artifactId>spring-ai-helloworld</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.3</version>
<relativePath/>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter</artifactId>
<version>1.0.0-M3.2</version>
</dependency>
</dependencies>

<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

</project>

4、配置yml文件

在工程的resources目录下的application-dev.yml文件里增加如下配置:

注意:chat.client.enabled=true 这个配置表示的是ChatClient自动装配,如果通过编程方式创建ChatClient,这里需要设置成false

server:
port: 8080

spring:
application:
name: spring-ai-helloworld
ai:
dashscope:
api-key: sk-b90ad31bb3eb4a158524928356d39dc5 (这里要替换成自己从阿里百炼平台申请的key)
chat:
client:
enabled: true

5、创建ChatController

创建一个普通 Controller Bean类, 注入 ChatClient 实例,这样你的 Bean 就具备与 AI 大模型智能对话的能力了。

package com.yuncheng;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/ai")
public class ChatController {
private static final Logger log = LoggerFactory.getLogger(ChatController.class);
private final ChatClient chatClient;

public ChatController(ChatClient.Builder builder) {
this.chatClient = builder.build();
}

@GetMapping("/chat")
public String chat(String input) {
log.info("人工提问:"+input);

String reply = this.chatClient.prompt()
.user(input)
.call()
.content();

log.info("大模型回复:"+reply);
return reply;
}
}

在这个示例中,就是使用的 Spring Boot 自动装配默认生成的 ChatClient.Builder 的 bean把它注入到您自己的类中。这里是根据用户提问并从模型得到文本回答,首先设置了用户消息的内容,call 方法向 AI 模型发送请求,content 方法以字符串形式返回 AI 模型的响应。

6、创建Application启动类

package com.yuncheng;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

@SpringBootApplication
public class AiApplication {

public static void main(String[] args) {
ConfigurableApplicationContext application = SpringApplication.run(AiApplication.class, args);
Environment env = application.getEnvironment();
String port = env.getProperty("server.port");
System.out.println("==============AiApplication启动成功,服务端口为:" + port);

}
}

启动Springboot工程并测试

工程启动成功后,可以向大模型提问,打开浏览器,在地址栏输入:

http://localhost:8080/ai/chat?input=世界上一共有多少个国家

AI大模型回复:

截至2023年,世界上共有195个国家。其中,193个是联合国的会员国,2个是非会员观察员国(梵蒂冈和巴勒斯坦)。需要注意的是,由于政治、历史等因素,某些地区的国家地位存在争议,这导致不同组织或国家对于世界国家总数的统计可能略有差异。但通常情况下,195这个数字被广泛接受。

总结

通过以上项目验证,证明了基于Spring AI开源框架,并使用阿里云国内大模型服务,在SpringBoot工程中集成并使用AI大模型服务是初步可行的。

点击这里复制本文地址 以上内容由莫古技术网整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
qrcode

莫古技术网 © All Rights Reserved.  滇ICP备2024046894号-2