From 2cbb6ac8d588805490a52f401d111e2b20bd326b Mon Sep 17 00:00:00 2001 From: cherishsince Date: Sun, 28 Apr 2024 16:36:35 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BC=98=E5=8C=96=E3=80=91=E4=BF=AE?= =?UTF-8?q?=E5=A4=8DimageSize=E4=B8=8D=E7=94=9F=E6=95=88=E9=97=AE=E9=A2=98?= =?UTF-8?q?=EF=BC=8C=E5=A2=9E=E5=8A=A0modal=E3=80=81style=E3=80=81size=20?= =?UTF-8?q?=E9=80=82=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ai/imageopenai/OpenAiImageClient.java | 31 ++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/imageopenai/OpenAiImageClient.java b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/imageopenai/OpenAiImageClient.java index a1083cd18..c5ae757d1 100644 --- a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/imageopenai/OpenAiImageClient.java +++ b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/imageopenai/OpenAiImageClient.java @@ -9,6 +9,7 @@ import cn.iocoder.yudao.framework.ai.image.*; import cn.iocoder.yudao.framework.ai.imageopenai.api.OpenAiImageRequest; import cn.iocoder.yudao.framework.ai.imageopenai.api.OpenAiImageResponse; import lombok.extern.slf4j.Slf4j; +import org.jetbrains.annotations.NotNull; import org.springframework.retry.RetryCallback; import org.springframework.retry.RetryContext; import org.springframework.retry.RetryListener; @@ -58,21 +59,14 @@ public class OpenAiImageClient implements ImageClient { @Override public ImageResponse call(ImagePrompt imagePrompt) { return this.retryTemplate.execute(ctx -> { - // 检查是否配置了 OpenAiImageOptions - if (defaultImageOptions == null && imagePrompt.getOptions() == null) { - throw new ChatException("OpenAiImageOptions 未配置参数!"); - } - // 优先使用 request 中的 ImageOptions - ImageOptions useImageOptions = imagePrompt.getOptions() == null ? defaultImageOptions : imagePrompt.getOptions(); - if (!(useImageOptions instanceof OpenAiImageOptions)) { - throw new ChatException("配置信息不正确,传入的必须是 OpenAiImageOptions!"); - } - // 转换 OpenAiImageOptions - OpenAiImageOptions openAiImageOptions = (OpenAiImageOptions) useImageOptions; + OpenAiImageOptions openAiImageOptions = getOpenAiImageOptions(imagePrompt); // 创建请求 OpenAiImageRequest request = new OpenAiImageRequest(); BeanUtil.copyProperties(openAiImageOptions, request); request.setPrompt(imagePrompt.getInstructions().get(0).getText()); + request.setModel(openAiImageOptions.getModel()); + request.setStyle(openAiImageOptions.getStyle().getStyle()); + request.setSize(openAiImageOptions.getSize()); // 发送请求 OpenAiImageResponse response = openAiImageApi.createImage(request); return new ImageResponse(response.getData().stream().map(res -> { @@ -83,4 +77,19 @@ public class OpenAiImageClient implements ImageClient { }); } + private @NotNull OpenAiImageOptions getOpenAiImageOptions(ImagePrompt imagePrompt) { + // 检查是否配置了 OpenAiImageOptions + if (defaultImageOptions == null && imagePrompt.getOptions() == null) { + throw new ChatException("OpenAiImageOptions 未配置参数!"); + } + // 优先使用 request 中的 ImageOptions + ImageOptions useImageOptions = imagePrompt.getOptions() == null ? defaultImageOptions : imagePrompt.getOptions(); + if (!(useImageOptions instanceof OpenAiImageOptions)) { + throw new ChatException("配置信息不正确,传入的必须是 OpenAiImageOptions!"); + } + // 转换 OpenAiImageOptions + OpenAiImageOptions openAiImageOptions = (OpenAiImageOptions) useImageOptions; + return openAiImageOptions; + } + }