Commit 2cbb6ac8d588805490a52f401d111e2b20bd326b
1 parent
330696fb
【优化】修复imageSize不生效问题,增加modal、style、size 适配
Showing
1 changed file
with
20 additions
and
11 deletions
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.*; | @@ -9,6 +9,7 @@ import cn.iocoder.yudao.framework.ai.image.*; | ||
| 9 | import cn.iocoder.yudao.framework.ai.imageopenai.api.OpenAiImageRequest; | 9 | import cn.iocoder.yudao.framework.ai.imageopenai.api.OpenAiImageRequest; |
| 10 | import cn.iocoder.yudao.framework.ai.imageopenai.api.OpenAiImageResponse; | 10 | import cn.iocoder.yudao.framework.ai.imageopenai.api.OpenAiImageResponse; |
| 11 | import lombok.extern.slf4j.Slf4j; | 11 | import lombok.extern.slf4j.Slf4j; |
| 12 | +import org.jetbrains.annotations.NotNull; | ||
| 12 | import org.springframework.retry.RetryCallback; | 13 | import org.springframework.retry.RetryCallback; |
| 13 | import org.springframework.retry.RetryContext; | 14 | import org.springframework.retry.RetryContext; |
| 14 | import org.springframework.retry.RetryListener; | 15 | import org.springframework.retry.RetryListener; |
| @@ -58,21 +59,14 @@ public class OpenAiImageClient implements ImageClient { | @@ -58,21 +59,14 @@ public class OpenAiImageClient implements ImageClient { | ||
| 58 | @Override | 59 | @Override |
| 59 | public ImageResponse call(ImagePrompt imagePrompt) { | 60 | public ImageResponse call(ImagePrompt imagePrompt) { |
| 60 | return this.retryTemplate.execute(ctx -> { | 61 | return this.retryTemplate.execute(ctx -> { |
| 61 | - // 检查是否配置了 OpenAiImageOptions | ||
| 62 | - if (defaultImageOptions == null && imagePrompt.getOptions() == null) { | ||
| 63 | - throw new ChatException("OpenAiImageOptions 未配置参数!"); | ||
| 64 | - } | ||
| 65 | - // 优先使用 request 中的 ImageOptions | ||
| 66 | - ImageOptions useImageOptions = imagePrompt.getOptions() == null ? defaultImageOptions : imagePrompt.getOptions(); | ||
| 67 | - if (!(useImageOptions instanceof OpenAiImageOptions)) { | ||
| 68 | - throw new ChatException("配置信息不正确,传入的必须是 OpenAiImageOptions!"); | ||
| 69 | - } | ||
| 70 | - // 转换 OpenAiImageOptions | ||
| 71 | - OpenAiImageOptions openAiImageOptions = (OpenAiImageOptions) useImageOptions; | 62 | + OpenAiImageOptions openAiImageOptions = getOpenAiImageOptions(imagePrompt); |
| 72 | // 创建请求 | 63 | // 创建请求 |
| 73 | OpenAiImageRequest request = new OpenAiImageRequest(); | 64 | OpenAiImageRequest request = new OpenAiImageRequest(); |
| 74 | BeanUtil.copyProperties(openAiImageOptions, request); | 65 | BeanUtil.copyProperties(openAiImageOptions, request); |
| 75 | request.setPrompt(imagePrompt.getInstructions().get(0).getText()); | 66 | request.setPrompt(imagePrompt.getInstructions().get(0).getText()); |
| 67 | + request.setModel(openAiImageOptions.getModel()); | ||
| 68 | + request.setStyle(openAiImageOptions.getStyle().getStyle()); | ||
| 69 | + request.setSize(openAiImageOptions.getSize()); | ||
| 76 | // 发送请求 | 70 | // 发送请求 |
| 77 | OpenAiImageResponse response = openAiImageApi.createImage(request); | 71 | OpenAiImageResponse response = openAiImageApi.createImage(request); |
| 78 | return new ImageResponse(response.getData().stream().map(res -> { | 72 | return new ImageResponse(response.getData().stream().map(res -> { |
| @@ -83,4 +77,19 @@ public class OpenAiImageClient implements ImageClient { | @@ -83,4 +77,19 @@ public class OpenAiImageClient implements ImageClient { | ||
| 83 | }); | 77 | }); |
| 84 | } | 78 | } |
| 85 | 79 | ||
| 80 | + private @NotNull OpenAiImageOptions getOpenAiImageOptions(ImagePrompt imagePrompt) { | ||
| 81 | + // 检查是否配置了 OpenAiImageOptions | ||
| 82 | + if (defaultImageOptions == null && imagePrompt.getOptions() == null) { | ||
| 83 | + throw new ChatException("OpenAiImageOptions 未配置参数!"); | ||
| 84 | + } | ||
| 85 | + // 优先使用 request 中的 ImageOptions | ||
| 86 | + ImageOptions useImageOptions = imagePrompt.getOptions() == null ? defaultImageOptions : imagePrompt.getOptions(); | ||
| 87 | + if (!(useImageOptions instanceof OpenAiImageOptions)) { | ||
| 88 | + throw new ChatException("配置信息不正确,传入的必须是 OpenAiImageOptions!"); | ||
| 89 | + } | ||
| 90 | + // 转换 OpenAiImageOptions | ||
| 91 | + OpenAiImageOptions openAiImageOptions = (OpenAiImageOptions) useImageOptions; | ||
| 92 | + return openAiImageOptions; | ||
| 93 | + } | ||
| 94 | + | ||
| 86 | } | 95 | } |