Commit f86e24bb865ca55d0b0712beb9e318b056a32c78
1 parent
f9854273
【调整】调整AI对话模块
Showing
11 changed files
with
130 additions
and
110 deletions
yudao-module-ai/yudao-module-ai-api/src/main/java/cn/iocoder/yudao/module/ai/AiCommonConstants.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.ai; | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * ai 常用的常量 | |
| 5 | + * | |
| 6 | + * @author fansili | |
| 7 | + * @time 2024/5/7 09:29 | |
| 8 | + * @since 1.0 | |
| 9 | + */ | |
| 10 | +public class AiCommonConstants { | |
| 11 | + | |
| 12 | + /** | |
| 13 | + * 对话 - 默认 title | |
| 14 | + */ | |
| 15 | + public static final String CONVERSATION_DEFAULT_TITLE = "新增对话"; | |
| 16 | +} | ... | ... |
yudao-module-ai/yudao-module-ai-api/src/main/java/cn/iocoder/yudao/module/ai/ErrorCodeConstants.java
| ... | ... | @@ -15,6 +15,10 @@ public interface ErrorCodeConstants { |
| 15 | 15 | |
| 16 | 16 | ErrorCode AI_MODULE_NOT_SUPPORTED = new ErrorCode(1_022_000_000, "AI 模型暂不支持!"); |
| 17 | 17 | ErrorCode AI_CHAT_ROLE_NOT_EXISTENT = new ErrorCode(1_022_000_001, "AI Role 不存在!");; |
| 18 | + | |
| 19 | + // conversation | |
| 20 | + | |
| 21 | + ErrorCode AI_CONVERSATION_NOT_EXISTS = new ErrorCode(1_022_000_002, "AI 对话不存在!");; | |
| 18 | 22 | ErrorCode AI_CHAT_CONTINUE_CONVERSATION_ID_NOT_NULL = new ErrorCode(1_022_000_002, "chat 继续对话,对话 id 不能为空!");; |
| 19 | 23 | ErrorCode AI_CHAT_CONTINUE_NOT_EXIST = new ErrorCode(1_022_000_020, "chat 对话不存在!"); |
| 20 | 24 | ErrorCode AI_CHAT_CONVERSATION_NOT_YOURS = new ErrorCode(1_022_000_021, "这条 chat 对话不是你的!"); | ... | ... |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/chat/AiChatConversationController.java
| ... | ... | @@ -2,12 +2,15 @@ package cn.iocoder.yudao.module.ai.controller.admin.chat; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 4 | 4 | import cn.iocoder.yudao.module.ai.controller.admin.chat.vo.conversation.AiChatConversationCreateReqVO; |
| 5 | +import cn.iocoder.yudao.module.ai.controller.admin.chat.vo.conversation.AiChatConversationListReqVO; | |
| 5 | 6 | import cn.iocoder.yudao.module.ai.controller.admin.chat.vo.conversation.AiChatConversationRespVO; |
| 6 | 7 | import cn.iocoder.yudao.module.ai.controller.admin.chat.vo.conversation.AiChatConversationUpdateReqVO; |
| 8 | +import cn.iocoder.yudao.module.ai.service.AiChatConversationService; | |
| 7 | 9 | import io.swagger.v3.oas.annotations.Operation; |
| 8 | 10 | import io.swagger.v3.oas.annotations.Parameter; |
| 9 | 11 | import io.swagger.v3.oas.annotations.tags.Tag; |
| 10 | 12 | import jakarta.validation.Valid; |
| 13 | +import lombok.AllArgsConstructor; | |
| 11 | 14 | import lombok.extern.slf4j.Slf4j; |
| 12 | 15 | import org.springframework.security.access.prepost.PreAuthorize; |
| 13 | 16 | import org.springframework.web.bind.annotation.*; |
| ... | ... | @@ -16,33 +19,36 @@ import java.util.List; |
| 16 | 19 | |
| 17 | 20 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; |
| 18 | 21 | |
| 22 | +@Slf4j | |
| 19 | 23 | @Tag(name = "管理后台 - 聊天会话") |
| 20 | 24 | @RestController |
| 21 | 25 | @RequestMapping("/ai/chat/conversation") |
| 22 | -@Slf4j | |
| 26 | +@AllArgsConstructor | |
| 23 | 27 | public class AiChatConversationController { |
| 24 | 28 | |
| 25 | - // TODO @fan:实现一下 | |
| 29 | + private final AiChatConversationService aiChatConversationService; | |
| 30 | + | |
| 31 | + // TODO done @fan:实现一下 | |
| 26 | 32 | @PostMapping("/create") |
| 27 | 33 | @Operation(summary = "创建聊天会话") |
| 28 | 34 | @PreAuthorize("@ss.hasPermission('ai:chat-conversation:create')") |
| 29 | 35 | public CommonResult<Long> createConversation(@RequestBody @Valid AiChatConversationCreateReqVO createReqVO) { |
| 30 | - return success(1L); | |
| 36 | + return success(aiChatConversationService.createConversation(createReqVO)); | |
| 31 | 37 | } |
| 32 | 38 | |
| 33 | - // TODO @fan:实现一下 | |
| 39 | + // TODO done @fan:实现一下 | |
| 34 | 40 | @PutMapping("/update") |
| 35 | 41 | @Operation(summary = "更新聊天会话") |
| 36 | 42 | @PreAuthorize("@ss.hasPermission('ai:chat-conversation:create')") |
| 37 | 43 | public CommonResult<Boolean> updateConversation(@RequestBody @Valid AiChatConversationUpdateReqVO updateReqVO) { |
| 38 | - return success(true); | |
| 44 | + return success(aiChatConversationService.updateConversation(updateReqVO)); | |
| 39 | 45 | } |
| 40 | 46 | |
| 41 | - // TODO @fan:实现一下 | |
| 47 | + // TODO done @fan:实现一下 | |
| 42 | 48 | @GetMapping("/list") |
| 43 | 49 | @Operation(summary = "获得聊天会话列表") |
| 44 | - public CommonResult<List<AiChatConversationRespVO>> getConversationList() { | |
| 45 | - return success(null); | |
| 50 | + public CommonResult<List<AiChatConversationRespVO>> getConversationList(@ModelAttribute AiChatConversationListReqVO listReqVO) { | |
| 51 | + return success(aiChatConversationService.listConversation(listReqVO)); | |
| 46 | 52 | } |
| 47 | 53 | |
| 48 | 54 | // TODO @fan:实现一下 |
| ... | ... | @@ -50,7 +56,7 @@ public class AiChatConversationController { |
| 50 | 56 | @Operation(summary = "获得聊天会话") |
| 51 | 57 | @Parameter(name = "id", required = true, description = "会话编号", example = "1024") |
| 52 | 58 | public CommonResult<AiChatConversationRespVO> getConversation(@RequestParam("id") Long id) { |
| 53 | - return success(null); | |
| 59 | + return success(aiChatConversationService.getConversationOfValidate(id)); | |
| 54 | 60 | } |
| 55 | 61 | |
| 56 | 62 | // TODO @fan:实现一下 |
| ... | ... | @@ -58,7 +64,7 @@ public class AiChatConversationController { |
| 58 | 64 | @Operation(summary = "删除聊天会话") |
| 59 | 65 | @Parameter(name = "id", required = true, description = "会话编号", example = "1024") |
| 60 | 66 | public CommonResult<Boolean> deleteConversation(@RequestParam("id") Long id) { |
| 61 | - return success(null); | |
| 67 | + return success(aiChatConversationService.deleteConversation(id)); | |
| 62 | 68 | } |
| 63 | 69 | |
| 64 | 70 | } | ... | ... |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/chat/vo/conversation/AiChatConversationListReqVO.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.ai.controller.admin.chat.vo.conversation; | |
| 2 | + | |
| 3 | +import io.swagger.v3.oas.annotations.media.Schema; | |
| 4 | +import lombok.Data; | |
| 5 | + | |
| 6 | +@Schema(description = "管理后台 - AI 聊天会话 Response VO") | |
| 7 | +@Data | |
| 8 | +public class AiChatConversationListReqVO { | |
| 9 | + | |
| 10 | + @Schema(description = "会话标题", requiredMode = Schema.RequiredMode.REQUIRED, example = "我是一个标题") | |
| 11 | + private String title; | |
| 12 | + | |
| 13 | +} | ... | ... |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/convert/AiChatConversationConvert.java
| 1 | 1 | package cn.iocoder.yudao.module.ai.convert; |
| 2 | 2 | |
| 3 | +import cn.iocoder.yudao.module.ai.controller.admin.chat.vo.conversation.AiChatConversationUpdateReqVO; | |
| 3 | 4 | import cn.iocoder.yudao.module.ai.dal.dataobject.chat.AiChatConversationDO; |
| 4 | 5 | import cn.iocoder.yudao.module.ai.controller.admin.chat.vo.conversation.AiChatConversationRespVO; |
| 5 | 6 | import org.mapstruct.Mapper; |
| ... | ... | @@ -34,4 +35,11 @@ public interface AiChatConversationConvert { |
| 34 | 35 | * @return |
| 35 | 36 | */ |
| 36 | 37 | AiChatConversationRespVO covnertChatConversationRes(AiChatConversationDO aiChatConversationDO); |
| 38 | + | |
| 39 | + /** | |
| 40 | + * 转换 - AiChatConversationDO | |
| 41 | + * | |
| 42 | + * @param updateReqVO | |
| 43 | + */ | |
| 44 | + AiChatConversationDO convertAiChatConversationDO(AiChatConversationUpdateReqVO updateReqVO); | |
| 37 | 45 | } | ... | ... |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/dal/mysql/AiChatModalMapper.java
| ... | ... | @@ -5,6 +5,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageParam; |
| 5 | 5 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 6 | 6 | import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; |
| 7 | 7 | import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; |
| 8 | +import cn.iocoder.yudao.module.ai.dal.dataobject.model.AiChatModalDO; | |
| 8 | 9 | import org.apache.ibatis.annotations.Mapper; |
| 9 | 10 | import org.springframework.stereotype.Repository; |
| 10 | 11 | ... | ... |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/AiChatConversationService.java
| 1 | 1 | package cn.iocoder.yudao.module.ai.service; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.module.ai.controller.admin.chat.vo.conversation.AiChatConversationCreateReqVO; |
| 4 | +import cn.iocoder.yudao.module.ai.controller.admin.chat.vo.conversation.AiChatConversationListReqVO; | |
| 4 | 5 | import cn.iocoder.yudao.module.ai.controller.admin.chat.vo.conversation.AiChatConversationRespVO; |
| 6 | +import cn.iocoder.yudao.module.ai.controller.admin.chat.vo.conversation.AiChatConversationUpdateReqVO; | |
| 5 | 7 | |
| 6 | 8 | import java.util.List; |
| 7 | 9 | |
| ... | ... | @@ -14,29 +16,20 @@ import java.util.List; |
| 14 | 16 | public interface AiChatConversationService { |
| 15 | 17 | |
| 16 | 18 | /** |
| 17 | - * 对话 - 创建普通对话 | |
| 19 | + * 对话 - 创建对话 | |
| 18 | 20 | * |
| 19 | 21 | * @param req |
| 20 | 22 | * @return |
| 21 | 23 | */ |
| 22 | - AiChatConversationRespVO createConversation(AiChatConversationCreateUserReq req); | |
| 24 | + Long createConversation(AiChatConversationCreateReqVO req); | |
| 23 | 25 | |
| 24 | 26 | /** |
| 25 | - * 对话 - 创建role对话 | |
| 27 | + * 对话 - 更新对话 | |
| 26 | 28 | * |
| 27 | - * @param req | |
| 28 | - * @return | |
| 29 | - */ | |
| 30 | - AiChatConversationRespVO createRoleConversation(AiChatConversationCreateReqVO req); | |
| 31 | - | |
| 32 | - | |
| 33 | - /** | |
| 34 | - * 获取 - 对话 | |
| 35 | - * | |
| 36 | - * @param id | |
| 29 | + * @param updateReqVO | |
| 37 | 30 | * @return |
| 38 | 31 | */ |
| 39 | - AiChatConversationRespVO getConversation(Long id); | |
| 32 | + Boolean updateConversation(AiChatConversationUpdateReqVO updateReqVO); | |
| 40 | 33 | |
| 41 | 34 | /** |
| 42 | 35 | * 获取 - 对话列表 |
| ... | ... | @@ -44,22 +37,21 @@ public interface AiChatConversationService { |
| 44 | 37 | * @param req |
| 45 | 38 | * @return |
| 46 | 39 | */ |
| 47 | - List<AiChatConversationRespVO> listConversation(AiChatConversationListReq req); | |
| 40 | + List<AiChatConversationRespVO> listConversation(AiChatConversationListReqVO req); | |
| 48 | 41 | |
| 49 | 42 | /** |
| 50 | - * 更新 - 更新模型 | |
| 43 | + * 获取 - 对话 | |
| 51 | 44 | * |
| 52 | 45 | * @param id |
| 53 | - * @param modalId | |
| 54 | 46 | * @return |
| 55 | 47 | */ |
| 56 | - void updateModal(Long id, Long modalId); | |
| 48 | + AiChatConversationRespVO getConversationOfValidate(Long id); | |
| 57 | 49 | |
| 58 | 50 | /** |
| 59 | 51 | * 删除 - 根据id |
| 60 | 52 | * |
| 61 | 53 | * @param id |
| 62 | 54 | */ |
| 63 | - void delete(Long id); | |
| 55 | + Boolean deleteConversation(Long id); | |
| 64 | 56 | |
| 65 | 57 | } | ... | ... |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/AiChatModalService.java
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/impl/AiChatConversationServiceImpl.java
| ... | ... | @@ -2,17 +2,20 @@ package cn.iocoder.yudao.module.ai.service.impl; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; |
| 4 | 4 | import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; |
| 5 | +import cn.iocoder.yudao.module.ai.AiCommonConstants; | |
| 5 | 6 | import cn.iocoder.yudao.module.ai.ErrorCodeConstants; |
| 6 | 7 | import cn.iocoder.yudao.module.ai.controller.admin.chat.vo.conversation.AiChatConversationCreateReqVO; |
| 8 | +import cn.iocoder.yudao.module.ai.controller.admin.chat.vo.conversation.AiChatConversationListReqVO; | |
| 7 | 9 | import cn.iocoder.yudao.module.ai.controller.admin.chat.vo.conversation.AiChatConversationRespVO; |
| 10 | +import cn.iocoder.yudao.module.ai.controller.admin.chat.vo.conversation.AiChatConversationUpdateReqVO; | |
| 8 | 11 | import cn.iocoder.yudao.module.ai.controller.admin.model.vo.model.AiChatModalRes; |
| 9 | 12 | import cn.iocoder.yudao.module.ai.controller.admin.model.vo.role.AiChatRoleRes; |
| 10 | 13 | import cn.iocoder.yudao.module.ai.convert.AiChatConversationConvert; |
| 11 | -import cn.iocoder.yudao.module.ai.enums.AiChatConversationTypeEnum; | |
| 12 | -import cn.iocoder.yudao.module.ai.enums.AiChatModalDisableEnum; | |
| 14 | +import cn.iocoder.yudao.module.ai.dal.dataobject.chat.AiChatConversationDO; | |
| 15 | +import cn.iocoder.yudao.module.ai.dal.dataobject.model.AiChatModalDO; | |
| 13 | 16 | import cn.iocoder.yudao.module.ai.dal.mysql.AiChatConversationMapper; |
| 14 | 17 | import cn.iocoder.yudao.module.ai.dal.mysql.AiChatModalMapper; |
| 15 | -import cn.iocoder.yudao.module.ai.dal.mysql.AiChatRoleMapper; | |
| 18 | +import cn.iocoder.yudao.module.ai.enums.AiChatModalDisableEnum; | |
| 16 | 19 | import cn.iocoder.yudao.module.ai.service.AiChatConversationService; |
| 17 | 20 | import cn.iocoder.yudao.module.ai.service.AiChatModalService; |
| 18 | 21 | import cn.iocoder.yudao.module.ai.service.AiChatRoleService; |
| ... | ... | @@ -34,119 +37,95 @@ import java.util.List; |
| 34 | 37 | @AllArgsConstructor |
| 35 | 38 | public class AiChatConversationServiceImpl implements AiChatConversationService { |
| 36 | 39 | |
| 37 | - private final AiChatRoleMapper aiChatRoleMapper; | |
| 38 | 40 | private final AiChatModalMapper aiChatModalMapper; |
| 39 | - private final AiChatConversationMapper aiChatConversationMapper; | |
| 40 | 41 | private final AiChatModalService aiChatModalService; |
| 41 | 42 | private final AiChatRoleService aiChatRoleService; |
| 43 | + private final AiChatConversationMapper aiChatConversationMapper; | |
| 42 | 44 | |
| 43 | 45 | @Override |
| 44 | - public AiChatConversationRespVO createConversation(AiChatConversationCreateUserReq req) { | |
| 46 | + public Long createConversation(AiChatConversationCreateReqVO req) { | |
| 45 | 47 | // 获取用户id |
| 46 | 48 | Long loginUserId = SecurityFrameworkUtils.getLoginUserId(); |
| 47 | - // 查询最新的对话 | |
| 48 | - AiChatConversationDO latestConversation = aiChatConversationMapper.selectLatestConversation(loginUserId); | |
| 49 | - // 如果有对话没有被使用过,那就返回这个 | |
| 50 | - if (latestConversation != null && latestConversation.getChatCount() <= 0) { | |
| 51 | - return AiChatConversationConvert.INSTANCE.covnertChatConversationRes(latestConversation); | |
| 52 | - } | |
| 53 | - // 获取第一个模型 | |
| 49 | + // 默认使用 sort 排序第一个模型 | |
| 54 | 50 | AiChatModalDO aiChatModalDO = aiChatModalMapper.selectFirstModal(); |
| 55 | - // 创建新的 Conversation | |
| 56 | - AiChatConversationDO insertConversation = saveConversation(req.getTitle(), loginUserId, | |
| 57 | - null, null, AiChatConversationTypeEnum.USER_CHAT, | |
| 58 | - aiChatModalDO.getId(), aiChatModalDO.getModal()); | |
| 59 | - // 转换 res | |
| 60 | - return AiChatConversationConvert.INSTANCE.covnertChatConversationRes(insertConversation); | |
| 61 | - } | |
| 62 | - | |
| 63 | - @Override | |
| 64 | - public AiChatConversationRespVO createRoleConversation(AiChatConversationCreateReqVO req) { | |
| 65 | - // 获取用户id | |
| 66 | - Long loginUserId = SecurityFrameworkUtils.getLoginUserId(); | |
| 67 | - // 查询最新的对话 | |
| 68 | -// AiChatConversationDO latestConversation = aiChatConversationMapper.selectLatestConversation(loginUserId); | |
| 69 | -// // 如果有对话没有被使用过,那就返回这个 | |
| 70 | -// if (latestConversation != null && latestConversation.getChatCount() <= 0) { | |
| 71 | -// return AiChatConversationConvert.INSTANCE.covnertChatConversationRes(latestConversation); | |
| 72 | -// } | |
| 73 | 51 | // 查询角色 |
| 74 | - AiChatRoleRes chatRoleRes = aiChatRoleService.getChatRole(req.getRoleId()); | |
| 75 | - // 获取第一个模型 | |
| 76 | - AiChatModalDO aiChatModalDO = aiChatModalMapper.selectFirstModal(); | |
| 52 | + AiChatRoleRes chatRoleRes = null; | |
| 53 | + if (req.getRoleId() != null) { | |
| 54 | + chatRoleRes = aiChatRoleService.getChatRole(req.getRoleId()); | |
| 55 | + } | |
| 56 | + Long chatRoleId = chatRoleRes != null ? chatRoleRes.getId() : null; | |
| 77 | 57 | // 创建新的 Conversation |
| 78 | - AiChatConversationDO insertConversation = saveConversation(req.getTitle(), loginUserId, | |
| 79 | - req.getRoleId(), chatRoleRes.getName(), AiChatConversationTypeEnum.ROLE_CHAT, | |
| 80 | - aiChatModalDO.getId(), aiChatModalDO.getModal()); | |
| 81 | - // 转换 res | |
| 82 | - return AiChatConversationConvert.INSTANCE.covnertChatConversationRes(insertConversation); | |
| 58 | + AiChatConversationDO insertConversation = saveConversation(AiCommonConstants.CONVERSATION_DEFAULT_TITLE, | |
| 59 | + loginUserId, chatRoleId, aiChatModalDO.getId(), aiChatModalDO.getModel() | |
| 60 | + ); | |
| 61 | + // 返回对话id | |
| 62 | + return insertConversation.getId(); | |
| 83 | 63 | } |
| 84 | 64 | |
| 85 | 65 | private @NotNull AiChatConversationDO saveConversation(String title, |
| 86 | 66 | Long userId, |
| 87 | 67 | Long roleId, |
| 88 | - String roleName, | |
| 89 | - AiChatConversationTypeEnum typeEnum, | |
| 90 | 68 | Long modalId, |
| 91 | - String modal) { | |
| 69 | + String model) { | |
| 92 | 70 | AiChatConversationDO insertConversation = new AiChatConversationDO(); |
| 93 | 71 | insertConversation.setId(null); |
| 94 | 72 | insertConversation.setUserId(userId); |
| 95 | - insertConversation.setRoleId(roleId); | |
| 96 | - insertConversation.setRoleName(roleName); | |
| 97 | 73 | insertConversation.setTitle(title); |
| 98 | - insertConversation.setChatCount(0); | |
| 99 | - insertConversation.setType(typeEnum.getType()); | |
| 100 | - insertConversation.setModalId(modalId); | |
| 101 | - insertConversation.setModal(modal); | |
| 74 | + insertConversation.setPinned(false); | |
| 75 | + | |
| 76 | + insertConversation.setRoleId(roleId); | |
| 77 | + insertConversation.setModelId(modalId); | |
| 78 | + insertConversation.setModel(model); | |
| 79 | + | |
| 80 | + insertConversation.setTemperature(null); | |
| 81 | + insertConversation.setMaxTokens(null); | |
| 82 | + insertConversation.setMaxContexts(null); | |
| 102 | 83 | aiChatConversationMapper.insert(insertConversation); |
| 103 | 84 | return insertConversation; |
| 104 | 85 | } |
| 105 | 86 | |
| 106 | 87 | @Override |
| 107 | - public AiChatConversationRespVO getConversation(Long id) { | |
| 108 | - AiChatConversationDO aiChatConversationDO = validateExists(id); | |
| 109 | - return AiChatConversationConvert.INSTANCE.covnertChatConversationRes(aiChatConversationDO); | |
| 110 | - } | |
| 111 | - | |
| 112 | - private @NotNull AiChatConversationDO validateExists(Long id) { | |
| 113 | - AiChatConversationDO aiChatConversationDO = aiChatConversationMapper.selectById(id); | |
| 114 | - if (aiChatConversationDO == null) { | |
| 115 | - throw ServiceExceptionUtil.exception(ErrorCodeConstants.AI_CHAT_CONTINUE_NOT_EXIST); | |
| 88 | + public Boolean updateConversation(AiChatConversationUpdateReqVO updateReqVO) { | |
| 89 | + // 校验对话是否存在 | |
| 90 | + validateExists(updateReqVO.getId()); | |
| 91 | + // 获取模型信息并验证 | |
| 92 | + AiChatModalRes chatModal = aiChatModalService.getChatModalOfValidate(updateReqVO.getModelId()); | |
| 93 | + // 校验modal是否可用 | |
| 94 | + if (AiChatModalDisableEnum.YES.getValue().equals(chatModal.getDisable())) { | |
| 95 | + throw ServiceExceptionUtil.exception(ErrorCodeConstants.AI_MODAL_DISABLE_NOT_USED); | |
| 116 | 96 | } |
| 117 | - return aiChatConversationDO; | |
| 97 | + // 更新对话信息 | |
| 98 | + AiChatConversationDO updateAiChatConversationDO | |
| 99 | + = AiChatConversationConvert.INSTANCE.convertAiChatConversationDO(updateReqVO); | |
| 100 | + return aiChatConversationMapper.updateById(updateAiChatConversationDO) > 0; | |
| 118 | 101 | } |
| 119 | 102 | |
| 120 | 103 | @Override |
| 121 | - public List<AiChatConversationRespVO> listConversation(AiChatConversationListReq req) { | |
| 104 | + public List<AiChatConversationRespVO> listConversation(AiChatConversationListReqVO listReqVO) { | |
| 122 | 105 | // 获取用户id |
| 123 | 106 | Long loginUserId = SecurityFrameworkUtils.getLoginUserId(); |
| 124 | 107 | // 查询前100对话 |
| 125 | 108 | List<AiChatConversationDO> top100Conversation |
| 126 | - = aiChatConversationMapper.selectTop100Conversation(loginUserId, req.getSearch()); | |
| 109 | + = aiChatConversationMapper.selectTop100Conversation(loginUserId, listReqVO.getTitle()); | |
| 127 | 110 | return AiChatConversationConvert.INSTANCE.covnertChatConversationResList(top100Conversation); |
| 128 | 111 | } |
| 129 | 112 | |
| 130 | 113 | @Override |
| 131 | - public void updateModal(Long id, Long modalId) { | |
| 132 | - // 校验对话是否存在 | |
| 133 | - validateExists(id); | |
| 134 | - // 获取模型 | |
| 135 | - AiChatModalRes chatModal = aiChatModalService.getChatModal(modalId); | |
| 136 | - // 判断模型是否禁用 | |
| 137 | - if (AiChatModalDisableEnum.YES.getValue().equals(chatModal.getDisable())) { | |
| 138 | - throw ServiceExceptionUtil.exception(ErrorCodeConstants.AI_MODAL_DISABLE_NOT_USED); | |
| 139 | - } | |
| 140 | - // 更新对话 | |
| 141 | - aiChatConversationMapper.updateById(new AiChatConversationDO() | |
| 142 | - .setId(id) | |
| 143 | - .setModalId(chatModal.getId()) | |
| 144 | - .setModal(chatModal.getModal()) | |
| 145 | - ); | |
| 114 | + public AiChatConversationRespVO getConversationOfValidate(Long id) { | |
| 115 | + AiChatConversationDO aiChatConversationDO = validateExists(id); | |
| 116 | + return AiChatConversationConvert.INSTANCE.covnertChatConversationRes(aiChatConversationDO); | |
| 146 | 117 | } |
| 147 | 118 | |
| 148 | 119 | @Override |
| 149 | - public void delete(Long id) { | |
| 150 | - aiChatConversationMapper.deleteById(id); | |
| 120 | + public Boolean deleteConversation(Long id) { | |
| 121 | + return aiChatConversationMapper.deleteById(id) > 0; | |
| 122 | + } | |
| 123 | + | |
| 124 | + private @NotNull AiChatConversationDO validateExists(Long id) { | |
| 125 | + AiChatConversationDO aiChatConversationDO = aiChatConversationMapper.selectById(id); | |
| 126 | + if (aiChatConversationDO == null) { | |
| 127 | + throw ServiceExceptionUtil.exception(ErrorCodeConstants.AI_CONVERSATION_NOT_EXISTS); | |
| 128 | + } | |
| 129 | + return aiChatConversationDO; | |
| 151 | 130 | } |
| 152 | 131 | } | ... | ... |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/impl/AiChatModalServiceImpl.java
| ... | ... | @@ -12,6 +12,7 @@ import cn.iocoder.yudao.framework.common.util.json.JsonUtils; |
| 12 | 12 | import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; |
| 13 | 13 | import cn.iocoder.yudao.module.ai.ErrorCodeConstants; |
| 14 | 14 | import cn.iocoder.yudao.module.ai.convert.AiChatModalConvert; |
| 15 | +import cn.iocoder.yudao.module.ai.dal.dataobject.model.AiChatModalDO; | |
| 15 | 16 | import cn.iocoder.yudao.module.ai.dal.vo.AiChatModalChatConfigVO; |
| 16 | 17 | import cn.iocoder.yudao.module.ai.dal.vo.AiChatModalConfigVO; |
| 17 | 18 | import cn.iocoder.yudao.module.ai.dal.vo.AiChatModalDallConfigVO; |
| ... | ... | @@ -109,7 +110,7 @@ public class AiChatModalServiceImpl implements AiChatModalService { |
| 109 | 110 | } |
| 110 | 111 | |
| 111 | 112 | @Override |
| 112 | - public AiChatModalRes getChatModal(Long modalId) { | |
| 113 | + public AiChatModalRes getChatModalOfValidate(Long modalId) { | |
| 113 | 114 | // 检查 modal 是否存在 |
| 114 | 115 | AiChatModalDO aiChatModalDO = validateChatModalExists(modalId); |
| 115 | 116 | return AiChatModalConvert.INSTANCE.convertAiChatModalRes(aiChatModalDO); | ... | ... |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/impl/AiChatServiceImpl.java
| ... | ... | @@ -58,7 +58,7 @@ public class AiChatServiceImpl implements AiChatService { |
| 58 | 58 | // 获取 client 类型 |
| 59 | 59 | AiPlatformEnum platformEnum = AiPlatformEnum.valueOfPlatform(req.getModal()); |
| 60 | 60 | // 获取对话信息 |
| 61 | - AiChatConversationRespVO conversationRes = chatConversationService.getConversation(req.getConversationId()); | |
| 61 | + AiChatConversationRespVO conversationRes = chatConversationService.getConversationOfValidate(req.getConversationId()); | |
| 62 | 62 | // 保存 chat message |
| 63 | 63 | saveChatMessage(req, conversationRes, loginUserId); |
| 64 | 64 | String content = null; |
| ... | ... | @@ -133,7 +133,7 @@ public class AiChatServiceImpl implements AiChatService { |
| 133 | 133 | // 获取 client 类型 |
| 134 | 134 | AiPlatformEnum platformEnum = AiPlatformEnum.valueOfPlatform(req.getModal()); |
| 135 | 135 | // 获取对话信息 |
| 136 | - AiChatConversationRespVO conversationRes = chatConversationService.getConversation(req.getConversationId()); | |
| 136 | + AiChatConversationRespVO conversationRes = chatConversationService.getConversationOfValidate(req.getConversationId()); | |
| 137 | 137 | // 创建 chat 需要的 Prompt |
| 138 | 138 | Prompt prompt = new Prompt(req.getPrompt()); |
| 139 | 139 | req.setTopK(req.getTopK()); | ... | ... |