From 6e3f34b0dbfc57d448c535d05fb12857e94dcc74 Mon Sep 17 00:00:00 2001 From: cherishsince Date: Tue, 7 May 2024 18:00:30 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BC=98=E5=8C=96=E3=80=91=E9=87=8D?= =?UTF-8?q?=E6=96=B0=E9=80=82=E9=85=8D=20dall2=E5=92=8Cdall3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/image/AiImageController.java | 14 +++--- ...gReq.java => AiImageDallDrawingReqVO.java} | 4 +- .../image/vo/AiImageDallDrawingRespVO.java | 45 +++++++++++++++++++ .../module/ai/convert/AiImageConvert.java | 27 +++++++++++ .../module/ai/service/AiImageService.java | 5 ++- .../ai/service/impl/AiImageServiceImpl.java | 30 ++++++++----- .../src/main/resources/http/image.http | 2 +- 7 files changed, 104 insertions(+), 23 deletions(-) rename yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/image/vo/{AiImageDallDrawingReq.java => AiImageDallDrawingReqVO.java} (88%) create mode 100644 yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/image/vo/AiImageDallDrawingRespVO.java create mode 100644 yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/convert/AiImageConvert.java diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/image/AiImageController.java b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/image/AiImageController.java index 274391777..982d43080 100644 --- a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/image/AiImageController.java +++ b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/image/AiImageController.java @@ -1,7 +1,8 @@ package cn.iocoder.yudao.module.ai.controller.admin.image; import cn.iocoder.yudao.framework.common.pojo.CommonResult; -import cn.iocoder.yudao.module.ai.controller.admin.image.vo.AiImageDallDrawingReq; +import cn.iocoder.yudao.module.ai.controller.admin.image.vo.AiImageDallDrawingReqVO; +import cn.iocoder.yudao.module.ai.controller.admin.image.vo.AiImageDallDrawingRespVO; import cn.iocoder.yudao.module.ai.controller.admin.image.vo.AiImageMidjourneyReq; import cn.iocoder.yudao.module.ai.service.AiImageService; import io.swagger.v3.oas.annotations.Operation; @@ -14,6 +15,8 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; + // TODO @芋艿:整理接口定义 /** * ai作图 @@ -33,17 +36,14 @@ public class AiImageController { @Operation(summary = "dall2/dall3绘画", description = "openAi dall3是付费的!") @PostMapping("/dallDrawing") - public void dallDrawing(@Validated @RequestBody AiImageDallDrawingReq req) { -// Utf8SseEmitter sseEmitter = new Utf8SseEmitter(); -// aiImageService.dallDrawing(req, sseEmitter); -// return sseEmitter; - + public AiImageDallDrawingRespVO dallDrawing(@Validated @RequestBody AiImageDallDrawingReqVO req) { + return aiImageService.dallDrawing(req); } @Operation(summary = "midjourney", description = "midjourney图片绘画流程:1、提交任务 2、获取完成的任务 3、选择对应功能 4、获取最终结果") @PostMapping("/midjourney") public CommonResult midjourney(@Validated @RequestBody AiImageMidjourneyReq req) { aiImageService.midjourney(req); - return CommonResult.success(null); + return success(null); } } diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/image/vo/AiImageDallDrawingReq.java b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/image/vo/AiImageDallDrawingReqVO.java similarity index 88% rename from yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/image/vo/AiImageDallDrawingReq.java rename to yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/image/vo/AiImageDallDrawingReqVO.java index 42887b7d3..3973eb2e9 100644 --- a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/image/vo/AiImageDallDrawingReq.java +++ b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/image/vo/AiImageDallDrawingReqVO.java @@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.ai.controller.admin.image.vo; import io.swagger.v3.oas.annotations.media.Schema; import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Size; import lombok.Data; import lombok.experimental.Accessors; @@ -14,10 +15,11 @@ import lombok.experimental.Accessors; */ @Data @Accessors(chain = true) -public class AiImageDallDrawingReq { +public class AiImageDallDrawingReqVO { @Schema(description = "提示词") @NotNull(message = "提示词不能为空!") + @Size(max = 1200, message = "提示词最大1200") private String prompt; @Schema(description = "模型") diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/image/vo/AiImageDallDrawingRespVO.java b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/image/vo/AiImageDallDrawingRespVO.java new file mode 100644 index 000000000..7e480a8f0 --- /dev/null +++ b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/image/vo/AiImageDallDrawingRespVO.java @@ -0,0 +1,45 @@ +package cn.iocoder.yudao.module.ai.controller.admin.image.vo; + +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Size; +import lombok.Data; +import lombok.experimental.Accessors; + +/** + * dall2/dall2 绘画 + * + * @author fansili + * @time 2024/4/25 16:24 + * @since 1.0 + */ +@Data +@Accessors(chain = true) +public class AiImageDallDrawingRespVO { + + + @Schema(description = "提示词") + @NotNull(message = "提示词不能为空!") + @Size(max = 1200, message = "提示词最大1200") + private String prompt; + + @Schema(description = "模型") + @NotNull(message = "模型不能为空") + private String modal; + + @Schema(description = "风格") + private String style; + + @Schema(description = "图片size 1024x1024 ...") + private String size; + + @Schema(description = "可以访问图像的URL。") + private String url; + + @Schema(description = "图片base64。") + private String base64; + + @Schema(description = "错误信息。") + private String errorMessage; + +} diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/convert/AiImageConvert.java b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/convert/AiImageConvert.java new file mode 100644 index 000000000..5f2d18564 --- /dev/null +++ b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/convert/AiImageConvert.java @@ -0,0 +1,27 @@ +package cn.iocoder.yudao.module.ai.convert; + +import cn.iocoder.yudao.module.ai.controller.admin.image.vo.AiImageDallDrawingReqVO; +import cn.iocoder.yudao.module.ai.controller.admin.image.vo.AiImageDallDrawingRespVO; +import org.mapstruct.Mapper; +import org.mapstruct.factory.Mappers; + +/** + * ai image convert + * + * @author fansili + * @time 2024/4/18 16:39 + * @since 1.0 + */ +@Mapper +public interface AiImageConvert { + + AiImageConvert INSTANCE = Mappers.getMapper(AiImageConvert.class); + + /** + * 转换 - AiImageDallDrawingRespVO + * + * @param req + * @return + */ + AiImageDallDrawingRespVO convertAiImageDallDrawingRespVO(AiImageDallDrawingReqVO req); +} diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/AiImageService.java b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/AiImageService.java index cf95483d2..04768757a 100644 --- a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/AiImageService.java +++ b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/AiImageService.java @@ -1,6 +1,7 @@ package cn.iocoder.yudao.module.ai.service; -import cn.iocoder.yudao.module.ai.controller.admin.image.vo.AiImageDallDrawingReq; +import cn.iocoder.yudao.module.ai.controller.admin.image.vo.AiImageDallDrawingReqVO; +import cn.iocoder.yudao.module.ai.controller.admin.image.vo.AiImageDallDrawingRespVO; import cn.iocoder.yudao.module.ai.controller.admin.image.vo.AiImageMidjourneyReq; /** @@ -17,7 +18,7 @@ public interface AiImageService { * * @param req */ - void dallDrawing(AiImageDallDrawingReq req); + AiImageDallDrawingRespVO dallDrawing(AiImageDallDrawingReqVO req); /** * midjourney 图片生成 diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/impl/AiImageServiceImpl.java b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/impl/AiImageServiceImpl.java index 5bacf9da5..90aaf0322 100644 --- a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/impl/AiImageServiceImpl.java +++ b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/impl/AiImageServiceImpl.java @@ -14,8 +14,10 @@ import cn.iocoder.yudao.framework.ai.midjourney.webSocket.WssNotify; import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; import cn.iocoder.yudao.module.ai.ErrorCodeConstants; -import cn.iocoder.yudao.module.ai.controller.admin.image.vo.AiImageDallDrawingReq; +import cn.iocoder.yudao.module.ai.controller.admin.image.vo.AiImageDallDrawingReqVO; +import cn.iocoder.yudao.module.ai.controller.admin.image.vo.AiImageDallDrawingRespVO; import cn.iocoder.yudao.module.ai.controller.admin.image.vo.AiImageMidjourneyReq; +import cn.iocoder.yudao.module.ai.convert.AiImageConvert; import cn.iocoder.yudao.module.ai.dal.dataobject.image.AiImageDO; import cn.iocoder.yudao.module.ai.dal.mysql.AiImageMapper; import cn.iocoder.yudao.module.ai.enums.AiChatDrawingStatusEnum; @@ -60,31 +62,35 @@ public class AiImageServiceImpl implements AiImageService { } @Override - public void dallDrawing(AiImageDallDrawingReq req) { + public AiImageDallDrawingRespVO dallDrawing(AiImageDallDrawingReqVO req) { // 获取 model OpenAiImageModelEnum openAiImageModelEnum = OpenAiImageModelEnum.valueOfModel(req.getModal()); OpenAiImageStyleEnum openAiImageStyleEnum = OpenAiImageStyleEnum.valueOfStyle(req.getStyle()); - // - OpenAiImageOptions openAiImageOptions = new OpenAiImageOptions(); - openAiImageOptions.setModel(openAiImageModelEnum); - openAiImageOptions.setStyle(openAiImageStyleEnum); - openAiImageOptions.setSize(req.getSize()); - ImageResponse imageResponse; + // 转换 AiImageDallDrawingRespVO + AiImageDallDrawingRespVO respVO = AiImageConvert.INSTANCE.convertAiImageDallDrawingRespVO(req); try { - imageResponse = openAiImageClient.call(new ImagePrompt(req.getPrompt(), openAiImageOptions)); + // 转换openai 参数 + OpenAiImageOptions openAiImageOptions = new OpenAiImageOptions(); + openAiImageOptions.setModel(openAiImageModelEnum); + openAiImageOptions.setStyle(openAiImageStyleEnum); + openAiImageOptions.setSize(req.getSize()); + ImageResponse imageResponse = openAiImageClient.call(new ImagePrompt(req.getPrompt(), openAiImageOptions)); // 发送 ImageGeneration imageGeneration = imageResponse.getResult(); - // 发送信息 -// sendSseEmitter(sseEmitter, imageGeneration); // 保存数据库 doSave(req.getPrompt(), req.getSize(), req.getModal(), imageGeneration.getOutput().getUrl(), AiChatDrawingStatusEnum.COMPLETE, null); + // 返回 flex + respVO.setUrl(imageGeneration.getOutput().getUrl()); + respVO.setBase64(imageGeneration.getOutput().getB64Json()); + return respVO; } catch (AiException aiException) { // 保存数据库 doSave(req.getPrompt(), req.getSize(), req.getModal(), null, AiChatDrawingStatusEnum.FAIL, aiException.getMessage()); // 发送错误信息 -// sendSseEmitter(sseEmitter, aiException.getMessage()); + respVO.setErrorMessage(aiException.getMessage()); + return respVO; } } diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/resources/http/image.http b/yudao-module-ai/yudao-module-ai-biz/src/main/resources/http/image.http index a5282df16..dda3f04f9 100644 --- a/yudao-module-ai/yudao-module-ai-biz/src/main/resources/http/image.http +++ b/yudao-module-ai/yudao-module-ai-biz/src/main/resources/http/image.http @@ -1,7 +1,7 @@ ### chat dallDrawing -POST {{baseUrl}}/ai/image/dallDrawing +POST {{baseUrl}}/admin-api/ai/image/dallDrawing Content-Type: application/json Authorization: {{token}}