Browse Source

增加开场白

zrd 3 tuần trước cách đây
mục cha
commit
e2418f91d2

+ 32 - 2
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/chat/AiChatMessageServiceImpl.java

@@ -4,10 +4,12 @@ import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.util.ObjUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.json.JSONObject;
+import cn.hutool.json.JSONUtil;
 import cn.iocoder.yudao.framework.ai.core.enums.AiPlatformEnum;
 import cn.iocoder.yudao.framework.ai.core.util.AiUtils;
 import cn.iocoder.yudao.framework.common.pojo.CommonResult;
 import cn.iocoder.yudao.framework.common.pojo.PageResult;
+import cn.iocoder.yudao.framework.common.util.http.HttpUtils;
 import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
 import cn.iocoder.yudao.framework.dict.core.DictFrameworkUtils;
 import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
@@ -26,6 +28,7 @@ import cn.iocoder.yudao.module.ai.dal.dataobject.model.AiToolDO;
 import cn.iocoder.yudao.module.ai.dal.mysql.chat.AiChatMessageMapper;
 import cn.iocoder.yudao.module.ai.enums.ErrorCodeConstants;
 import cn.iocoder.yudao.module.ai.service.chat.vo.DifyFlowResponse;
+import cn.iocoder.yudao.module.ai.service.chat.vo.KcbResponse;
 import cn.iocoder.yudao.module.ai.service.knowledge.AiKnowledgeDocumentService;
 import cn.iocoder.yudao.module.ai.service.knowledge.AiKnowledgeSegmentService;
 import cn.iocoder.yudao.module.ai.service.knowledge.bo.AiKnowledgeSegmentSearchReqBO;
@@ -44,6 +47,7 @@ import org.springframework.ai.chat.model.ChatResponse;
 import org.springframework.ai.chat.model.StreamingChatModel;
 import org.springframework.ai.chat.prompt.ChatOptions;
 import org.springframework.ai.chat.prompt.Prompt;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.http.MediaType;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -92,9 +96,11 @@ public class AiChatMessageServiceImpl implements AiChatMessageService {
     private AiKnowledgeDocumentService knowledgeDocumentService;
     @Resource
     private AiToolService toolService;
+    @Value("${dify.base-url}")
+    private String baseUrl;
     
     public AiChatMessageServiceImpl(WebClient.Builder webClientBuilder) {
-        this.webClient = webClientBuilder.baseUrl("http://42.194.163.46:9502").build();
+        this.webClient = webClientBuilder.baseUrl(baseUrl).build();
     }
     
     @Override
@@ -232,8 +238,11 @@ public class AiChatMessageServiceImpl implements AiChatMessageService {
                 null, sendReqVO.getContentType(), sendReqVO.getStateId());
         String apiKey = DictFrameworkUtils.parseDictDataValue("ai_key", "多轮对话");
         JSONObject requestBody = new JSONObject();
+        String kcb = "";
         if (StrUtil.isNotBlank(conversation.getDifyConversationId())) {
             requestBody.set("conversation_id", conversation.getDifyConversationId());
+        } else {
+            kcb = getKcb(apiKey);
         }
         String type = "";
         if (userId == 1L) {
@@ -290,8 +299,14 @@ public class AiChatMessageServiceImpl implements AiChatMessageService {
         StringBuffer contentBuffer = new StringBuffer();
         StringBuffer difyId = new StringBuffer();
         AiChatMessageDO assistantMessage = createChatMessage(conversation.getId(), userMessage.getId(), model,
-                userId, conversation.getRoleId(), MessageType.ASSISTANT, "", sendReqVO.getUseContext(),
+                userId, conversation.getRoleId(), MessageType.ASSISTANT, kcb, sendReqVO.getUseContext(),
                 knowledgeSegments, sendReqVO.getContentType(), sendReqVO.getStateId());
+        
+        if (StrUtil.isBlank(conversation.getDifyConversationId())) {
+            result.setReceive(BeanUtils.toBean(assistantMessage, AiChatMessageSendRespVO.Message.class));
+            return Flux.just(success(result))
+                    ;
+        }
         return flux.map(chunk -> {
             String newContent = chunk.getContent();
             contentBuffer.append(newContent);
@@ -332,6 +347,21 @@ public class AiChatMessageServiceImpl implements AiChatMessageService {
         
     }
     
+    /**
+     * 获取kcb
+     *
+     * @param apiKey api密钥
+     * @return {@link String }
+     */
+    private String getKcb(String apiKey) {
+        Map<String, String> header = new HashMap<>(4);
+        header.put("Authorization", "Bearer " + apiKey);
+        
+        header.put("Content-Type", "application/json");
+        String responseBody = HttpUtils.get(baseUrl + "v1/parameters", header);
+        KcbResponse kcbResponse = JSONUtil.toBean(responseBody, KcbResponse.class);
+        return kcbResponse.getOpening_statement();
+    }
     
     private List<AiKnowledgeSegmentSearchRespBO> recallKnowledgeSegment(String content,
             AiChatConversationDO conversation) {