Ollama4j
A Java library (wrapper/binding) for Ollama server.
Loading...
Searching...
No Matches
Tools.java
Go to the documentation of this file.
1/*
2 * Ollama4j - Java library for interacting with Ollama server.
3 * Copyright (c) 2025 Amith Koujalgi and contributors.
4 *
5 * Licensed under the MIT License (the "License");
6 * you may not use this file except in compliance with the License.
7 *
8*/
9package io.github.ollama4j.tools;
10
11import com.fasterxml.jackson.annotation.JsonIgnore;
12import com.fasterxml.jackson.annotation.JsonInclude;
13import com.fasterxml.jackson.annotation.JsonProperty;
14import com.fasterxml.jackson.core.type.TypeReference;
15import com.fasterxml.jackson.databind.ObjectMapper;
16import com.fasterxml.jackson.databind.node.ObjectNode;
17import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
18import io.modelcontextprotocol.client.transport.ServerParameters;
19import java.io.File;
20import java.util.ArrayList;
21import java.util.List;
22import java.util.Map;
23import lombok.AllArgsConstructor;
24import lombok.Builder;
25import lombok.Data;
26import lombok.NoArgsConstructor;
27
28public class Tools {
29 private Tools() {}
30
31 @Data
32 @Builder
33 @NoArgsConstructor
34 @AllArgsConstructor
35 public static class Tool {
36 @JsonProperty("function")
37 private ToolSpec toolSpec;
38
39 @Builder.Default private String type = "function";
40 @JsonIgnore private ToolFunction toolFunction;
41
42 @JsonIgnore @Builder.Default private boolean isMCPTool = false;
43 @JsonIgnore private String mcpServerName;
44 @JsonIgnore private ServerParameters mcpServerParameters;
45 }
46
47 @Data
48 @Builder
49 @NoArgsConstructor
50 @AllArgsConstructor
51 public static class ToolSpec {
52 private String name;
53 private String description;
54 private Parameters parameters;
55 }
56
57 @Data
58 @NoArgsConstructor
59 @AllArgsConstructor
60 public static class Parameters {
61 private Map<String, Property> properties;
62 private List<String> required = new ArrayList<>();
63
64 public static Parameters of(Map<String, Property> properties) {
65 Parameters params = new Parameters();
66 params.setProperties(properties);
67 // Optionally, populate required from properties' required flags
68 if (properties != null) {
69 for (Map.Entry<String, Property> entry : properties.entrySet()) {
70 if (entry.getValue() != null && entry.getValue().isRequired()) {
71 params.getRequired().add(entry.getKey());
72 }
73 }
74 }
75 return params;
76 }
77
78 @Override
79 public String toString() {
80 ObjectNode node =
81 com.fasterxml.jackson.databind.json.JsonMapper.builder()
82 .build()
83 .createObjectNode();
84 node.put("type", "object");
85 if (properties != null) {
86 ObjectNode propsNode = node.putObject("properties");
87 for (Map.Entry<String, Property> entry : properties.entrySet()) {
88 ObjectNode propNode = propsNode.putObject(entry.getKey());
89 Property prop = entry.getValue();
90 propNode.put("type", prop.getType());
91 propNode.put("description", prop.getDescription());
92 if (prop.getEnumValues() != null) {
93 propNode.putArray("enum")
94 .addAll(
95 prop.getEnumValues().stream()
96 .map(
97 com.fasterxml.jackson.databind.node.TextNode
98 ::new)
99 .collect(java.util.stream.Collectors.toList()));
100 }
101 }
102 }
103 if (required != null && !required.isEmpty()) {
104 node.putArray("required")
105 .addAll(
106 required.stream()
107 .map(com.fasterxml.jackson.databind.node.TextNode::new)
108 .collect(java.util.stream.Collectors.toList()));
109 }
110 return node.toPrettyString();
111 }
112 }
113
114 @Data
115 @Builder
116 @NoArgsConstructor
117 @AllArgsConstructor
118 public static class Property {
119 private String type;
120 private String description;
121
122 @JsonProperty("enum")
123 @JsonInclude(JsonInclude.Include.NON_NULL)
124 private List<String> enumValues;
125
126 @JsonIgnore private boolean required;
127 }
128
129 public static List<Tool> fromJSONFile(String filePath, Map<String, ToolFunction> functionMap) {
130 try {
131 ObjectMapper mapper = new ObjectMapper();
132 List<Map<String, Object>> rawTools =
133 mapper.readValue(
134 new File(filePath),
135 new com.fasterxml.jackson.core.type.TypeReference<>() {});
136
137 List<Tool> tools = new ArrayList<>();
138
139 for (Map<String, Object> rawTool : rawTools) {
140 String json = mapper.writeValueAsString(rawTool);
141 Tool tool = mapper.readValue(json, Tool.class);
142 String toolName = tool.getToolSpec().getName();
143 for (Map.Entry<String, ToolFunction> toolFunctionEntry : functionMap.entrySet()) {
144 if (toolFunctionEntry.getKey().equals(toolName)) {
145 tool.setToolFunction(toolFunctionEntry.getValue());
146 }
147 }
148 tools.add(tool);
149 }
150 return tools;
151 } catch (Exception e) {
152 throw new RuntimeException("Failed to load tools from file: " + filePath, e);
153 }
154 }
155
156 public static List<Tool> fromYAMLFile(String filePath, Map<String, ToolFunction> functionMap) {
157 try {
158 ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
159 List<Map<String, Object>> rawTools =
160 mapper.readValue(new File(filePath), new TypeReference<>() {});
161 List<Tool> tools = new ArrayList<>();
162 for (Map<String, Object> rawTool : rawTools) {
163 String yaml = mapper.writeValueAsString(rawTool);
164 Tool tool = mapper.readValue(yaml, Tool.class);
165 String toolName = tool.getToolSpec().getName();
166 ToolFunction function = functionMap.get(toolName);
167 if (function != null) {
168 tool.setToolFunction(function);
169 }
170 tools.add(tool);
171 }
172 return tools;
173 } catch (Exception e) {
174 throw new RuntimeException("Failed to load tools from YAML file: " + filePath, e);
175 }
176 }
177}
static List< Tool > fromJSONFile(String filePath, Map< String, ToolFunction > functionMap)
Definition Tools.java:129
static List< Tool > fromYAMLFile(String filePath, Map< String, ToolFunction > functionMap)
Definition Tools.java:156