【优化】修复imageSize不生效问题,增加modal、style、size 适配

This commit is contained in:
cherishsince 2024-04-28 16:36:35 +08:00
parent 330696fb90
commit 2cbb6ac8d5

View File

@ -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,6 +59,25 @@ public class OpenAiImageClient implements ImageClient {
@Override
public ImageResponse call(ImagePrompt imagePrompt) {
return this.retryTemplate.execute(ctx -> {
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 -> {
byte[] bytes = HttpUtil.downloadBytes(res.getUrl());
String base64 = Base64.encode(bytes);
return new ImageGeneration(new Image(res.getUrl(), base64));
}).toList());
});
}
private @NotNull OpenAiImageOptions getOpenAiImageOptions(ImagePrompt imagePrompt) {
// 检查是否配置了 OpenAiImageOptions
if (defaultImageOptions == null && imagePrompt.getOptions() == null) {
throw new ChatException("OpenAiImageOptions 未配置参数!");
@ -69,18 +89,7 @@ public class OpenAiImageClient implements ImageClient {
}
// 转换 OpenAiImageOptions
OpenAiImageOptions openAiImageOptions = (OpenAiImageOptions) useImageOptions;
// 创建请求
OpenAiImageRequest request = new OpenAiImageRequest();
BeanUtil.copyProperties(openAiImageOptions, request);
request.setPrompt(imagePrompt.getInstructions().get(0).getText());
// 发送请求
OpenAiImageResponse response = openAiImageApi.createImage(request);
return new ImageResponse(response.getData().stream().map(res -> {
byte[] bytes = HttpUtil.downloadBytes(res.getUrl());
String base64 = Base64.encode(bytes);
return new ImageGeneration(new Image(res.getUrl(), base64));
}).toList());
});
return openAiImageOptions;
}
}