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 java.io.File;
19import java.util.ArrayList;
20import java.util.List;
21import java.util.Map;
22import lombok.AllArgsConstructor;
23import lombok.Builder;
24import lombok.Data;
25import lombok.NoArgsConstructor;
26
27public class Tools {
28 private Tools() {}
29
30 @Data
31 @Builder
32 @NoArgsConstructor
33 @AllArgsConstructor
34 public static class Tool {
35 @JsonProperty("function")
36 private ToolSpec toolSpec;
37
38 @Builder.Default private String type = "function";
39 @JsonIgnore private ToolFunction toolFunction;
40 }
41
42 @Data
43 @Builder
44 @NoArgsConstructor
45 @AllArgsConstructor
46 public static class ToolSpec {
47 private String name;
48 private String description;
49 private Parameters parameters;
50 }
51
52 @Data
53 @NoArgsConstructor
54 @AllArgsConstructor
55 public static class Parameters {
56 private Map<String, Property> properties;
57 private List<String> required = new ArrayList<>();
58
59 public static Parameters of(Map<String, Property> properties) {
60 Parameters params = new Parameters();
61 params.setProperties(properties);
62 // Optionally, populate required from properties' required flags
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());
67 }
68 }
69 }
70 return params;
71 }
72
73 @Override
74 public String toString() {
75 ObjectNode node =
76 com.fasterxml.jackson.databind.json.JsonMapper.builder()
77 .build()
78 .createObjectNode();
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")
89 .addAll(
90 prop.getEnumValues().stream()
91 .map(
92 com.fasterxml.jackson.databind.node.TextNode
93 ::new)
94 .collect(java.util.stream.Collectors.toList()));
95 }
96 }
97 }
98 if (required != null && !required.isEmpty()) {
99 node.putArray("required")
100 .addAll(
101 required.stream()
102 .map(com.fasterxml.jackson.databind.node.TextNode::new)
103 .collect(java.util.stream.Collectors.toList()));
104 }
105 return node.toPrettyString();
106 }
107 }
108
109 @Data
110 @Builder
111 @NoArgsConstructor
112 @AllArgsConstructor
113 public static class Property {
114 private String type;
115 private String description;
116
117 @JsonProperty("enum")
118 @JsonInclude(JsonInclude.Include.NON_NULL)
119 private List<String> enumValues;
120
121 @JsonIgnore private boolean required;
122 }
123
124 public static List<Tool> fromJSONFile(String filePath, Map<String, ToolFunction> functionMap) {
125 try {
126 ObjectMapper mapper = new ObjectMapper();
127 List<Map<String, Object>> rawTools =
128 mapper.readValue(
129 new File(filePath),
130 new com.fasterxml.jackson.core.type.TypeReference<>() {});
131
132 List<Tool> tools = new ArrayList<>();
133
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());
141 }
142 }
143 tools.add(tool);
144 }
145 return tools;
146 } catch (Exception e) {
147 throw new RuntimeException("Failed to load tools from file: " + filePath, e);
148 }
149 }
150
151 public static List<Tool> fromYAMLFile(String filePath, Map<String, ToolFunction> functionMap) {
152 try {
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();
161 ToolFunction function = functionMap.get(toolName);
162 if (function != null) {
163 tool.setToolFunction(function);
164 }
165 tools.add(tool);
166 }
167 return tools;
168 } catch (Exception e) {
169 throw new RuntimeException("Failed to load tools from YAML file: " + filePath, e);
170 }
171 }
172}
static List< Tool > fromJSONFile(String filePath, Map< String, ToolFunction > functionMap)
Definition Tools.java:124
static List< Tool > fromYAMLFile(String filePath, Map< String, ToolFunction > functionMap)
Definition Tools.java:151