| | |
| | | import io.modelcontextprotocol.spec.McpSchema; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.ruoyi.chat.listener.SSEEventSourceListener; |
| | | import org.ruoyi.chat.service.chat.IChatService; |
| | | import org.ruoyi.common.chat.entity.chat.ChatChoice; |
| | | import org.ruoyi.common.chat.entity.chat.ChatCompletion; |
| | | import org.ruoyi.common.chat.entity.chat.ChatCompletionResponse; |
| | | import org.ruoyi.common.chat.entity.chat.Parameters; |
| | | import org.ruoyi.common.chat.entity.chat.*; |
| | | import org.ruoyi.common.chat.entity.chat.tool.ToolCallFunction; |
| | | import org.ruoyi.common.chat.entity.chat.tool.Tools; |
| | | import org.ruoyi.common.chat.entity.chat.tool.ToolsFunction; |
| | | import org.ruoyi.common.chat.openai.OpenAiStreamClient; |
| | | import org.ruoyi.common.chat.request.ChatRequest; |
| | | import org.ruoyi.common.core.exception.ServiceException; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.reactive.function.client.WebClient; |
| | | import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; |
| | |
| | | |
| | | private final OpenAiStreamClient openAiStreamClient; |
| | | |
| | | |
| | | @Override |
| | | public SseEmitter chat(ChatRequest chatRequest,SseEmitter emitter) { |
| | | String toolString = mcpChat(chatRequest); |
| | | |
| | | Message userMessage = Message.builder().content("工具返回信息:"+toolString).role(Message.Role.ASSISTANT).build(); |
| | | List<Message> messages = chatRequest.getMessages(); |
| | | messages.add(userMessage); |
| | | |
| | | SSEEventSourceListener openAIEventSourceListener = new SSEEventSourceListener(emitter); |
| | | ChatCompletion completion = ChatCompletion |
| | | .builder() |
| | | .messages(messages) |
| | | .model(chatRequest.getModel()) |
| | | .stream(true) |
| | | .build(); |
| | | openAiStreamClient.streamChatCompletion(completion, openAIEventSourceListener); |
| | | return emitter; |
| | | |
| | | } |
| | | |
| | | public String mcpChat(ChatRequest chatRequest) { |
| | | WebFluxSseClientTransport webFluxSseClientTransport = new WebFluxSseClientTransport(WebClient.builder().baseUrl("http://localhost:8080")); |
| | | ChatCompletion completion = ChatCompletion |
| | | .builder() |
| | |
| | | .stream(false) |
| | | .build(); |
| | | List<Tools> tools = new ArrayList<>(); |
| | | McpSchema.Content content = null; |
| | | try (var client = McpClient.sync(webFluxSseClientTransport).build()) { |
| | | client.initialize(); |
| | | McpSchema.ListToolsResult toolsList = client.listTools(); |
| | |
| | | .build(); |
| | | tools.add(tool); |
| | | } |
| | | |
| | | completion.setTools(tools); |
| | | ChatCompletionResponse chatCompletionResponse = openAiStreamClient.chatCompletion(completion); |
| | | String arguments = chatCompletionResponse.getChoices().get(0).getMessage().getToolCalls().get(0).getFunction().getArguments(); |
| | | String name = chatCompletionResponse.getChoices().get(0).getMessage().getToolCalls().get(0).getFunction().getName(); |
| | | Map<String, Object> map = JSONUtil.toBean(arguments, Map.class); |
| | | McpSchema.CallToolResult sumResult = client.callTool(new McpSchema.CallToolRequest(name, map)); |
| | | System.out.println("add a+ b = " + sumResult.content().get(0)); |
| | | |
| | | |
| | | McpSchema.Content content = sumResult.content().get(0); |
| | | |
| | | emitter.send(sumResult.content().get(0)); |
| | | |
| | | } catch (IOException e) { |
| | | emitter.completeWithError(e); |
| | | content= sumResult.content().get(0); |
| | | } catch (Exception e) { |
| | | throw new ServiceException("请求失败"+e); |
| | | } |
| | | emitter.complete(); |
| | | return emitter; |
| | | |
| | | return content.toString(); |
| | | } |
| | | |
| | | } |