2.使用Spring Boot&Spring AI快速构建AI应用程序

Spring AI 是基于 Spring Boot3.x 框架构建,Spring Boot官方提供了非常便捷的工具Spring Initializr帮助开发者快速的搭建Spring Boot应用程序,IDEA也集成了此工具。本文使用的开发工具IDEA+Spring Boot 3.4+Spring AI 1.0.0-SNAPSHOT+Maven

1.创建Spring Boot项目

使用IDEA Spring Boot模版创建项目,选择Maven+JDK17,因为Spring AI支持的Spring Boot版本从Spring Boot3.2.x开始,最低JDK版本是17

2.添加 Spring Snapshot Repositories和BOM

目前Spring AI最新快照版本是1.0.0-SNAPSHOT没有发布到maven中央仓库,所以需要手动添加Spring Snapshot Repositories

xml 复制代码
<repositories>
  <repository>
    <id>spring-snapshots</id>
    <name>Spring Snapshots</name>
    <url>https://repo.spring.io/snapshot</url>
    <releases>
      <enabled>false</enabled>
    </releases>
  </repository>
</repositories>

Spring AI BOM 声明了特定版本的 Spring AI依赖项的推荐版本。后续添加Spring AI相关模块无需指定具体的版本。

xml 复制代码
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-bom</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

3.添加Spring AI依赖

本文使用的是智谱AI大模型(特殊原因没使用OpenAI),如果要使用其它支持的大模型只需要把下面的依赖替换成对应大模型的spring boot starter即可。

xml 复制代码
  <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-zhipuai-spring-boot-starter</artifactId>
  </dependency>

4.pom.xml完整配置

xml 复制代码
<properties>
    <java.version>17</java.version>
    <spring-ai.version>1.0.0-SNAPSHOT</spring-ai.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.ai</groupId>
        <artifactId>spring-ai-zhipuai-spring-boot-starter</artifactId>
    </dependency>
</dependencies>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-bom</artifactId>
            <version>${spring-ai.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
<repositories>
    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/snapshot</url>
        <releases>
            <enabled>false</enabled>
        </releases>
    </repository>
</repositories>

5.申请AI大模型API keys

从智普AI官网https://bigmodel.cn/ 注册申请API keys

application.propertiesapplication.yml文件中配置 AI 模型相关参数。以 智谱AI为例,在application.properties中配置 API 密钥:

properties 复制代码
spring.ai.zhipuai.api-key=XXXXXXX

6.使用Spring AI ChatClient访问AI大模型

ChatClient 提供了一种Fluent API 用于与人工智能模型进行通信。它同时支持同步和流式(Reactive)编程模型。ChatClient是使用ChatClient.Builder对象创建的。可以使用Spring Boot 自动装配机制自动注入ChatClient.Builder Bean

java 复制代码
/**
 * Spring AI supports Spring Boot 3.2.x and 3.3.x
 * JDK 17
 * zhipuai
 */
@RestController
public class AiController {


        private final ChatClient chatClient;

        public AiController(ChatClient.Builder chatClientBuilder) {
            //自动注入ChatClient.Builder对象,并创建ChatClient对象。
            this.chatClient = chatClientBuilder.build();
        }

        @GetMapping("/ai")
        String generation(String message) {
           //用户输入的信息提交给大模型,使用的是ChatClient与大模型交互。
            return this.chatClient.prompt()
                    .user(message)
                    .call()
                    .content();
        }
}

在这个例子中,用户输入内容call()方法向人工智能模型发送请求,content()方法以字符串的形式返回人工智能模型的响应。

Prompt(提示词)是引导 AI 模型生成特定输出的输入。这些提示的设计和措辞直接影响模型的响应,后面有专门的Prompt章节来详细介绍。

7.接口测试

shell 复制代码
➜ ~ curl http://localhost:8080/ai?message=你好
你好👋!我是人工智能助手智谱清言(ChatGLM),很高兴见到你,欢迎问我任何问题。

8.总结

本文为主要介绍了从开发环境搭建、依赖配置、API 密钥获取到利用 ChatClient 与大模型交互并测试验证的详细过程,助力快速上手基于 Spring AI 与特定大模型构建应用程序。

Ben技术站关注Java技术,LLM,计算机科学等内容。

相关推荐
九章云极AladdinEdu20 小时前
超参数自动化调优指南:Optuna vs. Ray Tune 对比评测
运维·人工智能·深度学习·ai·自动化·gpu算力
人工智能训练师21 小时前
Ubuntu22.04如何安装新版本的Node.js和npm
linux·运维·前端·人工智能·ubuntu·npm·node.js
hdsoft_huge1 天前
Java & Spring Boot常见异常全解析:原因、危害、处理与防范
java·开发语言·spring boot
cxr8281 天前
SPARC方法论在Claude Code基于规则驱动开发中的应用
人工智能·驱动开发·claude·智能体
研梦非凡1 天前
ICCV 2025|从粗到细:用于高效3D高斯溅射的可学习离散小波变换
人工智能·深度学习·学习·3d
幂简集成1 天前
Realtime API 语音代理端到端接入全流程教程(含 Demo,延迟 280ms)
人工智能·个人开发
龙腾-虎跃1 天前
FreeSWITCH FunASR语音识别模块
人工智能·语音识别·xcode
智慧地球(AI·Earth)1 天前
给AI配一台手机+电脑?智谱AutoGLM上线!
人工智能·智能手机·电脑
Godspeed Zhao1 天前
自动驾驶中的传感器技术46——Radar(7)
人工智能·机器学习·自动驾驶
limengshi1383921 天前
机器学习面试:请介绍几种常用的学习率衰减方式
人工智能·学习·机器学习