【新增】AI:新建对话时,不使用默认角色

This commit is contained in:
YunaiV 2024-05-24 15:38:56 +08:00
parent 10ca7a93ac
commit 48c3210f5f
5 changed files with 12 additions and 36 deletions

View File

@ -20,14 +20,6 @@ import java.util.List;
@Mapper @Mapper
public interface AiChatRoleMapper extends BaseMapperX<AiChatRoleDO> { public interface AiChatRoleMapper extends BaseMapperX<AiChatRoleDO> {
default AiChatRoleDO selectFirstByPublicStatusAndStatus(Boolean publicStatus, Integer status) {
return selectOne(new QueryWrapperX<AiChatRoleDO>()
.eq("status", status)
.eq("public_status", publicStatus)
.limitN(1)
.orderByAsc("sort"));
}
default PageResult<AiChatRoleDO> selectPage(AiChatRolePageReqVO reqVO) { default PageResult<AiChatRoleDO> selectPage(AiChatRolePageReqVO reqVO) {
return selectPage(reqVO, new LambdaQueryWrapperX<AiChatRoleDO>() return selectPage(reqVO, new LambdaQueryWrapperX<AiChatRoleDO>()
.likeIfPresent(AiChatRoleDO::getName, reqVO.getName()) .likeIfPresent(AiChatRoleDO::getName, reqVO.getName())

View File

@ -46,21 +46,22 @@ public class AiChatConversationServiceImpl implements AiChatConversationService
@Override @Override
public Long createChatConversationMy(AiChatConversationCreateMyReqVO createReqVO, Long userId) { public Long createChatConversationMy(AiChatConversationCreateMyReqVO createReqVO, Long userId) {
// 1.1 获得 AiChatRoleDO 聊天角色 // 1.1 获得 AiChatRoleDO 聊天角色
AiChatRoleDO role = createReqVO.getRoleId() != null ? chatRoleService.validateChatRole(createReqVO.getRoleId()) AiChatRoleDO role = createReqVO.getRoleId() != null ? chatRoleService.validateChatRole(createReqVO.getRoleId()) : null;
: chatRoleService.getRequiredDefaultChatRole();
Assert.notNull(role, "必须找到聊天角色");
// 1.2 获得 AiChatModelDO 聊天模型 // 1.2 获得 AiChatModelDO 聊天模型
AiChatModelDO model = role.getModelId() != null ? chatModalService.validateChatModel(role.getModelId()) AiChatModelDO model = role != null && role.getModelId() != null ? chatModalService.validateChatModel(role.getModelId())
: chatModalService.getRequiredDefaultChatModel(); : chatModalService.getRequiredDefaultChatModel();
Assert.notNull(model, "必须找到默认模型"); Assert.notNull(model, "必须找到默认模型");
validateChatModel(model); validateChatModel(model);
// 2. 创建 AiChatConversationDO 聊天对话 // 2. 创建 AiChatConversationDO 聊天对话
String title = createReqVO.getRoleId() == null ? AiChatConversationDO.TITLE_DEFAULT : role.getName(); AiChatConversationDO conversation = new AiChatConversationDO().setUserId(userId).setPinned(false)
AiChatConversationDO conversation = new AiChatConversationDO() .setModelId(model.getId()).setModel(model.getModel())
.setUserId(userId).setTitle(title).setPinned(false)
.setRoleId(role.getId()).setModelId(model.getId()).setModel(model.getModel()).setSystemMessage(role.getSystemMessage())
.setTemperature(model.getTemperature()).setMaxTokens(model.getMaxTokens()).setMaxContexts(model.getMaxContexts()); .setTemperature(model.getTemperature()).setMaxTokens(model.getMaxTokens()).setMaxContexts(model.getMaxContexts());
if (role != null) {
conversation.setTitle(role.getName()).setRoleId(role.getId()).setSystemMessage(role.getSystemMessage());
} else {
conversation.setTitle(AiChatConversationDO.TITLE_DEFAULT);
}
chatConversationMapper.insert(conversation); chatConversationMapper.insert(conversation);
return conversation.getId(); return conversation.getId();
} }

View File

@ -165,7 +165,9 @@ public class AiChatMessageServiceImpl implements AiChatMessageService {
// 1. 构建 Prompt Message 列表 // 1. 构建 Prompt Message 列表
List<Message> chatMessages = new ArrayList<>(); List<Message> chatMessages = new ArrayList<>();
// 1.1 system context 角色设定 // 1.1 system context 角色设定
chatMessages.add(new SystemMessage(conversation.getSystemMessage())); if (StrUtil.isNotBlank(conversation.getSystemMessage())) {
chatMessages.add(new SystemMessage(conversation.getSystemMessage()));
}
// 1.2 history message 历史消息 // 1.2 history message 历史消息
List<AiChatMessageDO> contextMessages = filterContextMessages(messages, conversation, sendReqVO); List<AiChatMessageDO> contextMessages = filterContextMessages(messages, conversation, sendReqVO);
contextMessages.forEach(message -> chatMessages.add(new ChatMessage(message.getType().toUpperCase(), message.getContent()))); contextMessages.forEach(message -> chatMessages.add(new ChatMessage(message.getType().toUpperCase(), message.getContent())));

View File

@ -94,15 +94,6 @@ public interface AiChatRoleService {
*/ */
AiChatRoleDO validateChatRole(Long id); AiChatRoleDO validateChatRole(Long id);
/**
* 获得默认的聊天角色
*
* 如果获取不到则抛出 {@link cn.iocoder.yudao.framework.common.exception.ServiceException} 业务异常
*
* @return 聊天角色
*/
AiChatRoleDO getRequiredDefaultChatRole();
/** /**
* 获得聊天角色分页 * 获得聊天角色分页
* *

View File

@ -121,16 +121,6 @@ public class AiChatRoleServiceImpl implements AiChatRoleService {
return chatRole; return chatRole;
} }
@Override
public AiChatRoleDO getRequiredDefaultChatRole() {
AiChatRoleDO chatRole = chatRoleMapper.selectFirstByPublicStatusAndStatus(
true, CommonStatusEnum.ENABLE.getStatus());
if (chatRole == null) {
throw exception(CHAT_ROLE_DEFAULT_NOT_EXISTS);
}
return chatRole;
}
@Override @Override
public PageResult<AiChatRoleDO> getChatRolePage(AiChatRolePageReqVO pageReqVO) { public PageResult<AiChatRoleDO> getChatRolePage(AiChatRolePageReqVO pageReqVO) {
return chatRoleMapper.selectPage(pageReqVO); return chatRoleMapper.selectPage(pageReqVO);