【优化】代码优化,统一增加ai前缀

This commit is contained in:
cherishsince 2024-04-25 15:48:48 +08:00
parent 5cec6d52e0
commit 71e3240af6
39 changed files with 215 additions and 225 deletions

View File

@ -13,7 +13,7 @@ import lombok.Getter;
*/ */
@AllArgsConstructor @AllArgsConstructor
@Getter @Getter
public enum ChatConversationTypeEnum { public enum AiChatConversationTypeEnum {
// roleChatuserChat // roleChatuserChat
ROLE_CHAT("roleChat", "角色对话"), ROLE_CHAT("roleChat", "角色对话"),
@ -25,8 +25,8 @@ public enum ChatConversationTypeEnum {
private String name; private String name;
public static ChatConversationTypeEnum valueOfType(String type) { public static AiChatConversationTypeEnum valueOfType(String type) {
for (ChatConversationTypeEnum itemEnum : ChatConversationTypeEnum.values()) { for (AiChatConversationTypeEnum itemEnum : AiChatConversationTypeEnum.values()) {
if (itemEnum.getType().equals(type)) { if (itemEnum.getType().equals(type)) {
return itemEnum; return itemEnum;
} }

View File

@ -12,7 +12,7 @@ import lombok.Getter;
*/ */
@AllArgsConstructor @AllArgsConstructor
@Getter @Getter
public enum ChatRoleClassifyEnum { public enum AiChatRoleClassifyEnum {
WRITING("writing", "写作"), WRITING("writing", "写作"),
@ -26,8 +26,8 @@ public enum ChatRoleClassifyEnum {
private String name; private String name;
public static ChatRoleClassifyEnum valueOfClassify(String classify) { public static AiChatRoleClassifyEnum valueOfClassify(String classify) {
for (ChatRoleClassifyEnum itemEnum : ChatRoleClassifyEnum.values()) { for (AiChatRoleClassifyEnum itemEnum : AiChatRoleClassifyEnum.values()) {
if (itemEnum.getClassify().equals(classify)) { if (itemEnum.getClassify().equals(classify)) {
return itemEnum; return itemEnum;
} }

View File

@ -12,7 +12,7 @@ import lombok.Getter;
*/ */
@AllArgsConstructor @AllArgsConstructor
@Getter @Getter
public enum ChatRoleSourceEnum { public enum AiChatRoleSourceEnum {
SYSTEM("system", "系统"), SYSTEM("system", "系统"),
CUSTOMER("customer", "用户自定义"), CUSTOMER("customer", "用户自定义"),
@ -25,8 +25,8 @@ public enum ChatRoleSourceEnum {
private String name; private String name;
public static ChatRoleSourceEnum valueOfType(String type) { public static AiChatRoleSourceEnum valueOfType(String type) {
for (ChatRoleSourceEnum itemEnum : ChatRoleSourceEnum.values()) { for (AiChatRoleSourceEnum itemEnum : AiChatRoleSourceEnum.values()) {
if (itemEnum.getType().equals(type)) { if (itemEnum.getType().equals(type)) {
return itemEnum; return itemEnum;
} }

View File

@ -12,7 +12,7 @@ import lombok.Getter;
*/ */
@AllArgsConstructor @AllArgsConstructor
@Getter @Getter
public enum ChatRoleVisibilityEnum { public enum AiChatRoleVisibilityEnum {
PUBLIC("public", "公开"), PUBLIC("public", "公开"),
PRIVATE("private", "私有的"), PRIVATE("private", "私有的"),
@ -24,8 +24,8 @@ public enum ChatRoleVisibilityEnum {
private String name; private String name;
public static ChatRoleVisibilityEnum valueOfType(String type) { public static AiChatRoleVisibilityEnum valueOfType(String type) {
for (ChatRoleVisibilityEnum itemEnum : ChatRoleVisibilityEnum.values()) { for (AiChatRoleVisibilityEnum itemEnum : AiChatRoleVisibilityEnum.values()) {
if (itemEnum.getType().equals(type)) { if (itemEnum.getType().equals(type)) {
return itemEnum; return itemEnum;
} }

View File

@ -12,7 +12,7 @@ import lombok.Getter;
*/ */
@AllArgsConstructor @AllArgsConstructor
@Getter @Getter
public enum ChatTypeEnum { public enum AiChatTypeEnum {
ROLE_CHAT("roleChat", "角色模板聊天"), ROLE_CHAT("roleChat", "角色模板聊天"),
USER_CHAT("userChat", "用户普通聊天"), USER_CHAT("userChat", "用户普通聊天"),
@ -23,8 +23,8 @@ public enum ChatTypeEnum {
private String name; private String name;
public static ChatTypeEnum valueOfType(String type) { public static AiChatTypeEnum valueOfType(String type) {
for (ChatTypeEnum itemEnum : ChatTypeEnum.values()) { for (AiChatTypeEnum itemEnum : AiChatTypeEnum.values()) {
if (itemEnum.getType().equals(type)) { if (itemEnum.getType().equals(type)) {
return itemEnum; return itemEnum;
} }

View File

@ -10,7 +10,7 @@ import lombok.Getter;
*/ */
@Getter @Getter
@AllArgsConstructor @AllArgsConstructor
public enum OpenAiModelEnum { public enum AiOpenAiModelEnum {
/** /**
* open ai 3.5模型 * open ai 3.5模型

View File

@ -1,10 +1,8 @@
package cn.iocoder.yudao.module.ai.controller; package cn.iocoder.yudao.module.ai.controller;
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.iocoder.yudao.framework.ai.chat.ChatResponse;
import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.ai.service.ChatService; import cn.iocoder.yudao.module.ai.service.AiChatService;
import cn.iocoder.yudao.module.ai.vo.ChatReq; import cn.iocoder.yudao.module.ai.vo.AiChatReq;
import io.swagger.v3.oas.annotations.Operation; 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;
@ -17,10 +15,6 @@ import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import reactor.core.publisher.Flux;
import java.io.IOException;
import java.util.function.Consumer;
/** /**
* ia 模块 * ia 模块
@ -34,21 +28,21 @@ import java.util.function.Consumer;
@RequestMapping("/ai") @RequestMapping("/ai")
@Slf4j @Slf4j
@AllArgsConstructor @AllArgsConstructor
public class ChatController { public class AiChatController {
@Autowired @Autowired
private final ChatService chatService; private final AiChatService chatService;
@Operation(summary = "聊天-chat", description = "这个一般等待时间比较久,需要全部完成才会返回!") @Operation(summary = "聊天-chat", description = "这个一般等待时间比较久,需要全部完成才会返回!")
@GetMapping("/chat") @GetMapping("/chat")
public CommonResult<String> chat(@Validated @ModelAttribute ChatReq req) { public CommonResult<String> chat(@Validated @ModelAttribute AiChatReq req) {
return CommonResult.success(chatService.chat(req)); return CommonResult.success(chatService.chat(req));
} }
// TODO @芋艿调用这个方法异常Unable to handle the Spring Security Exception because the response is already committed. // TODO @芋艿调用这个方法异常Unable to handle the Spring Security Exception because the response is already committed.
@Operation(summary = "聊天-stream", description = "这里跟通义千问一样采用的是 Server-Sent Events (SSE) 通讯模式") @Operation(summary = "聊天-stream", description = "这里跟通义千问一样采用的是 Server-Sent Events (SSE) 通讯模式")
@GetMapping(value = "/chatStream", produces = MediaType.TEXT_EVENT_STREAM_VALUE) @GetMapping(value = "/chatStream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public SseEmitter chatStream(@Validated @ModelAttribute ChatReq req) { public SseEmitter chatStream(@Validated @ModelAttribute AiChatReq req) {
Utf8SseEmitter sseEmitter = new Utf8SseEmitter(); Utf8SseEmitter sseEmitter = new Utf8SseEmitter();
chatService.chatStream(req, sseEmitter); chatService.chatStream(req, sseEmitter);
return sseEmitter; return sseEmitter;

View File

@ -1,11 +1,11 @@
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.CommonResult;
import cn.iocoder.yudao.module.ai.service.ChatConversationService; import cn.iocoder.yudao.module.ai.service.AiChatConversationService;
import cn.iocoder.yudao.module.ai.vo.ChatConversationCreateRoleReq; import cn.iocoder.yudao.module.ai.vo.AiChatConversationCreateRoleReq;
import cn.iocoder.yudao.module.ai.vo.ChatConversationCreateUserReq; import cn.iocoder.yudao.module.ai.vo.AiChatConversationCreateUserReq;
import cn.iocoder.yudao.module.ai.vo.ChatConversationListReq; import cn.iocoder.yudao.module.ai.vo.AiChatConversationListReq;
import cn.iocoder.yudao.module.ai.vo.ChatConversationRes; import cn.iocoder.yudao.module.ai.vo.AiChatConversationRes;
import io.swagger.v3.oas.annotations.Operation; 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;
@ -27,31 +27,31 @@ import java.util.List;
@RequestMapping("/ai/chat/conversation") @RequestMapping("/ai/chat/conversation")
@Slf4j @Slf4j
@AllArgsConstructor @AllArgsConstructor
public class ChatConversationController { public class AiChatConversationController {
private final ChatConversationService chatConversationService; private final AiChatConversationService chatConversationService;
@Operation(summary = "创建 - 对话普通对话") @Operation(summary = "创建 - 对话普通对话")
@PostMapping("/createConversation") @PostMapping("/createConversation")
public CommonResult<ChatConversationRes> createConversation(@RequestBody @Validated ChatConversationCreateUserReq req) { public CommonResult<AiChatConversationRes> createConversation(@RequestBody @Validated AiChatConversationCreateUserReq req) {
return CommonResult.success(chatConversationService.createConversation(req)); return CommonResult.success(chatConversationService.createConversation(req));
} }
@Operation(summary = "创建 - 对话角色对话") @Operation(summary = "创建 - 对话角色对话")
@PostMapping("/createRoleConversation") @PostMapping("/createRoleConversation")
public CommonResult<ChatConversationRes> createRoleConversation(@RequestBody @Validated ChatConversationCreateRoleReq req) { public CommonResult<AiChatConversationRes> createRoleConversation(@RequestBody @Validated AiChatConversationCreateRoleReq req) {
return CommonResult.success(chatConversationService.createRoleConversation(req)); return CommonResult.success(chatConversationService.createRoleConversation(req));
} }
@Operation(summary = "获取 - 获取对话") @Operation(summary = "获取 - 获取对话")
@GetMapping("/{id}") @GetMapping("/{id}")
public CommonResult<ChatConversationRes> getConversation(@PathVariable("id") Long id) { public CommonResult<AiChatConversationRes> getConversation(@PathVariable("id") Long id) {
return CommonResult.success(chatConversationService.getConversation(id)); return CommonResult.success(chatConversationService.getConversation(id));
} }
@Operation(summary = "获取 - 获取对话list") @Operation(summary = "获取 - 获取对话list")
@GetMapping("/list") @GetMapping("/list")
public CommonResult<List<ChatConversationRes>> listConversation(@ModelAttribute @Validated ChatConversationListReq req) { public CommonResult<List<AiChatConversationRes>> listConversation(@ModelAttribute @Validated AiChatConversationListReq req) {
return CommonResult.success(chatConversationService.listConversation(req)); return CommonResult.success(chatConversationService.listConversation(req));
} }

View File

@ -2,9 +2,9 @@ package cn.iocoder.yudao.module.ai.controller;
import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.ai.service.ChatMessageService; import cn.iocoder.yudao.module.ai.service.AiChatMessageService;
import cn.iocoder.yudao.module.ai.vo.ChatMessageListRes; import cn.iocoder.yudao.module.ai.vo.AiChatMessageListRes;
import cn.iocoder.yudao.module.ai.vo.ChatMessageReq; import cn.iocoder.yudao.module.ai.vo.AiChatMessageReq;
import io.swagger.v3.oas.annotations.Operation; 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;
@ -24,13 +24,13 @@ import org.springframework.web.bind.annotation.*;
@RequestMapping("/ai/chat/message") @RequestMapping("/ai/chat/message")
@Slf4j @Slf4j
@AllArgsConstructor @AllArgsConstructor
public class ChatMessageController { public class AiChatMessageController {
private final ChatMessageService chatMessageService; private final AiChatMessageService chatMessageService;
@Operation(summary = "聊天记录", description = "查询个人的聊天记录") @Operation(summary = "聊天记录", description = "查询个人的聊天记录")
@GetMapping("/list") @GetMapping("/list")
public PageResult<ChatMessageListRes> list(@Validated @ModelAttribute ChatMessageReq req) { public PageResult<AiChatMessageListRes> list(@Validated @ModelAttribute AiChatMessageReq req) {
return chatMessageService.list(req); return chatMessageService.list(req);
} }

View File

@ -2,7 +2,7 @@ package cn.iocoder.yudao.module.ai.controller;
import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.ai.service.ChatRoleService; import cn.iocoder.yudao.module.ai.service.AiChatRoleService;
import cn.iocoder.yudao.module.ai.vo.*; import cn.iocoder.yudao.module.ai.vo.*;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
@ -22,19 +22,19 @@ import org.springframework.web.bind.annotation.*;
@RequestMapping("/ai/chat") @RequestMapping("/ai/chat")
@Slf4j @Slf4j
@AllArgsConstructor @AllArgsConstructor
public class ChatRoleController { public class AiChatRoleController {
private final ChatRoleService chatRoleService; private final AiChatRoleService chatRoleService;
@Operation(summary = "chat角色 - 角色列表") @Operation(summary = "chat角色 - 角色列表")
@GetMapping("/role/list") @GetMapping("/role/list")
public PageResult<ChatRoleListRes> list(@Validated @ModelAttribute ChatRoleListReq req) { public PageResult<AiChatRoleListRes> list(@Validated @ModelAttribute AiChatRoleListReq req) {
return chatRoleService.list(req); return chatRoleService.list(req);
} }
@Operation(summary = "chat角色 - 添加") @Operation(summary = "chat角色 - 添加")
@PutMapping("/role") @PutMapping("/role")
public CommonResult<Void> add(@Validated @RequestBody ChatRoleAddReq req) { public CommonResult<Void> add(@Validated @RequestBody AiChatRoleAddReq req) {
chatRoleService.add(req); chatRoleService.add(req);
return CommonResult.success(null); return CommonResult.success(null);
} }
@ -42,7 +42,7 @@ public class ChatRoleController {
@Operation(summary = "chat角色 - 修改") @Operation(summary = "chat角色 - 修改")
@PostMapping("/role/{id}") @PostMapping("/role/{id}")
public CommonResult<Void> update(@PathVariable("id") Long id, public CommonResult<Void> update(@PathVariable("id") Long id,
@Validated @RequestBody ChatRoleUpdateReq req) { @Validated @RequestBody AiChatRoleUpdateReq req) {
chatRoleService.update(id, req); chatRoleService.update(id, req);
return CommonResult.success(null); return CommonResult.success(null);
} }
@ -50,7 +50,7 @@ public class ChatRoleController {
@Operation(summary = "chat角色 - 修改可见性") @Operation(summary = "chat角色 - 修改可见性")
@PostMapping("/role/{id}/update-visibility") @PostMapping("/role/{id}/update-visibility")
public CommonResult<Void> updateVisibility(@PathVariable("id") Long id, public CommonResult<Void> updateVisibility(@PathVariable("id") Long id,
@Validated @RequestBody ChatRoleUpdateVisibilityReq req) { @Validated @RequestBody AiChatRoleUpdateVisibilityReq req) {
chatRoleService.updateVisibility(id, req); chatRoleService.updateVisibility(id, req);
return CommonResult.success(null); return CommonResult.success(null);
} }

View File

@ -1,7 +1,7 @@
package cn.iocoder.yudao.module.ai.convert; package cn.iocoder.yudao.module.ai.convert;
import cn.iocoder.yudao.module.ai.dal.dataobject.AiChatConversationDO; import cn.iocoder.yudao.module.ai.dal.dataobject.AiChatConversationDO;
import cn.iocoder.yudao.module.ai.vo.ChatConversationRes; import cn.iocoder.yudao.module.ai.vo.AiChatConversationRes;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
@ -15,9 +15,9 @@ import java.util.List;
* @since 1.0 * @since 1.0
*/ */
@Mapper @Mapper
public interface ChatConversationConvert { public interface AiChatConversationConvert {
ChatConversationConvert INSTANCE = Mappers.getMapper(ChatConversationConvert.class); AiChatConversationConvert INSTANCE = Mappers.getMapper(AiChatConversationConvert.class);
/** /**
* 转换 - 多个 ChatConversationRes * 转换 - 多个 ChatConversationRes
@ -25,7 +25,7 @@ public interface ChatConversationConvert {
* @param top100Conversation * @param top100Conversation
* @return * @return
*/ */
List<ChatConversationRes> covnertChatConversationResList(List<AiChatConversationDO> top100Conversation); List<AiChatConversationRes> covnertChatConversationResList(List<AiChatConversationDO> top100Conversation);
/** /**
* 转换 - 单个 ChatConversationRes * 转换 - 单个 ChatConversationRes
@ -33,5 +33,5 @@ public interface ChatConversationConvert {
* @param aiChatConversationDO * @param aiChatConversationDO
* @return * @return
*/ */
ChatConversationRes covnertChatConversationRes(AiChatConversationDO aiChatConversationDO); AiChatConversationRes covnertChatConversationRes(AiChatConversationDO aiChatConversationDO);
} }

View File

@ -1,7 +1,7 @@
package cn.iocoder.yudao.module.ai.convert; package cn.iocoder.yudao.module.ai.convert;
import cn.iocoder.yudao.module.ai.dal.dataobject.AiChatMessageDO; import cn.iocoder.yudao.module.ai.dal.dataobject.AiChatMessageDO;
import cn.iocoder.yudao.module.ai.vo.ChatMessageListRes; import cn.iocoder.yudao.module.ai.vo.AiChatMessageListRes;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
@ -15,9 +15,9 @@ import java.util.List;
* @since 1.0 * @since 1.0
*/ */
@Mapper @Mapper
public interface ChatMessageConvert { public interface AiChatMessageConvert {
ChatMessageConvert INSTANCE = Mappers.getMapper(ChatMessageConvert.class); AiChatMessageConvert INSTANCE = Mappers.getMapper(AiChatMessageConvert.class);
/** /**
* 转换 ChatMessageListRes * 转换 ChatMessageListRes
@ -25,5 +25,5 @@ public interface ChatMessageConvert {
* @param list * @param list
* @return * @return
*/ */
List<ChatMessageListRes> convert(List<AiChatMessageDO> list); List<AiChatMessageListRes> convert(List<AiChatMessageDO> list);
} }

View File

@ -16,9 +16,9 @@ import java.util.List;
* @since 1.0 * @since 1.0
*/ */
@Mapper @Mapper
public interface ChatModalConvert { public interface AiChatModalConvert {
ChatModalConvert INSTANCE = Mappers.getMapper(ChatModalConvert.class); AiChatModalConvert INSTANCE = Mappers.getMapper(AiChatModalConvert.class);
/** /**
* 转换 - AiChatModalListRes * 转换 - AiChatModalListRes

View File

@ -1,9 +1,9 @@
package cn.iocoder.yudao.module.ai.convert; package cn.iocoder.yudao.module.ai.convert;
import cn.iocoder.yudao.module.ai.dal.dataobject.AiChatRoleDO; import cn.iocoder.yudao.module.ai.dal.dataobject.AiChatRoleDO;
import cn.iocoder.yudao.module.ai.vo.ChatRoleAddReq; import cn.iocoder.yudao.module.ai.vo.AiChatRoleAddReq;
import cn.iocoder.yudao.module.ai.vo.ChatRoleListRes; import cn.iocoder.yudao.module.ai.vo.AiChatRoleUpdateReq;
import cn.iocoder.yudao.module.ai.vo.ChatRoleUpdateReq; import cn.iocoder.yudao.module.ai.vo.AiChatRoleListRes;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
@ -17,9 +17,9 @@ import java.util.List;
* @since 1.0 * @since 1.0
*/ */
@Mapper @Mapper
public interface ChatRoleConvert { public interface AiChatRoleConvert {
ChatRoleConvert INSTANCE = Mappers.getMapper(ChatRoleConvert.class); AiChatRoleConvert INSTANCE = Mappers.getMapper(AiChatRoleConvert.class);
/** /**
* 转换 - ChatRoleListRes * 转换 - ChatRoleListRes
@ -27,7 +27,7 @@ public interface ChatRoleConvert {
* @param roleList * @param roleList
* @return * @return
*/ */
List<ChatRoleListRes> convertChatRoleListRes(List<AiChatRoleDO> roleList); List<AiChatRoleListRes> convertChatRoleListRes(List<AiChatRoleDO> roleList);
/** /**
* 转换 - AiChatRoleDO * 转换 - AiChatRoleDO
@ -35,7 +35,7 @@ public interface ChatRoleConvert {
* @param req * @param req
* @return * @return
*/ */
AiChatRoleDO convertAiChatRoleDO(ChatRoleAddReq req); AiChatRoleDO convertAiChatRoleDO(AiChatRoleAddReq req);
/** /**
* 转换 - AiChatRoleDO * 转换 - AiChatRoleDO
@ -43,5 +43,5 @@ public interface ChatRoleConvert {
* @param req * @param req
* @return * @return
*/ */
AiChatRoleDO convertAiChatRoleDO(ChatRoleUpdateReq req); AiChatRoleDO convertAiChatRoleDO(AiChatRoleUpdateReq req);
} }

View File

@ -3,7 +3,6 @@ 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.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.ai.dal.dataobject.AiChatMessageDO; import cn.iocoder.yudao.module.ai.dal.dataobject.AiChatMessageDO;
import cn.iocoder.yudao.module.ai.vo.ChatMessageReq;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;

View File

@ -0,0 +1,58 @@
package cn.iocoder.yudao.module.ai.service;
import cn.iocoder.yudao.module.ai.vo.AiChatConversationCreateRoleReq;
import cn.iocoder.yudao.module.ai.vo.AiChatConversationCreateUserReq;
import cn.iocoder.yudao.module.ai.vo.AiChatConversationListReq;
import cn.iocoder.yudao.module.ai.vo.AiChatConversationRes;
import java.util.List;
/**
* chat 对话
*
* @fansili
* @since v1.0
*/
public interface AiChatConversationService {
/**
* 对话 - 创建普通对话
*
* @param req
* @return
*/
AiChatConversationRes createConversation(AiChatConversationCreateUserReq req);
/**
* 对话 - 创建role对话
*
* @param req
* @return
*/
AiChatConversationRes createRoleConversation(AiChatConversationCreateRoleReq req);
/**
* 获取 - 对话
*
* @param id
* @return
*/
AiChatConversationRes getConversation(Long id);
/**
* 获取 - 对话列表
*
* @param req
* @return
*/
List<AiChatConversationRes> listConversation(AiChatConversationListReq req);
/**
* 删除 - 根据id
*
* @param id
*/
void delete(Long id);
}

View File

@ -1,8 +1,8 @@
package cn.iocoder.yudao.module.ai.service; package cn.iocoder.yudao.module.ai.service;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.ai.vo.ChatMessageListRes; import cn.iocoder.yudao.module.ai.vo.AiChatMessageListRes;
import cn.iocoder.yudao.module.ai.vo.ChatMessageReq; import cn.iocoder.yudao.module.ai.vo.AiChatMessageReq;
/** /**
* chat message * chat message
@ -11,7 +11,7 @@ import cn.iocoder.yudao.module.ai.vo.ChatMessageReq;
* @time 2024/4/24 17:25 * @time 2024/4/24 17:25
* @since 1.0 * @since 1.0
*/ */
public interface ChatMessageService { public interface AiChatMessageService {
/** /**
* message - 列表 * message - 列表
@ -19,7 +19,7 @@ public interface ChatMessageService {
* @param req * @param req
* @return * @return
*/ */
PageResult<ChatMessageListRes> list(ChatMessageReq req); PageResult<AiChatMessageListRes> list(AiChatMessageReq req);
/** /**
* message - 删除 * message - 删除

View File

@ -1,18 +1,15 @@
package cn.iocoder.yudao.module.ai.service; package cn.iocoder.yudao.module.ai.service;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.ai.vo.*; import cn.iocoder.yudao.module.ai.vo.*;
import java.util.List;
/** /**
* chat 角色 * chat 角色
* *
* @fansili * @fansili
* @since v1.0 * @since v1.0
*/ */
public interface ChatRoleService { public interface AiChatRoleService {
/** /**
* 获取聊天角色列表 * 获取聊天角色列表
@ -20,7 +17,7 @@ public interface ChatRoleService {
* @param req * @param req
* @return * @return
*/ */
PageResult<ChatRoleListRes> list(ChatRoleListReq req); PageResult<AiChatRoleListRes> list(AiChatRoleListReq req);
/** /**
* chat角色 - 添加 * chat角色 - 添加
@ -28,7 +25,7 @@ public interface ChatRoleService {
* @param req * @param req
* @return * @return
*/ */
void add(ChatRoleAddReq req); void add(AiChatRoleAddReq req);
/** /**
* chat角色 - 修改 * chat角色 - 修改
@ -36,7 +33,7 @@ public interface ChatRoleService {
* @param id * @param id
* @param req * @param req
*/ */
void update(Long id, ChatRoleUpdateReq req); void update(Long id, AiChatRoleUpdateReq req);
/** /**
@ -45,7 +42,7 @@ public interface ChatRoleService {
* @param id * @param id
* @param req * @param req
*/ */
void updateVisibility(Long id, ChatRoleUpdateVisibilityReq req); void updateVisibility(Long id, AiChatRoleUpdateVisibilityReq req);
/** /**
* chat角色 - 删除 * chat角色 - 删除

View File

@ -1,7 +1,7 @@
package cn.iocoder.yudao.module.ai.service; package cn.iocoder.yudao.module.ai.service;
import cn.iocoder.yudao.module.ai.controller.Utf8SseEmitter; import cn.iocoder.yudao.module.ai.controller.Utf8SseEmitter;
import cn.iocoder.yudao.module.ai.vo.ChatReq; import cn.iocoder.yudao.module.ai.vo.AiChatReq;
/** /**
* 聊天 chat * 聊天 chat
@ -10,7 +10,7 @@ import cn.iocoder.yudao.module.ai.vo.ChatReq;
* @time 2024/4/14 15:55 * @time 2024/4/14 15:55
* @since 1.0 * @since 1.0
*/ */
public interface ChatService { public interface AiChatService {
/** /**
* chat * chat
@ -18,7 +18,7 @@ public interface ChatService {
* @param req * @param req
* @return * @return
*/ */
String chat(ChatReq req); String chat(AiChatReq req);
/** /**
* chat stream * chat stream
@ -27,5 +27,5 @@ public interface ChatService {
* @param sseEmitter * @param sseEmitter
* @return * @return
*/ */
void chatStream(ChatReq req, Utf8SseEmitter sseEmitter); void chatStream(AiChatReq req, Utf8SseEmitter sseEmitter);
} }

View File

@ -1,58 +0,0 @@
package cn.iocoder.yudao.module.ai.service;
import cn.iocoder.yudao.module.ai.vo.ChatConversationCreateRoleReq;
import cn.iocoder.yudao.module.ai.vo.ChatConversationCreateUserReq;
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 createConversation(ChatConversationCreateUserReq req);
/**
* 对话 - 创建role对话
*
* @param req
* @return
*/
ChatConversationRes createRoleConversation(ChatConversationCreateRoleReq 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

@ -3,17 +3,17 @@ package cn.iocoder.yudao.module.ai.service.impl;
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.ai.ErrorCodeConstants; import cn.iocoder.yudao.module.ai.ErrorCodeConstants;
import cn.iocoder.yudao.module.ai.convert.ChatConversationConvert; import cn.iocoder.yudao.module.ai.convert.AiChatConversationConvert;
import cn.iocoder.yudao.module.ai.dal.dataobject.AiChatConversationDO; import cn.iocoder.yudao.module.ai.dal.dataobject.AiChatConversationDO;
import cn.iocoder.yudao.module.ai.dal.dataobject.AiChatRoleDO; import cn.iocoder.yudao.module.ai.dal.dataobject.AiChatRoleDO;
import cn.iocoder.yudao.module.ai.enums.ChatConversationTypeEnum; import cn.iocoder.yudao.module.ai.enums.AiChatConversationTypeEnum;
import cn.iocoder.yudao.module.ai.mapper.AiChatConversationMapper; import cn.iocoder.yudao.module.ai.mapper.AiChatConversationMapper;
import cn.iocoder.yudao.module.ai.mapper.AiChatRoleMapper; import cn.iocoder.yudao.module.ai.mapper.AiChatRoleMapper;
import cn.iocoder.yudao.module.ai.service.ChatConversationService; import cn.iocoder.yudao.module.ai.service.AiChatConversationService;
import cn.iocoder.yudao.module.ai.vo.ChatConversationCreateRoleReq; import cn.iocoder.yudao.module.ai.vo.AiChatConversationCreateRoleReq;
import cn.iocoder.yudao.module.ai.vo.ChatConversationCreateUserReq; import cn.iocoder.yudao.module.ai.vo.AiChatConversationCreateUserReq;
import cn.iocoder.yudao.module.ai.vo.ChatConversationListReq; import cn.iocoder.yudao.module.ai.vo.AiChatConversationListReq;
import cn.iocoder.yudao.module.ai.vo.ChatConversationRes; import cn.iocoder.yudao.module.ai.vo.AiChatConversationRes;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -30,51 +30,51 @@ import java.util.List;
@Service @Service
@Slf4j @Slf4j
@AllArgsConstructor @AllArgsConstructor
public class ChatConversationServiceImpl implements ChatConversationService { public class AiChatConversationServiceImpl implements AiChatConversationService {
private final AiChatRoleMapper aiChatRoleMapper; private final AiChatRoleMapper aiChatRoleMapper;
private final AiChatConversationMapper aiChatConversationMapper; private final AiChatConversationMapper aiChatConversationMapper;
@Override @Override
public ChatConversationRes createConversation(ChatConversationCreateUserReq req) { public AiChatConversationRes createConversation(AiChatConversationCreateUserReq req) {
// 获取用户id // 获取用户id
Long loginUserId = SecurityFrameworkUtils.getLoginUserId(); Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
// 查询最新的对话 // 查询最新的对话
AiChatConversationDO latestConversation = aiChatConversationMapper.selectLatestConversation(loginUserId); AiChatConversationDO latestConversation = aiChatConversationMapper.selectLatestConversation(loginUserId);
// 如果有对话没有被使用过那就返回这个 // 如果有对话没有被使用过那就返回这个
if (latestConversation != null && latestConversation.getChatCount() <= 0) { if (latestConversation != null && latestConversation.getChatCount() <= 0) {
return ChatConversationConvert.INSTANCE.covnertChatConversationRes(latestConversation); return AiChatConversationConvert.INSTANCE.covnertChatConversationRes(latestConversation);
} }
// 创建新的 Conversation // 创建新的 Conversation
AiChatConversationDO insertConversation = saveConversation(req.getTitle(), loginUserId, AiChatConversationDO insertConversation = saveConversation(req.getTitle(), loginUserId,
null, null, ChatConversationTypeEnum.USER_CHAT); null, null, AiChatConversationTypeEnum.USER_CHAT);
// 转换 res // 转换 res
return ChatConversationConvert.INSTANCE.covnertChatConversationRes(insertConversation); return AiChatConversationConvert.INSTANCE.covnertChatConversationRes(insertConversation);
} }
@Override @Override
public ChatConversationRes createRoleConversation(ChatConversationCreateRoleReq req) { public AiChatConversationRes createRoleConversation(AiChatConversationCreateRoleReq req) {
// 获取用户id // 获取用户id
Long loginUserId = SecurityFrameworkUtils.getLoginUserId(); Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
// 查询最新的对话 // 查询最新的对话
AiChatConversationDO latestConversation = aiChatConversationMapper.selectLatestConversation(loginUserId); AiChatConversationDO latestConversation = aiChatConversationMapper.selectLatestConversation(loginUserId);
// 如果有对话没有被使用过那就返回这个 // 如果有对话没有被使用过那就返回这个
if (latestConversation != null && latestConversation.getChatCount() <= 0) { if (latestConversation != null && latestConversation.getChatCount() <= 0) {
return ChatConversationConvert.INSTANCE.covnertChatConversationRes(latestConversation); return AiChatConversationConvert.INSTANCE.covnertChatConversationRes(latestConversation);
} }
AiChatRoleDO aiChatRoleDO = aiChatRoleMapper.selectById(req.getChatRoleId()); AiChatRoleDO aiChatRoleDO = aiChatRoleMapper.selectById(req.getChatRoleId());
// 创建新的 Conversation // 创建新的 Conversation
AiChatConversationDO insertConversation = saveConversation(req.getTitle(), loginUserId, AiChatConversationDO insertConversation = saveConversation(req.getTitle(), loginUserId,
req.getChatRoleId(), aiChatRoleDO.getRoleName(), ChatConversationTypeEnum.ROLE_CHAT); req.getChatRoleId(), aiChatRoleDO.getRoleName(), AiChatConversationTypeEnum.ROLE_CHAT);
// 转换 res // 转换 res
return ChatConversationConvert.INSTANCE.covnertChatConversationRes(insertConversation); return AiChatConversationConvert.INSTANCE.covnertChatConversationRes(insertConversation);
} }
private @NotNull AiChatConversationDO saveConversation(String title, private @NotNull AiChatConversationDO saveConversation(String title,
Long userId, Long userId,
Long chatRoleId, Long chatRoleId,
String chatRoleName, String chatRoleName,
ChatConversationTypeEnum typeEnum) { AiChatConversationTypeEnum typeEnum) {
AiChatConversationDO insertConversation = new AiChatConversationDO(); AiChatConversationDO insertConversation = new AiChatConversationDO();
insertConversation.setId(null); insertConversation.setId(null);
insertConversation.setUserId(userId); insertConversation.setUserId(userId);
@ -88,22 +88,22 @@ public class ChatConversationServiceImpl implements ChatConversationService {
} }
@Override @Override
public ChatConversationRes getConversation(Long id) { public AiChatConversationRes getConversation(Long id) {
AiChatConversationDO aiChatConversationDO = aiChatConversationMapper.selectById(id); AiChatConversationDO aiChatConversationDO = aiChatConversationMapper.selectById(id);
if (aiChatConversationDO == null) { if (aiChatConversationDO == null) {
throw ServiceExceptionUtil.exception(ErrorCodeConstants.AI_CHAT_CONTINUE_NOT_EXIST); throw ServiceExceptionUtil.exception(ErrorCodeConstants.AI_CHAT_CONTINUE_NOT_EXIST);
} }
return ChatConversationConvert.INSTANCE.covnertChatConversationRes(aiChatConversationDO); return AiChatConversationConvert.INSTANCE.covnertChatConversationRes(aiChatConversationDO);
} }
@Override @Override
public List<ChatConversationRes> listConversation(ChatConversationListReq req) { public List<AiChatConversationRes> listConversation(AiChatConversationListReq req) {
// 获取用户id // 获取用户id
Long loginUserId = SecurityFrameworkUtils.getLoginUserId(); Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
// 查询前100对话 // 查询前100对话
List<AiChatConversationDO> top100Conversation List<AiChatConversationDO> top100Conversation
= aiChatConversationMapper.selectTop100Conversation(loginUserId, req.getSearch()); = aiChatConversationMapper.selectTop100Conversation(loginUserId, req.getSearch());
return ChatConversationConvert.INSTANCE.covnertChatConversationResList(top100Conversation); return AiChatConversationConvert.INSTANCE.covnertChatConversationResList(top100Conversation);
} }
@Override @Override

View File

@ -5,14 +5,14 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.ai.ErrorCodeConstants; import cn.iocoder.yudao.module.ai.ErrorCodeConstants;
import cn.iocoder.yudao.module.ai.convert.ChatMessageConvert; import cn.iocoder.yudao.module.ai.convert.AiChatMessageConvert;
import cn.iocoder.yudao.module.ai.dal.dataobject.AiChatConversationDO; import cn.iocoder.yudao.module.ai.dal.dataobject.AiChatConversationDO;
import cn.iocoder.yudao.module.ai.dal.dataobject.AiChatMessageDO; import cn.iocoder.yudao.module.ai.dal.dataobject.AiChatMessageDO;
import cn.iocoder.yudao.module.ai.mapper.AiChatConversationMapper; import cn.iocoder.yudao.module.ai.mapper.AiChatConversationMapper;
import cn.iocoder.yudao.module.ai.mapper.AiChatMessageMapper; import cn.iocoder.yudao.module.ai.mapper.AiChatMessageMapper;
import cn.iocoder.yudao.module.ai.service.ChatMessageService; import cn.iocoder.yudao.module.ai.service.AiChatMessageService;
import cn.iocoder.yudao.module.ai.vo.ChatMessageListRes; import cn.iocoder.yudao.module.ai.vo.AiChatMessageListRes;
import cn.iocoder.yudao.module.ai.vo.ChatMessageReq; import cn.iocoder.yudao.module.ai.vo.AiChatMessageReq;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -29,13 +29,13 @@ import java.util.List;
@AllArgsConstructor @AllArgsConstructor
@Service @Service
@Slf4j @Slf4j
public class ChatMessageServiceImpl implements ChatMessageService { public class AiChatMessageServiceImpl implements AiChatMessageService {
private final AiChatMessageMapper aiChatMessageMapper; private final AiChatMessageMapper aiChatMessageMapper;
private final AiChatConversationMapper aiChatConversationMapper; private final AiChatConversationMapper aiChatConversationMapper;
@Override @Override
public PageResult<ChatMessageListRes> list(ChatMessageReq req) { public PageResult<AiChatMessageListRes> list(AiChatMessageReq req) {
// 查询 // 查询
LambdaQueryWrapperX<AiChatMessageDO> queryWrapperX = new LambdaQueryWrapperX<>(); LambdaQueryWrapperX<AiChatMessageDO> queryWrapperX = new LambdaQueryWrapperX<>();
queryWrapperX.eq(AiChatMessageDO::getChatConversationId, req.getChatConversationId()); queryWrapperX.eq(AiChatMessageDO::getChatConversationId, req.getChatConversationId());
@ -43,7 +43,7 @@ public class ChatMessageServiceImpl implements ChatMessageService {
queryWrapperX.orderByDesc(AiChatMessageDO::getId); queryWrapperX.orderByDesc(AiChatMessageDO::getId);
PageResult<AiChatMessageDO> pageResult = aiChatMessageMapper.selectPage(req, queryWrapperX); PageResult<AiChatMessageDO> pageResult = aiChatMessageMapper.selectPage(req, queryWrapperX);
// 转换 res // 转换 res
List<ChatMessageListRes> messageListResList = ChatMessageConvert.INSTANCE.convert(pageResult.getList()); List<AiChatMessageListRes> messageListResList = AiChatMessageConvert.INSTANCE.convert(pageResult.getList());
return new PageResult(messageListResList, pageResult.getTotal()); return new PageResult(messageListResList, pageResult.getTotal());
} }

View File

@ -5,7 +5,7 @@ import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.ai.ErrorCodeConstants; import cn.iocoder.yudao.module.ai.ErrorCodeConstants;
import cn.iocoder.yudao.module.ai.convert.ChatModalConvert; import cn.iocoder.yudao.module.ai.convert.AiChatModalConvert;
import cn.iocoder.yudao.module.ai.dal.dataobject.AiChatModalDO; import cn.iocoder.yudao.module.ai.dal.dataobject.AiChatModalDO;
import cn.iocoder.yudao.module.ai.enums.AiChatModalDisableEnum; import cn.iocoder.yudao.module.ai.enums.AiChatModalDisableEnum;
import cn.iocoder.yudao.module.ai.mapper.AiChatModalMapper; import cn.iocoder.yudao.module.ai.mapper.AiChatModalMapper;
@ -47,14 +47,14 @@ public class AiChatModalServiceImpl implements AiChatModalService {
// 查询 // 查询
PageResult<AiChatModalDO> aiChatModalDOPageResult = aiChatModalMapper.selectPage(req, queryWrapperX); PageResult<AiChatModalDO> aiChatModalDOPageResult = aiChatModalMapper.selectPage(req, queryWrapperX);
// 转换 res // 转换 res
List<AiChatModalListRes> resList = ChatModalConvert.INSTANCE.convertAiChatModalListRes(aiChatModalDOPageResult.getList()); List<AiChatModalListRes> resList = AiChatModalConvert.INSTANCE.convertAiChatModalListRes(aiChatModalDOPageResult.getList());
return new PageResult<>(resList, aiChatModalDOPageResult.getTotal()); return new PageResult<>(resList, aiChatModalDOPageResult.getTotal());
} }
@Override @Override
public void add(AiChatModalAddReq req) { public void add(AiChatModalAddReq req) {
// 转换 do // 转换 do
AiChatModalDO insertChatModalDO = ChatModalConvert.INSTANCE.convertAiChatModalDO(req); AiChatModalDO insertChatModalDO = AiChatModalConvert.INSTANCE.convertAiChatModalDO(req);
// //
insertChatModalDO.setDisable(AiChatModalDisableEnum.NO.getValue()); insertChatModalDO.setDisable(AiChatModalDisableEnum.NO.getValue());
// 保存数据库 // 保存数据库
@ -66,7 +66,7 @@ public class AiChatModalServiceImpl implements AiChatModalService {
// 校验模型是否存在 // 校验模型是否存在
validateChatModalExists(id); validateChatModalExists(id);
// 转换 updateChatModalDO // 转换 updateChatModalDO
AiChatModalDO updateChatModalDO = ChatModalConvert.INSTANCE.convertAiChatModalDO(req); AiChatModalDO updateChatModalDO = AiChatModalConvert.INSTANCE.convertAiChatModalDO(req);
updateChatModalDO.setId(id); updateChatModalDO.setId(id);
// 更新数据库 // 更新数据库
aiChatModalMapper.updateById(updateChatModalDO); aiChatModalMapper.updateById(updateChatModalDO);

View File

@ -6,13 +6,13 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.ai.ErrorCodeConstants; import cn.iocoder.yudao.module.ai.ErrorCodeConstants;
import cn.iocoder.yudao.module.ai.convert.ChatRoleConvert; import cn.iocoder.yudao.module.ai.convert.AiChatRoleConvert;
import cn.iocoder.yudao.module.ai.dal.dataobject.AiChatRoleDO; import cn.iocoder.yudao.module.ai.dal.dataobject.AiChatRoleDO;
import cn.iocoder.yudao.module.ai.enums.ChatRoleClassifyEnum; import cn.iocoder.yudao.module.ai.enums.AiChatRoleClassifyEnum;
import cn.iocoder.yudao.module.ai.enums.ChatRoleSourceEnum; import cn.iocoder.yudao.module.ai.enums.AiChatRoleSourceEnum;
import cn.iocoder.yudao.module.ai.enums.ChatRoleVisibilityEnum; import cn.iocoder.yudao.module.ai.enums.AiChatRoleVisibilityEnum;
import cn.iocoder.yudao.module.ai.mapper.AiChatRoleMapper; import cn.iocoder.yudao.module.ai.mapper.AiChatRoleMapper;
import cn.iocoder.yudao.module.ai.service.ChatRoleService; import cn.iocoder.yudao.module.ai.service.AiChatRoleService;
import cn.iocoder.yudao.module.ai.vo.*; import cn.iocoder.yudao.module.ai.vo.*;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -29,12 +29,12 @@ import java.util.List;
@Service @Service
@AllArgsConstructor @AllArgsConstructor
@Slf4j @Slf4j
public class ChatRoleServiceImpl implements ChatRoleService { public class AiChatRoleServiceImpl implements AiChatRoleService {
private final AiChatRoleMapper aiChatRoleMapper; private final AiChatRoleMapper aiChatRoleMapper;
@Override @Override
public PageResult<ChatRoleListRes> list(ChatRoleListReq req) { public PageResult<AiChatRoleListRes> list(AiChatRoleListReq req) {
// 查询条件 // 查询条件
LambdaQueryWrapperX<AiChatRoleDO> queryWrapperX = new LambdaQueryWrapperX<>(); LambdaQueryWrapperX<AiChatRoleDO> queryWrapperX = new LambdaQueryWrapperX<>();
// search 查询 // search 查询
@ -48,18 +48,18 @@ public class ChatRoleServiceImpl implements ChatRoleService {
Long total = aiChatRoleDOPageResult.getTotal(); Long total = aiChatRoleDOPageResult.getTotal();
List<AiChatRoleDO> roleList = aiChatRoleDOPageResult.getList(); List<AiChatRoleDO> roleList = aiChatRoleDOPageResult.getList();
// 换货res // 换货res
List<ChatRoleListRes> chatRoleListResList = ChatRoleConvert.INSTANCE.convertChatRoleListRes(roleList); List<AiChatRoleListRes> chatRoleListResList = AiChatRoleConvert.INSTANCE.convertChatRoleListRes(roleList);
return new PageResult<>(chatRoleListResList, total); return new PageResult<>(chatRoleListResList, total);
} }
@Override @Override
public void add(ChatRoleAddReq req) { public void add(AiChatRoleAddReq req) {
// 转换enum并校验enum // 转换enum并校验enum
ChatRoleClassifyEnum.valueOfClassify(req.getClassify()); AiChatRoleClassifyEnum.valueOfClassify(req.getClassify());
ChatRoleVisibilityEnum.valueOfType(req.getVisibility()); AiChatRoleVisibilityEnum.valueOfType(req.getVisibility());
ChatRoleSourceEnum.valueOfType(req.getRoleSource()); AiChatRoleSourceEnum.valueOfType(req.getRoleSource());
// 转换do // 转换do
AiChatRoleDO insertAiChatRoleDO = ChatRoleConvert.INSTANCE.convertAiChatRoleDO(req); AiChatRoleDO insertAiChatRoleDO = AiChatRoleConvert.INSTANCE.convertAiChatRoleDO(req);
insertAiChatRoleDO.setUserId(SecurityFrameworkUtils.getLoginUserId()); insertAiChatRoleDO.setUserId(SecurityFrameworkUtils.getLoginUserId());
insertAiChatRoleDO.setUseCount(0); insertAiChatRoleDO.setUseCount(0);
// 保存 // 保存
@ -67,24 +67,24 @@ public class ChatRoleServiceImpl implements ChatRoleService {
} }
@Override @Override
public void update(Long id, ChatRoleUpdateReq req) { public void update(Long id, AiChatRoleUpdateReq req) {
// 转换enum并校验enum // 转换enum并校验enum
ChatRoleClassifyEnum.valueOfClassify(req.getClassify()); AiChatRoleClassifyEnum.valueOfClassify(req.getClassify());
ChatRoleVisibilityEnum.valueOfType(req.getVisibility()); AiChatRoleVisibilityEnum.valueOfType(req.getVisibility());
ChatRoleSourceEnum.valueOfType(req.getRoleSource()); AiChatRoleSourceEnum.valueOfType(req.getRoleSource());
// 检查角色是否存在 // 检查角色是否存在
validateChatRoleExists(id); validateChatRoleExists(id);
// 转换do // 转换do
AiChatRoleDO updateChatRole = ChatRoleConvert.INSTANCE.convertAiChatRoleDO(req); AiChatRoleDO updateChatRole = AiChatRoleConvert.INSTANCE.convertAiChatRoleDO(req);
updateChatRole.setId(id); updateChatRole.setId(id);
aiChatRoleMapper.updateById(updateChatRole); aiChatRoleMapper.updateById(updateChatRole);
} }
@Override @Override
public void updateVisibility(Long id, ChatRoleUpdateVisibilityReq req) { public void updateVisibility(Long id, AiChatRoleUpdateVisibilityReq req) {
// 转换enum并校验enum // 转换enum并校验enum
ChatRoleVisibilityEnum.valueOfType(req.getVisibility()); AiChatRoleVisibilityEnum.valueOfType(req.getVisibility());
// 检查角色是否存在 // 检查角色是否存在
validateChatRoleExists(id); validateChatRoleExists(id);
// 更新 // 更新

View File

@ -12,10 +12,10 @@ import cn.iocoder.yudao.module.ai.enums.AiClientNameEnum;
import cn.iocoder.yudao.module.ai.mapper.AiChatConversationMapper; import cn.iocoder.yudao.module.ai.mapper.AiChatConversationMapper;
import cn.iocoder.yudao.module.ai.mapper.AiChatMessageMapper; import cn.iocoder.yudao.module.ai.mapper.AiChatMessageMapper;
import cn.iocoder.yudao.module.ai.mapper.AiChatRoleMapper; import cn.iocoder.yudao.module.ai.mapper.AiChatRoleMapper;
import cn.iocoder.yudao.module.ai.service.ChatConversationService; import cn.iocoder.yudao.module.ai.service.AiChatConversationService;
import cn.iocoder.yudao.module.ai.service.ChatService; import cn.iocoder.yudao.module.ai.service.AiChatService;
import cn.iocoder.yudao.module.ai.vo.ChatConversationRes; import cn.iocoder.yudao.module.ai.vo.AiChatConversationRes;
import cn.iocoder.yudao.module.ai.vo.ChatReq; import cn.iocoder.yudao.module.ai.vo.AiChatReq;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@ -36,13 +36,13 @@ import java.util.function.Consumer;
@Slf4j @Slf4j
@Service @Service
@AllArgsConstructor @AllArgsConstructor
public class ChatServiceImpl implements ChatService { public class AiChatServiceImpl implements AiChatService {
private final AiClient aiClient; private final AiClient aiClient;
private final AiChatRoleMapper aiChatRoleMapper; private final AiChatRoleMapper aiChatRoleMapper;
private final AiChatMessageMapper aiChatMessageMapper; private final AiChatMessageMapper aiChatMessageMapper;
private final AiChatConversationMapper aiChatConversationMapper; private final AiChatConversationMapper aiChatConversationMapper;
private final ChatConversationService chatConversationService; private final AiChatConversationService chatConversationService;
/** /**
* chat * chat
@ -51,12 +51,12 @@ public class ChatServiceImpl implements ChatService {
* @return * @return
*/ */
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public String chat(ChatReq req) { public String chat(AiChatReq req) {
Long loginUserId = SecurityFrameworkUtils.getLoginUserId(); Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
// 获取 client 类型 // 获取 client 类型
AiClientNameEnum clientNameEnum = AiClientNameEnum.valueOfName(req.getModal()); AiClientNameEnum clientNameEnum = AiClientNameEnum.valueOfName(req.getModal());
// 获取对话信息 // 获取对话信息
ChatConversationRes conversationRes = chatConversationService.getConversation(req.getConversationId()); AiChatConversationRes conversationRes = chatConversationService.getConversation(req.getConversationId());
// 保存 chat message // 保存 chat message
saveChatMessage(req, conversationRes, loginUserId); saveChatMessage(req, conversationRes, loginUserId);
String content = null; String content = null;
@ -80,7 +80,7 @@ public class ChatServiceImpl implements ChatService {
return content; return content;
} }
private void saveChatMessage(ChatReq req, ChatConversationRes conversationRes, Long loginUserId) { private void saveChatMessage(AiChatReq req, AiChatConversationRes conversationRes, Long loginUserId) {
Long chatConversationId = conversationRes.getId(); Long chatConversationId = conversationRes.getId();
// 增加 chat message 记录 // 增加 chat message 记录
aiChatMessageMapper.insert( aiChatMessageMapper.insert(
@ -98,7 +98,7 @@ public class ChatServiceImpl implements ChatService {
aiChatConversationMapper.updateIncrChatCount(req.getConversationId()); aiChatConversationMapper.updateIncrChatCount(req.getConversationId());
} }
public void saveSystemChatMessage(ChatReq req, ChatConversationRes conversationRes, Long loginUserId, String systemPrompts) { public void saveSystemChatMessage(AiChatReq req, AiChatConversationRes conversationRes, Long loginUserId, String systemPrompts) {
Long chatConversationId = conversationRes.getId(); Long chatConversationId = conversationRes.getId();
// 增加 chat message 记录 // 增加 chat message 记录
aiChatMessageMapper.insert( aiChatMessageMapper.insert(
@ -125,12 +125,12 @@ public class ChatServiceImpl implements ChatService {
* @return * @return
*/ */
@Override @Override
public void chatStream(ChatReq req, Utf8SseEmitter sseEmitter) { public void chatStream(AiChatReq req, Utf8SseEmitter sseEmitter) {
Long loginUserId = SecurityFrameworkUtils.getLoginUserId(); Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
// 获取 client 类型 // 获取 client 类型
AiClientNameEnum clientNameEnum = AiClientNameEnum.valueOfName(req.getModal()); AiClientNameEnum clientNameEnum = AiClientNameEnum.valueOfName(req.getModal());
// 获取对话信息 // 获取对话信息
ChatConversationRes conversationRes = chatConversationService.getConversation(req.getConversationId()); AiChatConversationRes conversationRes = chatConversationService.getConversation(req.getConversationId());
// 创建 chat 需要的 Prompt // 创建 chat 需要的 Prompt
Prompt prompt = new Prompt(req.getPrompt()); Prompt prompt = new Prompt(req.getPrompt());
req.setTopK(req.getTopK()); req.setTopK(req.getTopK());

View File

@ -14,7 +14,7 @@ import lombok.experimental.Accessors;
*/ */
@Data @Data
@Accessors(chain = true) @Accessors(chain = true)
public class ChatConversationCreateRoleReq { public class AiChatConversationCreateRoleReq {
@Schema(description = "chat角色Id") @Schema(description = "chat角色Id")
@NotNull(message = "聊天角色id不能为空!") @NotNull(message = "聊天角色id不能为空!")

View File

@ -14,7 +14,7 @@ import lombok.experimental.Accessors;
*/ */
@Data @Data
@Accessors(chain = true) @Accessors(chain = true)
public class ChatConversationCreateUserReq { public class AiChatConversationCreateUserReq {
@Schema(description = "对话标题") @Schema(description = "对话标题")
@NotNull(message = "标题不能为空!") @NotNull(message = "标题不能为空!")

View File

@ -13,7 +13,7 @@ import lombok.experimental.Accessors;
*/ */
@Data @Data
@Accessors(chain = true) @Accessors(chain = true)
public class ChatConversationListReq { public class AiChatConversationListReq {
@Schema(description = "查询根据title") @Schema(description = "查询根据title")
private String search; private String search;

View File

@ -13,7 +13,7 @@ import lombok.experimental.Accessors;
*/ */
@Data @Data
@Accessors(chain = true) @Accessors(chain = true)
public class ChatConversationRes { public class AiChatConversationRes {
@Schema(description = "id") @Schema(description = "id")
private Long id; private Long id;

View File

@ -15,7 +15,7 @@ import lombok.experimental.Accessors;
*/ */
@Data @Data
@Accessors(chain = true) @Accessors(chain = true)
public class ChatMessageDeleteReq extends PageParam { public class AiChatMessageDeleteReq extends PageParam {
@Schema(description = "id") @Schema(description = "id")
@NotNull @NotNull

View File

@ -13,7 +13,7 @@ import lombok.experimental.Accessors;
*/ */
@Data @Data
@Accessors(chain = true) @Accessors(chain = true)
public class ChatMessageListRes { public class AiChatMessageListRes {
@Schema(description = "编号") @Schema(description = "编号")
private Long id; private Long id;

View File

@ -15,7 +15,7 @@ import lombok.experimental.Accessors;
*/ */
@Data @Data
@Accessors(chain = true) @Accessors(chain = true)
public class ChatMessageReq extends PageParam { public class AiChatMessageReq extends PageParam {
@Schema(description = "聊天ID关联到特定的会话或对话") @Schema(description = "聊天ID关联到特定的会话或对话")
@NotNull @NotNull

View File

@ -15,7 +15,7 @@ import lombok.experimental.Accessors;
*/ */
@Data @Data
@Accessors(chain = true) @Accessors(chain = true)
public class ChatReq { public class AiChatReq {
@Schema(description = "ai模型(查看 AiClientNameEnum)") @Schema(description = "ai模型(查看 AiClientNameEnum)")
@NotNull(message = "模型不能为空!") @NotNull(message = "模型不能为空!")

View File

@ -1,6 +1,6 @@
package cn.iocoder.yudao.module.ai.vo; package cn.iocoder.yudao.module.ai.vo;
import cn.iocoder.yudao.module.ai.enums.OpenAiModelEnum; import cn.iocoder.yudao.module.ai.enums.AiOpenAiModelEnum;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import lombok.Data; import lombok.Data;
@ -16,6 +16,6 @@ public class AiChatReqVO {
@Schema(description = "AI模型", requiredMode = Schema.RequiredMode.REQUIRED) @Schema(description = "AI模型", requiredMode = Schema.RequiredMode.REQUIRED)
@NotNull(message = "AI模型不能为空") @NotNull(message = "AI模型不能为空")
private OpenAiModelEnum aiModel; private AiOpenAiModelEnum aiModel;
} }

View File

@ -14,7 +14,7 @@ import lombok.experimental.Accessors;
*/ */
@Data @Data
@Accessors(chain = true) @Accessors(chain = true)
public class ChatRoleAddReq extends PageParam { public class AiChatRoleAddReq extends PageParam {
@NotNull @NotNull
@Schema(description = "模型编号,关联到角色使用的特定模型") @Schema(description = "模型编号,关联到角色使用的特定模型")

View File

@ -13,7 +13,7 @@ import lombok.experimental.Accessors;
*/ */
@Data @Data
@Accessors(chain = true) @Accessors(chain = true)
public class ChatRoleListReq extends PageParam { public class AiChatRoleListReq extends PageParam {
@Schema(description = "查询") @Schema(description = "查询")
private String search; private String search;

View File

@ -12,7 +12,7 @@ import lombok.experimental.Accessors;
*/ */
@Data @Data
@Accessors(chain = true) @Accessors(chain = true)
public class ChatRoleListRes { public class AiChatRoleListRes {
@Schema(description = "id") @Schema(description = "id")
private Long id; private Long id;

View File

@ -14,7 +14,7 @@ import lombok.experimental.Accessors;
*/ */
@Data @Data
@Accessors(chain = true) @Accessors(chain = true)
public class ChatRoleUpdateReq extends PageParam { public class AiChatRoleUpdateReq extends PageParam {
@NotNull @NotNull
@Schema(description = "模型编号,关联到角色使用的特定模型") @Schema(description = "模型编号,关联到角色使用的特定模型")

View File

@ -14,7 +14,7 @@ import lombok.experimental.Accessors;
*/ */
@Data @Data
@Accessors(chain = true) @Accessors(chain = true)
public class ChatRoleUpdateVisibilityReq extends PageParam { public class AiChatRoleUpdateVisibilityReq extends PageParam {
@NotNull @NotNull
@Schema(description = "发布状态0表示仅自己可见1表示公开2表示禁用") @Schema(description = "发布状态0表示仅自己可见1表示公开2表示禁用")