mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-10-30 01:38:43 +08:00 
			
		
		
		
	【优化】处理 openAi image 返回错误信息
This commit is contained in:
		| @@ -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); | ||||
|   | ||||
| @@ -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; | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 cherishsince
					cherishsince