Commit 64fdd045a5006c8fae0bbae02395da10e51fb36d

Authored by cherishsince
1 parent 1f5878ed

增加 chat message 删除

yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/ChatMessageController.java
1 package cn.iocoder.yudao.module.ai.controller; 1 package cn.iocoder.yudao.module.ai.controller;
2 2
  3 +import cn.iocoder.yudao.framework.common.pojo.CommonResult;
  4 +import cn.iocoder.yudao.framework.common.pojo.PageResult;
  5 +import cn.iocoder.yudao.module.ai.service.ChatMessageService;
  6 +import cn.iocoder.yudao.module.ai.vo.ChatMessageListRes;
  7 +import cn.iocoder.yudao.module.ai.vo.ChatMessageReq;
  8 +import io.swagger.v3.oas.annotations.Operation;
3 import io.swagger.v3.oas.annotations.tags.Tag; 9 import io.swagger.v3.oas.annotations.tags.Tag;
4 import lombok.AllArgsConstructor; 10 import lombok.AllArgsConstructor;
5 import lombok.extern.slf4j.Slf4j; 11 import lombok.extern.slf4j.Slf4j;
6 -import org.springframework.web.bind.annotation.RequestMapping;  
7 -import org.springframework.web.bind.annotation.RestController; 12 +import org.springframework.validation.annotation.Validated;
  13 +import org.springframework.web.bind.annotation.*;
8 14
9 /** 15 /**
10 * chat message 16 * chat message
@@ -15,8 +21,24 @@ import org.springframework.web.bind.annotation.RestController; @@ -15,8 +21,24 @@ import org.springframework.web.bind.annotation.RestController;
15 */ 21 */
16 @Tag(name = "A3-聊天-对话") 22 @Tag(name = "A3-聊天-对话")
17 @RestController 23 @RestController
18 -@RequestMapping("/ai/chat/conversation") 24 +@RequestMapping("/ai/chat/message")
19 @Slf4j 25 @Slf4j
20 @AllArgsConstructor 26 @AllArgsConstructor
21 public class ChatMessageController { 27 public class ChatMessageController {
  28 +
  29 + private final ChatMessageService chatMessageService;
  30 +
  31 + @Operation(summary = "聊天记录", description = "查询个人的聊天记录")
  32 + @GetMapping("/list")
  33 + public PageResult<ChatMessageListRes> list(@Validated @ModelAttribute ChatMessageReq req) {
  34 + return chatMessageService.list(req);
  35 + }
  36 +
  37 + @Operation(summary = "聊天记录", description = "查询个人的聊天记录")
  38 + @DeleteMapping("/{chatConversationId}/{id}")
  39 + public CommonResult delete(@PathVariable("chatConversationId") Long chatConversationId,
  40 + @PathVariable("id") Long id) {
  41 + chatMessageService.delete(chatConversationId, id);
  42 + return CommonResult.success(null);
  43 + }
22 } 44 }
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/mapper/AiChatMessageMapper.java
1 package cn.iocoder.yudao.module.ai.mapper; 1 package cn.iocoder.yudao.module.ai.mapper;
2 2
3 import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; 3 import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
  4 +import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
