之前做個幾個大模型的應用,都是使用Python語言,后來有一個項目使用了Java,并使用了Spring AI框架。隨著Spring AI不斷地完善,最近它發布了1.0正式版,意味著它已經能很好的作為企業級生產環境的使用。對于Java開發者來說真是一個福音,其功能已經能滿足基于大模型開發企業級應用。借著這次機會,給大家分享一下Spring AI框架。
注意:由于框架不同版本改造會有些使用的不同,因此本次系列中使用基本框架是 Spring AI-1.0.0,JDK版本使用的是19,Spring-AI-Alibaba-1.0.0.3-SNAPSHOT。
代碼參考: https://github.com/forever1986/springai-study
目錄
- 1 embedding模型
- 1.1 說明
- 1.1 代碼演示
- 2 ImageModel
- 2.1 說明
- 2.2 代碼演示
- 3 語音合成模型
- 3.1 說明
- 3.2 代碼演示
- 4 其它模型
在上一章中通過Spring AI Alibaba框架,可以引入更多的聊天記憶存儲類型。這一章將針對除了聊天模型之外的模型進行演示
代碼參考lesson24子模塊的ali-model子模塊
前期準備:
1)在lesson24子模塊下,新建ali-model子模塊,其pom引入如下:
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.alibaba.cloud.ai</groupId><artifactId>spring-ai-alibaba-starter-dashscope</artifactId></dependency>
</dependencies>
2)新建application.properties配置文件
spring.ai.dashscope.api-key=你的阿里百煉API KEY
1 embedding模型
1.1 說明
向量化模型能夠將文本、圖像、視頻等數據轉換為數學空間中的向量。通過計算向量之間的距離或夾角,可以量化數據的相似度,從而作用于精準搜索、智能推薦、自動分類及異常檢測等任務。之前在《系列之二十一 - EmbeddingModel》中講過。這里通過Spring AI Alibaba框架,可以使用阿里云上的很多個embedding模型。具體可以參考《官方文檔》,除了阿里本身的模型之外,還支持多模態的embedding模型:
1.1 代碼演示
代碼示例:
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.embedding.EmbeddingResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.util.List;
import java.util.Map;@RestController
public class EmbeddingModelController {private final EmbeddingModel embeddingModel;@Autowiredpublic EmbeddingModelController(EmbeddingModel embeddingModel) {this.embeddingModel = embeddingModel;}@GetMapping("/ai/embedding")public Map embed(@RequestParam(value = "message", defaultValue = "測試embedding") String message) {EmbeddingResponse embeddingResponse = this.embeddingModel.embedForResponse(List.of(message));return Map.of("embedding", embeddingResponse);}
}
演示效果:
http://localhost:8080/ai/embedding?message=測試embedding
2 ImageModel
2.1 說明
通義萬相-文生圖模型支持通過一句話生成圖像,分為V2版和V1版。全面升級的文生圖V2版模型提升了語義理解能力,通過預置智能改寫功能幫助您快速上手圖像創作。此外,V2版支持任意分辨率,輸出圖像最高可達200萬像素。推薦優先使用文生圖V2版模型。可以其《官方文檔》
除了阿里的通義萬相,也支持Stable Diffusion、FLUX等第三方模型
2.2 代碼演示
代碼示例:
import com.alibaba.cloud.ai.dashscope.image.DashScopeImageModel;
import com.alibaba.cloud.ai.dashscope.image.DashScopeImageOptions;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.ai.image.ImageMessage;
import org.springframework.ai.image.ImageOptions;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.List;@RestController
public class ImageModelController {private final DashScopeImageModel dashScopeImageModel;@Autowiredpublic ImageModelController(DashScopeImageModel dashScopeImageModel) {this.dashScopeImageModel = dashScopeImageModel;}@GetMapping("/ai/imagegenerate")public void generate(@RequestParam(value = "message", defaultValue = "生成一只老虎!") String message, HttpServletResponse response) throws IOException {ImageResponse image = this.dashScopeImageModel.call(new ImagePrompt(new ImageMessage(message), DashScopeImageOptions.builder().build()));// 返回的URLString url = image.getResult().getOutput().getUrl();// 將URL轉為Stream輸出到HttpServletResponseURL imageURL = URI.create(url).toURL();InputStream inputStream = imageURL.openStream();response.setHeader("Content-Type", MediaType.IMAGE_PNG_VALUE);response.getOutputStream().write(inputStream.readAllBytes());response.getOutputStream().flush();}}
演示效果:
http://localhost:8080/ai/imagegenerate
3 語音合成模型
3.1 說明
Qwen-TTS 系列是通義千問系列的語音合成模型,支持輸入中文、英文、中英混合的文本,并流式輸出音頻。除了Qwen系列,還支持,詳情見《官方文檔》
3.2 代碼演示
代碼示例:
import com.alibaba.cloud.ai.dashscope.api.DashScopeSpeechSynthesisApi;
import com.alibaba.cloud.ai.dashscope.audio.DashScopeSpeechSynthesisModel;
import com.alibaba.cloud.ai.dashscope.audio.DashScopeSpeechSynthesisOptions;
import com.alibaba.cloud.ai.dashscope.audio.synthesis.SpeechSynthesisMessage;
import com.alibaba.cloud.ai.dashscope.audio.synthesis.SpeechSynthesisPrompt;
import com.alibaba.cloud.ai.dashscope.audio.synthesis.SpeechSynthesisResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;@RestController
public class AudioSpeechController {private final DashScopeSpeechSynthesisModel dashScopeSpeechSynthesisModel;@Autowiredpublic AudioSpeechController(DashScopeSpeechSynthesisModel dashScopeSpeechSynthesisModel) {this.dashScopeSpeechSynthesisModel = dashScopeSpeechSynthesisModel;}@GetMapping("/ai/audio")public void generate(@RequestParam(value = "message", defaultValue = "你怎么這樣憑空污人清白……竊書不能算偷……竊書!……讀書人的事,能算偷么?") String message) {DashScopeSpeechSynthesisOptions speechOptions = DashScopeSpeechSynthesisOptions.builder().model("cosyvoice-v2") // 使用扣子模型.voice("longtao_v2") // 使用粵語語音.responseFormat(DashScopeSpeechSynthesisApi.ResponseFormat.WAV) // WAV格式.speed(1.0f) // 正常語速.build();SpeechSynthesisPrompt speechPrompt = new SpeechSynthesisPrompt(new SpeechSynthesisMessage(message), speechOptions);SpeechSynthesisResponse speechResponse = dashScopeSpeechSynthesisModel.call(speechPrompt);File file = new File("lesson24/ali-model/ali-output.wav");try (FileOutputStream fileOutputStream = new FileOutputStream(file)){fileOutputStream.write(speechResponse.getResult().getOutput().getAudio().array());fileOutputStream.flush();}catch (IOException e){// do something}}
}
演示效果:
http://localhost:8080/ai/audio
4 其它模型
在阿里的百煉平臺上,還有很多其它模型,比如語音合成與識別、視頻編輯與生成、特定行業模型等。這里就不一一列舉了。之所以寫這一章,是為了讓用戶能夠知道使用Spring AI Alibaba框架,基本上和Spring AI用法一致,而且很容易就能集成百煉平臺的各類模型。
結語:通過本章演示不同類型大模型的使用,相信用戶現在可以知道Spring AI Alibaba的價值。下一章繼續講解Spring AI Alibaba基于企業級應用實踐的一個擴展點:基于Nacos的MCP
Spring AI系列上一章:《Spring AI 系列之二十九 - Spring AI Alibaba-聊天記憶》
Spring AI系列下一章:《Spring AI 系列之三十一 - Spring AI Alibaba-基于Nacos的MCP》