Commit 0311c77e7b9a9b31aec1612013e8fae1c7aca5a0

Authored by cherishsince
1 parent ad46eb57

【优化】处理 openAi image 返回错误信息

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 2  
3 3 import cn.hutool.core.bean.BeanUtil;
4 4 import cn.hutool.core.codec.Base64;
  5 +import cn.hutool.core.util.StrUtil;
5 6 import cn.hutool.http.HttpUtil;
6 7 import cn.iocoder.yudao.framework.ai.chat.ChatException;
7 8 import cn.iocoder.yudao.framework.ai.chatyiyan.exception.YiYanApiException;
  9 +import cn.iocoder.yudao.framework.ai.exception.AiException;
8 10 import cn.iocoder.yudao.framework.ai.image.*;
9 11 import cn.iocoder.yudao.framework.ai.imageopenai.api.OpenAiImageRequest;
10 12 import cn.iocoder.yudao.framework.ai.imageopenai.api.OpenAiImageResponse;
... ... @@ -69,6 +71,10 @@ public class OpenAiImageClient implements ImageClient {
69 71 request.setSize(openAiImageOptions.getSize());
70 72 // 发送请求
71 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 78 return new ImageResponse(response.getData().stream().map(res -> {
73 79 byte[] bytes = HttpUtil.downloadBytes(res.getUrl());
74 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 17  
18 18 private long created;
19 19 private List<Item> data;
  20 + private Error error;
20 21  
21 22 @Data
22 23 @Accessors(chain = true)
... ... @@ -26,4 +27,13 @@ public class OpenAiImageResponse {
26 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 }
... ...