34 public static class Tool {
35 @JsonProperty(
"function")
36 private ToolSpec toolSpec;
38 @Builder.Default
private String type =
"function";
46 public static class ToolSpec {
48 private String description;
49 private Parameters parameters;
55 public static class Parameters {
56 private Map<String, Property> properties;
57 private List<String> required =
new ArrayList<>();
59 public static Parameters of(Map<String, Property> properties) {
60 Parameters params =
new Parameters();
61 params.setProperties(properties);
63 if (properties !=
null) {
64 for (Map.Entry<String, Property> entry : properties.entrySet()) {
65 if (entry.getValue() !=
null && entry.getValue().isRequired()) {
66 params.getRequired().add(entry.getKey());
74 public String toString() {
76 com.fasterxml.jackson.databind.json.JsonMapper.builder()
79 node.put(
"type",
"object");
80 if (properties !=
null) {
81 ObjectNode propsNode = node.putObject(
"properties");
82 for (Map.Entry<String, Property> entry : properties.entrySet()) {
83 ObjectNode propNode = propsNode.putObject(entry.getKey());
84 Property prop = entry.getValue();
85 propNode.put(
"type", prop.getType());
86 propNode.put(
"description", prop.getDescription());
87 if (prop.getEnumValues() !=
null) {
88 propNode.putArray(
"enum")
90 prop.getEnumValues().stream()
92 com.fasterxml.jackson.databind.node.TextNode
94 .collect(java.util.stream.Collectors.toList()));
98 if (required !=
null && !required.isEmpty()) {
99 node.putArray(
"required")
102 .map(com.fasterxml.jackson.databind.node.TextNode::new)
103 .collect(java.util.stream.Collectors.toList()));
105 return node.toPrettyString();
113 public static class Property {
115 private String description;
117 @JsonProperty(
"enum")
118 @JsonInclude(JsonInclude.Include.NON_NULL)
119 private List<String> enumValues;
121 @JsonIgnore
private boolean required;
124 public static List<Tool>
fromJSONFile(String filePath, Map<String, ToolFunction> functionMap) {
126 ObjectMapper mapper =
new ObjectMapper();
127 List<Map<String, Object>> rawTools =
130 new com.fasterxml.jackson.core.type.TypeReference<>() {});
132 List<Tool> tools =
new ArrayList<>();
134 for (Map<String, Object> rawTool : rawTools) {
135 String json = mapper.writeValueAsString(rawTool);
136 Tool tool = mapper.readValue(json, Tool.class);
137 String toolName = tool.getToolSpec().getName();
138 for (Map.Entry<String,
ToolFunction> toolFunctionEntry : functionMap.entrySet()) {
139 if (toolFunctionEntry.getKey().equals(toolName)) {
140 tool.setToolFunction(toolFunctionEntry.getValue());
146 }
catch (Exception e) {
147 throw new RuntimeException(
"Failed to load tools from file: " + filePath, e);
151 public static List<Tool>
fromYAMLFile(String filePath, Map<String, ToolFunction> functionMap) {
153 ObjectMapper mapper =
new ObjectMapper(
new YAMLFactory());
154 List<Map<String, Object>> rawTools =
155 mapper.readValue(
new File(filePath),
new TypeReference<>() {});
156 List<Tool> tools =
new ArrayList<>();
157 for (Map<String, Object> rawTool : rawTools) {
158 String yaml = mapper.writeValueAsString(rawTool);
159 Tool tool = mapper.readValue(yaml, Tool.class);
160 String toolName = tool.getToolSpec().getName();
162 if (
function !=
null) {
163 tool.setToolFunction(
function);
168 }
catch (Exception e) {
169 throw new RuntimeException(
"Failed to load tools from YAML file: " + filePath, e);