增加chat 对话,列表、创建、单个、删除

This commit is contained in:
cherishsince 2024-04-18 16:59:15 +08:00
parent f84d25d3b7
commit 00c2ca8b48
8 changed files with 282 additions and 0 deletions

View File

@ -0,0 +1,58 @@
package cn.iocoder.yudao.module.ai.controller;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.ai.service.ChatConversationService;
import cn.iocoder.yudao.module.ai.vo.ChatConversationCreateReq;
import cn.iocoder.yudao.module.ai.vo.ChatConversationCreateRes;
import cn.iocoder.yudao.module.ai.vo.ChatConversationListReq;
import cn.iocoder.yudao.module.ai.vo.ChatConversationRes;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* ia 模块
*
* @author fansili
* @time 2024/4/13 17:44
* @since 1.0
*/
@Tag(name = "A2聊天-对话")
@RestController
@RequestMapping("/ai/chat/conversation")
@Slf4j
@AllArgsConstructor
public class ChatConversationController {
private final ChatConversationService chatConversationService;
@Operation(summary = "创建 - 对话")
@PostMapping("/create")
public CommonResult<ChatConversationRes> create(@RequestBody @Validated ChatConversationCreateReq req) {
return CommonResult.success(chatConversationService.create(req));
}
@Operation(summary = "获取 - 获取对话")
@GetMapping("/getConversation")
public CommonResult<ChatConversationRes> getConversation(@RequestParam("id") Long id) {
return CommonResult.success(chatConversationService.getConversation(id));
}
@Operation(summary = "获取 - 获取对话list")
@GetMapping("/listConversation")
public CommonResult<List<ChatConversationRes>> listConversation(@ModelAttribute @Validated ChatConversationListReq req) {
return CommonResult.success(chatConversationService.listConversation(req));
}
@Operation(summary = "删除")
@DeleteMapping("/listConversation")
public CommonResult<Void> delete(@RequestParam("id") Long id) {
chatConversationService.delete(id);
return CommonResult.success(null);
}
}

View File

@ -0,0 +1,37 @@
package cn.iocoder.yudao.module.ai.convert;
import cn.iocoder.yudao.module.ai.dataobject.AiChatConversationDO;
import cn.iocoder.yudao.module.ai.vo.ChatConversationRes;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import java.util.List;
/**
* 聊天 对话 convert
*
* @author fansili
* @time 2024/4/18 16:39
* @since 1.0
*/
@Mapper
public interface ChatConversationConvert {
ChatConversationConvert INSTANCE = Mappers.getMapper(ChatConversationConvert.class);
/**
* 转换 - 多个 ChatConversationRes
*
* @param top100Conversation
* @return
*/
List<ChatConversationRes> covnertChatConversationResList(List<AiChatConversationDO> top100Conversation);
/**
* 转换 - 单个 ChatConversationRes
*
* @param aiChatConversationDO
* @return
*/
ChatConversationRes covnertChatConversationRes(AiChatConversationDO aiChatConversationDO);
}

View File

@ -0,0 +1,48 @@
package cn.iocoder.yudao.module.ai.service;
import cn.iocoder.yudao.module.ai.vo.ChatConversationCreateReq;
import cn.iocoder.yudao.module.ai.vo.ChatConversationCreateRes;
import cn.iocoder.yudao.module.ai.vo.ChatConversationListReq;
import cn.iocoder.yudao.module.ai.vo.ChatConversationRes;
import java.util.List;
/**
* chat 对话
*
* @fansili
* @since v1.0
*/
public interface ChatConversationService {
/**
* 对话 - 创建
*
* @param req
* @return
*/
ChatConversationRes create(ChatConversationCreateReq req);
/**
* 获取 - 对话
*
* @param id
* @return
*/
ChatConversationRes getConversation(Long id);
/**
* 获取 - 对话列表
*
* @param req
* @return
*/
List<ChatConversationRes> listConversation(ChatConversationListReq req);
/**
* 删除 - 根据id
*
* @param id
*/
void delete(Long id);
}

