Skip to content

Commit

Permalink
add cluster tools
Browse files Browse the repository at this point in the history
  • Loading branch information
lhpqaq committed Dec 20, 2024
1 parent de461bb commit 8cb1d36
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -131,7 +134,7 @@ private AIAssistant buildAIAssistant(
getPlatformType(platformName),
getAIAssistantConfig(model, credentials),
threadId,
new AiServiceToolsProvider(command));
aiServiceToolsProvider.getToolsProvide(command));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<ToolSpecification, ToolExecutor> 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<ToolSpecification, ToolExecutor> 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<String, Object> 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<ToolSpecification, ToolExecutor> 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<String, Object> arguments = JsonUtils.readFromString(toolExecutionRequest.arguments());
String clusterName = arguments.get("clusterName").toString();
List<ClusterVO> 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();
}
}

0 comments on commit 8cb1d36

Please sign in to comment.