掘金 人工智能 04月03日 10:58
聊聊Spring AI的Image Model
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文深入探讨了Spring AI框架中Image Model的实现细节,重点分析了其核心接口、类以及关键配置。文章详细介绍了ImageModel、ImagePrompt、ImageMessage、ImageOptions、ImageResponse、ImageGeneration等核心组件,并结合ZhiPuAiImageModel的示例,展示了如何在Spring AI中集成和使用图像生成模型。通过对配置文件的解读,揭示了Spring AI如何根据不同供应商的配置自动装配图像模型,为开发者提供了清晰的参考。

🖼️ **ImageModel 接口:** ImageModel 接口继承自 Model 接口,定义了处理图像生成请求的核心方法 call,该方法接收 ImagePrompt 作为输入,并返回 ImageResponse。

📝 **ImagePrompt 类:** ImagePrompt 实现了 ModelRequest 接口,用于封装图像生成请求的参数,包括图像描述信息 (messages) 和图像生成选项 (imageModelOptions)。

💬 **ImageMessage 类:** ImageMessage 用于定义图像描述文本,包含 text 和 weight 属性,用于描述图像生成的内容。

⚙️ **ImageOptions 接口:** ImageOptions 接口继承自 ModelOptions,定义了图像生成的各种配置选项,例如模型选择、图像尺寸、响应格式等。

🖼️ **ImageResponse 类:** ImageResponse 实现了 ModelResponse 接口,用于封装图像生成的响应结果,包含图像生成结果列表 (imageGenerations) 和元数据 (imageResponseMetadata)。

🔌 **ZhiPuAiImageModel 实现:** Spring AI 提供了 ZhiPuAiImageModel,通过 ZhiPuAiImageApi 与智谱AI的图像生成服务进行交互,并支持通过配置文件进行灵活配置。

本文主要研究一下Spring AI的Image Model

ImageModel

org/springframework/ai/image/ImageModel.java

@FunctionalInterfacepublic interface ImageModel extends Model<ImagePrompt, ImageResponse> {    ImageResponse call(ImagePrompt request);}

ImageModel继承了Model接口,其call方法入参为ImagePrompt,返回ImageResponse

ImagePrompt

org/springframework/ai/image/ImagePrompt.java

public class ImagePrompt implements ModelRequest<List<ImageMessage>> {   private final List<ImageMessage> messages;    private ImageOptions imageModelOptions;   public ImagePrompt(List<ImageMessage> messages) {       this.messages = messages; }   public ImagePrompt(List<ImageMessage> messages, ImageOptions imageModelOptions) {       this.messages = messages;     this.imageModelOptions = imageModelOptions;   }   public ImagePrompt(ImageMessage imageMessage, ImageOptions imageOptions) {        this(Collections.singletonList(imageMessage), imageOptions);  }   public ImagePrompt(String instructions, ImageOptions imageOptions) {      this(new ImageMessage(instructions), imageOptions);   }   public ImagePrompt(String instructions) {     this(new ImageMessage(instructions), ImageOptionsBuilder.builder().build());  }   @Override    public List<ImageMessage> getInstructions() {       return this.messages;   }   @Override    public ImageOptions getOptions() {        return this.imageModelOptions;  }   }   

ImagePrompt实现了ModelRequest接口,它定义了messages及imageModelOptions属性,其getInstructions返回的是List<ImageMessage>类型

ImageMessage

org/springframework/ai/image/ImageMessage.java

public class ImageMessage {  private String text;   private Float weight; public ImageMessage(String text) {     this.text = text; }   public ImageMessage(String text, Float weight) {       this.text = text;     this.weight = weight; }   }   

ImageMessage定义了text及weight属性

ImageOptions

org/springframework/ai/image/ImageOptions.java

public interface ImageOptions extends ModelOptions {    @Nullable    Integer getN();  @Nullable    String getModel();   @Nullable    Integer getWidth();  @Nullable    Integer getHeight(); @Nullable    String getResponseFormat();  @Nullable    String getStyle();}

ImageOptions继承了ModelOptions,它定义了getN、getModel、getWidth、getHeight、getResponseFormat、getStyle方法

ImageResponse

org/springframework/ai/image/ImageResponse.java

