增加 chat message 删除

This commit is contained in:
cherishsince 2024-04-24 17:50:11 +08:00
parent 1f5878ed5b
commit 64fdd045a5
6 changed files with 192 additions and 3 deletions

View File

@ -1,10 +1,16 @@
package cn.iocoder.yudao.module.ai.controller; package cn.iocoder.yudao.module.ai.controller;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.ai.service.ChatMessageService;
import cn.iocoder.yudao.module.ai.vo.ChatMessageListRes;
import cn.iocoder.yudao.module.ai.vo.ChatMessageReq;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.*;
/** /**
* chat message * chat message
@ -15,8 +21,24 @@ import org.springframework.web.bind.annotation.RestController;
*/ */
@Tag(name = "A3-聊天-对话") @Tag(name = "A3-聊天-对话")
@RestController @RestController
@RequestMapping("/ai/chat/conversation") @RequestMapping("/ai/chat/message")
@Slf4j @Slf4j
@AllArgsConstructor @AllArgsConstructor
public class ChatMessageController { public class ChatMessageController {
private final ChatMessageService chatMessageService;
@Operation(summary = "聊天记录", description = "查询个人的聊天记录")
@GetMapping("/list")
public PageResult<ChatMessageListRes> list(@Validated @ModelAttribute ChatMessageReq req) {
return chatMessageService.list(req);
}
@Operation(summary = "聊天记录", description = "查询个人的聊天记录")
@DeleteMapping("/{chatConversationId}/{id}")
public CommonResult delete(@PathVariable("chatConversationId") Long chatConversationId,
@PathVariable("id") Long id) {
chatMessageService.delete(chatConversationId, id);
return CommonResult.success(null);
}
} }

View File

@ -1,6 +1,7 @@
package cn.iocoder.yudao.module.ai.mapper; package cn.iocoder.yudao.module.ai.mapper;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.ai.dal.dataobject.AiChatMessageDO; import cn.iocoder.yudao.module.ai.dal.dataobject.AiChatMessageDO;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@ -14,4 +15,17 @@ import org.springframework.stereotype.Repository;
@Repository @Repository
@Mapper @Mapper
public interface AiChatMessageMapper extends BaseMapperX<AiChatMessageDO> { public interface AiChatMessageMapper extends BaseMapperX<AiChatMessageDO> {
/**
* 删除 - 根据 Conversation id
*
* @param chatConversationId
* @param id
*/
default int deleteByConversationAndId(Long chatConversationId, Long id) {
return this.delete(new LambdaQueryWrapperX<AiChatMessageDO>()
.eq(AiChatMessageDO::getChatConversationId, chatConversationId)
.eq(AiChatMessageDO::getId, id)
);
}
} }

View File

@ -0,0 +1,31 @@
package cn.iocoder.yudao.module.ai.service;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.ai.vo.ChatMessageListRes;
import cn.iocoder.yudao.module.ai.vo.ChatMessageReq;
/**
* chat message
*
* @author fansili
* @time 2024/4/24 17:25
* @since 1.0
*/
public interface ChatMessageService {
/**
* message - 列表
*
* @param req
* @return
*/
PageResult<ChatMessageListRes> list(ChatMessageReq req);
/**
* message - 删除
*
* @param chatConversationId
* @param id
*/
void delete(Long chatConversationId, Long id);
}

View File

@ -0,0 +1,57 @@
package cn.iocoder.yudao.module.ai.service.impl;
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.ai.ErrorCodeConstants;
import cn.iocoder.yudao.module.ai.dal.dataobject.AiChatConversationDO;
import cn.iocoder.yudao.module.ai.mapper.AiChatConversationMapper;
import cn.iocoder.yudao.module.ai.mapper.AiChatMessageMapper;
import cn.iocoder.yudao.module.ai.service.ChatMessageService;
import cn.iocoder.yudao.module.ai.vo.ChatMessageListRes;
import cn.iocoder.yudao.module.ai.vo.ChatMessageReq;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
/**
* chat message
*
* @author fansili
* @time 2024/4/24 17:25
* @since 1.0
*/
@AllArgsConstructor
@Service
@Slf4j
public class ChatMessageServiceImpl implements ChatMessageService {
private final AiChatMessageMapper aiChatMessageMapper;
private final AiChatConversationMapper aiChatConversationMapper;
@Override
public PageResult<ChatMessageListRes> list(ChatMessageReq req) {
return null;
}
@Override
public void delete(Long chatConversationId, Long id) {
// 获取登录用户
Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
// 校验 ChatConversation
validateChatConversation(chatConversationId, loginUserId);
// 删除
aiChatMessageMapper.deleteByConversationAndId(chatConversationId, id);
}
private AiChatConversationDO validateChatConversation(Long chatConversationId, Long loginUserId) {
AiChatConversationDO aiChatConversationDO = aiChatConversationMapper.selectById(chatConversationId);
if (aiChatConversationDO == null) {
throw ServiceExceptionUtil.exception(ErrorCodeConstants.AI_CHAT_CONTINUE_NOT_EXIST);
}
if (!aiChatConversationDO.getUserId().equals(loginUserId)) {
throw ServiceExceptionUtil.exception(ErrorCodeConstants.AI_CHAT_CONVERSATION_NOT_YOURS);
}
return aiChatConversationDO;
}
}

View File

@ -0,0 +1,42 @@
package cn.iocoder.yudao.module.ai.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.experimental.Accessors;
/**
* 看板 message list req
*
* @author fansili
* @time 2024/4/24 17:28
* @since 1.0
*/
@Data
@Accessors(chain = true)
public class ChatMessageListRes {
@Schema(description = "编号")
private Long id;
@Schema(description = "聊天ID关联到特定的会话或对话")
private Long chatConversationId;
@Schema(description = "角色ID用于标识发送消息的用户或系统的身份")
private Long userId;
@Schema(description = "消息具体内容,存储用户的发言或者系统响应的文字信息")
private String message;
@Schema(description = "消息类型,枚举值可能包括'system'(系统消息)、'user'(用户消息)和'assistant'(助手消息)")
private String messageType;
@Schema(description = "在生成消息时采用的Top-K采样大小")
private Double topK;
@Schema(description = "Top-P核采样方法的概率阈值")
private Double topP;
@Schema(description = "温度参数,用于调整生成回复的随机性和多样性程度,")
private Double temperature;
}

View File

@ -0,0 +1,23 @@
package cn.iocoder.yudao.module.ai.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import lombok.experimental.Accessors;
/**
* chat message list req
*
* @author fansili
* @time 2024/4/14 16:12
* @since 1.0
*/
@Data
@Accessors(chain = true)
public class ChatMessageReq {
@Schema(description = "聊天ID关联到特定的会话或对话")
@NotNull
private Long chatConversationId;
}