Skip to content

Commit

Permalink
add stack info tools
Browse files Browse the repository at this point in the history
  • Loading branch information
lhpqaq committed Dec 20, 2024
1 parent 463611c commit a926a96
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* 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.functions;

import org.apache.bigtop.manager.common.utils.JsonUtils;
import org.apache.bigtop.manager.server.model.vo.ServiceVO;
import org.apache.bigtop.manager.server.model.vo.StackVO;
import org.apache.bigtop.manager.server.service.StackService;

import org.springframework.stereotype.Component;

import dev.langchain4j.agent.tool.JsonSchemaProperty;
import dev.langchain4j.agent.tool.ToolSpecification;
import dev.langchain4j.service.tool.ToolExecutor;
import lombok.extern.slf4j.Slf4j;

import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class StackInfoFunctions {
@Resource
private StackService stackService;

public Map<ToolSpecification, ToolExecutor> list() {
ToolSpecification toolSpecification = ToolSpecification.builder()
.name("list")
.description("Retrieve the list of services in each stack")
.build();
ToolExecutor toolExecutor = (toolExecutionRequest, memoryId) -> {
Map<String, List<String>> stackInfo = new HashMap<>();
for (StackVO stackVO : stackService.list()) {
List<String> services = new ArrayList<>();
for (ServiceVO serviceVO : stackVO.getServices()) {
services.add(serviceVO.getName());
}
stackInfo.put(stackVO.getStackName(), services);
}
return stackInfo.toString();
};
return Map.of(toolSpecification, toolExecutor);
}

public Map<ToolSpecification, ToolExecutor> getServiceByName() {
ToolSpecification toolSpecification = ToolSpecification.builder()
.name("getByName")
.description("Get service information and configs based on service name")
.addParameter(
"serviceName",
JsonSchemaProperty.STRING,
JsonSchemaProperty.description("Lowercase service name"))
.build();
ToolExecutor toolExecutor = (toolExecutionRequest, memoryId) -> {
Map<String, Object> arguments = JsonUtils.readFromString(toolExecutionRequest.arguments());
String serviceName = arguments.get("serviceName").toString();
for (StackVO stackVO : stackService.list()) {
for (ServiceVO serviceVO : stackVO.getServices()) {
if (serviceVO.getName().equals(serviceName)) {
return serviceVO.toString();
}
}
}
return "Service not found";
};

return Map.of(toolSpecification, toolExecutor);
}

public Map<ToolSpecification, ToolExecutor> getAllFunctions() {
Map<ToolSpecification, ToolExecutor> functions = new HashMap<>();
functions.putAll(list());
functions.putAll(getServiceByName());
return functions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.apache.bigtop.manager.server.tools.functions.ClusterInfoFunctions;
import org.apache.bigtop.manager.server.tools.functions.HostInfoFunctions;
import org.apache.bigtop.manager.server.tools.functions.StackInfoFunctions;

import org.springframework.stereotype.Component;

Expand All @@ -39,11 +40,15 @@ public class InfoToolsProvider implements ToolProvider {
@Resource
private HostInfoFunctions hostInfoFunctions;

@Resource
private StackInfoFunctions stackInfoFunctions;

@Override
public ToolProviderResult provideTools(ToolProviderRequest toolProviderRequest) {
return ToolProviderResult.builder()
.addAll(clusterInfoFunctions.getAllFunctions())
.addAll(hostInfoFunctions.getAllFunctions())
.addAll(stackInfoFunctions.getAllFunctions())
.build();
}
}

0 comments on commit a926a96

Please sign in to comment.