admin
2025-05-29 ef4c38330371547b66bc0c5b7ebc02d13c81cb2c
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:aiBot自带大模型信息
            try {
                // 从aiBot构建自定义LLM实现
                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);
            // 发送HTTP请求
            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();
                // 添加插件相关的function 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("自定义LLM配置错误");
            }
            // 发送SSE响应
            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;