Spring AI从入门到放弃(3)

1. Embeddings Model API

2. Image Model API

Spring Image Model API 被设计为一个简单易移植的接口,用于与专门用于图像生成的各种AI进行交互,允许开发人员以最少的代码更改在不同的图像相关模型之间切换。
Spring Image API 构建在 Spring AI 通用模型 API 之上,提供特定于图像的抽象和实现。

3.1 一些API

主要是一些接口,具体是实现类需要根据使用的模型进行实现。

1.ImageModel

@FunctionalInterface
public interface ImageModel extends Model<ImagePrompt, ImageResponse> {

	ImageResponse call(ImagePrompt request);

}

2.ImagePompt

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
}

3.ImageMessage

public class ImageMessage {

	private String text;

	private Float weight;

    public String getText() {...}

	public Float getWeight() {...}

   // constructors and utility methods omitted
}

3.ImageOptions

public interface ImageOptions extends ModelOptions {

	Integer getN();

	String getModel();

	Integer getWidth();

	Integer getHeight();

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

}

4.ImageResponse

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

}

5.ImageGeneration

public class ImageGeneration implements ModelResult<Image> {

	private ImageGenerationMetadata imageGenerationMetadata;

	private Image image;

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

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

    // other methods omitted

}

3.2 OpenAI

1. 依赖

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>

2. Image Model Properties

  1. Image Generation Properties
  • spring.ai.openai.image.enabled : 是否启用OpenAI Image Model
  • spring.ai.openai.image.base-url : 提供图片Model Url,用于覆盖spring.ai.openai.base-url
  • spring.ai.openai.image.api-key : 提供图片Model Key,用于覆盖spring.ai.openai.api-key
  • spring.ai.openai.image.options.n : 要生成的图像数量,必须介于1-10之间,dall-e-3,仅支持n=1
  • spring.ai.openai.image.options.model : 使用的模型,默认使用的是DALL_E_3
  • spring.ai.openai.image.options.quality : 生成的图片的质量,HD 创建的图像具有更精细的细节和更高的图像一致性。仅 dall-e-3 支持此参数。
  • spring.ai.openai.image.options.response_format :生成的图片的格式,仅支持 url or base64
  • spring.ai.openai.image.options.size : 生成的图像的大小。对于 dall-e-2,必须是 256x256、512x512 或 1024x1024 之一。对于 dall-e-3 型号,必须是 1024x1024、1792x1024 或 1024x1792 之一
  • spring.ai.openai.image.options.size_width : 生成的图像的宽度。对于 dall-e-2,必须是 256、512 或 1024 之一
  • spring.ai.openai.image.options.size_height: 生成的图像的高度。对于 dall-e-2,必须是 256、512 或 1024 之一
  • spring.ai.openai.image.options.style : Must be one of vivid or natural,也是仅有dall-e-3支持
  • spring.ai.openai.image.options.user : 最终用户的唯一标识符。
  1. Retry Properties
    OpenAI重试机制的相关配置
  • spring.ai.retry.max-attempts: 重试的最大次数, 默认为10次
  • spring.ai.retry.on-client-error: 如果为 false,则引发 NonTransientAiException,并且不要尝试重试 4xx 客户端错误代码
  • spring.ai.retry.backoff.initial-interval: 重试尝试的最大次数
  • spring.ai.retry.backoff.multiplier: 回退间隔乘数
  • spring.ai.retry.backoff.max-interval: 最大回退持续时间
  • spring.ai.retry.exclude-on-http-codes:不应触发重试的 HTTP 状态代码列表(例如,抛出 NonTransientAiException)
  • spring.ai.retry.on-http-codes: 应触发重试的 HTTP 状态代码列表(例如,抛出 TransientAiException)
  1. Connection Properties
  • spring.ai.openai.base-url: Oneapi Url
  • spring.ai.openai.api-key: API key

3.Runtime Options

@GetMapping("/chat02")
public ImageResponse chat02(@RequestParam("message") String message) {
    return imageModel
            .call(new ImagePrompt(message,
                    OpenAiImageOptions
                            .builder()
                            .withN(1)
                            .withHeight(1024)
                            .withWidth(1024)
                            .build()));
}

3. Audio Model API

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值