From ef4c38330371547b66bc0c5b7ebc02d13c81cb2c Mon Sep 17 00:00:00 2001
From: admin <cgf12138@163.com>
Date: 星期四, 29 五月 2025 18:40:56 +0800
Subject: [PATCH] 0529

---
 aiflowy-modules/aiflowy-module-ai/src/main/java/tech/aiflowy/ai/controller/AiBotController.java |  152 ++++++++++++++++++++++++++------------------------
 1 files changed, 80 insertions(+), 72 deletions(-)

diff --git a/aiflowy-modules/aiflowy-module-ai/src/main/java/tech/aiflowy/ai/controller/AiBotController.java b/aiflowy-modules/aiflowy-module-ai/src/main/java/tech/aiflowy/ai/controller/AiBotController.java
index 60af52c..c66f69e 100644
--- a/aiflowy-modules/aiflowy-module-ai/src/main/java/tech/aiflowy/ai/controller/AiBotController.java
+++ b/aiflowy-modules/aiflowy-module-ai/src/main/java/tech/aiflowy/ai/controller/AiBotController.java
@@ -2,6 +2,8 @@
 
 import cn.dev33.satoken.annotation.SaIgnore;
 import cn.hutool.core.util.ObjectUtil;
+import cn.hutool.json.JSONArray;
+import cn.hutool.json.JSONObject;
 import com.agentsflex.core.llm.ChatContext;
 import com.agentsflex.core.llm.Llm;
 import com.agentsflex.core.llm.StreamResponseListener;
@@ -16,24 +18,31 @@
 import com.agentsflex.core.util.CollectionUtil;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.serializer.SerializeConfig;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
 import com.jfinal.template.stat.ast.Break;
+import com.mybatisflex.core.paginate.Page;
 import com.mybatisflex.core.query.QueryWrapper;
 import com.mybatisflex.core.table.TableInfo;
 import com.mybatisflex.core.table.TableInfoFactory;
 import io.milvus.param.R;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.ResponseEntity;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.*;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.client.RestTemplate;
 import org.springframework.web.context.request.RequestContextHolder;
 import org.springframework.web.context.request.ServletRequestAttributes;
 import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
 import tech.aiflowy.ai.entity.*;
 import tech.aiflowy.ai.mapper.AiBotConversationMessageMapper;
 import tech.aiflowy.ai.service.*;
+import tech.aiflowy.ai.vo.ModelConfig;
 import tech.aiflowy.common.ai.ChatManager;
 import tech.aiflowy.common.ai.MySseEmitter;
 import tech.aiflowy.common.domain.Result;
@@ -48,7 +57,9 @@
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
 import java.math.BigInteger;
+import java.time.Duration;
 import java.util.*;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.atomic.AtomicBoolean;
@@ -77,6 +88,8 @@
     private AiBotConversationMessageMapper aiBotConversationMessageMapper;
 
     private static final Logger logger = LoggerFactory.getLogger(AiBotController.class);
