1
0

AiBotChatService.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package com.webchat.aigc.llm;
  2. import com.webchat.aigc.service.BotService;
  3. import com.webchat.common.constants.ConnectConstants;
  4. import com.webchat.common.enums.AiFunctionEnum;
  5. import com.webchat.common.enums.PromptTemplateEnum;
  6. import com.webchat.common.helper.SseEmitterHelper;
  7. import com.webchat.common.util.JsonUtil;
  8. import com.webchat.domain.dto.bot.BotDTO;
  9. import com.webchat.domain.vo.llm.FunctionCallResponse;
  10. import com.webchat.domain.vo.request.mess.ChatMessageRequestVO;
  11. import lombok.extern.slf4j.Slf4j;
  12. import org.apache.commons.collections.CollectionUtils;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.stereotype.Service;
  15. import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
  16. import java.util.ArrayList;
  17. import java.util.HashMap;
  18. import java.util.List;
  19. import java.util.Map;
  20. import java.util.stream.Collectors;
  21. @Slf4j
  22. @Service
  23. public class AiBotChatService {
  24. @Autowired
  25. private AiFunctionCallService aiFunctionCallService;
  26. @Autowired
  27. private AiGenImageService aiGenImageService;
  28. @Autowired
  29. private AiBotPluginService aiBotPluginService;
  30. @Autowired
  31. private GPTChatService gptChatService;
  32. @Autowired
  33. private BotService botService;
  34. @Autowired
  35. private AiBotQAService aiBotQAService;
  36. private String getSSEBizCode() {
  37. return ConnectConstants.ConnectBiz.getBizCode(ConnectConstants.ClientEnum.PC,
  38. ConnectConstants.AppEnum.WEB,
  39. ConnectConstants.BizEnum.CHAT);
  40. }
  41. /**
  42. * 我的ai助手对话功能实现
  43. *
  44. * @param messageJson
  45. */
  46. public void chat(String messageJson) {
  47. ChatMessageRequestVO chatMessage = JsonUtil.fromJson(messageJson, ChatMessageRequestVO.class);
  48. // 消息发送人
  49. String senderId = chatMessage.getSenderId();
  50. // 用户输入消息
  51. String message = chatMessage.getMessage();
  52. String bizCode = getSSEBizCode();
  53. SseEmitter sseEmitter = SseEmitterHelper.get(bizCode, senderId);
  54. if (sseEmitter == null) {
  55. // 链接对象为空,说明当前用户sse链接不在当前节点,直接return,由集群其他节点处理任务
  56. return;
  57. }
  58. /************** 当前用户的 sse 链接在当前阶段,由当前节点完成AiBot对话服务处理 **************/
  59. /**************************************** 意图识别 ************************************/
  60. SseEmitterHelper.send(this.getSSEBizCode(), senderId, "意图识别中...");
  61. FunctionCallResponse functionCallResponse = this.getFunction(senderId, message);
  62. if (functionCallResponse == null) {
  63. SseEmitterHelper.send(this.getSSEBizCode(), senderId, "意图识别失败,请重试~");
  64. return;
  65. }
  66. String function = functionCallResponse.getFunction();
  67. AiFunctionEnum aiFunctionEnum = AiFunctionEnum.getFunction(function);
  68. String funcName = "未识别到意图";
  69. if (aiFunctionEnum == null) {
  70. // 查询平台查询意图信息
  71. BotDTO botDTO = botService.getBotPluginFromCache(function);
  72. if (botDTO == null) {
  73. SseEmitterHelper.send(this.getSSEBizCode(), senderId, "意图识别失败,请重试~");
  74. return;
  75. }
  76. funcName = botDTO.getName();
  77. } else {
  78. funcName = aiFunctionEnum.getFunctionName();
  79. }
  80. String functionInfo = "意图识别:"+ funcName;
  81. SseEmitterHelper.send(this.getSSEBizCode(), senderId, functionInfo);
  82. String aiInput = functionCallResponse.getPrompt();
  83. /****************************** 意图处理 **********************************/
  84. if (AiFunctionEnum.IMAGE.name().equals(function)) {
  85. /**
  86. * 通用文生图
  87. */
  88. aiGenImageService.doGenerate(aiInput, this.getSSEBizCode(), senderId);
  89. } else if (AiFunctionEnum.CHAT.name().equals(function)) {
  90. /**
  91. * 通用对话
  92. */
  93. aiBotQAService.chat(sseEmitter, chatMessage);
  94. } else {
  95. /**
  96. * 插件类
  97. */
  98. aiBotPluginService.doChat(chatMessage, bizCode, function);
  99. }
  100. }
  101. private FunctionCallResponse getFunction(String senderId, String message) {
  102. FunctionCallResponse functionCallResponse = null;
  103. try {
  104. List<String> pluginList = new ArrayList<>();
  105. List<BotDTO> botDTOList = botService.getPublishBotDetailFromCache();
  106. if (CollectionUtils.isNotEmpty(botDTOList)) {
  107. pluginList = botDTOList.stream().map(bot ->
  108. bot.getCode().concat(":").concat(bot.getDescription()))
  109. .collect(Collectors.toList());
  110. } else {
  111. pluginList.add("");
  112. }
  113. Map<String, Object> vars = new HashMap<>();
  114. vars.put("input", message);
  115. vars.put("pluginFuncList", pluginList);
  116. functionCallResponse =
  117. aiFunctionCallService.getFunction(vars, PromptTemplateEnum.AIBOT_FC);
  118. log.info("意图识别结果 =====> input: {}, response:{}",
  119. message, JsonUtil.toJsonString(functionCallResponse));
  120. } catch (Exception e) {
  121. log.error("意图识别异常 =====> input: {}",message, e);
  122. SseEmitterHelper.send(this.getSSEBizCode(), senderId, "意图识别失败,稍后重试~");
  123. }
  124. return functionCallResponse;
  125. }
  126. }