From 0311c77e7b9a9b31aec1612013e8fae1c7aca5a0 Mon Sep 17 00:00:00 2001 From: cherishsince Date: Sun, 28 Apr 2024 17:25:47 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BC=98=E5=8C=96=E3=80=91=E5=A4=84?= =?UTF-8?q?=E7=90=86=20openAi=20image=20=E8=BF=94=E5=9B=9E=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/ai/imageopenai/OpenAiImageClient.java | 6 ++++++ .../ai/imageopenai/api/OpenAiImageResponse.java | 10 ++++++++++ 2 files changed, 16 insertions(+) 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 c5ae757d1..4dd536f99 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 @@ -2,9 +2,11 @@ package cn.iocoder.yudao.framework.ai.imageopenai; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.codec.Base64; +import cn.hutool.core.util.StrUtil; import cn.hutool.http.HttpUtil; import cn.iocoder.yudao.framework.ai.chat.ChatException; import cn.iocoder.yudao.framework.ai.chatyiyan.exception.YiYanApiException; +import cn.iocoder.yudao.framework.ai.exception.AiException; 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; @@ -69,6 +71,10 @@ public class OpenAiImageClient implements ImageClient { request.setSize(openAiImageOptions.getSize()); // 发送请求 OpenAiImageResponse response = openAiImageApi.createImage(request); + if (response.getError() != null && !StrUtil.isBlank(response.getError().getMessage())) { + // code 错误没有编码,就先根据 message 来进行判断 + throw new AiException("openAi 图片生成失败! " + response.getError().getMessage()); + } return new ImageResponse(response.getData().stream().map(res -> { byte[] bytes = HttpUtil.downloadBytes(res.getUrl()); String base64 = Base64.encode(bytes); diff --git a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/imageopenai/api/OpenAiImageResponse.java b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/imageopenai/api/OpenAiImageResponse.java index 1f4ab6152..02698963e 100644 --- a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/imageopenai/api/OpenAiImageResponse.java +++ b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/imageopenai/api/OpenAiImageResponse.java @@ -17,6 +17,7 @@ public class OpenAiImageResponse { private long created; private List data; + private Error error; @Data @Accessors(chain = true) @@ -26,4 +27,13 @@ public class OpenAiImageResponse { private String b64_json; } + + @Data + @Accessors(chain = true) + public static class Error { + private String code; + private String message; + private String param; + private String type; + } }