public class ImageResponse implements ModelResponse<ImageGeneration> {  private final ImageResponseMetadata imageResponseMetadata;      private final List<ImageGeneration> imageGenerations;     public ImageResponse(List<ImageGeneration> generations) {        this(generations, new ImageResponseMetadata()); }       public ImageResponse(List<ImageGeneration> generations, ImageResponseMetadata imageResponseMetadata) {       this.imageResponseMetadata = imageResponseMetadata;      this.imageGenerations = List.copyOf(generations);    }   }   

ImageResponse实现了ModelResponse接口,其getResult返回的是ImageGeneration类型

ImageGeneration

org/springframework/ai/image/ImageGeneration.java

public class ImageGeneration implements ModelResult<Image> {  private ImageGenerationMetadata imageGenerationMetadata;  private Image image;  public ImageGeneration(Image image) {     this.image = image;   }   public ImageGeneration(Image image, ImageGenerationMetadata imageGenerationMetadata) {        this.image = image;       this.imageGenerationMetadata = imageGenerationMetadata;   }   @Override    public Image getOutput() {        return this.image;  }   @Override    public ImageGenerationMetadata getMetadata() {        return this.imageGenerationMetadata;    }   @Override    public String toString() {        return "ImageGeneration{" + "imageGenerationMetadata=" + this.imageGenerationMetadata + ", image=" + this.image                + '}'; }}

ImageGeneration实现了ModelResult接口,其getOutput返回的是Image类型,其getMetadata返回ImageGenerationMetadata

Image

org/springframework/ai/image/Image.java

public class Image {       private String url;        private String b64Json;    }   

Image定义了url与b64Json属性

ImageGenerationMetadata

org/springframework/ai/image/ImageGenerationMetadata.java

public interface ImageGenerationMetadata extends ResultMetadata {}

ImageGenerationMetadata目前仅仅是继承了ResultMetadata

示例

pom.xml

        <dependency>         <groupId>org.springframework.ai</groupId>         <artifactId>spring-ai-starter-model-zhipuai</artifactId>      </dependency>

配置

spring:  ai:    model:      image: zhipuai    zhipuai:      api-key: xxxx      base-url: https://open.bigmodel.cn/api/paas      images:        options:          model: cogview-3

代码

    @Test    public void testGenImage() {        ImagePrompt imagePrompt = new ImagePrompt("A light cream colored mini golden doodle",                ZhiPuAiImageOptions.builder()                        .model("cogview-3").build());        ImageResponse response = imageModel.call(imagePrompt);        log.info("resp:{}", JSON.toJSONString(response));    }

输出示例

resp:{"metadata":{"created":1743578654097,"empty":true,"rawMap":{}},"result":{"output":{"url":"https://sfile.chatglm.cn/testpath/xxx.png"}},"results":[{"$ref":"$.result"}]}

源码

ZhiPuAiImageAutoConfiguration

org/springframework/ai/model/zhipuai/autoconfigure/ZhiPuAiImageAutoConfiguration.java

@AutoConfiguration(after = { RestClientAutoConfiguration.class, SpringAiRetryAutoConfiguration.class })@ConditionalOnClass(ZhiPuAiApi.class)@ConditionalOnProperty(name = SpringAIModelProperties.IMAGE_MODEL, havingValue = SpringAIModels.ZHIPUAI,      matchIfMissing = true)@EnableConfigurationProperties({ ZhiPuAiConnectionProperties.class, ZhiPuAiImageProperties.class })public class ZhiPuAiImageAutoConfiguration {    @Bean    @ConditionalOnMissingBean    public ZhiPuAiImageModel zhiPuAiImageModel(ZhiPuAiConnectionProperties commonProperties,         ZhiPuAiImageProperties imageProperties, RestClient.Builder restClientBuilder, RetryTemplate retryTemplate,          ResponseErrorHandler responseErrorHandler) {        String apiKey = StringUtils.hasText(imageProperties.getApiKey()) ? imageProperties.getApiKey()             : commonProperties.getApiKey();     String baseUrl = StringUtils.hasText(imageProperties.getBaseUrl()) ? imageProperties.getBaseUrl()              : commonProperties.getBaseUrl();        Assert.hasText(apiKey, "ZhiPuAI API key must be set");     Assert.hasText(baseUrl, "ZhiPuAI base URL must be set");       var zhiPuAiImageApi = new ZhiPuAiImageApi(baseUrl, apiKey, restClientBuilder, responseErrorHandler);        return new ZhiPuAiImageModel(zhiPuAiImageApi, imageProperties.getOptions(), retryTemplate); }}

