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,计算机科学等内容。

相关推荐
魔乐社区13 分钟前
DeepSeek在昇腾上的模型部署 - 常见问题及解决方案
人工智能·深度学习·deepseek
m0_7482386320 分钟前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
夜幕龙25 分钟前
深度生成模型(二)——基本概念与数学建模
人工智能·深度学习·transformer
游王子30 分钟前
OpenCV(11):人脸检测、物体识别
人工智能·opencv·计算机视觉
山海青风31 分钟前
从零开始玩转TensorFlow:小明的机器学习故事 3
人工智能·机器学习·tensorflow
@心都32 分钟前
机器学习数学基础:35.效度
人工智能·机器学习
幻想趾于现实34 分钟前
傅里叶分析
人工智能
春末的南方城市42 分钟前
VidSketch:具有扩散控制的手绘草图驱动视频生成
人工智能·深度学习·计算机视觉·aigc
Toky丶1 小时前
【文献阅读】A Survey on Model Compression for Large Language Models
人工智能·语言模型·自然语言处理
Williams101 小时前
解锁高效开发新姿势:Trae AI编辑器深度体验
人工智能·编辑器