+    @Autowired
+    private RestTemplate restTemplate;
 
     public AiBotController(AiBotService service, AiLlmService aiLlmService, AiBotWorkflowService aiBotWorkflowService, AiBotKnowledgeService aiBotKnowledgeService, AiBotMessageService aiBotMessageService) {
         super(service);
@@ -132,6 +145,10 @@
      * @param response
      * @return
      */
+
+    @Autowired
+    private ObjectMapper objectMapper;
+
     @PostMapping("chat")
     public SseEmitter chat(@JsonBody(value = "prompt", required = true) String prompt,
                            @JsonBody(value = "botId", required = true) BigInteger botId,
@@ -143,86 +160,77 @@
         if (aiBot == null) {
             return ChatManager.getInstance().sseEmitterForContent("鏈哄櫒浜轰笉瀛樺湪");
         }
-        if (StringUtil.hasText(aiBot.getApiEndpoint())){
-            // 鎯呭喌1锛歛iBot鑷甫澶фā鍨嬩俊鎭�
-            try {
-                // 浠巃iBot鏋勫缓鑷畾涔塋LM瀹炵幇
-                Llm llm = null;
-                if (llm == null) {
-                    return ChatManager.getInstance().sseEmitterForContent("LLM鑾峰彇涓虹┖");
-                }
+        if (StringUtil.hasText(aiBot.getModelAPI())){
+            // 浣跨敤Dify妯″瀷鐨勯�昏緫
+            String apiUrl = aiBot.getModelAPI();
+            String bearerToken = aiBot.getModelKey();
 
-                AiBotMessageMemory memory = new AiBotMessageMemory(botId, SaTokenUtil.getLoginAccount().getId(),
-                        sessionId, isExternalMsg, aiBotMessageService, aiBotConversationMessageMapper,
-                        aiBotConversationMessageService);
+            // 鏋勫缓璇锋眰JSON
+            JSONObject jsonBody = new JSONObject();
+            jsonBody.put("inputs", new JSONObject());
+            jsonBody.put("query", prompt);
+            jsonBody.put("response_mode", "blocking");
+            jsonBody.put("conversation_id", "");
+            jsonBody.put("user", userId.toString());
 
-                final HistoriesPrompt historiesPrompt = new HistoriesPrompt();
+            JSONArray files = new JSONArray();
+            JSONObject fileObj = new JSONObject();
+            fileObj.put("type", "");
+            fileObj.put("transfer_method", "");
+            fileObj.put("url", "");
+            files.put(fileObj);
+            jsonBody.put("files", files);
 
-                historiesPrompt.setMemory(memory);
+            // 鍙戦�丠TTP璇锋眰
+            HttpClient client = HttpClient.newBuilder()
+                    .connectTimeout(Duration.ofSeconds(30))
+                    .build();
 
-                HumanMessage humanMessage = new HumanMessage(prompt);
+            HttpRequest request = HttpRequest.newBuilder()
+                    .uri(URI.create(apiUrl))
+                    .header("Content-Type", "application/json")
+                    .header("Authorization", "Bearer " + bearerToken)
+                    .POST(HttpRequest.BodyPublishers.ofString(jsonBody.toString()))
+                    .build();
 
-                // 娣诲姞鎻掍欢鐩稿叧鐨刦unction calling
-                appendPluginToolFunction(botId, humanMessage);
+            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
 
-                //娣诲姞宸ヤ綔娴佺浉鍏崇殑 Function Calling
-                appendWorkflowFunctions(botId, humanMessage);
+            // 澶勭悊鎴愬姛鍝嶅簲
+            JSONObject responseJson = new JSONObject(response.body());
 
-                //娣诲姞鐭ヨ瘑搴撶浉鍏崇殑 Function Calling
-                appendKnowledgeFunctions(botId, humanMessage);
+            // 淇濆瓨鐢ㄦ埛娑堟伅鍒板巻鍙茶褰�
+            ChatHistory userChat = new ChatHistory();
+            userChat.setContent(prompt);
+            userChat.setRole("user");
+            userChat.setUserId(userId);
+            userChat.setBotId(botId.intValue());
+            userChat.setModel("dify");
+            userChat.setChatId(responseJson.optString("conversation_id", ""));
+            Integer userHistoryId = chatHistoryService.saveChatHistory(userChat);
 
-                historiesPrompt.addMessage(humanMessage);
+            // 淇濆瓨AI鍥炲鍒板巻鍙茶褰�
+            ChatHistory assistantChat = new ChatHistory();
+            assistantChat.setContent(responseJson.getString("answer"));
+            assistantChat.setRole("assistant");
+            assistantChat.setUserId(userId);
+            assistantChat.setBotId(botId.intValue());
+            assistantChat.setModel("dify");
+            assistantChat.setChatId(userChat.getChatId());
+            Integer assistantHistoryId = chatHistoryService.saveChatHistory(assistantChat);
 
-                MySseEmitter emitter = new MySseEmitter((long) (1000 * 60 * 2));
+            // 鏋勫缓鍝嶅簲鏁版嵁
+            int count = chatHistoryService.getChatHistoryCount(userId, botId.intValue());
+            JSONObject result = new JSONObject();
+            result.put("Count", count);
+            result.put("Data", responseJson.getString("answer"));
+            result.put("UserId", userHistoryId);
+            result.put("AssistantId", assistantHistoryId);
 
-                final Boolean[] needClose = {true};
-
-                ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
-                // 缁熶竴浣跨敤娴佸紡澶勭悊锛屾棤璁烘槸鍚︽湁 Function Calling
-                llm.chatStream(historiesPrompt, new StreamResponseListener() {
-                    @Override
-                    public void onMessage(ChatContext context, AiMessageResponse response) {
-                        try {
-                            RequestContextHolder.setRequestAttributes(sra, true);
-                            if (response != null) {
-                                // 妫�鏌ユ槸鍚﹂渶瑕佽Е鍙� Function Calling
-                                if (response.getFunctionCallers() != null && CollectionUtil.hasItems(response.getFunctionCallers())) {
-                                    needClose[0] = false;
-                                    function_call(response, emitter, needClose, historiesPrompt, llm, prompt, false);
-                                } else {
-                                    // 寮哄埗娴佸紡杩斿洖锛屽嵆浣挎湁 Function Calling 涔熷厛杩斿洖閮ㄥ垎缁撴灉
-                                    if (response.getMessage() != null) {
-                                        String content = response.getMessage().getContent();
-                                        if (StringUtil.hasText(content)) {
-                                            emitter.send(JSON.toJSONString(response.getMessage()));
-                                        }
-                                    }
-                                }
-                            }
-
-
-                        } catch (Exception e) {
-                            emitter.completeWithError(e);
-                        }
-                    }
-
-                    @Override
-                    public void onStop(ChatContext context) {
-                        if (needClose[0]) {
-                            emitter.complete();
-                        }
-                    }
-
-                    @Override
-                    public void onFailure(ChatContext context, Throwable throwable) {
-                        emitter.completeWithError(throwable);
-                    }
-                });
-
-                return emitter;
-            } catch (Exception e) {
-                return ChatManager.getInstance().sseEmitterForContent("鑷畾涔塋LM閰嶇疆閿欒");
-            }
+            // 鍙戦�丼SE鍝嶅簲
+            emitter.send(SseEmitter.event()
+                    .name("message")
+                    .data(result.toString()));
+            emitter.complete();
         }else{
             Map<String, Object> llmOptions = aiBot.getLlmOptions();
             String systemPrompt = llmOptions != null ? (String) llmOptions.get("systemPrompt") : null;

--
Gitblit v1.8.0