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

相关推荐
杰夫贾维斯10 分钟前
CentOS Linux 8 的系统部署 Qwen2.5-7B -Instruct-AWQ
linux·运维·人工智能·机器学习·centos
m0_7033236711 分钟前
SEO外包服务甄选指南:避开陷阱,精准匹配
大数据·人工智能
咖啡啡不加糖15 分钟前
RabbitMQ 消息队列:从入门到Spring Boot实战
java·spring boot·rabbitmq
金智维科技17 分钟前
多系统、跨流程、高重复?看烟草企业如何用数字员工撬动运营变革
人工智能
PyAIExplorer28 分钟前
图像处理中的边缘填充:原理与实践
图像处理·人工智能
AI大模型技术社38 分钟前
🔥企业级必读:筛选高可用MCP服务的黄金标准
人工智能·mcp
zzywxc7871 小时前
AI技术通过提示词工程(Prompt Engineering)正在深度重塑职场生态和行业格局,这种变革不仅体现在效率提升,更在重构人机协作模式。
java·大数据·开发语言·人工智能·spring·重构·prompt
Java中文社群1 小时前
炸裂!Dify新版发布:内置MCP双向支持!
人工智能·后端
金智维科技1 小时前
智能化财务管理如何简化审批流程?
人工智能
HollowKnightZ1 小时前
论文阅读笔记:VI-Net: Boosting Category-level 6D Object Pose Estimation
人工智能·深度学习·计算机视觉