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.AllArgsConstructor;
10import lombok.Builder;
11import lombok.Data;
12import lombok.NoArgsConstructor;
13
14import java.util.ArrayList;
15import java.util.HashMap;
16import java.util.List;
17import java.util.Map;
18
19public class Tools {
20 @Data
21 @Builder
22 public static class ToolSpecification {
23 private String functionName;
24 private String functionDescription;
25 private PromptFuncDefinition toolPrompt;
26 private ToolFunction toolFunction;
27 }
28
29 @Data
30 @JsonIgnoreProperties(ignoreUnknown = true)
31 @Builder
32 @NoArgsConstructor
33 @AllArgsConstructor
34 public static class PromptFuncDefinition {
35 private String type;
36 private PromptFuncSpec function;
37
38 @Data
39 @Builder
40 @NoArgsConstructor
41 @AllArgsConstructor
42 public static class PromptFuncSpec {
43 private String name;
44 private String description;
45 private Parameters parameters;
46 }
47
48 @Data
49 @Builder
50 @NoArgsConstructor
51 @AllArgsConstructor
52 public static class Parameters {
53 private String type;
54 private Map<String, Property> properties;
55 private List<String> required;
56 }
57
58 @Data
59 @Builder
60 @NoArgsConstructor
61 @AllArgsConstructor
62 public static class Property {
63 private String type;
64 private String description;
65 @JsonProperty("enum")
66 @JsonInclude(JsonInclude.Include.NON_NULL)
67 private List<String> enumValues;
68 @JsonIgnore
69 private boolean required;
70 }
71 }
72
73 public static class PropsBuilder {
74 private final Map<String, PromptFuncDefinition.Property> props = new HashMap<>();
75
76 public PropsBuilder withProperty(String key, PromptFuncDefinition.Property property) {
77 props.put(key, property);
78 return this;
79 }
80
81 public Map<String, PromptFuncDefinition.Property> build() {
82 return props;
83 }
84 }
85
86 public static class PromptBuilder {
87 private final List<PromptFuncDefinition> tools = new ArrayList<>();
88
89 private String promptText;
90
91 public String build() throws JsonProcessingException {
92 return "[AVAILABLE_TOOLS] " + Utils.getObjectMapper().writeValueAsString(tools) + "[/AVAILABLE_TOOLS][INST] " + promptText + " [/INST]";
93 }
94
95 public PromptBuilder withPrompt(String prompt) throws JsonProcessingException {
96 promptText = prompt;
97 return this;
98 }
99
100 public PromptBuilder withToolSpecification(ToolSpecification spec) {
101 PromptFuncDefinition def = new PromptFuncDefinition();
102 def.setType("function");
103
104 PromptFuncDefinition.PromptFuncSpec functionDetail = new PromptFuncDefinition.PromptFuncSpec();
105 functionDetail.setName(spec.getFunctionName());
106 functionDetail.setDescription(spec.getFunctionDescription());
107
108 PromptFuncDefinition.Parameters parameters = new PromptFuncDefinition.Parameters();
109 parameters.setType("object");
110 parameters.setProperties(spec.getToolPrompt().getFunction().parameters.getProperties());
111
112 List<String> requiredValues = new ArrayList<>();
113 for (Map.Entry<String, PromptFuncDefinition.Property> p : spec.getToolPrompt().getFunction().getParameters().getProperties().entrySet()) {
114 if (p.getValue().isRequired()) {
115 requiredValues.add(p.getKey());
116 }
117 }
118 parameters.setRequired(requiredValues);
119 functionDetail.setParameters(parameters);
120 def.setFunction(functionDetail);
121
122 tools.add(def);
123 return this;
124 }
125 }
126}
static ObjectMapper getObjectMapper()
Definition Utils.java:17