ZhiPuAiImageAutoConfiguration在spring.ai.model.imagezhipuai时启用,它会根据ZhiPuAiConnectionProperties及ZhiPuAiImageProperties自动配置ZhiPuAiImageModel

ZhiPuAiConnectionProperties

@ConfigurationProperties(ZhiPuAiConnectionProperties.CONFIG_PREFIX)public class ZhiPuAiConnectionProperties extends ZhiPuAiParentProperties {    public static final String CONFIG_PREFIX = "spring.ai.zhipuai";    public static final String DEFAULT_BASE_URL = "https://open.bigmodel.cn/api/paas"; public ZhiPuAiConnectionProperties() {     super.setBaseUrl(DEFAULT_BASE_URL); }}class ZhiPuAiParentProperties {  private String apiKey; private String baseUrl;    public String getApiKey() {     return this.apiKey;   }   public void setApiKey(String apiKey) {       this.apiKey = apiKey;   }   public String getBaseUrl() {        return this.baseUrl;  }   public void setBaseUrl(String baseUrl) {     this.baseUrl = baseUrl; }}

ZhiPuAiConnectionProperties继承了ZhiPuAiParentProperties属性,ZhiPuAiParentProperties定义了apiKey、baseUrl属性

ZhiPuAiImageProperties

org/springframework/ai/model/zhipuai/autoconfigure/ZhiPuAiImageProperties.java

@ConfigurationProperties(ZhiPuAiImageProperties.CONFIG_PREFIX)public class ZhiPuAiImageProperties extends ZhiPuAiParentProperties {  public static final String CONFIG_PREFIX = "spring.ai.zhipuai.image";       @NestedConfigurationProperty private ZhiPuAiImageOptions options = ZhiPuAiImageOptions.builder().build(); public ZhiPuAiImageOptions getOptions() {      return this.options;   }   public void setOptions(ZhiPuAiImageOptions options) {        this.options = options;  }}

ZhiPuAiImageProperties继承了ZhiPuAiParentProperties,它主要配置spring.ai.zhipuai.image.options

ZhiPuAiImageOptions

org/springframework/ai/zhipuai/ZhiPuAiImageOptions.java

@JsonInclude(JsonInclude.Include.NON_NULL)public class ZhiPuAiImageOptions implements ImageOptions {      @JsonProperty("model")  private String model = ZhiPuAiImageApi.DEFAULT_IMAGE_MODEL;      @JsonProperty("user_id")    private String user;   public static Builder builder() {     return new Builder();    }   @Override    @JsonIgnore  public Integer getN() {     return null;    }   @Override    public String getModel() {      return this.model;    }   public void setModel(String model) {     this.model = model; }   @Override    @JsonIgnore  public Integer getWidth() {     return null;    }   @Override    @JsonIgnore  public Integer getHeight() {        return null;    }   @Override    @JsonIgnore  public String getResponseFormat() {     return null;    }   @Override    @JsonIgnore  public String getStyle() {      return null;    }   public String getUser() {       return this.user; }   public void setUser(String user) {       this.user = user;   }   }   

ZhiPuAiImageOptions实现了ImageOptions接口,主要支持了getModel、getUser方法

小结

Spring AI提供了ImageModel接口,它继承了Model接口,其call方法入参为ImagePrompt,返回ImageResponse。spring-ai-starter-model-openai提供了OpenAiImageModel,spring-ai-starter-model-azure-openai提供了AzureOpenAiImageModel,spring-ai-starter-model-qianfan提供了QianFanImageModel,spring-ai-starter-model-stability-ai提供了StabilityAiImageModel,spring-ai-starter-model-zhipuai提供了ZhiPuAiImageModel。

doc

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

Spring AI 图像模型 ZhiPuAI Java
相关文章