之前做個幾個大模型的應用,都是使用Python語言,后來有一個項目使用了Java,并使用了Spring AI框架。隨著Spring AI不斷地完善,最近它發布了1.0正式版,意味著它已經能很好的作為企業級生產環境的使用。對于Java開發者來說真是一個福音,其功能已經能滿足基于大模型開發企業級應用。借著這次機會,給大家分享一下Spring AI框架。
注意:由于框架不同版本改造會有些使用的不同,因此本次系列中使用基本框架是 Spring AI-1.0.0,JDK版本使用的是19。
代碼參考: https://github.com/forever1986/springai-study
目錄
- 1 Spring AI 的 ImageModel
- 1.1 ImagePrompt
- 1.2 ImageResponse
- 2 示例演示
上一章講了一個非常重要的EmbeddingModel,這一章來講講ImageModel。
1 Spring AI 的 ImageModel
前面提到聊天模型和嵌入模型時,都說過Spring AI封裝了一個ChatModel和EmbeddingModel,所以關于圖像模型,Spring AI 同樣封裝了一個ImageModel接口,為了統一各個模型供應商的封裝。下面是ImageModel的源碼:
import org.springframework.ai.model.Model;@FunctionalInterface
public interface ImageModel extends Model<ImagePrompt, ImageResponse> {// 返回圖像ImageResponse call(ImagePrompt request);
}
可以看到ImageModel 非常簡單,就是只有一個方法call用于調用大模型的API,并返回圖像。但是關于call方法,需要關注的是其參數ImagePrompt和返回值ImageResponse。
1.1 ImagePrompt
關于ImagePrompt,它包括兩個重要部分:ImageMessage和ImageOptions:
- ImageMessage:這個參數包括一個提示語和一個權重值,提示詞當然就是提示生成圖片的提示語,而權重值則需要模型能夠支持權重設置的模型才起作用
- ImageOptions:這個是配置選項,包括:模型、生成幾張圖片、高度、寬度等等信息,當然這個也是需要模型本身支持某些配置選擇才行。比如可以看一下智譜實現的ZhiPuAiImageOptions,里面僅僅包括設置模型,并沒有其它配置選項。
1.2 ImageResponse
關于ImageResponse,它包括兩個重要部分:ImageGeneration和ImageResponseMetadata:
- ImageGeneration:繼承自 ModelResult 類,用于表示輸出響應(也就是圖片)以及關于該圖片的相關元數據。這是一個list,也就是支持多張圖片生成(如果模型允許的話)
- ImageResponseMetadata:該對象用于存儲有關人工智能模型響應的元數據。
2 示例演示
代碼參考lesson18
示例說明:這里使用智譜的免費圖像模型CogView-3-Flash進行圖片生成演示
1)關于如何找圖片大模型,可以去各大模型平臺上面找,比如下面是在智譜官方的文生圖模型
說明:這次選用CogView-3-Flash,因為免費。。。
2)新建lesson18子模塊,其pom引入如下:
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-starter-model-zhipuai</artifactId></dependency>
</dependencies>
3)配置application.properties文件
# API KEY
spring.ai.zhipuai.api-key=你的智譜模型的API KEY
4)新建演示類ImageModelController:
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.ai.image.ImageMessage;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.zhipuai.ZhiPuAiImageModel;
import org.springframework.ai.zhipuai.ZhiPuAiImageOptions;
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;@RestController
public class ImageModelController {private final ZhiPuAiImageModel imageModel;@Autowiredpublic ImageModelController(ZhiPuAiImageModel imageModel) {this.imageModel = imageModel;}@GetMapping("/ai/imagegenerate")public void generate(@RequestParam(value = "message", defaultValue = "生成一只老虎!") String message, HttpServletResponse response) throws IOException {ImageResponse image = this.imageModel.call(new ImagePrompt(new ImageMessage(message)// ZhiPuAiImageOptions參數可以設置模型、圖片數量、圖片大小等信息,這里必須是圖像模型, ZhiPuAiImageOptions.builder().model("cogview-3-flash").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();}}
5)新建啟動類Lesson18Application:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class Lesson18Application {public static void main(String[] args) {SpringApplication.run(Lesson18Application.class, args);}}
6)演示效果:
http://localhost:8080/ai/imagegenerate
結語:本章介紹Spring AI的ImageModel,包括其源碼、參數和返回值。同時也通過示例演示如何訪問智譜的圖像模型,可以看出Spring AI對于圖像模型也是封裝了一個非常簡便的ImageModel,讓用戶無需關心底層的訪問邏輯。下一章將繼續講非聊天大模型之音頻大模型。
Spring AI系列上一章:《Spring AI 系列之二十一 - EmbeddingModel》
Spring AI系列下一章:《Spring AI 系列之二十三 - AudioModels》