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 分钟前
车e估牵头正式启动乘用车金融价值评估师编制
大数据·人工智能·金融
JXL186032 分钟前
机器学习概念(面试题库)
人工智能·机器学习
星期天要睡觉32 分钟前
机器学习深度学习 所需数据的清洗实战案例 (结构清晰、万字解析、完整代码)包括机器学习方法预测缺失值的实践
人工智能·深度学习·机器学习·数据挖掘
岁月静好202537 分钟前
BadNets: Identifying Vulnerabilities in the Machine Learning Model Supply Chain
人工智能·机器学习
说私域39 分钟前
基于开源 AI 大模型 AI 智能名片 S2B2C 商城小程序视角下的企业组织能力建设与破圈升级
人工智能·小程序
2401_8588698040 分钟前
K近邻算法(knn)
人工智能
aneasystone本尊1 小时前
学习 Coze Studio 的知识库入库逻辑(续)
人工智能
renhongxia11 小时前
大模型微调RAG、LORA、强化学习
人工智能·深度学习·算法·语言模型
张较瘦_1 小时前
[论文阅读] 人工智能 | 当Hugging Face遇上GitHub:预训练语言模型的跨平台同步难题与解决方案
论文阅读·人工智能·github
Cloud Traveler1 小时前
从 0 到 1 开发校园二手交易系统:飞算 JavaAI 全流程实战
人工智能·java开发·飞算javaai炫技赛