Commit 431e91561f8f338dfe247a7ab86fe7ecef14e20c
1 parent
4ef55fa9
【解决todo】AI 写作:语言、语气等改为字典
Showing
8 changed files
with
32 additions
and
204 deletions
yudao-module-ai/yudao-module-ai-api/src/main/java/cn/iocoder/yudao/module/ai/enums/DictTypeConstants.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.ai.enums; | ||
| 2 | + | ||
| 3 | +/** | ||
| 4 | + * AI 字典类型的枚举类 | ||
| 5 | + * | ||
| 6 | + * @author xiaoxin | ||
| 7 | + */ | ||
| 8 | +public interface DictTypeConstants { | ||
| 9 | + | ||
| 10 | + // ========== AI Write ========== | ||
| 11 | + String AI_WRITE_FORMAT = "ai_write_format"; // 写作格式 | ||
| 12 | + String AI_WRITE_LENGTH = "ai_write_length"; // 写作长度 | ||
| 13 | + String AI_WRITE_LANGUAGE = "ai_write_language"; // 写作语言 | ||
| 14 | + String AI_WRITE_TONE = "ai_write_tone"; // 写作语气 | ||
| 15 | + | ||
| 16 | +} |
yudao-module-ai/yudao-module-ai-api/src/main/java/cn/iocoder/yudao/module/ai/enums/write/AiLanguageEnum.java deleted
| 1 | -package cn.iocoder.yudao.module.ai.enums.write; | ||
| 2 | - | ||
| 3 | -import cn.iocoder.yudao.framework.common.core.IntArrayValuable; | ||
| 4 | -import lombok.AllArgsConstructor; | ||
| 5 | -import lombok.Getter; | ||
| 6 | - | ||
| 7 | -import java.util.Arrays; | ||
| 8 | - | ||
| 9 | -// TODO @xin:写作的几个,不用枚举类哈。直接搞字段就好了。AiWriteTypeEnum 还是需要的哈 | ||
| 10 | -@AllArgsConstructor | ||
| 11 | -@Getter | ||
| 12 | -public enum AiLanguageEnum implements IntArrayValuable { | ||
| 13 | - | ||
| 14 | - AUTO(1, "自动"), | ||
| 15 | - CHINESE(2, "中文"), | ||
| 16 | - ENGLISH(3, "英文"), | ||
| 17 | - KOREAN(4, "韩语"), | ||
| 18 | - JAPANESE(5, "日语"); | ||
| 19 | - | ||
| 20 | - /** | ||
| 21 | - * Language code | ||
| 22 | - */ | ||
| 23 | - private final Integer language; | ||
| 24 | - /** | ||
| 25 | - * Language name | ||
| 26 | - */ | ||
| 27 | - private final String name; | ||
| 28 | - | ||
| 29 | - public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(AiLanguageEnum::getLanguage).toArray(); | ||
| 30 | - | ||
| 31 | - @Override | ||
| 32 | - public int[] array() { | ||
| 33 | - return ARRAYS; | ||
| 34 | - } | ||
| 35 | - | ||
| 36 | - public static AiLanguageEnum valueOfLanguage(Integer language) { | ||
| 37 | - for (AiLanguageEnum languageEnum : AiLanguageEnum.values()) { | ||
| 38 | - if (languageEnum.getLanguage().equals(language)) { | ||
| 39 | - return languageEnum; | ||
| 40 | - } | ||
| 41 | - } | ||
| 42 | - throw new IllegalArgumentException("未知语言: " + language); | ||
| 43 | - } | ||
| 44 | - | ||
| 45 | -} | ||
| 46 | \ No newline at end of file | 0 | \ No newline at end of file |
yudao-module-ai/yudao-module-ai-api/src/main/java/cn/iocoder/yudao/module/ai/enums/write/AiWriteFormatEnum.java deleted
| 1 | -package cn.iocoder.yudao.module.ai.enums.write; | ||
| 2 | - | ||
| 3 | -import cn.iocoder.yudao.framework.common.core.IntArrayValuable; | ||
| 4 | -import lombok.AllArgsConstructor; | ||
| 5 | -import lombok.Getter; | ||
| 6 | - | ||
| 7 | -import java.util.Arrays; | ||
| 8 | - | ||
| 9 | -/** | ||
| 10 | - * AI 写作类型的枚举 | ||
| 11 | - * | ||
| 12 | - * @author xiaoxin | ||
| 13 | - */ | ||
| 14 | -@AllArgsConstructor | ||
| 15 | -@Getter | ||
| 16 | -public enum AiWriteFormatEnum implements IntArrayValuable { | ||
| 17 | - | ||
| 18 | - AUTO(1, "自动"), | ||
| 19 | - EMAIL(2, "电子邮件"), | ||
| 20 | - MESSAGE(3, "消息"), | ||
| 21 | - COMMENT(4, "评论"), | ||
| 22 | - PARAGRAPH(5, "段落"), | ||
| 23 | - ARTICLE(6, "文章"), | ||
| 24 | - BLOG_POST(7, "博客文章"), | ||
| 25 | - IDEA(8, "想法"), | ||
| 26 | - OUTLINE(9, "大纲"); | ||
| 27 | - | ||
| 28 | - /** | ||
| 29 | - * 格式 | ||
| 30 | - */ | ||
| 31 | - private final Integer format; | ||
| 32 | - /** | ||
| 33 | - * 格式名 | ||
| 34 | - */ | ||
| 35 | - private final String name; | ||
| 36 | - | ||
| 37 | - public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(AiWriteFormatEnum::getFormat).toArray(); | ||
| 38 | - | ||
| 39 | - @Override | ||
| 40 | - public int[] array() { | ||
| 41 | - return ARRAYS; | ||
| 42 | - } | ||
| 43 | - | ||
| 44 | - public static AiWriteFormatEnum valueOfFormat(Integer format) { | ||
| 45 | - for (AiWriteFormatEnum formatEnum : AiWriteFormatEnum.values()) { | ||
| 46 | - if (formatEnum.getFormat().equals(format)) { | ||
| 47 | - return formatEnum; | ||
| 48 | - } | ||
| 49 | - } | ||
| 50 | - throw new IllegalArgumentException("未知格式: " + format); | ||
| 51 | - } | ||
| 52 | - | ||
| 53 | -} |
yudao-module-ai/yudao-module-ai-api/src/main/java/cn/iocoder/yudao/module/ai/enums/write/AiWriteLengthEnum.java deleted
| 1 | -package cn.iocoder.yudao.module.ai.enums.write; | ||
| 2 | - | ||
| 3 | -import cn.iocoder.yudao.framework.common.core.IntArrayValuable; | ||
| 4 | -import lombok.AllArgsConstructor; | ||
| 5 | -import lombok.Getter; | ||
| 6 | - | ||
| 7 | -import java.util.Arrays; | ||
| 8 | - | ||
| 9 | -/** | ||
| 10 | - * AI 写作类型的枚举 | ||
| 11 | - * | ||
| 12 | - * @author xiaoxin | ||
| 13 | - */ | ||
| 14 | -@AllArgsConstructor | ||
| 15 | -@Getter | ||
| 16 | -public enum AiWriteLengthEnum implements IntArrayValuable { | ||
| 17 | - | ||
| 18 | - AUTO(1, "自动"), | ||
| 19 | - SHORT(2, "短"), | ||
| 20 | - MEDIUM(3, "中"), | ||
| 21 | - LONG(4, "长"); | ||
| 22 | - | ||
| 23 | - /** | ||
| 24 | - * 长度 | ||
| 25 | - */ | ||
| 26 | - private final Integer length; | ||
| 27 | - /** | ||
| 28 | - * 长度名 | ||
| 29 | - */ | ||
| 30 | - private final String name; | ||
| 31 | - | ||
| 32 | - public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(AiWriteLengthEnum::getLength).toArray(); | ||
| 33 | - | ||
| 34 | - @Override | ||
| 35 | - public int[] array() { | ||
| 36 | - return ARRAYS; | ||
| 37 | - } | ||
| 38 | - | ||
| 39 | - public static AiWriteLengthEnum valueOfLength(Integer length) { | ||
| 40 | - for (AiWriteLengthEnum lengthEnum : AiWriteLengthEnum.values()) { | ||
| 41 | - if (lengthEnum.getLength().equals(length)) { | ||
| 42 | - return lengthEnum; | ||
| 43 | - } | ||
| 44 | - } | ||
| 45 | - throw new IllegalArgumentException("未知长度: " + length); | ||
| 46 | - } | ||
| 47 | -} |
yudao-module-ai/yudao-module-ai-api/src/main/java/cn/iocoder/yudao/module/ai/enums/write/AiWriteToneEnum.java deleted
| 1 | -package cn.iocoder.yudao.module.ai.enums.write; | ||
| 2 | - | ||
| 3 | -import cn.iocoder.yudao.framework.common.core.IntArrayValuable; | ||
| 4 | -import lombok.AllArgsConstructor; | ||
| 5 | -import lombok.Getter; | ||
| 6 | - | ||
| 7 | -import java.util.Arrays; | ||
| 8 | - | ||
| 9 | -@AllArgsConstructor | ||
| 10 | -@Getter | ||
| 11 | -public enum AiWriteToneEnum implements IntArrayValuable { | ||
| 12 | - | ||
| 13 | - AUTO(1, "自动"), | ||
| 14 | - FRIENDLY(2, "友善"), | ||
| 15 | - CASUAL(3, "随意"), | ||
| 16 | - KIND(4, "友好"), | ||
| 17 | - PROFESSIONAL(5, "专业"), | ||
| 18 | - HUMOROUS(6, "谈谐"), | ||
| 19 | - INTERESTING(7, "有趣"), | ||
| 20 | - FORMAL(8, "正式"); | ||
| 21 | - | ||
| 22 | - /** | ||
| 23 | - * 语气 | ||
| 24 | - */ | ||
| 25 | - private final Integer tone; | ||
| 26 | - /** | ||
| 27 | - * 语气名 | ||
| 28 | - */ | ||
| 29 | - private final String name; | ||
| 30 | - | ||
| 31 | - public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(AiWriteToneEnum::getTone).toArray(); | ||
| 32 | - | ||
| 33 | - @Override | ||
| 34 | - public int[] array() { | ||
| 35 | - return ARRAYS; | ||
| 36 | - } | ||
| 37 | - | ||
| 38 | - public static AiWriteToneEnum valueOfTone(Integer tone) { | ||
| 39 | - for (AiWriteToneEnum toneEnum : AiWriteToneEnum.values()) { | ||
| 40 | - if (toneEnum.getTone().equals(tone)) { | ||
| 41 | - return toneEnum; | ||
| 42 | - } | ||
| 43 | - } | ||
| 44 | - throw new IllegalArgumentException("未知语气: " + tone); | ||
| 45 | - } | ||
| 46 | -} | ||
| 47 | \ No newline at end of file | 0 | \ No newline at end of file |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/write/vo/AiWriteGenerateReqVO.java
| @@ -14,11 +14,10 @@ public class AiWriteGenerateReqVO { | @@ -14,11 +14,10 @@ public class AiWriteGenerateReqVO { | ||
| 14 | @InEnum(AiWriteTypeEnum.class) | 14 | @InEnum(AiWriteTypeEnum.class) |
| 15 | private Integer type; | 15 | private Integer type; |
| 16 | 16 | ||
| 17 | - // TODO @xin:如果非必填,可以不用写 requiredMode | ||
| 18 | - @Schema(description = "写作内容提示", requiredMode = Schema.RequiredMode.NOT_REQUIRED, example = "1.撰写:田忌赛马;2.回复:不批") | 17 | + @Schema(description = "写作内容提示", example = "1.撰写:田忌赛马;2.回复:不批") |
| 19 | private String prompt; | 18 | private String prompt; |
| 20 | 19 | ||
| 21 | - @Schema(description = "原文", requiredMode = Schema.RequiredMode.NOT_REQUIRED, example = "领导我要辞职") | 20 | + @Schema(description = "原文", example = "领导我要辞职") |
| 22 | private String originalContent; | 21 | private String originalContent; |
| 23 | 22 | ||
| 24 | @Schema(description = "长度", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") | 23 | @Schema(description = "长度", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/music/AiMusicServiceImpl.java
| @@ -54,7 +54,6 @@ public class AiMusicServiceImpl implements AiMusicService { | @@ -54,7 +54,6 @@ public class AiMusicServiceImpl implements AiMusicService { | ||
| 54 | public List<Long> generateMusic(Long userId, AiSunoGenerateReqVO reqVO) { | 54 | public List<Long> generateMusic(Long userId, AiSunoGenerateReqVO reqVO) { |
| 55 | // 1. 调用 Suno 生成音乐 | 55 | // 1. 调用 Suno 生成音乐 |
| 56 | SunoApi sunoApi = apiKeyService.getSunoApi(); | 56 | SunoApi sunoApi = apiKeyService.getSunoApi(); |
| 57 | - // TODO 芋艿:这两个貌似一直没跑成功,你那可以么?用的请求是 AiMusicController.http 的 --xin:大部分ok的,补充了error_message | ||
| 58 | List<SunoApi.MusicData> musicDataList; | 57 | List<SunoApi.MusicData> musicDataList; |
| 59 | if (Objects.equals(AiMusicGenerateModeEnum.DESCRIPTION.getMode(), reqVO.getGenerateMode())) { | 58 | if (Objects.equals(AiMusicGenerateModeEnum.DESCRIPTION.getMode(), reqVO.getGenerateMode())) { |
| 60 | // 1.1 描述模式 | 59 | // 1.1 描述模式 |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/write/AiWriteServiceImpl.java
| @@ -10,10 +10,12 @@ import cn.iocoder.yudao.module.ai.controller.admin.write.vo.AiWriteGenerateReqVO | @@ -10,10 +10,12 @@ import cn.iocoder.yudao.module.ai.controller.admin.write.vo.AiWriteGenerateReqVO | ||
| 10 | import cn.iocoder.yudao.module.ai.dal.dataobject.model.AiChatModelDO; | 10 | import cn.iocoder.yudao.module.ai.dal.dataobject.model.AiChatModelDO; |
| 11 | import cn.iocoder.yudao.module.ai.dal.dataobject.write.AiWriteDO; | 11 | import cn.iocoder.yudao.module.ai.dal.dataobject.write.AiWriteDO; |
| 12 | import cn.iocoder.yudao.module.ai.dal.mysql.write.AiWriteMapper; | 12 | import cn.iocoder.yudao.module.ai.dal.mysql.write.AiWriteMapper; |
| 13 | +import cn.iocoder.yudao.module.ai.enums.DictTypeConstants; | ||
| 13 | import cn.iocoder.yudao.module.ai.enums.ErrorCodeConstants; | 14 | import cn.iocoder.yudao.module.ai.enums.ErrorCodeConstants; |
| 14 | -import cn.iocoder.yudao.module.ai.enums.write.*; | 15 | +import cn.iocoder.yudao.module.ai.enums.write.AiWriteTypeEnum; |
| 15 | import cn.iocoder.yudao.module.ai.service.model.AiApiKeyService; | 16 | import cn.iocoder.yudao.module.ai.service.model.AiApiKeyService; |
| 16 | import cn.iocoder.yudao.module.ai.service.model.AiChatModelService; | 17 | import cn.iocoder.yudao.module.ai.service.model.AiChatModelService; |
| 18 | +import cn.iocoder.yudao.module.system.api.dict.DictDataApi; | ||
| 17 | import com.alibaba.cloud.ai.tongyi.chat.TongYiChatOptions; | 19 | import com.alibaba.cloud.ai.tongyi.chat.TongYiChatOptions; |
| 18 | import jakarta.annotation.Resource; | 20 | import jakarta.annotation.Resource; |
| 19 | import lombok.extern.slf4j.Slf4j; | 21 | import lombok.extern.slf4j.Slf4j; |
| @@ -45,14 +47,17 @@ public class AiWriteServiceImpl implements AiWriteService { | @@ -45,14 +47,17 @@ public class AiWriteServiceImpl implements AiWriteService { | ||
| 45 | private AiApiKeyService apiKeyService; | 47 | private AiApiKeyService apiKeyService; |
| 46 | @Resource | 48 | @Resource |
| 47 | private AiChatModelService chatModalService; | 49 | private AiChatModelService chatModalService; |
| 50 | + | ||
| 51 | + @Resource | ||
| 52 | + private DictDataApi dictDataApi; | ||
| 53 | + | ||
| 48 | @Resource | 54 | @Resource |
| 49 | - private AiWriteMapper writeMapper; // TODO @xin:上面空一行;因为同类之间不要空行,非同类空行; | 55 | + private AiWriteMapper writeMapper; |
| 50 | 56 | ||
| 51 | @Override | 57 | @Override |
| 52 | public Flux<CommonResult<String>> generateWriteContent(AiWriteGenerateReqVO generateReqVO, Long userId) { | 58 | public Flux<CommonResult<String>> generateWriteContent(AiWriteGenerateReqVO generateReqVO, Long userId) { |
| 53 | - // 1.1 校验模型 | ||
| 54 | - // TODO @xin:可以约定大于配置先,查询某个名字。例如说,写作助手!然后写作助手,上面是有个 model 的,可以使用它。 | ||
| 55 | - AiChatModelDO model = chatModalService.validateChatModel(14L); | 59 | + // 1.1 校验模型 TODO 芋艿 是不是取默认的模型也ok? |
| 60 | + AiChatModelDO model = chatModalService.getRequiredDefaultChatModel(); | ||
| 56 | StreamingChatModel chatClient = apiKeyService.getChatClient(model.getKeyId()); | 61 | StreamingChatModel chatClient = apiKeyService.getChatClient(model.getKeyId()); |
| 57 | AiPlatformEnum platform = AiPlatformEnum.validatePlatform(model.getPlatform()); | 62 | AiPlatformEnum platform = AiPlatformEnum.validatePlatform(model.getPlatform()); |
| 58 | ChatOptions chatOptions = buildChatOptions(platform, model.getModel(), model.getTemperature(), model.getMaxTokens()); | 63 | ChatOptions chatOptions = buildChatOptions(platform, model.getModel(), model.getTemperature(), model.getMaxTokens()); |
| @@ -83,10 +88,10 @@ public class AiWriteServiceImpl implements AiWriteService { | @@ -83,10 +88,10 @@ public class AiWriteServiceImpl implements AiWriteService { | ||
| 83 | private String buildWritingPrompt(AiWriteGenerateReqVO generateReqVO) { | 88 | private String buildWritingPrompt(AiWriteGenerateReqVO generateReqVO) { |
| 84 | String template; | 89 | String template; |
| 85 | Integer writeType = generateReqVO.getType(); | 90 | Integer writeType = generateReqVO.getType(); |
| 86 | - String format = AiWriteFormatEnum.valueOfFormat(generateReqVO.getFormat()).getName(); | ||
| 87 | - String tone = AiWriteToneEnum.valueOfTone(generateReqVO.getTone()).getName(); | ||
| 88 | - String language = AiLanguageEnum.valueOfLanguage(generateReqVO.getLanguage()).getName(); | ||
| 89 | - String length = AiWriteLengthEnum.valueOfLength(generateReqVO.getLength()).getName(); | 91 | + String format = dictDataApi.getDictDataLabel(DictTypeConstants.AI_WRITE_FORMAT, generateReqVO.getFormat()); |
| 92 | + String tone = dictDataApi.getDictDataLabel(DictTypeConstants.AI_WRITE_TONE, generateReqVO.getFormat()); | ||
| 93 | + String language = dictDataApi.getDictDataLabel(DictTypeConstants.AI_WRITE_LANGUAGE, generateReqVO.getFormat()); | ||
| 94 | + String length = dictDataApi.getDictDataLabel(DictTypeConstants.AI_WRITE_LENGTH, generateReqVO.getFormat()); | ||
| 90 | if (Objects.equals(writeType, AiWriteTypeEnum.WRITING.getType())) { | 95 | if (Objects.equals(writeType, AiWriteTypeEnum.WRITING.getType())) { |
| 91 | template = "请撰写一篇关于 [{}] 的文章。文章的内容格式为:[{}],语气为:[{}],语言为:[{}],长度为:[{}]。请确保涵盖主要内容,不需要除了正文内容外的其他回复,如标题、额外的解释或道歉。"; | 96 | template = "请撰写一篇关于 [{}] 的文章。文章的内容格式为:[{}],语气为:[{}],语言为:[{}],长度为:[{}]。请确保涵盖主要内容,不需要除了正文内容外的其他回复,如标题、额外的解释或道歉。"; |
| 92 | return StrUtil.format(template, generateReqVO.getPrompt(), format, tone, language, length); | 97 | return StrUtil.format(template, generateReqVO.getPrompt(), format, tone, language, length); |