View File

@ -0,0 +1,74 @@
package cn.iocoder.yudao.module.ai.service.impl;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.ai.convert.ChatConversationConvert;
import cn.iocoder.yudao.module.ai.dataobject.AiChatConversationDO;
import cn.iocoder.yudao.module.ai.mapper.AiChatConversationMapper;
import cn.iocoder.yudao.module.ai.service.ChatConversationService;
import cn.iocoder.yudao.module.ai.vo.ChatConversationCreateReq;
import cn.iocoder.yudao.module.ai.vo.ChatConversationListReq;
import cn.iocoder.yudao.module.ai.vo.ChatConversationRes;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* chat 对话
*
* @fansili
* @since v1.0
*/
@Service
@Slf4j
@AllArgsConstructor
public class ChatConversationServiceImpl implements ChatConversationService {
private final AiChatConversationMapper aiChatConversationMapper;
@Override
public ChatConversationRes create(ChatConversationCreateReq req) {
// 获取用户id
Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
// 查询最新的对话
AiChatConversationDO latestConversation = aiChatConversationMapper.selectLatestConversation(loginUserId);
// 如果有对话没有被使用过那就返回这个
if (latestConversation != null && latestConversation.getChatCount() <= 0) {
return ChatConversationConvert.INSTANCE.covnertChatConversationRes(latestConversation);
}
// 创建新的 Conversation
AiChatConversationDO insertConversation = new AiChatConversationDO();
insertConversation.setId(null);
insertConversation.setUserId(loginUserId);
insertConversation.setChatRoleId(null);
insertConversation.setChatRoleName(null);
insertConversation.setChatTitle(null);
insertConversation.setChatCount(0);
insertConversation.setChatType(req.getChatType());
aiChatConversationMapper.insert(insertConversation);
// 转换 res
return ChatConversationConvert.INSTANCE.covnertChatConversationRes(latestConversation);
}
@Override
public ChatConversationRes getConversation(Long id) {
AiChatConversationDO aiChatConversationDO = aiChatConversationMapper.selectById(id);
return ChatConversationConvert.INSTANCE.covnertChatConversationRes(aiChatConversationDO);
}
@Override
public List<ChatConversationRes> listConversation(ChatConversationListReq req) {
// 获取用户id
Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
// 查询前100对话
List<AiChatConversationDO> top100Conversation
= aiChatConversationMapper.selectTop100Conversation(loginUserId, req.getSearch());
return ChatConversationConvert.INSTANCE.covnertChatConversationResList(top100Conversation);
}
@Override
public void delete(Long id) {
aiChatConversationMapper.deleteById(id);
}
}

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;
/**
* 聊天对话
*
* @author fansili
* @time 2024/4/18 16:24
* @since 1.0
*/
@Data
@Accessors(chain = true)
public class ChatConversationCreateReq {
@Schema(description = "对话类型(roleChat、userChat)")
@NotNull(message = "聊天类型不能为空!")
private String chatType;
}

View File

@ -0,0 +1,11 @@
package cn.iocoder.yudao.module.ai.vo;
/**
* 聊天对话 res
*
* @author fansili
* @time 2024/4/18 16:24
* @since 1.0
*/
public class ChatConversationCreateRes {
}

View File

@ -0,0 +1,20 @@
package cn.iocoder.yudao.module.ai.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.experimental.Accessors;
/**
* 聊天对话 list req
*
* @author fansili
* @time 2024/4/18 16:24
* @since 1.0
*/
@Data
@Accessors(chain = true)
public class ChatConversationListReq {
@Schema(description = "查询根据title")
private String search;
}

View File

@ -0,0 +1,11 @@
package cn.iocoder.yudao.module.ai.vo;
/**
* 聊天对话 res
*
* @author fansili
* @time 2024/4/18 16:24
* @since 1.0
*/
public class ChatConversationRes {
}