4 import cn.iocoder.yudao.module.ai.dal.dataobject.AiChatMessageDO; 5 import cn.iocoder.yudao.module.ai.dal.dataobject.AiChatMessageDO;
5 import org.apache.ibatis.annotations.Mapper; 6 import org.apache.ibatis.annotations.Mapper;
6 import org.springframework.stereotype.Repository; 7 import org.springframework.stereotype.Repository;
@@ -14,4 +15,17 @@ import org.springframework.stereotype.Repository; @@ -14,4 +15,17 @@ import org.springframework.stereotype.Repository;
14 @Repository 15 @Repository
15 @Mapper 16 @Mapper
16 public interface AiChatMessageMapper extends BaseMapperX<AiChatMessageDO> { 17 public interface AiChatMessageMapper extends BaseMapperX<AiChatMessageDO> {
  18 +
  19 + /**
  20 + * 删除 - 根据 Conversation 和 id
  21 + *
  22 + * @param chatConversationId
  23 + * @param id
  24 + */
  25 + default int deleteByConversationAndId(Long chatConversationId, Long id) {
  26 + return this.delete(new LambdaQueryWrapperX<AiChatMessageDO>()
  27 + .eq(AiChatMessageDO::getChatConversationId, chatConversationId)
  28 + .eq(AiChatMessageDO::getId, id)
  29 + );
  30 + }
17 } 31 }
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/ChatMessageService.java 0 → 100644
  1 +package cn.iocoder.yudao.module.ai.service;
  2 +
  3 +import cn.iocoder.yudao.framework.common.pojo.PageResult;
  4 +import cn.iocoder.yudao.module.ai.vo.ChatMessageListRes;
  5 +import cn.iocoder.yudao.module.ai.vo.ChatMessageReq;
  6 +
  7 +/**
  8 + * chat message
  9 + *
  10 + * @author fansili
  11 + * @time 2024/4/24 17:25
  12 + * @since 1.0
  13 + */
  14 +public interface ChatMessageService {
  15 +
  16 + /**
  17 + * message - 列表
  18 + *
  19 + * @param req
  20 + * @return
  21 + */
  22 + PageResult<ChatMessageListRes> list(ChatMessageReq req);
  23 +
  24 + /**
  25 + * message - 删除
  26 + *
  27 + * @param chatConversationId
  28 + * @param id
  29 + */
  30 + void delete(Long chatConversationId, Long id);
  31 +}
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/impl/ChatMessageServiceImpl.java 0 → 100644
  1 +package cn.iocoder.yudao.module.ai.service.impl;
  2 +
  3 +import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
  4 +import cn.iocoder.yudao.framework.common.pojo.PageResult;
  5 +import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
  6 +import cn.iocoder.yudao.module.ai.ErrorCodeConstants;
  7 +import cn.iocoder.yudao.module.ai.dal.dataobject.AiChatConversationDO;
  8 +import cn.iocoder.yudao.module.ai.mapper.AiChatConversationMapper;
  9 +import cn.iocoder.yudao.module.ai.mapper.AiChatMessageMapper;
  10 +import cn.iocoder.yudao.module.ai.service.ChatMessageService;
  11 +import cn.iocoder.yudao.module.ai.vo.ChatMessageListRes;
  12 +import cn.iocoder.yudao.module.ai.vo.ChatMessageReq;
  13 +import lombok.AllArgsConstructor;
  14 +import lombok.extern.slf4j.Slf4j;
  15 +import org.springframework.stereotype.Service;
  16 +
  17 +/**
  18 + * chat message
  19 + *
  20 + * @author fansili
  21 + * @time 2024/4/24 17:25
  22 + * @since 1.0
  23 + */
  24 +@AllArgsConstructor
  25 +@Service
  26 +@Slf4j
  27 +public class ChatMessageServiceImpl implements ChatMessageService {
  28 +
  29 + private final AiChatMessageMapper aiChatMessageMapper;
  30 + private final AiChatConversationMapper aiChatConversationMapper;
  31 +
  32 + @Override
  33 + public PageResult<ChatMessageListRes> list(ChatMessageReq req) {
  34 + return null;
  35 + }
  36 +
  37 + @Override
  38 + public void delete(Long chatConversationId, Long id) {
  39 + // 获取登录用户
  40 + Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
  41 + // 校验 ChatConversation
  42 + validateChatConversation(chatConversationId, loginUserId);
  43 + // 删除
  44 + aiChatMessageMapper.deleteByConversationAndId(chatConversationId, id);
  45 + }
  46 +
  47 + private AiChatConversationDO validateChatConversation(Long chatConversationId, Long loginUserId) {
  48 + AiChatConversationDO aiChatConversationDO = aiChatConversationMapper.selectById(chatConversationId);
  49 + if (aiChatConversationDO == null) {
  50 + throw ServiceExceptionUtil.exception(ErrorCodeConstants.AI_CHAT_CONTINUE_NOT_EXIST);
  51 + }
  52 + if (!aiChatConversationDO.getUserId().equals(loginUserId)) {
  53 + throw ServiceExceptionUtil.exception(ErrorCodeConstants.AI_CHAT_CONVERSATION_NOT_YOURS);
  54 + }
  55 + return aiChatConversationDO;
  56 + }
  57 +}
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/vo/ChatMessageListRes.java 0 → 100644
  1 +package cn.iocoder.yudao.module.ai.vo;
  2 +
  3 +import io.swagger.v3.oas.annotations.media.Schema;
  4 +import lombok.Data;
  5 +import lombok.experimental.Accessors;
  6 +
  7 +/**
  8 + * 看板 message list req
  9 + *
  10 + * @author fansili
  11 + * @time 2024/4/24 17:28
  12 + * @since 1.0
  13 + */
  14 +@Data
  15 +@Accessors(chain = true)
  16 +public class ChatMessageListRes {
  17 +
  18 + @Schema(description = "编号")
  19 + private Long id;
  20 +
  21 + @Schema(description = "聊天ID,关联到特定的会话或对话")
  22 + private Long chatConversationId;
  23 +
  24 + @Schema(description = "角色ID,用于标识发送消息的用户或系统的身份")
  25 + private Long userId;
  26 +
  27 + @Schema(description = "消息具体内容,存储用户的发言或者系统响应的文字信息")
  28 + private String message;
  29 +
  30 + @Schema(description = "消息类型,枚举值可能包括'system'(系统消息)、'user'(用户消息)和'assistant'(助手消息)")
  31 + private String messageType;
  32 +
  33 + @Schema(description = "在生成消息时采用的Top-K采样大小")
  34 + private Double topK;
  35 +
  36 + @Schema(description = "Top-P核采样方法的概率阈值")
  37 + private Double topP;
  38 +
  39 + @Schema(description = "温度参数,用于调整生成回复的随机性和多样性程度,")
  40 + private Double temperature;
  41 +
  42 +}
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/vo/ChatMessageReq.java 0 → 100644
  1 +package cn.iocoder.yudao.module.ai.vo;
  2 +
  3 +import io.swagger.v3.oas.annotations.media.Schema;
  4 +import jakarta.validation.constraints.NotNull;
  5 +import lombok.Data;
  6 +import lombok.experimental.Accessors;
  7 +
  8 +/**
  9 + * chat message list req
  10 + *
  11 + * @author fansili
  12 + * @time 2024/4/14 16:12
  13 + * @since 1.0
  14 + */
  15 +@Data
  16 +@Accessors(chain = true)
  17 +public class ChatMessageReq {
  18 +
  19 + @Schema(description = "聊天ID,关联到特定的会话或对话")
  20 + @NotNull
  21 + private Long chatConversationId;
  22 +
  23 +}