|
@@ -6,8 +6,6 @@ import io.github.ollama4j.models.chat.OllamaChatMessageRole;
|
|
import io.github.ollama4j.models.chat.OllamaChatRequestBuilder;
|
|
import io.github.ollama4j.models.chat.OllamaChatRequestBuilder;
|
|
import io.github.ollama4j.models.chat.OllamaChatRequestModel;
|
|
import io.github.ollama4j.models.chat.OllamaChatRequestModel;
|
|
import io.github.ollama4j.models.generate.OllamaStreamHandler;
|
|
import io.github.ollama4j.models.generate.OllamaStreamHandler;
|
|
-import jakarta.servlet.http.HttpServletRequest;
|
|
|
|
-import lombok.RequiredArgsConstructor;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.ruoyi.chat.service.chat.IChatService;
|
|
import org.ruoyi.chat.service.chat.IChatService;
|
|
import org.ruoyi.chat.util.SSEUtil;
|
|
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.common.chat.request.ChatRequest;
|
|
import org.ruoyi.domain.vo.ChatModelVo;
|
|
import org.ruoyi.domain.vo.ChatModelVo;
|
|
import org.ruoyi.service.IChatModelService;
|
|
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.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.http.MediaType;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
@@ -32,6 +39,13 @@ public class OllamaServiceImpl implements IChatService {
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
private IChatModelService chatModelService;
|
|
private IChatModelService chatModelService;
|
|
|
|
+ @Autowired
|
|
|
|
+ private ChatClient chatClient;
|
|
|
|
+ @Autowired
|
|
|
|
+ private ToolCallbackProvider tools;
|
|
|
|
+
|
|
|
|
+ private final ChatMemory chatMemory = new InMemoryChatMemory();
|
|
|
|
+
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public SseEmitter chat(ChatRequest chatRequest,SseEmitter emitter) {
|
|
public SseEmitter chat(ChatRequest chatRequest,SseEmitter emitter) {
|
|
@@ -77,4 +91,50 @@ public class OllamaServiceImpl implements IChatService {
|
|
|
|
|
|
return emitter;
|
|
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;
|
|
|
|
+ }
|
|
}
|
|
}
|