From 188dc1e55e3abbfb00397d67e2b4eed52cead356 Mon Sep 17 00:00:00 2001 From: ageerle <ageerle@163.com> Date: 星期一, 14 四月 2025 17:23:33 +0800 Subject: [PATCH] feat: mcp 测试版本 --- ruoyi-modules/ruoyi-chat/src/main/java/org/ruoyi/chat/service/chat/impl/SseServiceImpl.java | 62 +++++++++++++++++++++++-------- 1 files changed, 46 insertions(+), 16 deletions(-) diff --git a/ruoyi-modules/ruoyi-chat/src/main/java/org/ruoyi/chat/service/chat/impl/SseServiceImpl.java b/ruoyi-modules/ruoyi-chat/src/main/java/org/ruoyi/chat/service/chat/impl/SseServiceImpl.java index c06fa44..4f5dcc5 100644 --- a/ruoyi-modules/ruoyi-chat/src/main/java/org/ruoyi/chat/service/chat/impl/SseServiceImpl.java +++ b/ruoyi-modules/ruoyi-chat/src/main/java/org/ruoyi/chat/service/chat/impl/SseServiceImpl.java @@ -11,11 +11,13 @@ import lombok.extern.slf4j.Slf4j; import okhttp3.*; +import org.ruoyi.chat.config.ChatConfig; +import org.ruoyi.chat.listener.SSEEventSourceListener; import org.ruoyi.chat.service.chat.IChatCostService; import org.ruoyi.chat.service.chat.IChatService; import org.ruoyi.chat.service.chat.ISseService; import org.ruoyi.chat.util.IpUtil; -import org.ruoyi.chat.util.SSEUtil; +import org.ruoyi.common.chat.config.LocalCache; import org.ruoyi.common.chat.request.ChatRequest; import org.ruoyi.common.chat.entity.Tts.TextToSpeech; import org.ruoyi.common.chat.entity.chat.ChatCompletion; @@ -33,7 +35,9 @@ import org.ruoyi.common.redis.utils.RedisUtils; +import org.ruoyi.domain.vo.ChatModelVo; import org.ruoyi.service.EmbeddingService; +import org.ruoyi.service.IChatModelService; import org.ruoyi.service.VectorStoreService; import org.springframework.core.io.InputStreamResource; import org.springframework.core.io.Resource; @@ -74,27 +78,35 @@ private final IChatService chatService; + private final IChatModelService chatModelService; private static final String requestIdTemplate = "company-%d"; private static final ObjectMapper mapper = new ObjectMapper(); + private final ChatConfig chatConfig; + @Override public SseEmitter sseChat(ChatRequest chatRequest, HttpServletRequest request) { - SseEmitter sseEmitter = new SseEmitter(0L); + SseEmitter sseEmitter = new SseEmitter(); try { // 鏋勫缓娑堟伅鍒楄〃澧炲姞鑱旂綉銆佺煡璇嗗簱绛夊唴瀹� buildChatMessageList(chatRequest); + if (!StpUtil.isLogin()) { + // 鏈櫥褰曠敤鎴烽檺鍒跺璇濇鏁� + checkUnauthenticatedUserChatLimit(request); + }else { + LocalCache.CACHE.put("userId", chatCostService.getUserId()); + + chatRequest.setUserId(chatCostService.getUserId()); + // 淇濆瓨娑堟伅璁板綍 骞舵墸闄よ垂鐢� + // chatCostService.deductToken(chatRequest); + } // 鏍规嵁妯″瀷鍚嶇О鍓嶇紑璋冪敤涓嶅悓鐨勫鐞嗛�昏緫 switchModelAndHandle(chatRequest,sseEmitter); - // 鏈櫥褰曠敤鎴烽檺鍒跺璇濇鏁� - checkUnauthenticatedUserChatLimit(request); - // 淇濆瓨娑堟伅璁板綍 骞舵墸闄よ垂鐢� - chatCostService.deductToken(chatRequest); } catch (Exception e) { - String message = e.getMessage(); - SSEUtil.sendErrorEvent(sseEmitter, message); - return sseEmitter; + log.error(e.getMessage(),e); + sseEmitter.completeWithError(e); } return sseEmitter; } @@ -106,8 +118,7 @@ * @throws ServiceException 濡傛灉褰撴棩鍏嶈垂娆℃暟宸茬敤瀹� */ public void checkUnauthenticatedUserChatLimit(HttpServletRequest request) throws ServiceException { - // 鏈櫥褰曠敤鎴烽檺鍒跺璇濇鏁� - if (!StpUtil.isLogin()) { + String clientIp = IpUtil.getClientIp(request); // 璁垮姣忓ぉ榛樿鍙兘瀵硅瘽5娆� int timeWindowInSeconds = 5; @@ -125,13 +136,14 @@ count++; RedisUtils.setCacheObject(redisKey, count); } - } + } /** * 鏍规嵁妯″瀷鍚嶇О鍓嶇紑璋冪敤涓嶅悓鐨勫鐞嗛�昏緫 */ private void switchModelAndHandle(ChatRequest chatRequest,SseEmitter emitter) { + SSEEventSourceListener openAIEventSourceListener = new SSEEventSourceListener(emitter); String model = chatRequest.getModel(); // 濡傛灉妯″瀷鍚嶇О浠llama寮�澶达紝鍒欒皟鐢╫llama涓儴缃茬殑鏈湴妯″瀷 if (model.startsWith("ollama-")) { @@ -142,8 +154,24 @@ } else { throw new IllegalArgumentException("Invalid ollama model name: " + chatRequest.getModel()); } - } else if (model.startsWith("gpt-4-gizmo")) { - chatRequest.setModel("gpt-4-gizmo"); + } else { + + if (model.startsWith("gpt-4-gizmo")) { + chatRequest.setModel("gpt-4-gizmo"); + } + ChatModelVo chatModelVo = chatModelService.selectModelByName(chatRequest.getModel()); + //openAiStreamClient = chatConfig.createOpenAiStreamClient(chatModelVo.getApiHost(), chatModelVo.getApiKey()); + + ChatCompletion completion = ChatCompletion + .builder() + .messages(chatRequest.getMessages()) + .model(chatRequest.getModel()) + .temperature(0.2) + .topP(1.0) + .stream(true) + .build(); + openAiStreamClient.streamChatCompletion(completion, openAIEventSourceListener); + } } @@ -151,9 +179,10 @@ * 鏋勫缓娑堟伅鍒楄〃 */ private void buildChatMessageList(ChatRequest chatRequest){ + ChatModelVo chatModelVo = chatModelService.selectModelByName(chatRequest.getModel()); // 鑾峰彇瀵硅瘽娑堟伅鍒楄〃 List<Message> messages = chatRequest.getMessages(); - String sysPrompt = chatRequest.getSysPrompt(); + String sysPrompt = chatModelVo.getSystemPrompt(); if(StringUtils.isEmpty(sysPrompt)){ sysPrompt ="浣犳槸涓�涓敱RuoYI-AI寮�鍙戠殑浜哄伐鏅鸿兘鍔╂墜锛屽悕瀛楀彨鐔婄尗鍔╂墜銆備綘鎿呴暱涓嫳鏂囧璇濓紝鑳藉鐞嗚В骞跺鐞嗗悇绉嶉棶棰橈紝鎻愪緵瀹夊叏銆佹湁甯姪銆佸噯纭殑鍥炵瓟銆�" + "褰撳墠鏃堕棿锛�"+ DateUtils.getDate(); @@ -162,8 +191,9 @@ Message sysMessage = Message.builder().content(sysPrompt).role(Message.Role.SYSTEM).build(); messages.add(0,sysMessage); + chatRequest.setSysPrompt(sysPrompt); // 鏌ヨ鍚戦噺搴撶浉鍏充俊鎭姞鍏ュ埌涓婁笅鏂� - if(chatRequest.getKid()!=null){ + if(StringUtils.isNotEmpty(chatRequest.getKid())){ List<Message> knMessages = new ArrayList<>(); String content = messages.get(messages.size() - 1).getContent().toString(); List<String> nearestList; -- Gitblit v1.9.3