Commit 7467c241f360eaeeaddf340991ab747409c30a80
1 parent
844ae285
【增加】aiImage 增加 dall-e-2、dall-e-3
Showing
5 changed files
with
85 additions
and
4 deletions
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/AiImageController.java
| ... | ... | @@ -34,6 +34,6 @@ public class AiImageController { |
| 34 | 34 | public SseEmitter dallDrawing(@Validated @ModelAttribute AiImageDallDrawingReq req) { |
| 35 | 35 | Utf8SseEmitter sseEmitter = new Utf8SseEmitter(); |
| 36 | 36 | aiImageService.dallDrawing(req, sseEmitter); |
| 37 | - return null; | |
| 37 | + return sseEmitter; | |
| 38 | 38 | } |
| 39 | 39 | } | ... | ... |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/dal/dataobject/AiDrawingImageDO.java renamed to yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/dal/dataobject/AiImageDO.java
| ... | ... | @@ -17,15 +17,24 @@ import lombok.experimental.Accessors; |
| 17 | 17 | */ |
| 18 | 18 | @Data |
| 19 | 19 | @Accessors(chain = true) |
| 20 | -@TableName("ai_drawing_image") | |
| 21 | -public class AiDrawingImageDO extends BaseDO { | |
| 20 | +@TableName("ai_image") | |
| 21 | +public class AiImageDO extends BaseDO { | |
| 22 | 22 | |
| 23 | 23 | @TableId(type = IdType.AUTO) |
| 24 | 24 | private Long id; |
| 25 | 25 | |
| 26 | + @Schema(description = "用户id") | |
| 27 | + private Long userId; | |
| 28 | + | |
| 26 | 29 | @Schema(description = "提示词") |
| 27 | 30 | private String prompt; |
| 28 | 31 | |
| 32 | + @Schema(description = "模型") | |
| 33 | + private String modal; | |
| 34 | + | |
| 35 | + @Schema(description = "生成图像的尺寸大小。对于dall-e-2模型,尺寸可为256x256, 512x512, 或 1024x1024。对于dall-e-3模型,尺寸可为1024x1024, 1792x1024, 或 1024x1792。") | |
| 36 | + private String size; | |
| 37 | + | |
| 29 | 38 | @Schema(description = "绘画状态:提交、排队、绘画中、绘画完成、绘画失败") |
| 30 | 39 | private String drawingStatus; |
| 31 | 40 | ... | ... |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/mapper/AiImageMapper.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.ai.mapper; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; | |
| 4 | +import cn.iocoder.yudao.module.ai.dal.dataobject.AiImageDO; | |
| 5 | +import org.apache.ibatis.annotations.Mapper; | |
| 6 | +import org.springframework.stereotype.Repository; | |
| 7 | + | |
| 8 | +/** | |
| 9 | + * ai image | |
| 10 | + * | |
| 11 | + * @author fansili | |
| 12 | + * @time 2024/4/28 14:01 | |
| 13 | + * @since 1.0 | |
| 14 | + */ | |
| 15 | +@Repository | |
| 16 | +@Mapper | |
| 17 | +public interface AiImageMapper extends BaseMapperX<AiImageDO> { | |
| 18 | + | |
| 19 | + | |
| 20 | +} | ... | ... |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/impl/AiImageServiceImpl.java
| 1 | 1 | package cn.iocoder.yudao.module.ai.service.impl; |
| 2 | 2 | |
| 3 | +import cn.iocoder.yudao.framework.ai.image.Image; | |
| 4 | +import cn.iocoder.yudao.framework.ai.image.ImageGeneration; | |
| 5 | +import cn.iocoder.yudao.framework.ai.image.ImagePrompt; | |
| 6 | +import cn.iocoder.yudao.framework.ai.image.ImageResponse; | |
| 7 | +import cn.iocoder.yudao.framework.ai.imageopenai.OpenAiImageClient; | |
| 8 | +import cn.iocoder.yudao.framework.ai.imageopenai.OpenAiImageModelEnum; | |
| 9 | +import cn.iocoder.yudao.framework.ai.imageopenai.OpenAiImageOptions; | |
| 10 | +import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; | |
| 3 | 11 | import cn.iocoder.yudao.module.ai.controller.Utf8SseEmitter; |
| 12 | +import cn.iocoder.yudao.module.ai.dal.dataobject.AiImageDO; | |
| 13 | +import cn.iocoder.yudao.module.ai.mapper.AiImageMapper; | |
| 4 | 14 | import cn.iocoder.yudao.module.ai.service.AiImageService; |
| 5 | 15 | import cn.iocoder.yudao.module.ai.vo.AiImageDallDrawingReq; |
| 6 | 16 | import lombok.AllArgsConstructor; |
| 7 | 17 | import lombok.extern.slf4j.Slf4j; |
| 18 | +import org.springframework.http.MediaType; | |
| 8 | 19 | import org.springframework.stereotype.Service; |
| 9 | 20 | |
| 21 | +import java.io.IOException; | |
| 22 | + | |
| 10 | 23 | /** |
| 11 | 24 | * ai 作图 |
| 12 | 25 | * |
| ... | ... | @@ -19,9 +32,39 @@ import org.springframework.stereotype.Service; |
| 19 | 32 | @Slf4j |
| 20 | 33 | public class AiImageServiceImpl implements AiImageService { |
| 21 | 34 | |
| 35 | + private final AiImageMapper aiImageMapper; | |
| 36 | + private final OpenAiImageClient openAiImageClient; | |
| 22 | 37 | |
| 23 | 38 | @Override |
| 24 | 39 | public void dallDrawing(AiImageDallDrawingReq req, Utf8SseEmitter sseEmitter) { |
| 25 | - | |
| 40 | + // 获取 model | |
| 41 | + OpenAiImageModelEnum openAiImageModelEnum = OpenAiImageModelEnum.valueOfModel(req.getModal()); | |
| 42 | + // | |
| 43 | + OpenAiImageOptions openAiImageOptions = new OpenAiImageOptions(); | |
| 44 | + openAiImageOptions.setModel(openAiImageModelEnum); | |
| 45 | + ImageResponse imageResponse = openAiImageClient.call(new ImagePrompt(req.getPrompt(), openAiImageOptions)); | |
| 46 | + // 发送 | |
| 47 | + ImageGeneration imageGeneration = imageResponse.getResult(); | |
| 48 | + try { | |
| 49 | + sseEmitter.send(imageGeneration, MediaType.APPLICATION_JSON); | |
| 50 | + } catch (IOException e) { | |
| 51 | + throw new RuntimeException(e); | |
| 52 | + } finally { | |
| 53 | + // 发送 complete | |
| 54 | + sseEmitter.complete(); | |
| 55 | + } | |
| 56 | + Long loginUserId = SecurityFrameworkUtils.getLoginUserId(); | |
| 57 | + // | |
| 58 | + // 保存数据库 | |
| 59 | + Image output = imageGeneration.getOutput(); | |
| 60 | + String imageUrl = output.getUrl(); | |
| 61 | + AiImageDO aiImageDO = new AiImageDO(); | |
| 62 | + aiImageDO.setId(null); | |
| 63 | + aiImageDO.setPrompt(req.getPrompt()); | |
| 64 | + aiImageDO.setSize(req.getSize()); | |
| 65 | + aiImageDO.setModal(req.getModal()); | |
| 66 | + aiImageDO.setUserId(loginUserId); | |
| 67 | + aiImageDO.setDrawingImageUrl(imageUrl); | |
| 68 | + aiImageMapper.insert(aiImageDO); | |
| 26 | 69 | } |
| 27 | 70 | } | ... | ... |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/vo/AiImageDallDrawingReq.java
| ... | ... | @@ -19,4 +19,13 @@ public class AiImageDallDrawingReq { |
| 19 | 19 | @Schema(description = "提示词") |
| 20 | 20 | @NotNull(message = "提示词不能为空!") |
| 21 | 21 | private String prompt; |
| 22 | + | |
| 23 | + @Schema(description = "模型") | |
| 24 | + @NotNull(message = "模型不能为空") | |
| 25 | + private String modal; | |
| 26 | + | |
| 27 | + @Schema(description = "生成图像的尺寸大小。对于dall-e-2模型,尺寸可为256x256, 512x512, 或 1024x1024。对于dall-e-3模型,尺寸可为1024x1024, 1792x1024, 或 1024x1792。") | |
| 28 | + @NotNull(message = "size不能为空!") | |
| 29 | + private String size; | |
| 30 | + | |
| 22 | 31 | } | ... | ... |