Commit 0311c77e7b9a9b31aec1612013e8fae1c7aca5a0
1 parent
ad46eb57
【优化】处理 openAi image 返回错误信息
Showing
2 changed files
with
16 additions
and
0 deletions
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; | @@ -2,9 +2,11 @@ package cn.iocoder.yudao.framework.ai.imageopenai; | ||
| 2 | 2 | ||
| 3 | import cn.hutool.core.bean.BeanUtil; | 3 | import cn.hutool.core.bean.BeanUtil; |
| 4 | import cn.hutool.core.codec.Base64; | 4 | import cn.hutool.core.codec.Base64; |
| 5 | +import cn.hutool.core.util.StrUtil; | ||
| 5 | import cn.hutool.http.HttpUtil; | 6 | import cn.hutool.http.HttpUtil; |
| 6 | import cn.iocoder.yudao.framework.ai.chat.ChatException; | 7 | import cn.iocoder.yudao.framework.ai.chat.ChatException; |
| 7 | import cn.iocoder.yudao.framework.ai.chatyiyan.exception.YiYanApiException; | 8 | import cn.iocoder.yudao.framework.ai.chatyiyan.exception.YiYanApiException; |
| 9 | +import cn.iocoder.yudao.framework.ai.exception.AiException; | ||
| 8 | import cn.iocoder.yudao.framework.ai.image.*; | 10 | import cn.iocoder.yudao.framework.ai.image.*; |
| 9 | import cn.iocoder.yudao.framework.ai.imageopenai.api.OpenAiImageRequest; | 11 | import cn.iocoder.yudao.framework.ai.imageopenai.api.OpenAiImageRequest; |
| 10 | import cn.iocoder.yudao.framework.ai.imageopenai.api.OpenAiImageResponse; | 12 | import cn.iocoder.yudao.framework.ai.imageopenai.api.OpenAiImageResponse; |
| @@ -69,6 +71,10 @@ public class OpenAiImageClient implements ImageClient { | @@ -69,6 +71,10 @@ public class OpenAiImageClient implements ImageClient { | ||
| 69 | request.setSize(openAiImageOptions.getSize()); | 71 | request.setSize(openAiImageOptions.getSize()); |
| 70 | // 发送请求 | 72 | // 发送请求 |
| 71 | OpenAiImageResponse response = openAiImageApi.createImage(request); | 73 | OpenAiImageResponse response = openAiImageApi.createImage(request); |
| 74 | + if (response.getError() != null && !StrUtil.isBlank(response.getError().getMessage())) { | ||
| 75 | + // code 错误没有编码,就先根据 message 来进行判断 | ||
| 76 | + throw new AiException("openAi 图片生成失败! " + response.getError().getMessage()); | ||
| 77 | + } | ||
| 72 | return new ImageResponse(response.getData().stream().map(res -> { | 78 | return new ImageResponse(response.getData().stream().map(res -> { |
| 73 | byte[] bytes = HttpUtil.downloadBytes(res.getUrl()); | 79 | byte[] bytes = HttpUtil.downloadBytes(res.getUrl()); |
| 74 | String base64 = Base64.encode(bytes); | 80 | String base64 = Base64.encode(bytes); |
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 { | @@ -17,6 +17,7 @@ public class OpenAiImageResponse { | ||
| 17 | 17 | ||
| 18 | private long created; | 18 | private long created; |
| 19 | private List<Item> data; | 19 | private List<Item> data; |
| 20 | + private Error error; | ||
| 20 | 21 | ||
| 21 | @Data | 22 | @Data |
| 22 | @Accessors(chain = true) | 23 | @Accessors(chain = true) |
| @@ -26,4 +27,13 @@ public class OpenAiImageResponse { | @@ -26,4 +27,13 @@ public class OpenAiImageResponse { | ||
| 26 | private String b64_json; | 27 | private String b64_json; |
| 27 | 28 | ||
| 28 | } | 29 | } |
| 30 | + | ||
| 31 | + @Data | ||
| 32 | + @Accessors(chain = true) | ||
| 33 | + public static class Error { | ||
| 34 | + private String code; | ||
| 35 | + private String message; | ||
| 36 | + private String param; | ||
| 37 | + private String type; | ||
| 38 | + } | ||
| 29 | } | 39 | } |