From 870b1bf3695cddd9abfe1f28d2f396fa5e3b0835 Mon Sep 17 00:00:00 2001 From: cherishsince Date: Wed, 10 Jul 2024 10:27:46 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E3=80=90=E5=A2=9E=E5=8A=A0=E3=80=91?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=80=9A=E4=B9=89=E4=B8=87=E8=B1=A1=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ai/image/TongYiImagesModelTests.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/image/TongYiImagesModelTests.java diff --git a/yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/image/TongYiImagesModelTests.java b/yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/image/TongYiImagesModelTests.java new file mode 100644 index 000000000..c927a0d57 --- /dev/null +++ b/yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/image/TongYiImagesModelTests.java @@ -0,0 +1,38 @@ +package cn.iocoder.yudao.framework.ai.image; + +import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesis; +import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisParam; +import com.alibaba.dashscope.aigc.imagesynthesis.ImageSynthesisResult; +import com.alibaba.dashscope.exception.NoApiKeyException; +import com.alibaba.dashscope.utils.Constants; +import com.alibaba.fastjson.JSON; +import org.junit.jupiter.api.Test; + +import java.util.Map; + +/** + * 通义万象 - 测试 + */ +public class TongYiImagesModelTests { + + @Test + public void imageCallTest() throws NoApiKeyException { + // 设置 api key + Constants.apiKey="sk-Zsd81gZYg7"; + ImageSynthesisParam param = + ImageSynthesisParam.builder() + .model(ImageSynthesis.Models.WANX_V1) + .n(4) + .size("1024*1024") + .prompt("雄鹰自由自在的在蓝天白云下飞翔") + .build(); + // 创建 ImageSynthesis + ImageSynthesis is = new ImageSynthesis(); + // 调用 call 生成 image + ImageSynthesisResult call = is.call(param); + System.err.println(JSON.toJSON(call)); + for (Map result : call.getOutput().getResults()) { + System.err.println("地址: " + result.get("url")); + } + } +} From 4c21ae32fede299aeeca1d4297924514261fb3ff Mon Sep 17 00:00:00 2001 From: YunaiV Date: Wed, 10 Jul 2024 12:59:21 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E3=80=90=E4=BB=A3=E7=A0=81=E8=AF=84?= =?UTF-8?q?=E5=AE=A1=E3=80=91AI=EF=BC=9A=E5=86=99=E4=BD=9C=E9=83=A8?= =?UTF-8?q?=E5=88=86=E7=9A=84=E5=BB=BA=E8=AE=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ai/enums/write/AiWriteTypeEnum.java | 2 +- .../ai/service/write/AiWriteServiceImpl.java | 23 +++++++++++-------- .../ai/image/TongYiImagesModelTests.java | 5 ++-- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/yudao-module-ai/yudao-module-ai-api/src/main/java/cn/iocoder/yudao/module/ai/enums/write/AiWriteTypeEnum.java b/yudao-module-ai/yudao-module-ai-api/src/main/java/cn/iocoder/yudao/module/ai/enums/write/AiWriteTypeEnum.java index 69989e5b6..18bf99710 100644 --- a/yudao-module-ai/yudao-module-ai-api/src/main/java/cn/iocoder/yudao/module/ai/enums/write/AiWriteTypeEnum.java +++ b/yudao-module-ai/yudao-module-ai-api/src/main/java/cn/iocoder/yudao/module/ai/enums/write/AiWriteTypeEnum.java @@ -32,7 +32,7 @@ public enum AiWriteTypeEnum implements IntArrayValuable { /** * 模版 */ - private final String template; + private final String prompt; public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(AiWriteTypeEnum::getType).toArray(); diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/write/AiWriteServiceImpl.java b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/write/AiWriteServiceImpl.java index e958f06f8..aa2a259a7 100644 --- a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/write/AiWriteServiceImpl.java +++ b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/write/AiWriteServiceImpl.java @@ -63,26 +63,26 @@ public class AiWriteServiceImpl implements AiWriteService { // 1.1 获取写作模型 尝试获取写作助手角色,如果没有则使用默认模型 AiChatRoleDO writeRole = selectOneWriteRole(); AiChatModelDO model; + // TODO @xin:writeRole.getModelId 可能为空。所以,最好是先通过 chatRole 拿。如果它没拿到,通过 getRequiredDefaultChatModel 再拿。 if (Objects.nonNull(writeRole)) { model = chatModalService.getChatModel(writeRole.getModelId()); } else { model = chatModalService.getRequiredDefaultChatModel(); } - + // 1.2 校验平台 AiPlatformEnum platform = AiPlatformEnum.validatePlatform(model.getPlatform()); - StreamingChatModel chatModel = apiKeyService.getChatModel(model.getKeyId()); - // 1.2 插入写作信息 + // 2. 插入写作信息 AiWriteDO writeDO = BeanUtils.toBean(generateReqVO, AiWriteDO.class, e -> e.setUserId(userId).setModel(model.getModel()).setPlatform(platform.getPlatform())); writeMapper.insert(writeDO); - // 2.1 构建提示词 + // 3.1 构建提示词 ChatOptions chatOptions = AiUtils.buildChatOptions(platform, model.getModel(), model.getTemperature(), model.getMaxTokens()); Prompt prompt = new Prompt(buildWritingPrompt(generateReqVO), chatOptions); Flux streamResponse = chatModel.stream(prompt); - // 2.2 流式返回 + // 3.2 流式返回 StringBuffer contentBuffer = new StringBuffer(); return streamResponse.map(chunk -> { String newContent = chunk.getResult() != null ? chunk.getResult().getOutput().getContent() : null; @@ -102,10 +102,13 @@ public class AiWriteServiceImpl implements AiWriteService { }).onErrorResume(error -> Flux.just(error(ErrorCodeConstants.WRITE_STREAM_ERROR))); } + // TODO @xin:chatRoleService 增加一个 getChatRoleListByName; private AiChatRoleDO selectOneWriteRole() { AiChatRoleDO chatRoleDO = null; + // TODO @xin:"写作助手" 枚举下。 PageResult writeRolePage = chatRoleService.getChatRolePage(new AiChatRolePageReqVO().setName("写作助手")); List list = writeRolePage.getList(); + // TODO @xin:CollUtil.getFirst 简化下 if (CollUtil.isNotEmpty(list)) { chatRoleDO = list.get(0); } @@ -113,19 +116,19 @@ public class AiWriteServiceImpl implements AiWriteService { } private String buildWritingPrompt(AiWriteGenerateReqVO generateReqVO) { + // 校验写作类型是否合法 Integer type = generateReqVO.getType(); + // TODO @xin:这里可以搞到 validator 的校验。InEnum + AiWriteTypeEnum.validateType(type); String format = dictDataApi.getDictDataLabel(DictTypeConstants.AI_WRITE_FORMAT, generateReqVO.getFormat()); String tone = dictDataApi.getDictDataLabel(DictTypeConstants.AI_WRITE_TONE, generateReqVO.getTone()); String language = dictDataApi.getDictDataLabel(DictTypeConstants.AI_WRITE_LANGUAGE, generateReqVO.getLanguage()); String length = dictDataApi.getDictDataLabel(DictTypeConstants.AI_WRITE_LENGTH, generateReqVO.getLength()); String prompt = generateReqVO.getPrompt(); - // 校验写作类型是否合法 - AiWriteTypeEnum.validateType(type); - if (Objects.equals(type, AiWriteTypeEnum.WRITING.getType())) { - return StrUtil.format(AiWriteTypeEnum.WRITING.getTemplate(), prompt, format, tone, language, length); + return StrUtil.format(AiWriteTypeEnum.WRITING.getPrompt(), prompt, format, tone, language, length); } else { - return StrUtil.format(AiWriteTypeEnum.REPLY.getTemplate(), generateReqVO.getOriginalContent(), prompt, format, tone, language, length); + return StrUtil.format(AiWriteTypeEnum.REPLY.getPrompt(), generateReqVO.getOriginalContent(), prompt, format, tone, language, length); } } diff --git a/yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/image/TongYiImagesModelTests.java b/yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/image/TongYiImagesModelTests.java index c927a0d57..7f44873b5 100644 --- a/yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/image/TongYiImagesModelTests.java +++ b/yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/image/TongYiImagesModelTests.java @@ -10,15 +10,16 @@ import org.junit.jupiter.api.Test; import java.util.Map; +// TODO @fan:改成 TongYiImagesModel 哈 /** - * 通义万象 - 测试 + * 通义万象 */ public class TongYiImagesModelTests { @Test public void imageCallTest() throws NoApiKeyException { // 设置 api key - Constants.apiKey="sk-Zsd81gZYg7"; + Constants.apiKey = "sk-Zsd81gZYg7"; ImageSynthesisParam param = ImageSynthesisParam.builder() .model(ImageSynthesis.Models.WANX_V1)