Commit 00c2ca8b48267b464327a1a14cc6fa1fab34625d
1 parent
f84d25d3
增加chat 对话,列表、创建、单个、删除
Showing
8 changed files
with
282 additions
and
0 deletions
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/ChatConversationController.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.ai.controller; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.framework.common.pojo.CommonResult; | |
| 4 | +import cn.iocoder.yudao.module.ai.service.ChatConversationService; | |
| 5 | +import cn.iocoder.yudao.module.ai.vo.ChatConversationCreateReq; | |
| 6 | +import cn.iocoder.yudao.module.ai.vo.ChatConversationCreateRes; | |
| 7 | +import cn.iocoder.yudao.module.ai.vo.ChatConversationListReq; | |
| 8 | +import cn.iocoder.yudao.module.ai.vo.ChatConversationRes; | |
| 9 | +import io.swagger.v3.oas.annotations.Operation; | |
| 10 | +import io.swagger.v3.oas.annotations.tags.Tag; | |
| 11 | +import lombok.AllArgsConstructor; | |
| 12 | +import lombok.extern.slf4j.Slf4j; | |
| 13 | +import org.springframework.validation.annotation.Validated; | |
| 14 | +import org.springframework.web.bind.annotation.*; | |
| 15 | + | |
| 16 | +import java.util.List; | |
| 17 | + | |
| 18 | +/** | |
| 19 | + * ia 模块 | |
| 20 | + * | |
| 21 | + * @author fansili | |
| 22 | + * @time 2024/4/13 17:44 | |
| 23 | + * @since 1.0 | |
| 24 | + */ | |
| 25 | +@Tag(name = "A2聊天-对话") | |
| 26 | +@RestController | |
| 27 | +@RequestMapping("/ai/chat/conversation") | |
| 28 | +@Slf4j | |
| 29 | +@AllArgsConstructor | |
| 30 | +public class ChatConversationController { | |
| 31 | + | |
| 32 | + private final ChatConversationService chatConversationService; | |
| 33 | + | |
| 34 | + @Operation(summary = "创建 - 对话") | |
| 35 | + @PostMapping("/create") | |
| 36 | + public CommonResult<ChatConversationRes> create(@RequestBody @Validated ChatConversationCreateReq req) { | |
| 37 | + return CommonResult.success(chatConversationService.create(req)); | |
| 38 | + } | |
| 39 | + | |
| 40 | + @Operation(summary = "获取 - 获取对话") | |
| 41 | + @GetMapping("/getConversation") | |
| 42 | + public CommonResult<ChatConversationRes> getConversation(@RequestParam("id") Long id) { | |
| 43 | + return CommonResult.success(chatConversationService.getConversation(id)); | |
| 44 | + } | |
| 45 | + | |
| 46 | + @Operation(summary = "获取 - 获取对话list") | |
| 47 | + @GetMapping("/listConversation") | |
| 48 | + public CommonResult<List<ChatConversationRes>> listConversation(@ModelAttribute @Validated ChatConversationListReq req) { | |
| 49 | + return CommonResult.success(chatConversationService.listConversation(req)); | |
| 50 | + } | |
| 51 | + | |
| 52 | + @Operation(summary = "删除") | |
| 53 | + @DeleteMapping("/listConversation") | |
| 54 | + public CommonResult<Void> delete(@RequestParam("id") Long id) { | |
| 55 | + chatConversationService.delete(id); | |
| 56 | + return CommonResult.success(null); | |
| 57 | + } | |
| 58 | +} | ... | ... |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/convert/ChatConversationConvert.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.ai.convert; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.module.ai.dataobject.AiChatConversationDO; | |
| 4 | +import cn.iocoder.yudao.module.ai.vo.ChatConversationRes; | |
| 5 | +import org.mapstruct.Mapper; | |
| 6 | +import org.mapstruct.factory.Mappers; | |
| 7 | + | |
| 8 | +import java.util.List; | |
| 9 | + | |
| 10 | +/** | |
| 11 | + * 聊天 对话 convert | |
| 12 | + * | |
| 13 | + * @author fansili | |
| 14 | + * @time 2024/4/18 16:39 | |
| 15 | + * @since 1.0 | |
| 16 | + */ | |
| 17 | +@Mapper | |
| 18 | +public interface ChatConversationConvert { | |
| 19 | + | |
| 20 | + ChatConversationConvert INSTANCE = Mappers.getMapper(ChatConversationConvert.class); | |
| 21 | + | |
| 22 | + /** | |
| 23 | + * 转换 - 多个 ChatConversationRes | |
| 24 | + * | |
| 25 | + * @param top100Conversation | |
| 26 | + * @return | |
| 27 | + */ | |
| 28 | + List<ChatConversationRes> covnertChatConversationResList(List<AiChatConversationDO> top100Conversation); | |
| 29 | + | |
| 30 | + /** | |
| 31 | + * 转换 - 单个 ChatConversationRes | |
| 32 | + * | |
| 33 | + * @param aiChatConversationDO | |
| 34 | + * @return | |
| 35 | + */ | |
| 36 | + ChatConversationRes covnertChatConversationRes(AiChatConversationDO aiChatConversationDO); | |
| 37 | +} | ... | ... |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/ChatConversationService.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.ai.service; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.module.ai.vo.ChatConversationCreateReq; | |
| 4 | +import cn.iocoder.yudao.module.ai.vo.ChatConversationCreateRes; | |
| 5 | +import cn.iocoder.yudao.module.ai.vo.ChatConversationListReq; | |
| 6 | +import cn.iocoder.yudao.module.ai.vo.ChatConversationRes; | |
| 7 | + | |
| 8 | +import java.util.List; | |
| 9 | + | |
| 10 | +/** | |
| 11 | + * chat 对话 | |
| 12 | + * | |
| 13 | + * @fansili | |
| 14 | + * @since v1.0 | |
| 15 | + */ | |
| 16 | +public interface ChatConversationService { | |
| 17 | + | |
| 18 | + /** | |
| 19 | + * 对话 - 创建 | |
| 20 | + * | |
| 21 | + * @param req | |
| 22 | + * @return | |
| 23 | + */ | |
| 24 | + ChatConversationRes create(ChatConversationCreateReq req); | |
| 25 | + | |
| 26 | + /** | |
| 27 | + * 获取 - 对话 | |
| 28 | + * | |
| 29 | + * @param id | |
| 30 | + * @return | |
| 31 | + */ | |
| 32 | + ChatConversationRes getConversation(Long id); | |
| 33 | + | |
| 34 | + /** | |
| 35 | + * 获取 - 对话列表 | |
| 36 | + * | |
| 37 | + * @param req | |
| 38 | + * @return | |
| 39 | + */ | |
| 40 | + List<ChatConversationRes> listConversation(ChatConversationListReq req); | |
| 41 | + | |
| 42 | + /** | |
| 43 | + * 删除 - 根据id | |
| 44 | + * | |
| 45 | + * @param id | |
| 46 | + */ | |
| 47 | + void delete(Long id); | |
| 48 | +} | ... | ... |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/impl/ChatConversationServiceImpl.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.ai.service.impl; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; | |
| 4 | +import cn.iocoder.yudao.module.ai.convert.ChatConversationConvert; | |
| 5 | +import cn.iocoder.yudao.module.ai.dataobject.AiChatConversationDO; | |
| 6 | +import cn.iocoder.yudao.module.ai.mapper.AiChatConversationMapper; | |
| 7 | +import cn.iocoder.yudao.module.ai.service.ChatConversationService; | |
| 8 | +import cn.iocoder.yudao.module.ai.vo.ChatConversationCreateReq; | |
| 9 | +import cn.iocoder.yudao.module.ai.vo.ChatConversationListReq; | |
| 10 | +import cn.iocoder.yudao.module.ai.vo.ChatConversationRes; | |
| 11 | +import lombok.AllArgsConstructor; | |
| 12 | +import lombok.extern.slf4j.Slf4j; | |
| 13 | +import org.springframework.stereotype.Service; | |
| 14 | + | |
| 15 | +import java.util.List; | |
| 16 | + | |
| 17 | +/** | |
| 18 | + * chat 对话 | |
| 19 | + * | |
| 20 | + * @fansili | |
| 21 | + * @since v1.0 | |
| 22 | + */ | |
| 23 | +@Service | |
| 24 | +@Slf4j | |
| 25 | +@AllArgsConstructor | |
| 26 | +public class ChatConversationServiceImpl implements ChatConversationService { | |
| 27 | + | |
| 28 | + private final AiChatConversationMapper aiChatConversationMapper; | |
| 29 | + | |
| 30 | + @Override | |
| 31 | + public ChatConversationRes create(ChatConversationCreateReq req) { | |
| 32 | + // 获取用户id | |
| 33 | + Long loginUserId = SecurityFrameworkUtils.getLoginUserId(); | |
| 34 | + // 查询最新的对话 | |
| 35 | + AiChatConversationDO latestConversation = aiChatConversationMapper.selectLatestConversation(loginUserId); | |
| 36 | + // 如果有对话没有被使用过,那就返回这个 | |
| 37 | + if (latestConversation != null && latestConversation.getChatCount() <= 0) { | |
| 38 | + return ChatConversationConvert.INSTANCE.covnertChatConversationRes(latestConversation); | |
| 39 | + } | |
| 40 | + // 创建新的 Conversation | |
| 41 | + AiChatConversationDO insertConversation = new AiChatConversationDO(); | |
| 42 | + insertConversation.setId(null); | |
| 43 | + insertConversation.setUserId(loginUserId); | |
| 44 | + insertConversation.setChatRoleId(null); | |
| 45 | + insertConversation.setChatRoleName(null); | |
| 46 | + insertConversation.setChatTitle(null); | |
| 47 | + insertConversation.setChatCount(0); | |
| 48 | + insertConversation.setChatType(req.getChatType()); | |
| 49 | + aiChatConversationMapper.insert(insertConversation); | |
| 50 | + // 转换 res | |
| 51 | + return ChatConversationConvert.INSTANCE.covnertChatConversationRes(latestConversation); | |
| 52 | + } | |
| 53 | + | |
| 54 | + @Override | |
| 55 | + public ChatConversationRes getConversation(Long id) { | |
| 56 | + AiChatConversationDO aiChatConversationDO = aiChatConversationMapper.selectById(id); | |
| 57 | + return ChatConversationConvert.INSTANCE.covnertChatConversationRes(aiChatConversationDO); | |
| 58 | + } | |
| 59 | + | |
| 60 | + @Override | |
| 61 | + public List<ChatConversationRes> listConversation(ChatConversationListReq req) { | |
| 62 | + // 获取用户id | |
| 63 | + Long loginUserId = SecurityFrameworkUtils.getLoginUserId(); | |
| 64 | + // 查询前100对话 | |
| 65 | + List<AiChatConversationDO> top100Conversation | |
| 66 | + = aiChatConversationMapper.selectTop100Conversation(loginUserId, req.getSearch()); | |
| 67 | + return ChatConversationConvert.INSTANCE.covnertChatConversationResList(top100Conversation); | |
| 68 | + } | |
| 69 | + | |
| 70 | + @Override | |
| 71 | + public void delete(Long id) { | |
| 72 | + aiChatConversationMapper.deleteById(id); | |
| 73 | + } | |
| 74 | +} | ... | ... |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/vo/ChatConversationCreateReq.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 | + * 聊天对话 | |
| 10 | + * | |
| 11 | + * @author fansili | |
| 12 | + * @time 2024/4/18 16:24 | |
| 13 | + * @since 1.0 | |
| 14 | + */ | |
| 15 | +@Data | |
| 16 | +@Accessors(chain = true) | |
| 17 | +public class ChatConversationCreateReq { | |
| 18 | + | |
| 19 | + @Schema(description = "对话类型(roleChat、userChat)") | |
| 20 | + @NotNull(message = "聊天类型不能为空!") | |
| 21 | + private String chatType; | |
| 22 | + | |
| 23 | +} | ... | ... |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/vo/ChatConversationCreateRes.java
0 → 100644
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/vo/ChatConversationListReq.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 | + * 聊天对话 list req | |
| 9 | + * | |
| 10 | + * @author fansili | |
| 11 | + * @time 2024/4/18 16:24 | |
| 12 | + * @since 1.0 | |
| 13 | + */ | |
| 14 | +@Data | |
| 15 | +@Accessors(chain = true) | |
| 16 | +public class ChatConversationListReq { | |
| 17 | + | |
| 18 | + @Schema(description = "查询根据title") | |
| 19 | + private String search; | |
| 20 | +} | ... | ... |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/vo/ChatConversationRes.java
0 → 100644