Spring Cloud Alibaba AI 入門與實(shí)踐
一、概述
Spring AI 是 Spring 官方社區(qū)項(xiàng)目,旨在簡(jiǎn)化 Java AI 應(yīng)用程序開(kāi)發(fā),讓 Java 開(kāi)發(fā)者像使用 Spring 開(kāi)發(fā)普通應(yīng)用一樣開(kāi)發(fā) AI 應(yīng)用。
Spring Cloud Alibaba AI 是一個(gè)將 Spring Cloud 微服務(wù)生態(tài)與阿里巴巴 AI 能力無(wú)縫集成的框架,幫助開(kāi)發(fā)者快速構(gòu)建具備 AI 功能的現(xiàn)代化應(yīng)用。本文將介紹 Spring Cloud Alibaba AI 的基本概念、主要特性和功能,并演示如何完成一個(gè) 在線聊天 和 在線畫(huà)圖 的 AI 應(yīng)用。
二、主要特性和功能
Spring Cloud Alibaba AI 目前基于 Spring AI 0.8.1 版本 API 完成通義系列大模型的接入。通義接入是基于阿里云 阿里云百煉 服務(wù);而 阿里云百煉 建立在 模型即服務(wù)(MaaS) 的理念基礎(chǔ)之上,圍繞 AI 各領(lǐng)域模型,通過(guò)標(biāo)準(zhǔn)化的 API 提供包括模型推理、模型微調(diào)訓(xùn)練在內(nèi)的多種模型服務(wù)。
主要提供以下核心功能:
2.1. 簡(jiǎn)單易用的集成
通過(guò) Spring Boot 風(fēng)格的自動(dòng)配置機(jī)制,開(kāi)發(fā)者只需少量代碼配置,即可快速接入阿里云的 AI 服務(wù)。
2.2. 豐富的 AI 服務(wù)支持
支持以下核心能力:
- 自然語(yǔ)言處理(NLP):文本分析、智能問(wèn)答、翻譯。
- 計(jì)算機(jī)視覺(jué)(CV):圖像生成、圖像識(shí)別、目標(biāo)檢測(cè)。
- 語(yǔ)音處理:語(yǔ)音識(shí)別、語(yǔ)音合成。
- 數(shù)據(jù)分析與預(yù)測(cè):數(shù)據(jù)建模、趨勢(shì)分析。
2.3. 高度擴(kuò)展性
通過(guò)配置中心和注冊(cè)中心(如 Nacos)實(shí)現(xiàn)動(dòng)態(tài)擴(kuò)展,支持微服務(wù)架構(gòu)的擴(kuò)展需求。 提供接口定義,方便接入第三方 AI 平臺(tái)。
三、構(gòu)建 AI 應(yīng)用
Spring Cloud Alibaba AI 對(duì) Java 版本有要求,所以需要提前預(yù)裝好 Java 17 環(huán)境。
3.1. 申請(qǐng) API-KEY
登錄阿里云,進(jìn)入 阿里云百煉 的頁(yè)面:
https://bailian.console.aliyun.com/?apiKey=1#/api-key
創(chuàng)建自己的 API-KEY
圖片
3.2. 添加依賴
在 Spring Boot 項(xiàng)目的 pom.xml 中添加 alibaba-ai 依賴
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-ai</artifactId>
</dependency>
<repositories>
<repository>
<id>alimaven</id>
<url>https://maven.aliyun.com/repository/public</url>
</repository>
<repository>
<id>spring-milestones</id>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-snapshots</id>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
3.3. 配置 API-KEY
在 application.yml 中配置 Kafka 的相關(guān)屬性,包括服務(wù)器地址、認(rèn)證信息等。
spring:
cloud:
ai:
tongyi:
connection:
api-key: sk-xxxxxx
- api-key 配置在阿里云百煉里申請(qǐng)的api-key
3.4. 創(chuàng)建模型調(diào)用服務(wù)
@Service
@Slf4j
publicclass TongYiSimpleService {
@Resource
private TongYiChatModel chatClient;
@Resource
private TongYiImagesModel imageClient;
public String chat(String message) {
Prompt prompt = new Prompt(new UserMessage(message));
return chatClient.call(prompt).getResult().getOutput().getContent();
}
public String image(String message) {
ImagePrompt prompt = new ImagePrompt(message);
Image image = imageClient.call(prompt).getResult().getOutput();
return image.getB64Json();
}
}
聊天和圖片的服務(wù),分別通過(guò)注入 TongYiChatModel 和 TongYiImagesModel 對(duì)象來(lái)實(shí)現(xiàn),屏蔽底層通義大模型交互細(xì)節(jié)。
3.5. 創(chuàng)建controller
@RestController
@RequestMapping("/ai")
publicclass TongYiController {
@Resource
private TongYiSimpleService tongYiSimpleService;
@GetMapping("/chat")
public String chat(@RequestParam(value = "message") String message) {
return tongYiSimpleService.chat(message);
}
@GetMapping("/image")
public ResponseEntity<byte[]> image(@RequestParam(value = "message") String message) {
String b64Str = tongYiSimpleService.image(message);
byte[] imageBytes = Base64.getDecoder().decode(b64Str);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_JPEG);
returnnew ResponseEntity<>(imageBytes, headers, HttpStatus.OK);
}
}
3.6. 測(cè)試效果
3.6.1. 聊天接口
在瀏覽器輸入:http://localhost:8009/ai/chat?message=你是誰(shuí)
圖片
3.6.2. 圖片接口
在瀏覽器輸入:http://localhost:8009/ai/image?message=意大利面拌42號(hào)混凝土
圖片
3.6.3. 搭配聊天頁(yè)面
圖片
四、總結(jié)
當(dāng)前版本的 Spring Cloud Alibaba AI 主要完成了幾種常見(jiàn)生成式模型的適配,涵蓋對(duì)話、文生圖、文生語(yǔ)音等。在未來(lái)的版本中將繼續(xù)推進(jìn) VectorStore、Embedding、ETL Pipeline、RAG 等更多 AI 應(yīng)用開(kāi)發(fā)場(chǎng)景的建設(shè)。