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

This commit is contained in:
cherishsince 2024-04-28 17:25:47 +08:00
parent ad46eb57a5
commit 0311c77e7b
2 changed files with 16 additions and 0 deletions

View File

@ -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);

View File

@ -17,6 +17,7 @@ public class OpenAiImageResponse {
private long created;
private List<Item> 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;
}
}