AI 时代,Java 程序员也需要与时俱进,这两个框架必须掌握。
Spring AI 是一个AI工程领域的应用程序框架,它的目标是将 Spring生态系统的设计原则应用于人工智能领域。
但是, Spring AI 不支持获取接入国内的大部分常见模型的,比如文心一言、通义千问等。
所以,对于国内开发者来说,能够正常使用(不需要魔法)Spring AI 就是刚需了,即 Spring AI Alibaba成为我们的首选。
Spring AI Alibaba官网:https://java2ai.com/docs/dev/overview/
Spring AI Alibaba 是一款 Java 语言实现的 AI 应用开发框架,旨在简化 Java AI 应用程序开发,让 Java 开发者像使用 Spring 开发普通应用一样开发 AI 应用。
Spring AI Alibaba 基于 Spring AI 开源项目构建,默认提供阿里云基础模型服务、开源及商业生态组件的集成与最佳实践。
注意:
因为 Spring AI Alibaba 基于 Spring Boot 3.x 开发,因此对 JDK 要求 17 及以上版本。
总的来说,
以下是 Spring AI Alibaba 支持的核心能力,未来更多高级功能将以这些核心能力为基础。请参考官网文档 Spring AI Alibaba 核心概念以及 AI 应用开发最佳实践。
登录阿里云官网,实名认证之后,前往阿里云百炼平台,单击创建API-KEY。
阿里云百炼-获取API Key:https://help.aliyun.com/zh/model-studio/developer-reference/get-api-key
在已创建的API Key操作列,单击查看,获取API KEY。
新用户开通即享每个模型100万免费tokens。
新人免费额度有效期通常是30~180天,从开通百炼或模型申请通过之日起计算。超过有效期或免费额度消耗完后,继续使用模型推理服务将产生计费。
获得 API Key之后,建议将其配置到环境变量中,以便在调用模型或应用时使用。这样可以避免在代码中显式地配置 API Key,从而降低API Key泄漏的风险。
阿里云百炼-将API Key配置到环境变量:https://help.aliyun.com/zh/model-studio/developer-reference/configure-api-key-through-environment-variables
这里在Windows系统中,将API Key配置到环境变量。
Spring AI Alibaba 实现了与阿里云通义模型的完整适配,下面使用 spring ai alibaba 开发一个基于通义模型服务的智能聊天应用。
因为 Spring AI Alibaba 基于 Spring Boot 3.x 开发,因此对 JDK 要求 17 及以上版本。
创建 Spring Boot 3.x项目,引入 Spring AI Alibaba依赖:
在项目中添加 spring-ai-alibaba-starter 依赖,它将通过 Spring Boot 自动装配机制初始化与阿里云通义大模型通信的 ChatClient、ChatModel 相关实例。
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<version>3.4.0version>
dependency>
<dependency>
<groupId>com.alibaba.cloud.aigroupId>
<artifactId>spring-ai-alibaba-starterartifactId>
<version>1.0.0-M6.1version>
dependency>
注意:
由于 spring-ai 相关依赖包还没有发布到中央仓库,如果出现 spring-ai-core 等相关依赖解析问题,请在项目的 pom.xml 依赖中加入如下仓库配置。
<repositories>
<repository>
<id>spring-milestonesid>
<name>Spring Milestonesname>
<url>https://repo.spring.io/milestoneurl>
<snapshots>
<enabled>falseenabled>
snapshots>
repository>
<repository>
<id>aliyunmavenid>
<name>aliyunname>
<url>https://maven.aliyun.com/repository/publicurl>
repository>
repositories>
在 Windows系统中,打开CMD,输入下面命令查看配置的环境变量:
echo %ALI_AI_DASHSCOPE_API_KEY%
spring:
ai:
dashscope:
api-key: ${ALI_AI_DASHSCOPE_API_KEY}
在普通 Controller Bean 中注入 ChatClient 实例,这样你的 Bean 就具备与 AI 大模型智能对话的能力了。
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@RequestMapping("/dashscope/chat-client")
public class DashScopeChatClientController {
private static final String DEFAULT_PROMPT = "你好,介绍下你自己!";
private final ChatClient dashScopeChatClient;
// 使用如下的方式注入 ChatClient
public DashScopeChatClientController(ChatClient.Builder chatClientBuilder) {
this.dashScopeChatClient = chatClientBuilder.build();
}
/**
* ChatClient 简单调用
*/
@GetMapping("/simple/chat")
public String simpleChat(String prompt) {
if (StringUtils.isBlank(prompt)) {
prompt = DEFAULT_PROMPT;
}
String content = dashScopeChatClient.prompt(prompt).call().content();
log.info("simpleChat --> \n prompt ={}, \n content = {}", prompt, content);
return content;
}
}
启动项目,访问接口与 AI 大模型智能对话。
– 求知若饥,虚心若愚。