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

相关推荐
海域云赵从友7 分钟前
助力DeepSeek私有化部署服务:让企业AI落地更简单、更安全
人工智能·安全
伊一大数据&人工智能学习日志20 分钟前
自然语言处理NLP 04案例——苏宁易购优质评论与差评分析
人工智能·python·机器学习·自然语言处理·数据挖掘
刀客12325 分钟前
python3+TensorFlow 2.x(六)自编码器
人工智能·python·tensorflow
大模型之路41 分钟前
Grok-3:人工智能领域的新突破
人工智能·llm·grok-3
m0_748247551 小时前
springboot中配置logback-spring.xml
spring boot·spring·logback
m0_748250031 小时前
springboot使用logback自定义日志
java·spring boot·logback
程序猿熊跃晖1 小时前
多环境日志管理:使用Logback与Logstash集成实现高效日志处理
spring boot·elk·logback
闻道且行之1 小时前
LLaMA-Factory|微调大语言模型初探索(4),64G显存微调13b模型
人工智能·语言模型·llama·qlora·fsdp
喝不完一杯咖啡1 小时前
【AI时代】可视化训练模型工具LLaMA-Factory安装与使用
人工智能·llm·sft·llama·llama-factory
KingDol_MIni2 小时前
Spring Boot 集成 T-io 实现客户端服务器通信
java·服务器·spring boot