Spring Cloud Alibaba AI 入門與實踐
一、概述
Spring AI 是 Spring 官方社區項目,旨在簡化 Java AI 應用程序開發,讓 Java 開發者像使用 Spring 開發普通應用一樣開發 AI 應用。
Spring Cloud Alibaba AI 是一個將 Spring Cloud 微服務生態與阿里巴巴 AI 能力無縫集成的框架,幫助開發者快速構建具備 AI 功能的現代化應用。本文將介紹 Spring Cloud Alibaba AI 的基本概念、主要特性和功能,并演示如何完成一個 在線聊天 和 在線畫圖 的 AI 應用。
二、主要特性和功能
Spring Cloud Alibaba AI 目前基于 Spring AI 0.8.1 版本 API 完成通義系列大模型的接入。通義接入是基于阿里云 阿里云百煉 服務;而 阿里云百煉 建立在 模型即服務(MaaS) 的理念基礎之上,圍繞 AI 各領域模型,通過標準化的 API 提供包括模型推理、模型微調訓練在內的多種模型服務。
主要提供以下核心功能:
2.1. 簡單易用的集成
通過 Spring Boot 風格的自動配置機制,開發者只需少量代碼配置,即可快速接入阿里云的 AI 服務。
2.2. 豐富的 AI 服務支持
支持以下核心能力:
- 自然語言處理(NLP):文本分析、智能問答、翻譯。
- 計算機視覺(CV):圖像生成、圖像識別、目標檢測。
- 語音處理:語音識別、語音合成。
- 數據分析與預測:數據建模、趨勢分析。
2.3. 高度擴展性
通過配置中心和注冊中心(如 Nacos)實現動態擴展,支持微服務架構的擴展需求。 提供接口定義,方便接入第三方 AI 平臺。
三、構建 AI 應用
Spring Cloud Alibaba AI 對 Java 版本有要求,所以需要提前預裝好 Java 17 環境。
3.1. 申請 API-KEY
登錄阿里云,進入 阿里云百煉 的頁面:
https://bailian.console.aliyun.com/?apiKey=1#/api-key
創建自己的 API-KEY
圖片
3.2. 添加依賴
在 Spring Boot 項目的 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 的相關屬性,包括服務器地址、認證信息等。
spring:
cloud:
ai:
tongyi:
connection:
api-key: sk-xxxxxx
- api-key 配置在阿里云百煉里申請的api-key
3.4. 創建模型調用服務
@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();
}
}
聊天和圖片的服務,分別通過注入 TongYiChatModel 和 TongYiImagesModel 對象來實現,屏蔽底層通義大模型交互細節。
3.5. 創建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. 測試效果
3.6.1. 聊天接口
在瀏覽器輸入:http://localhost:8009/ai/chat?message=你是誰
圖片
3.6.2. 圖片接口
在瀏覽器輸入:http://localhost:8009/ai/image?message=意大利面拌42號混凝土
圖片
3.6.3. 搭配聊天頁面
圖片
四、總結
當前版本的 Spring Cloud Alibaba AI 主要完成了幾種常見生成式模型的適配,涵蓋對話、文生圖、文生語音等。在未來的版本中將繼續推進 VectorStore、Embedding、ETL Pipeline、RAG 等更多 AI 應用開發場景的建設。