diff --git a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/service/impl/ChatbotServiceImpl.java b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/service/impl/ChatbotServiceImpl.java index a27b64a9e..c4ce5b722 100644 --- a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/service/impl/ChatbotServiceImpl.java +++ b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/service/impl/ChatbotServiceImpl.java @@ -77,6 +77,9 @@ public class ChatbotServiceImpl implements ChatbotService { @Resource private ChatMessageDao chatMessageDao; + @Resource + private AiServiceToolsProvider aiServiceToolsProvider; + private AIAssistantFactory aiAssistantFactory; private static final int CHAT_THREAD_NAME_LENGTH = 100; @@ -131,7 +134,7 @@ private AIAssistant buildAIAssistant( getPlatformType(platformName), getAIAssistantConfig(model, credentials), threadId, - new AiServiceToolsProvider(command)); + aiServiceToolsProvider.getToolsProvide(command)); } } diff --git a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/tools/AiServiceToolsProvider.java b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/tools/AiServiceToolsProvider.java index ffcabfcca..dc94c26ef 100644 --- a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/tools/AiServiceToolsProvider.java +++ b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/tools/AiServiceToolsProvider.java @@ -20,27 +20,20 @@ import org.apache.bigtop.manager.server.enums.ChatbotCommand; -import dev.langchain4j.service.tool.ToolProvider; -import dev.langchain4j.service.tool.ToolProviderRequest; -import dev.langchain4j.service.tool.ToolProviderResult; +import org.springframework.stereotype.Component; -public class AiServiceToolsProvider implements ToolProvider { +import dev.langchain4j.service.tool.ToolProvider; - ChatbotCommand chatbotCommand; +import jakarta.annotation.Resource; - public AiServiceToolsProvider(ChatbotCommand chatbotCommand) { - this.chatbotCommand = chatbotCommand; - } - - public AiServiceToolsProvider() { - this.chatbotCommand = null; - } +@Component +public class AiServiceToolsProvider { + @Resource + private ClusterInfoToolsProvider clusterInfoToolsProvider; - @Override - public ToolProviderResult provideTools(ToolProviderRequest toolProviderRequest) { + public ToolProvider getToolsProvide(ChatbotCommand chatbotCommand) { if (chatbotCommand.equals(ChatbotCommand.INFO)) { - ClusterInfoTools clusterInfoTools = new ClusterInfoTools(); - return ToolProviderResult.builder().addAll(clusterInfoTools.list()).build(); + return clusterInfoToolsProvider; } return null; } diff --git a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/tools/ClusterInfoTools.java b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/tools/ClusterInfoTools.java deleted file mode 100644 index 3f6548914..000000000 --- a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/tools/ClusterInfoTools.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.bigtop.manager.server.tools; - -import org.apache.bigtop.manager.dao.po.ClusterPO; -import org.apache.bigtop.manager.server.model.converter.ClusterConverter; - -import dev.langchain4j.agent.tool.Tool; -import dev.langchain4j.agent.tool.ToolSpecification; -import dev.langchain4j.service.tool.ToolExecutor; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -public class ClusterInfoTools { - - @Tool("Get cluster list") - public Map list() { - ToolSpecification toolSpecification = ToolSpecification.builder() - .name("list") - .description("Get cluster list") - .build(); - ToolExecutor toolExecutor = (toolExecutionRequest, memoryId) -> { - List clusterPOList = new ArrayList<>(); - ClusterPO mockClusterPO = new ClusterPO(); - mockClusterPO.setId(1L); - mockClusterPO.setName("mock-cluster"); - clusterPOList.add(mockClusterPO); - return ClusterConverter.INSTANCE.fromPO2VO(clusterPOList).toString(); - }; - - return Map.of(toolSpecification, toolExecutor); - } -} diff --git a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/tools/ClusterInfoToolsProvider.java b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/tools/ClusterInfoToolsProvider.java new file mode 100644 index 000000000..b14a3b67c --- /dev/null +++ b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/tools/ClusterInfoToolsProvider.java @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.bigtop.manager.server.tools; + +import org.apache.bigtop.manager.common.utils.JsonUtils; +import org.apache.bigtop.manager.server.model.vo.ClusterVO; +import org.apache.bigtop.manager.server.service.ClusterService; + +import org.springframework.stereotype.Component; + +import dev.langchain4j.agent.tool.JsonSchemaProperty; +import dev.langchain4j.agent.tool.Tool; +import dev.langchain4j.agent.tool.ToolSpecification; +import dev.langchain4j.service.tool.ToolExecutor; +import dev.langchain4j.service.tool.ToolProvider; +import dev.langchain4j.service.tool.ToolProviderRequest; +import dev.langchain4j.service.tool.ToolProviderResult; +import lombok.extern.slf4j.Slf4j; + +import jakarta.annotation.Resource; +import java.util.List; +import java.util.Map; + +@Component +@Slf4j +public class ClusterInfoToolsProvider implements ToolProvider { + @Resource + private ClusterService clusterService; + + @Tool("Get cluster list") + public Map list() { + ToolSpecification toolSpecification = ToolSpecification.builder() + .name("list") + .description("Get cluster list") + .build(); + ToolExecutor toolExecutor = + (toolExecutionRequest, memoryId) -> clusterService.list().toString(); + + return Map.of(toolSpecification, toolExecutor); + } + + @Tool("Get cluster information based on ID") + public Map get() { + ToolSpecification toolSpecification = ToolSpecification.builder() + .name("get") + .description("Get cluster information based on ID") + .addParameter("clusterId", JsonSchemaProperty.NUMBER, JsonSchemaProperty.description("cluster id")) + .build(); + ToolExecutor toolExecutor = (toolExecutionRequest, memoryId) -> { + Map arguments = JsonUtils.readFromString(toolExecutionRequest.arguments()); + Long clusterId = Long.valueOf(arguments.get("clusterId").toString()); + return clusterService.get(clusterId).toString(); + }; + + return Map.of(toolSpecification, toolExecutor); + } + + @Tool("Get cluster information based on cluster name") + public Map getByName() { + ToolSpecification toolSpecification = ToolSpecification.builder() + .name("getByName") + .description("Get cluster information based on cluster name") + .addParameter("clusterName", JsonSchemaProperty.STRING, JsonSchemaProperty.description("cluster name")) + .build(); + ToolExecutor toolExecutor = (toolExecutionRequest, memoryId) -> { + Map arguments = JsonUtils.readFromString(toolExecutionRequest.arguments()); + String clusterName = arguments.get("clusterName").toString(); + List clusterVOS = clusterService.list(); + for (ClusterVO clusterVO : clusterVOS) { + if (clusterVO.getName().equals(clusterName)) { + return clusterVO.toString(); + } + } + return "Cluster not found"; + }; + + return Map.of(toolSpecification, toolExecutor); + } + + @Override + public ToolProviderResult provideTools(ToolProviderRequest toolProviderRequest) { + return ToolProviderResult.builder() + .addAll(list()) + .addAll(get()) + .addAll(getByName()) + .build(); + } +}