Ollama4j
A Java library (wrapper/binding) for Ollama server.
Loading...
Searching...
No Matches
Tools.java
Go to the documentation of this file.
1package io.github.ollama4j.tools;
2
3import com.fasterxml.jackson.annotation.JsonIgnore;
4import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
5import com.fasterxml.jackson.annotation.JsonInclude;
6import com.fasterxml.jackson.annotation.JsonProperty;
7import com.fasterxml.jackson.core.JsonProcessingException;
8import io.github.ollama4j.utils.Utils;
9import lombok.Builder;
10import lombok.Data;
11
12import java.util.ArrayList;
13import java.util.HashMap;
14import java.util.List;
15import java.util.Map;
16
17public class Tools {
18 @Data
19 @Builder
20 public static class ToolSpecification {
21 private String functionName;
22 private String functionDescription;
23 private Map<String, PromptFuncDefinition.Property> properties;
24 private ToolFunction toolDefinition;
25 }
26
27 @Data
28 @JsonIgnoreProperties(ignoreUnknown = true)
29 public static class PromptFuncDefinition {
30 private String type;
31 private PromptFuncSpec function;
32
33 @Data
34 public static class PromptFuncSpec {
35 private String name;
36 private String description;
37 private Parameters parameters;
38 }
39
40 @Data
41 public static class Parameters {
42 private String type;
43 private Map<String, Property> properties;
44 private List<String> required;
45 }
46
47 @Data
48 @Builder
49 public static class Property {
50 private String type;
51 private String description;
52 @JsonProperty("enum")
53 @JsonInclude(JsonInclude.Include.NON_NULL)
54 private List<String> enumValues;
55 @JsonIgnore
56 private boolean required;
57 }
58 }
59
60 public static class PropsBuilder {
61 private final Map<String, PromptFuncDefinition.Property> props = new HashMap<>();
62
63 public PropsBuilder withProperty(String key, PromptFuncDefinition.Property property) {
64 props.put(key, property);
65 return this;
66 }
67
68 public Map<String, PromptFuncDefinition.Property> build() {
69 return props;
70 }
71 }
72
73 public static class PromptBuilder {
74 private final List<PromptFuncDefinition> tools = new ArrayList<>();
75
76 private String promptText;
77
78 public String build() throws JsonProcessingException {
79 return "[AVAILABLE_TOOLS] " + Utils.getObjectMapper().writeValueAsString(tools) + "[/AVAILABLE_TOOLS][INST] " + promptText + " [/INST]";
80 }
81
82 public PromptBuilder withPrompt(String prompt) throws JsonProcessingException {
83 promptText = prompt;
84 return this;
85 }
86
87 public PromptBuilder withToolSpecification(ToolSpecification spec) {
88 PromptFuncDefinition def = new PromptFuncDefinition();
89 def.setType("function");
90
91 PromptFuncDefinition.PromptFuncSpec functionDetail = new PromptFuncDefinition.PromptFuncSpec();
92 functionDetail.setName(spec.getFunctionName());
93 functionDetail.setDescription(spec.getFunctionDescription());
94
95 PromptFuncDefinition.Parameters parameters = new PromptFuncDefinition.Parameters();
96 parameters.setType("object");
97 parameters.setProperties(spec.getProperties());
98
99 List<String> requiredValues = new ArrayList<>();
100 for (Map.Entry<String, PromptFuncDefinition.Property> p : spec.getProperties().entrySet()) {
101 if (p.getValue().isRequired()) {
102 requiredValues.add(p.getKey());
103 }
104 }
105 parameters.setRequired(requiredValues);
106 functionDetail.setParameters(parameters);
107 def.setFunction(functionDetail);
108
109 tools.add(def);
110 return this;
111 }
112 }
113}
static ObjectMapper getObjectMapper()
Definition Utils.java:17