mirror of
https://gitcode.com/ageerle/ruoyi-ai.git
synced 2026-04-04 07:26:10 +00:00
feat: mcp支持
This commit is contained in:
@@ -16,4 +16,11 @@ public interface IChatService {
|
||||
* @param chatRequest 请求对象
|
||||
*/
|
||||
SseEmitter chat(ChatRequest chatRequest,SseEmitter emitter);
|
||||
|
||||
|
||||
/**
|
||||
* 客户端发送消息到服务端
|
||||
* @param chatRequest 请求对象
|
||||
*/
|
||||
SseEmitter mcpChat(ChatRequest chatRequest,SseEmitter emitter);
|
||||
}
|
||||
|
||||
@@ -6,8 +6,6 @@ import io.github.ollama4j.models.chat.OllamaChatMessageRole;
|
||||
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.entity.chat.Message;
|
||||
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 @@ public class OllamaServiceImpl implements IChatService {
|
||||
|
||||
@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 @@ public class OllamaServiceImpl implements IChatService {
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,14 +5,29 @@ import org.ruoyi.chat.config.ChatConfig;
|
||||
import org.ruoyi.chat.listener.SSEEventSourceListener;
|
||||
import org.ruoyi.chat.service.chat.IChatService;
|
||||
import org.ruoyi.common.chat.entity.chat.ChatCompletion;
|
||||
import org.ruoyi.common.chat.entity.chat.Message;
|
||||
import org.ruoyi.common.chat.openai.OpenAiStreamClient;
|
||||
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.openai.OpenAiChatOptions;
|
||||
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;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author ageer
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class OpenAIServiceImpl implements IChatService {
|
||||
@@ -23,6 +38,13 @@ public class OpenAIServiceImpl implements IChatService {
|
||||
private ChatConfig chatConfig;
|
||||
@Autowired
|
||||
private OpenAiStreamClient openAiStreamClient;
|
||||
@Autowired
|
||||
private ChatClient chatClient;
|
||||
|
||||
@Autowired
|
||||
private ToolCallbackProvider tools;
|
||||
|
||||
private final ChatMemory chatMemory = new InMemoryChatMemory();
|
||||
|
||||
@Override
|
||||
public SseEmitter chat(ChatRequest chatRequest,SseEmitter emitter) {
|
||||
@@ -47,4 +69,46 @@ public class OpenAIServiceImpl implements IChatService {
|
||||
|
||||
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(OpenAiChatOptions.builder().model(chatRequest.getModel()).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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user