From 9b32e3b3b226723cd43bfa37026e1e4487f6ef69 Mon Sep 17 00:00:00 2001
From: ageer <ageerle@163.com>
Date: 星期六, 12 四月 2025 10:08:51 +0800
Subject: [PATCH] feat: mcp支持

---
 ruoyi-modules/ruoyi-chat/src/main/java/org/ruoyi/chat/service/chat/impl/OllamaServiceImpl.java |   64 +++++++++++++++++++++++++++++++-
 1 files changed, 62 insertions(+), 2 deletions(-)

diff --git a/ruoyi-modules/ruoyi-chat/src/main/java/org/ruoyi/chat/service/chat/impl/OllamaServiceImpl.java b/ruoyi-modules/ruoyi-chat/src/main/java/org/ruoyi/chat/service/chat/impl/OllamaServiceImpl.java
index 7537e0b..8b337b2 100644
--- a/ruoyi-modules/ruoyi-chat/src/main/java/org/ruoyi/chat/service/chat/impl/OllamaServiceImpl.java
+++ b/ruoyi-modules/ruoyi-chat/src/main/java/org/ruoyi/chat/service/chat/impl/OllamaServiceImpl.java
@@ -6,8 +6,6 @@
 import io.github.ollama4j.models.chat.OllamaChatRequestBuilder;
 import io.github.ollama4j.models.chat.OllamaChatRequestModel;
 import io.github.ollama4j.models.generate.OllamaStreamHandler;
-import jakarta.servlet.http.HttpServletRequest;
-import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.ruoyi.chat.service.chat.IChatService;
 import org.ruoyi.chat.util.SSEUtil;
@@ -15,7 +13,16 @@
 import org.ruoyi.common.chat.request.ChatRequest;
 import org.ruoyi.domain.vo.ChatModelVo;
 import org.ruoyi.service.IChatModelService;
+import org.springframework.ai.chat.client.ChatClient;
+import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
+import org.springframework.ai.chat.memory.ChatMemory;
+import org.springframework.ai.chat.memory.InMemoryChatMemory;
+import org.springframework.ai.chat.messages.UserMessage;
+import org.springframework.ai.ollama.api.OllamaModel;
+import org.springframework.ai.ollama.api.OllamaOptions;
+import org.springframework.ai.tool.ToolCallbackProvider;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.MediaType;
 import org.springframework.stereotype.Service;
 
 import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
@@ -32,6 +39,13 @@
 
     @Autowired
     private  IChatModelService chatModelService;
+    @Autowired
+    private ChatClient chatClient;
+    @Autowired
+    private ToolCallbackProvider tools;
+
+    private final ChatMemory chatMemory = new InMemoryChatMemory();
+
 
     @Override
     public SseEmitter chat(ChatRequest chatRequest,SseEmitter emitter) {
@@ -77,4 +91,50 @@
 
         return emitter;
     }
+
+    @Override
+    public SseEmitter mcpChat(ChatRequest chatRequest, SseEmitter emitter) {
+        List<Message> msgList = chatRequest.getMessages();
+        // 娣诲姞璁板繂
+        for (int i = 0; i < msgList.size(); i++) {
+            org.springframework.ai.chat.messages.Message springAiMessage = new UserMessage(msgList.get(i).getContent().toString());
+            chatMemory.add(String.valueOf(i),springAiMessage);
+        }
+        var messageChatMemoryAdvisor = new MessageChatMemoryAdvisor(chatMemory, chatRequest.getUserId(), 10);
+
+        this.chatClient.prompt(chatRequest.getPrompt())
+                .advisors(messageChatMemoryAdvisor)
+                .tools(tools)
+                .options(OllamaOptions.builder()
+                        .model(OllamaModel.QWEN_2_5_7B)
+                        .temperature(0.4)
+                        .build())
+                .stream()
+                .chatResponse()
+                .subscribe(
+                        chatResponse -> {
+                            try {
+                                emitter.send(chatResponse, MediaType.APPLICATION_JSON);
+                            } catch (IOException e) {
+                                e.printStackTrace();
+                            }
+                        },
+                        error -> {
+                            try {
+                                emitter.completeWithError(error);
+                            } catch (Exception e) {
+                                e.printStackTrace();
+                            }
+                        },
+                        () -> {
+                            try {
+                                emitter.complete();
+                            } catch (Exception e) {
+                                e.printStackTrace();
+                            }
+                        }
+                );
+
+        return emitter;
+    }
 }

--
Gitblit v1.9.3