Commit 6dfbfe51679b0e70a0142a177a943d7bbd329a08
1 parent
b80a76d1
【优化】AI 写作:格式、语气、语言等抽枚举
Showing
8 changed files
with
221 additions
and
31 deletions
yudao-module-ai/yudao-module-ai-api/src/main/java/cn/iocoder/yudao/module/ai/enums/write/AiLanguageEnum.java
0 → 100644
| 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 AiLanguageEnum implements IntArrayValuable { | ||
| 12 | + | ||
| 13 | + AUTO(1, "自动"), | ||
| 14 | + CHINESE(2, "中文"), | ||
| 15 | + ENGLISH(3, "英文"), | ||
| 16 | + KOREAN(4, "韩语"), | ||
| 17 | + JAPANESE(5, "日语"); | ||
| 18 | + | ||
| 19 | + /** | ||
| 20 | + * Language code | ||
| 21 | + */ | ||
| 22 | + private final Integer language; | ||
| 23 | + /** | ||
| 24 | + * Language name | ||
| 25 | + */ | ||
| 26 | + private final String name; | ||
| 27 | + | ||
| 28 | + public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(AiLanguageEnum::getLanguage).toArray(); | ||
| 29 | + | ||
| 30 | + @Override | ||
| 31 | + public int[] array() { | ||
| 32 | + return ARRAYS; | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + public static AiLanguageEnum valueOfLanguage(Integer language) { | ||
| 36 | + for (AiLanguageEnum languageEnum : AiLanguageEnum.values()) { | ||
| 37 | + if (languageEnum.getLanguage().equals(language)) { | ||
| 38 | + return languageEnum; | ||
| 39 | + } | ||
| 40 | + } | ||
| 41 | + throw new IllegalArgumentException("未知语言: " + language); | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | +} | ||
| 0 | \ No newline at end of file | 45 | \ 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
0 → 100644
| 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
0 → 100644
| 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
0 → 100644
| 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 | +} | ||
| 0 | \ No newline at end of file | 47 | \ No newline at end of file |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/write/AiWriteController.java
| @@ -28,7 +28,7 @@ public class AiWriteController { | @@ -28,7 +28,7 @@ public class AiWriteController { | ||
| 28 | @PostMapping(value = "/generate-stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE) | 28 | @PostMapping(value = "/generate-stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE) |
| 29 | @PermitAll | 29 | @PermitAll |
| 30 | @Operation(summary = "写作生成(流式)", description = "流式返回,响应较快") | 30 | @Operation(summary = "写作生成(流式)", description = "流式返回,响应较快") |
| 31 | - public Flux<CommonResult<String>> generateComposition(@RequestBody @Valid AiWriteGenerateReqVO generateReqVO) { | 31 | + public Flux<CommonResult<String>> generateWriteContent(@RequestBody @Valid AiWriteGenerateReqVO generateReqVO) { |
| 32 | return writeService.generateWriteContent(generateReqVO, getLoginUserId()); | 32 | return writeService.generateWriteContent(generateReqVO, getLoginUserId()); |
| 33 | } | 33 | } |
| 34 | } | 34 | } |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/write/vo/AiWriteGenerateReqVO.java
| 1 | package cn.iocoder.yudao.module.ai.controller.admin.write.vo; | 1 | package cn.iocoder.yudao.module.ai.controller.admin.write.vo; |
| 2 | 2 | ||
| 3 | import io.swagger.v3.oas.annotations.media.Schema; | 3 | import io.swagger.v3.oas.annotations.media.Schema; |
| 4 | -import jakarta.validation.constraints.NotBlank; | 4 | +import jakarta.validation.constraints.NotNull; |
| 5 | import lombok.Data; | 5 | import lombok.Data; |
| 6 | 6 | ||
| 7 | @Schema(description = "管理后台 - AI 写作生成 Request VO") | 7 | @Schema(description = "管理后台 - AI 写作生成 Request VO") |
| 8 | @Data | 8 | @Data |
| 9 | public class AiWriteGenerateReqVO { | 9 | public class AiWriteGenerateReqVO { |
| 10 | 10 | ||
| 11 | - @Schema(description = "写作内容提示", requiredMode = Schema.RequiredMode.NOT_REQUIRED, example = "田忌赛马") | ||
| 12 | - private String contentPrompt; | 11 | + @Schema(description = "写作内容提示", requiredMode = Schema.RequiredMode.NOT_REQUIRED, example = "1.撰写:田忌赛马;2.回复:不批") |
| 12 | + private String prompt; | ||
| 13 | 13 | ||
| 14 | @Schema(description = "原文", requiredMode = Schema.RequiredMode.NOT_REQUIRED, example = "领导我要辞职") | 14 | @Schema(description = "原文", requiredMode = Schema.RequiredMode.NOT_REQUIRED, example = "领导我要辞职") |
| 15 | private String originalContent; | 15 | private String originalContent; |
| 16 | 16 | ||
| 17 | - @Schema(description = "长度", requiredMode = Schema.RequiredMode.REQUIRED, example = "中等") | ||
| 18 | - @NotBlank(message = "长度不能为空") | ||
| 19 | - private String length; | 17 | + @Schema(description = "长度", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") |
| 18 | + @NotNull(message = "长度不能为空") | ||
| 19 | + private Integer length; | ||
| 20 | 20 | ||
| 21 | - @Schema(description = "格式", requiredMode = Schema.RequiredMode.REQUIRED, example = "文章") | ||
| 22 | - @NotBlank(message = "格式不能为空") | ||
| 23 | - private String format; | 21 | + @Schema(description = "格式", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") |
| 22 | + @NotNull(message = "格式不能为空") | ||
| 23 | + private Integer format; | ||
| 24 | 24 | ||
| 25 | - @Schema(description = "语气", requiredMode = Schema.RequiredMode.REQUIRED, example = "随意") | ||
| 26 | - @NotBlank(message = "语气不能为空") | ||
| 27 | - private String tone; | 25 | + @Schema(description = "语气", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") |
| 26 | + @NotNull(message = "语气不能为空") | ||
| 27 | + private Integer tone; | ||
| 28 | 28 | ||
| 29 | - @Schema(description = "语言", requiredMode = Schema.RequiredMode.REQUIRED, example = "中文") | ||
| 30 | - @NotBlank(message = "语言不能为空") | ||
| 31 | - private String language; | 29 | + @Schema(description = "语言", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") |
| 30 | + @NotNull(message = "语言不能为空") | ||
| 31 | + private Integer language; | ||
| 32 | 32 | ||
| 33 | 33 | ||
| 34 | @Schema(description = "写作类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") | 34 | @Schema(description = "写作类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") |
| 35 | - private Integer writeType; //参见 AiWriteTypeEnum 枚举 | 35 | + private Integer type; //参见 AiWriteTypeEnum 枚举 |
| 36 | } | 36 | } |
| 37 | \ No newline at end of file | 37 | \ No newline at end of file |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/dal/dataobject/write/AiWriteDO.java
| @@ -32,12 +32,12 @@ public class AiWriteDO extends BaseDO { | @@ -32,12 +32,12 @@ public class AiWriteDO extends BaseDO { | ||
| 32 | * <p> | 32 | * <p> |
| 33 | * 枚举 {@link AiWriteTypeEnum} | 33 | * 枚举 {@link AiWriteTypeEnum} |
| 34 | */ | 34 | */ |
| 35 | - private Integer writeType; | 35 | + private Integer type; |
| 36 | 36 | ||
| 37 | /** | 37 | /** |
| 38 | * 生成内容提示 | 38 | * 生成内容提示 |
| 39 | */ | 39 | */ |
| 40 | - private String contentPrompt; | 40 | + private String prompt; |
| 41 | 41 | ||
| 42 | /** | 42 | /** |
| 43 | * 生成的内容 | 43 | * 生成的内容 |
| @@ -52,22 +52,22 @@ public class AiWriteDO extends BaseDO { | @@ -52,22 +52,22 @@ public class AiWriteDO extends BaseDO { | ||
| 52 | /** | 52 | /** |
| 53 | * 长度提示词 | 53 | * 长度提示词 |
| 54 | */ | 54 | */ |
| 55 | - private String length; | 55 | + private Integer length; |
| 56 | 56 | ||
| 57 | /** | 57 | /** |
| 58 | * 格式提示词 | 58 | * 格式提示词 |
| 59 | */ | 59 | */ |
| 60 | - private String format; | 60 | + private Integer format; |
| 61 | 61 | ||
| 62 | /** | 62 | /** |
| 63 | * 语气提示词 | 63 | * 语气提示词 |
| 64 | */ | 64 | */ |
| 65 | - private String tone; | 65 | + private Integer tone; |
| 66 | 66 | ||
| 67 | /** | 67 | /** |
| 68 | * 语言提示词 | 68 | * 语言提示词 |
| 69 | */ | 69 | */ |
| 70 | - private String language; | 70 | + private Integer language; |
| 71 | 71 | ||
| 72 | /** | 72 | /** |
| 73 | * 模型 | 73 | * 模型 |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/write/AiWriteServiceImpl.java
| @@ -11,7 +11,7 @@ import cn.iocoder.yudao.module.ai.dal.dataobject.model.AiChatModelDO; | @@ -11,7 +11,7 @@ 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.ErrorCodeConstants; | 13 | import cn.iocoder.yudao.module.ai.enums.ErrorCodeConstants; |
| 14 | -import cn.iocoder.yudao.module.ai.enums.write.AiWriteTypeEnum; | 14 | +import cn.iocoder.yudao.module.ai.enums.write.*; |
| 15 | import cn.iocoder.yudao.module.ai.service.model.AiApiKeyService; | 15 | import cn.iocoder.yudao.module.ai.service.model.AiApiKeyService; |
| 16 | import cn.iocoder.yudao.module.ai.service.model.AiChatModelService; | 16 | import cn.iocoder.yudao.module.ai.service.model.AiChatModelService; |
| 17 | import com.alibaba.cloud.ai.tongyi.chat.TongYiChatOptions; | 17 | import com.alibaba.cloud.ai.tongyi.chat.TongYiChatOptions; |
| @@ -84,17 +84,17 @@ public class AiWriteServiceImpl implements AiWriteService { | @@ -84,17 +84,17 @@ public class AiWriteServiceImpl implements AiWriteService { | ||
| 84 | 84 | ||
| 85 | private String buildWritingPrompt(AiWriteGenerateReqVO generateReqVO) { | 85 | private String buildWritingPrompt(AiWriteGenerateReqVO generateReqVO) { |
| 86 | String template; | 86 | String template; |
| 87 | - Integer writeType = generateReqVO.getWriteType(); | ||
| 88 | - String format = generateReqVO.getFormat(); | ||
| 89 | - String tone = generateReqVO.getTone(); | ||
| 90 | - String language = generateReqVO.getLanguage(); | ||
| 91 | - String length = generateReqVO.getLength(); | 87 | + Integer writeType = generateReqVO.getType(); |
| 88 | + String format = AiWriteFormatEnum.valueOfFormat(generateReqVO.getFormat()).getName(); | ||
| 89 | + String tone = AiWriteToneEnum.valueOfTone(generateReqVO.getTone()).getName(); | ||
| 90 | + String language = AiLanguageEnum.valueOfLanguage(generateReqVO.getLanguage()).getName(); | ||
| 91 | + String length = AiWriteLengthEnum.valueOfLength(generateReqVO.getLength()).getName(); | ||
| 92 | if (Objects.equals(writeType, AiWriteTypeEnum.WRITING.getType())) { | 92 | if (Objects.equals(writeType, AiWriteTypeEnum.WRITING.getType())) { |
| 93 | template = "请撰写一篇关于 [{}] 的文章。文章的内容格式为:[{}],语气为:[{}],语言为:[{}],长度为:[{}]。请确保涵盖主要内容,不需要除了正文内容外的其他回复,如标题、额外的解释或道歉。"; | 93 | template = "请撰写一篇关于 [{}] 的文章。文章的内容格式为:[{}],语气为:[{}],语言为:[{}],长度为:[{}]。请确保涵盖主要内容,不需要除了正文内容外的其他回复,如标题、额外的解释或道歉。"; |
| 94 | - return StrUtil.format(template, generateReqVO.getContentPrompt(), format, tone, language, length); | 94 | + return StrUtil.format(template, generateReqVO.getPrompt(), format, tone, language, length); |
| 95 | } else if (Objects.equals(writeType, AiWriteTypeEnum.REPLY.getType())) { | 95 | } else if (Objects.equals(writeType, AiWriteTypeEnum.REPLY.getType())) { |
| 96 | template = "请针对如下内容:[{}] 做个回复。回复内容参考:[{}], 回复的内容格式为:[{}],语气为:[{}],语言为:[{}],长度为:[{}]。不需要除了正文内容外的其他回复,如标题、额外的解释或道歉。"; | 96 | template = "请针对如下内容:[{}] 做个回复。回复内容参考:[{}], 回复的内容格式为:[{}],语气为:[{}],语言为:[{}],长度为:[{}]。不需要除了正文内容外的其他回复,如标题、额外的解释或道歉。"; |
| 97 | - return StrUtil.format(template, generateReqVO.getOriginalContent(), generateReqVO.getContentPrompt(), format, tone, language, length); | 97 | + return StrUtil.format(template, generateReqVO.getOriginalContent(), generateReqVO.getPrompt(), format, tone, language, length); |
| 98 | } else { | 98 | } else { |
| 99 | throw new IllegalArgumentException(StrUtil.format("未知写作类型({})", writeType)); | 99 | throw new IllegalArgumentException(StrUtil.format("未知写作类型({})", writeType)); |
| 100 | } | 100 | } |