mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2025-02-07 21:10:05 +08:00
【优化】处理 openAi image 返回错误信息
This commit is contained in:
parent
ad46eb57a5
commit
0311c77e7b
@ -2,9 +2,11 @@ package cn.iocoder.yudao.framework.ai.imageopenai;
|
|||||||
|
|
||||||
import cn.hutool.core.bean.BeanUtil;
|
import cn.hutool.core.bean.BeanUtil;
|
||||||
import cn.hutool.core.codec.Base64;
|
import cn.hutool.core.codec.Base64;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.http.HttpUtil;
|
import cn.hutool.http.HttpUtil;
|
||||||
import cn.iocoder.yudao.framework.ai.chat.ChatException;
|
import cn.iocoder.yudao.framework.ai.chat.ChatException;
|
||||||
import cn.iocoder.yudao.framework.ai.chatyiyan.exception.YiYanApiException;
|
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.image.*;
|
||||||
import cn.iocoder.yudao.framework.ai.imageopenai.api.OpenAiImageRequest;
|
import cn.iocoder.yudao.framework.ai.imageopenai.api.OpenAiImageRequest;
|
||||||
import cn.iocoder.yudao.framework.ai.imageopenai.api.OpenAiImageResponse;
|
import cn.iocoder.yudao.framework.ai.imageopenai.api.OpenAiImageResponse;
|
||||||
@ -69,6 +71,10 @@ public class OpenAiImageClient implements ImageClient {
|
|||||||
request.setSize(openAiImageOptions.getSize());
|
request.setSize(openAiImageOptions.getSize());
|
||||||
// 发送请求
|
// 发送请求
|
||||||
OpenAiImageResponse response = openAiImageApi.createImage(request);
|
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 -> {
|
return new ImageResponse(response.getData().stream().map(res -> {
|
||||||
byte[] bytes = HttpUtil.downloadBytes(res.getUrl());
|
byte[] bytes = HttpUtil.downloadBytes(res.getUrl());
|
||||||
String base64 = Base64.encode(bytes);
|
String base64 = Base64.encode(bytes);
|
||||||
|
@ -17,6 +17,7 @@ public class OpenAiImageResponse {
|
|||||||
|
|
||||||
private long created;
|
private long created;
|
||||||
private List<Item> data;
|
private List<Item> data;
|
||||||
|
private Error error;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@Accessors(chain = true)
|
@Accessors(chain = true)
|
||||||
@ -26,4 +27,13 @@ public class OpenAiImageResponse {
|
|||||||
private String b64_json;
|
private String b64_json;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Accessors(chain = true)
|
||||||
|
public static class Error {
|
||||||
|
private String code;
|
||||||
|
private String message;
|
||||||
|
private String param;
|
||||||
|
private String type;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user