|
@@ -7,11 +7,11 @@ import com.backendsys.modules.ai.chat.dao.ChatDao;
|
|
|
import com.backendsys.modules.ai.chat.dao.ChatHistoryDao;
|
|
|
import com.backendsys.modules.ai.chat.entity.Chat;
|
|
|
import com.backendsys.modules.ai.chat.entity.ChatHistory;
|
|
|
+import com.backendsys.modules.ai.chat.entity.ChatResult;
|
|
|
import com.backendsys.modules.ai.chat.service.ChatService;
|
|
|
import com.backendsys.modules.common.config.security.utils.SecurityUtil;
|
|
|
-import com.backendsys.modules.sdk.deepseek.entity.DSRequestMessage;
|
|
|
-import com.backendsys.modules.sdk.deepseek.entity.DSResponse;
|
|
|
import com.backendsys.modules.sdk.deepseek.service.DeepSeekClient;
|
|
|
+import com.backendsys.modules.sdk.tencentcloud.huanyuan.service.HunYuanClient;
|
|
|
import com.backendsys.utils.response.PageEntity;
|
|
|
import com.backendsys.utils.response.PageInfoResult;
|
|
|
import com.backendsys.utils.v2.PageUtils;
|
|
@@ -19,7 +19,6 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.UUID;
|
|
@@ -28,6 +27,8 @@ import java.util.concurrent.CompletableFuture;
|
|
|
@Service
|
|
|
public class ChatServiceImpl implements ChatService {
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private HunYuanClient hunYuanClient;
|
|
|
@Autowired
|
|
|
private DeepSeekClient deepSeekClient;
|
|
|
|
|
@@ -51,6 +52,10 @@ public class ChatServiceImpl implements ChatService {
|
|
|
String model_version = chat.getModel_version();
|
|
|
String prompt = chat.getContent();
|
|
|
|
|
|
+ if (("DEEPSEEK".equals(model)) && StrUtil.isEmpty(model_version)) {
|
|
|
+ throw new CustException("请选择(Deepseek)模型版本");
|
|
|
+ }
|
|
|
+
|
|
|
// -- 是否首次创建 (如果历史记录code为空,即是首次创建) ---------------
|
|
|
Boolean is_create = false;
|
|
|
String history_code = chat.getHistory_code();
|
|
@@ -86,65 +91,71 @@ public class ChatServiceImpl implements ChatService {
|
|
|
|
|
|
// -- 对话 -----------------------------------------------------
|
|
|
|
|
|
- // 再记录一下耗时
|
|
|
+ // 创建一个 CompletableFuture 来执行异步任务
|
|
|
+ String finalHistory_code = history_code;
|
|
|
+ CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
|
|
|
+ try {
|
|
|
|
|
|
- // ------------------------------------------------------------
|
|
|
- if ("DEEPSEEK".equals(model)) {
|
|
|
- // 创建一个 CompletableFuture 来执行异步任务
|
|
|
- String finalHistory_code = history_code;
|
|
|
- CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
|
|
|
+ // [DB] 查询对话历史记录 (按时间升序排序,最新的在最后面) (用于对话上下文)
|
|
|
+ LambdaQueryWrapper<Chat> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ wrapper.eq(Chat::getDel_flag, -1);
|
|
|
+ wrapper.eq(Chat::getHistory_code, finalHistory_code);
|
|
|
+ wrapper.orderByDesc(Chat::getCreate_time);
|
|
|
+ List<Chat> chatList = chatDao.selectList(wrapper);
|
|
|
|
|
|
- try {
|
|
|
|
|
|
- // [DB] 查询对话历史记录 (按时间升序排序,最新的在最后面) (用于对话上下文)
|
|
|
- LambdaQueryWrapper<Chat> wrapper = new LambdaQueryWrapper<>();
|
|
|
- wrapper.eq(Chat::getDel_flag, -1);
|
|
|
- wrapper.eq(Chat::getHistory_code, finalHistory_code);
|
|
|
- wrapper.orderByDesc(Chat::getCreate_time);
|
|
|
- List<Chat> chatList = chatDao.selectList(wrapper);
|
|
|
+ // [INSERT][提问]
|
|
|
+ Chat userChat = new Chat();
|
|
|
+ BeanUtil.copyProperties(chat, userChat);
|
|
|
+ chatDao.insert(userChat);
|
|
|
|
|
|
+
|
|
|
+ ChatResult chatResult = null;
|
|
|
+ // ------------------------------------------------------------
|
|
|
+ if ("DEEPSEEK".equals(model)) {
|
|
|
// [DeepSeek] 发起对话
|
|
|
- DSResponse dsResponse = deepSeekClient.chatCompletion(model_version, prompt, chatList);
|
|
|
-
|
|
|
- // [DB][提问]
|
|
|
- Chat userChat = new Chat();
|
|
|
- BeanUtil.copyProperties(chat, userChat);
|
|
|
- chatDao.insert(userChat);
|
|
|
-
|
|
|
- // [DB][思考] THINK
|
|
|
- String reasoningContent = dsResponse.getReasoning_content();
|
|
|
- if (StrUtil.isNotEmpty(reasoningContent)) {
|
|
|
- //
|
|
|
- Chat thinkChat = new Chat();
|
|
|
- BeanUtil.copyProperties(chat, thinkChat);
|
|
|
- //
|
|
|
- thinkChat.setRole("assistant");
|
|
|
- thinkChat.setContent_type("THINK");
|
|
|
- thinkChat.setContent(reasoningContent);
|
|
|
- thinkChat.setDuration(dsResponse.getReasoning_duration());
|
|
|
- chatDao.insert(thinkChat);
|
|
|
- }
|
|
|
-
|
|
|
- // [DB][回复] REPLY
|
|
|
- String content = dsResponse.getContent();
|
|
|
- if (StrUtil.isNotEmpty(content)) {
|
|
|
- //
|
|
|
- Chat replyChat = new Chat();
|
|
|
- BeanUtil.copyProperties(chat, replyChat);
|
|
|
- //
|
|
|
- replyChat.setRole("assistant");
|
|
|
- replyChat.setContent_type("REPLY");
|
|
|
- replyChat.setContent(content);
|
|
|
- replyChat.setDuration(dsResponse.getContent_duration());
|
|
|
- chatDao.insert(replyChat);
|
|
|
- }
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- System.out.println(e.getMessage());
|
|
|
+ chatResult = deepSeekClient.chatCompletion(model_version, prompt, chatList);
|
|
|
+ }
|
|
|
+ // ------------------------------------------------------------
|
|
|
+ if ("HUNYUAN".equals(model)) {
|
|
|
+ // [混元] 发起对话
|
|
|
+ chatResult = hunYuanClient.chatCompletion(prompt, chatList);
|
|
|
+ }
|
|
|
+ // ------------------------------------------------------------
|
|
|
+
|
|
|
+ // [INSERT][思考] THINK
|
|
|
+ String reasoningContent = chatResult.getReasoning_content();
|
|
|
+ if (StrUtil.isNotEmpty(reasoningContent)) {
|
|
|
+ //
|
|
|
+ Chat thinkChat = new Chat();
|
|
|
+ BeanUtil.copyProperties(chat, thinkChat);
|
|
|
+ //
|
|
|
+ thinkChat.setRole("assistant");
|
|
|
+ thinkChat.setContent_type("THINK");
|
|
|
+ thinkChat.setContent(reasoningContent);
|
|
|
+ thinkChat.setDuration(chatResult.getReasoning_duration());
|
|
|
+ chatDao.insert(thinkChat);
|
|
|
}
|
|
|
|
|
|
- });
|
|
|
- }
|
|
|
+ // [INSERT][回复] REPLY
|
|
|
+ String content = chatResult.getContent();
|
|
|
+ if (StrUtil.isNotEmpty(content)) {
|
|
|
+ //
|
|
|
+ Chat replyChat = new Chat();
|
|
|
+ BeanUtil.copyProperties(chat, replyChat);
|
|
|
+ //
|
|
|
+ replyChat.setRole("assistant");
|
|
|
+ replyChat.setContent_type("REPLY");
|
|
|
+ replyChat.setContent(content);
|
|
|
+ replyChat.setDuration(chatResult.getContent_duration());
|
|
|
+ chatDao.insert(replyChat);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.out.println(e.getMessage());
|
|
|
+ }
|
|
|
+ });
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
|
return Map.of("history_code", history_code);
|
|
@@ -156,12 +167,14 @@ public class ChatServiceImpl implements ChatService {
|
|
|
* 获取我的对话列表 (升序, 最新的在最后面出现)
|
|
|
*/
|
|
|
@Override
|
|
|
- public PageEntity selectChatList(String history_code) {
|
|
|
+ public PageEntity selectChatList(Chat chat) {
|
|
|
+
|
|
|
+ String history_code = chat.getHistory_code();
|
|
|
|
|
|
ChatHistory chatHistory = chatHistoryDao.selectOne(new LambdaQueryWrapper<ChatHistory>()
|
|
|
.eq(ChatHistory::getDel_flag, -1)
|
|
|
.eq(ChatHistory::getHistory_code, history_code));
|
|
|
- if (chatHistory == null) throw new CustException("history_code 不存在");
|
|
|
+ if (chatHistory == null) throw new CustException("对话历史不存在");
|
|
|
|
|
|
PageUtils.startPage(); // 分页
|
|
|
LambdaQueryWrapper<Chat> wrapper = new LambdaQueryWrapper<>();
|
|
@@ -174,4 +187,24 @@ public class ChatServiceImpl implements ChatService {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
+ * 删除我的对话 (软删除)
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> deleteChat(Chat chat) {
|
|
|
+
|
|
|
+ String history_code = chat.getHistory_code();
|
|
|
+
|
|
|
+ LambdaQueryWrapper<Chat> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ wrapper.eq(Chat::getDel_flag, -1);
|
|
|
+ wrapper.eq(Chat::getHistory_code, history_code);
|
|
|
+
|
|
|
+ // [软删除] 删除对话
|
|
|
+ chat.setDel_flag(1);
|
|
|
+ chatDao.update(chat, wrapper);
|
|
|
+
|
|
|
+ return Map.of("history_code", chat.getHistory_code());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|