From 6ca3d7c61ee8d763f00847d90f4365e348f43078 Mon Sep 17 00:00:00 2001
From: admin <cgf12138@163.com>
Date: 星期四, 29 五月 2025 13:35:16 +0800
Subject: [PATCH] 0529

---
 aiflowy-modules/aiflowy-module-ai/src/main/java/tech/aiflowy/ai/controller/AiBotController.java |  213 +++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 149 insertions(+), 64 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 d7139a4..60af52c 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
@@ -18,6 +18,9 @@
 import com.alibaba.fastjson.serializer.SerializeConfig;
 import com.jfinal.template.stat.ast.Break;
 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;
@@ -34,6 +37,7 @@
 import tech.aiflowy.common.ai.ChatManager;
 import tech.aiflowy.common.ai.MySseEmitter;
 import tech.aiflowy.common.domain.Result;
+import tech.aiflowy.common.entity.LoginAccount;
 import tech.aiflowy.common.satoken.util.SaTokenUtil;
 import tech.aiflowy.common.util.StringUtil;
 import tech.aiflowy.common.web.controller.BaseCurdController;
@@ -139,92 +143,173 @@
         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鑾峰彇涓虹┖");
+                }
 
-        Map<String, Object> llmOptions = aiBot.getLlmOptions();
-        String systemPrompt = llmOptions != null ? (String) llmOptions.get("systemPrompt") : null;
-        AiLlm aiLlm = aiLlmService.getById(aiBot.getLlmId());
+                AiBotMessageMemory memory = new AiBotMessageMemory(botId, SaTokenUtil.getLoginAccount().getId(),
+                        sessionId, isExternalMsg, aiBotMessageService, aiBotConversationMessageMapper,
+                        aiBotConversationMessageService);
 
-        if (aiLlm == null) {
-            return ChatManager.getInstance().sseEmitterForContent("LLM涓嶅瓨鍦�");
-        }
+                final HistoriesPrompt historiesPrompt = new HistoriesPrompt();
 
-        Llm llm = aiLlm.toLlm();
+                historiesPrompt.setMemory(memory);
 
-        if (llm == null) {
-            return ChatManager.getInstance().sseEmitterForContent("LLM鑾峰彇涓虹┖");
-        }
+                HumanMessage humanMessage = new HumanMessage(prompt);
 
-        AiBotMessageMemory memory = new AiBotMessageMemory(botId, SaTokenUtil.getLoginAccount().getId(),
-                sessionId, isExternalMsg, aiBotMessageService, aiBotConversationMessageMapper,
-                aiBotConversationMessageService);
+                // 娣诲姞鎻掍欢鐩稿叧鐨刦unction calling
+                appendPluginToolFunction(botId, humanMessage);
 
-        final HistoriesPrompt historiesPrompt = new HistoriesPrompt();
-        if (systemPrompt != null) {
-            historiesPrompt.setSystemMessage(SystemMessage.of(systemPrompt));
-        }
+                //娣诲姞宸ヤ綔娴佺浉鍏崇殑 Function Calling
+                appendWorkflowFunctions(botId, humanMessage);
 
-        historiesPrompt.setMemory(memory);
+                //娣诲姞鐭ヨ瘑搴撶浉鍏崇殑 Function Calling
+                appendKnowledgeFunctions(botId, humanMessage);
 
-        HumanMessage humanMessage = new HumanMessage(prompt);
+                historiesPrompt.addMessage(humanMessage);
 
-        // 娣诲姞鎻掍欢鐩稿叧鐨刦unction calling
-        appendPluginToolFunction(botId, humanMessage);
+                MySseEmitter emitter = new MySseEmitter((long) (1000 * 60 * 2));
 
-        //娣诲姞宸ヤ綔娴佺浉鍏崇殑 Function Calling
-        appendWorkflowFunctions(botId, humanMessage);
+                final Boolean[] needClose = {true};
 
-        //娣诲姞鐭ヨ瘑搴撶浉鍏崇殑 Function Calling
-        appendKnowledgeFunctions(botId, humanMessage);
-
-        historiesPrompt.addMessage(humanMessage);
-
-        MySseEmitter emitter = new MySseEmitter((long) (1000 * 60 * 2));
-
-        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()));
+                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();
+                        }
+                    }
 
-                } catch (Exception e) {
-                    emitter.completeWithError(e);
+                    @Override
+                    public void onFailure(ChatContext context, Throwable throwable) {
+                        emitter.completeWithError(throwable);
+                    }
+                });
+
+                return emitter;
+            } catch (Exception e) {
+                return ChatManager.getInstance().sseEmitterForContent("鑷畾涔塋LM閰嶇疆閿欒");
+            }
+        }else{
+            Map<String, Object> llmOptions = aiBot.getLlmOptions();
+            String systemPrompt = llmOptions != null ? (String) llmOptions.get("systemPrompt") : null;
+            AiLlm aiLlm = aiLlmService.getById(aiBot.getLlmId());
+
+            if (aiLlm == null) {
+                return ChatManager.getInstance().sseEmitterForContent("LLM涓嶅瓨鍦�");
+            }
+
+            Llm llm = aiLlm.toLlm();
+            if (llm == null) {
+                return ChatManager.getInstance().sseEmitterForContent("LLM鑾峰彇涓虹┖");
+            }
+
+            AiBotMessageMemory memory = new AiBotMessageMemory(botId, SaTokenUtil.getLoginAccount().getId(),
+                    sessionId, isExternalMsg, aiBotMessageService, aiBotConversationMessageMapper,
+                    aiBotConversationMessageService);
+
+            final HistoriesPrompt historiesPrompt = new HistoriesPrompt();
+            if (systemPrompt != null) {
+                historiesPrompt.setSystemMessage(SystemMessage.of(systemPrompt));
+            }
+
+            historiesPrompt.setMemory(memory);
+
+            HumanMessage humanMessage = new HumanMessage(prompt);
+
+            // 娣诲姞鎻掍欢鐩稿叧鐨刦unction calling
+            appendPluginToolFunction(botId, humanMessage);
+
+            //娣诲姞宸ヤ綔娴佺浉鍏崇殑 Function Calling
+            appendWorkflowFunctions(botId, humanMessage);
+
+            //娣诲姞鐭ヨ瘑搴撶浉鍏崇殑 Function Calling
+            appendKnowledgeFunctions(botId, humanMessage);
+
+            historiesPrompt.addMessage(humanMessage);
+
+            MySseEmitter emitter = new MySseEmitter((long) (1000 * 60 * 2));
+
+            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 onStop(ChatContext context) {
+                    if (needClose[0]) {
+                        emitter.complete();
+                    }
                 }
-            }
 
-            @Override
-            public void onFailure(ChatContext context, Throwable throwable) {
-                emitter.completeWithError(throwable);
-            }
-        });
+                @Override
+                public void onFailure(ChatContext context, Throwable throwable) {
+                    emitter.completeWithError(throwable);
+                }
+            });
 
-        return emitter;
+            return emitter;
+        }
+
     }
 
     /**

--
Gitblit v1.8.0