Image Generation API

Spring Image Generation API 被设计为一个简单便携的界面,用于与专用于图像生成的各种 AI Models 交互,允许开发人员通过最少代码更改在不同的图像相关模型之间切换。此设计与 Spring 的模块化和可交换性的理念一致,确保开发人员可以对其应用程序快速应用与图像处理相关的不同 AI 能力。

The Spring Image Generation API is designed to be a simple and portable interface for interacting with various AI Models specialized in image generation, allowing developers to switch between different image-related models with minimal code changes. This design aligns with Spring’s philosophy of modularity and interchangeability, ensuring developers can quickly adapt their applications to different AI capabilities related to image processing.

此外,通过支持如 ImagePrompt(用于输入封装)和 ImageResponse(用于输出处理)之类的伴随类,Image Generation API 统一了与专门从事图像生成的 AI 模型的通信。它管理请求准备和响应解析的复杂性,提供用于图像生成功能的直接简化的 API 交互。

Additionally, with the support of companion classes like ImagePrompt for input encapsulation and ImageResponse for output handling, the Image Generation API unifies the communication with AI Models dedicated to image generation. It manages the complexity of request preparation and response parsing, offering a direct and simplified API interaction for image-generation functionalities.

Spring Image Generation API 构建在 Spring AI Generic Model API 的基础上,提供特定于图像的抽象和实现。

The Spring Image Generation API is built on top of the Spring AI Generic Model API, providing image-specific abstractions and implementations.

API Overview

本节提供 Spring Image Generation API 界面和关联类的指南。

This section provides a guide to the Spring Image Generation API interface and associated classes.

Image Client

此处是 ImageClient 界面定义:

Here is the ImageClient interface definition:

@FunctionalInterface
public interface ImageClient extends ModelClient<ImagePrompt, ImageResponse> {

	ImageResponse call(ImagePrompt request);

}

ImagePrompt

ImagePromptModelRequest,封装一个 ImageMessage 对象列表和可选模型请求选项。以下列表显示了 ImagePrompt 类的缩略版本,不包括构造函数和其他实用程序方法:

The ImagePrompt is a ModelRequest that encapsulates a list of ImageMessage objects and optional model request options. The following listing shows a truncated version of the ImagePrompt class, excluding constructors and other utility methods:

public class ImagePrompt implements ModelRequest<List<ImageMessage>> {

    private final List<ImageMessage> messages;

	private ImageOptions imageModelOptions;

    @Override
	public List<ImageMessage> getInstructions() {...}

	@Override
	public ImageOptions getOptions() {...}

    // constructors and utility methods omitted
}

ImageMessage

ImageMessage 类封装要使用的文本以及文本在影响生成的图像中应该具有的权重。对于支持权重的模型,它们可以是正数或负数。

The ImageMessage class encapsulates the text to use and the weight that the text should have in influencing the generated image. For models that support weights, they can be positive or negative.

public class ImageMessage {

	private String text;

	private Float weight;

    public String getText() {...}

	public Float getWeight() {...}

   // constructors and utility methods omitted

ImageOptions

表示可传递给图像生成模型的选项。ImageOptions 类扩展 ModelOptions 界面,用于定义一些可传递给 AI 模型的可移植选项。

Represents the options that can be passed to the Image generation model. The ImageOptions class extends the ModelOptions interface and is used to define few portable options that can be passed to the AI model.

ImageOptions 类的定义如下:

The ImageOptions class is defined as follows:

public interface ImageOptions extends ModelOptions {

	Integer getN();

	String getModel();

	Integer getWidth();

	Integer getHeight();

	String getResponseFormat(); // openai - url or base64 : stability ai byte[] or base64

}

此外,每个特定于模型的 ImageClient 实现都有自己的选项,可以传递给 AI 模型。例如,OpenAI 图像生成模型有自己的选项,如 qualitystyle 等。

Additionally, every model specific ImageClient implementation can have its own options that can be passed to the AI model. For example, the OpenAI Image Generation model has its own options like quality, style, etc.

这是一个强大的功能,允许开发者在启动应用程序时使用特定于模型的选项,然后在运行时使用 ImagePrompt 覆盖它们。

This is a powerful feature that allows developers to use model specific options when starting the application and then override them with at runtime using the ImagePrompt.

ImageResponse

ChatResponse 类的结构如下:

The structure of the ChatResponse class is as follows:

public class ImageResponse implements ModelResponse<ImageGeneration> {

	private final ImageResponseMetadata imageResponseMetadata;

	private final List<ImageGeneration> imageGenerations;

	@Override
	public ImageGeneration getResult() {
		// get the first result
	}

	@Override
	public List<ImageGeneration> getResults() {...}

	@Override
	public ImageResponseMetadata getMetadata() {...}

    // other methods omitted

}

ImageResponse 类保存 AI 模型的输出,每个 ImageGeneration 实例含有一个来自单个提示的多个输出(潜在)。

The ImageResponse class holds the AI Model’s output, with each ImageGeneration instance containing one of potentially multiple outputs resulting from a single prompt.

ImageResponse 类还包含一个 ImageResponseMetadata 元数据,用于表示 AI 模型的响应。

The ImageResponse class also carries a ImageResponseMetadata metadata about the AI Model’s response.

ImageGeneration

最后, ImageGeneration 类扩展自 ModelResult 以表示输出响应和与该结果相关的元数据:

Finally, the ImageGeneration class extends from the ModelResult to represent the output response and related metadata about this result:

public class ImageGeneration implements ModelResult<Image> {

	private ImageGenerationMetadata imageGenerationMetadata;

	private Image image;

    	@Override
	public Image getOutput() {...}

	@Override
	public ImageGenerationMetadata getMetadata() {...}

    // other methods omitted

}

Available Implementations

ImageClient 实现为以下模型提供商提供:

ImageClient implementations are provided for the following Model providers:

API Docs

您可以找到 Javadoc here

You can find the Javadoc here.

Feedback and Contributions

该项目的 GitHub discussions 是发送反馈的一个好地方。

The project’s GitHub discussions is a great place to send feedback.