From f24ff5bbdd4f6385d23aaff5d6da646cfccf1597 Mon Sep 17 00:00:00 2001 From: ageerle <ageerle@163.com> Date: 星期二, 15 四月 2025 17:34:21 +0800 Subject: [PATCH] feat: mcp 测试版本 --- ruoyi-modules/ruoyi-chat/src/main/java/org/ruoyi/chat/service/chat/impl/SseServiceImpl.java | 126 ++++------------------------------------- 1 files changed, 13 insertions(+), 113 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 4f5dcc5..178f58f 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 @@ -2,39 +2,29 @@ import cn.dev33.satoken.stp.StpUtil; import cn.hutool.core.collection.CollectionUtil; -import com.fasterxml.jackson.databind.ObjectMapper; import com.google.protobuf.ServiceException; -import com.zhipu.oapi.ClientV4; -import com.zhipu.oapi.service.v4.tools.*; import jakarta.servlet.http.HttpServletRequest; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import okhttp3.*; - -import org.ruoyi.chat.config.ChatConfig; -import org.ruoyi.chat.listener.SSEEventSourceListener; +import okhttp3.ResponseBody; +import org.ruoyi.chat.enums.ChatModeType; 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.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; import org.ruoyi.common.chat.entity.chat.ChatCompletionResponse; - import org.ruoyi.common.chat.entity.chat.Message; import org.ruoyi.common.chat.entity.files.UploadFileResponse; import org.ruoyi.common.chat.entity.whisper.WhisperResponse; import org.ruoyi.common.chat.openai.OpenAiStreamClient; -import org.ruoyi.common.core.service.ConfigService; +import org.ruoyi.common.chat.request.ChatRequest; import org.ruoyi.common.core.utils.DateUtils; import org.ruoyi.common.core.utils.StringUtils; import org.ruoyi.common.core.utils.file.FileUtils; import org.ruoyi.common.core.utils.file.MimeTypeUtils; - import org.ruoyi.common.redis.utils.RedisUtils; - import org.ruoyi.domain.vo.ChatModelVo; import org.ruoyi.service.EmbeddingService; import org.ruoyi.service.IChatModelService; @@ -55,11 +45,7 @@ import java.nio.file.Path; import java.time.Duration; import java.util.ArrayList; -import java.util.Collections; import java.util.List; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicReference; @Service @Slf4j @@ -72,19 +58,16 @@ private final VectorStoreService vectorStore; - private final ConfigService configService; - private final IChatCostService chatCostService; - - private final IChatService chatService; private final IChatModelService chatModelService; - private static final String requestIdTemplate = "company-%d"; + private final OpenAIServiceImpl openAIService; - private static final ObjectMapper mapper = new ObjectMapper(); + private final OllamaServiceImpl ollamaService; - private final ChatConfig chatConfig; + private ChatModelVo chatModelVo; + @Override public SseEmitter sseChat(ChatRequest chatRequest, HttpServletRequest request) { @@ -100,7 +83,7 @@ chatRequest.setUserId(chatCostService.getUserId()); // 淇濆瓨娑堟伅璁板綍 骞舵墸闄よ垂鐢� - // chatCostService.deductToken(chatRequest); + chatCostService.deductToken(chatRequest); } // 鏍规嵁妯″瀷鍚嶇О鍓嶇紑璋冪敤涓嶅悓鐨勫鐞嗛�昏緫 switchModelAndHandle(chatRequest,sseEmitter); @@ -143,35 +126,11 @@ * 鏍规嵁妯″瀷鍚嶇О鍓嶇紑璋冪敤涓嶅悓鐨勫鐞嗛�昏緫 */ private void switchModelAndHandle(ChatRequest chatRequest,SseEmitter emitter) { - SSEEventSourceListener openAIEventSourceListener = new SSEEventSourceListener(emitter); - String model = chatRequest.getModel(); - // 濡傛灉妯″瀷鍚嶇О浠llama寮�澶达紝鍒欒皟鐢╫llama涓儴缃茬殑鏈湴妯″瀷 - if (model.startsWith("ollama-")) { - String[] parts = chatRequest.getModel().split("ollama-", 2); - if (parts.length > 1) { - chatRequest.setModel(parts[1]); - chatService.mcpChat(chatRequest,emitter); - } else { - throw new IllegalArgumentException("Invalid ollama model name: " + chatRequest.getModel()); - } + // 璋冪敤ollama涓儴缃茬殑鏈湴妯″瀷 + if (ChatModeType.OLLAMA.getCode().equals(chatModelVo.getCategory())) { + ollamaService.chat(chatRequest,emitter); } 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); - + openAIService.chat(chatRequest,emitter); } } @@ -179,7 +138,7 @@ * 鏋勫缓娑堟伅鍒楄〃 */ private void buildChatMessageList(ChatRequest chatRequest){ - ChatModelVo chatModelVo = chatModelService.selectModelByName(chatRequest.getModel()); + chatModelVo = chatModelService.selectModelByName(chatRequest.getModel()); // 鑾峰彇瀵硅瘽娑堟伅鍒楄〃 List<Message> messages = chatRequest.getMessages(); String sysPrompt = chatModelVo.getSystemPrompt(); @@ -220,11 +179,6 @@ } // 璁剧疆瀵硅瘽淇℃伅 chatRequest.setPrompt(chatString); - // 鍔犺浇鑱旂綉淇℃伅 - if(chatRequest.getSearch()){ - Message message = Message.builder().role(Message.Role.ASSISTANT).content("鑱旂綉淇℃伅:"+webSearch(chatString)).build(); - messages.add(message); - } } @@ -331,60 +285,6 @@ .build(); ChatCompletionResponse chatCompletionResponse = openAiStreamClient.chatCompletion(chatCompletion); return chatCompletionResponse.getChoices().get(0).getMessage().getContent().toString(); - } - - @Override - public String webSearch (String prompt) { - String zpValue = configService.getConfigValue("zhipu", "key"); - if(StringUtils.isEmpty(zpValue)){ - throw new IllegalStateException("璇峰湪chat_config涓厤缃櫤璋眐ey淇℃伅"); - }else { - ClientV4 client = new ClientV4.Builder(zpValue) - .networkConfig(300, 100, 100, 100, TimeUnit.SECONDS) - .connectionPool(new okhttp3.ConnectionPool(8, 1, TimeUnit.SECONDS)) - .build(); - - SearchChatMessage jsonNodes = new SearchChatMessage(); - jsonNodes.setRole(Message.Role.USER.getName()); - jsonNodes.setContent(prompt); - - String requestId = String.format(requestIdTemplate, System.currentTimeMillis()); - WebSearchParamsRequest chatCompletionRequest = WebSearchParamsRequest.builder() - .model("web-search-pro") - .stream(Boolean.TRUE) - .messages(Collections.singletonList(jsonNodes)) - .requestId(requestId) - .build(); - WebSearchApiResponse webSearchApiResponse = client.webSearchProStreamingInvoke(chatCompletionRequest); - List<ChoiceDelta> choices = new ArrayList<>(); - if (webSearchApiResponse.isSuccess()) { - AtomicBoolean isFirst = new AtomicBoolean(true); - - AtomicReference<WebSearchPro> lastAccumulator = new AtomicReference<>(); - - webSearchApiResponse.getFlowable().map(result -> result) - .doOnNext(accumulator -> { - { - if (isFirst.getAndSet(false)) { - log.info("Response: "); - } - ChoiceDelta delta = accumulator.getChoices().get(0).getDelta(); - if (delta != null && delta.getToolCalls() != null) { - log.info("tool_calls: {}", mapper.writeValueAsString(delta.getToolCalls())); - } - choices.add(delta); - } - }) - .doOnComplete(() -> System.out.println("Stream completed.")) - .doOnError(throwable -> System.err.println("Error: " + throwable)) - .blockingSubscribe(); - - WebSearchPro chatMessageAccumulator = lastAccumulator.get(); - webSearchApiResponse.setFlowable(null); - webSearchApiResponse.setData(chatMessageAccumulator); - } - return choices.get(1).getToolCalls().toString(); - } } } -- Gitblit v1.9.3