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.databind.node.ObjectNode;
15import java.util.ArrayList;
16import java.util.List;
17import java.util.Map;
18import lombok.AllArgsConstructor;
19import lombok.Builder;
20import lombok.Data;
21import lombok.NoArgsConstructor;
22
23public class Tools {
24 private Tools() {}
25
26 @Data
27 @Builder
28 @NoArgsConstructor
29 @AllArgsConstructor
30 public static class Tool {
31 @JsonProperty("function")
32 private ToolSpec toolSpec;
33
34 @Builder.Default private String type = "function";
35 @JsonIgnore private ToolFunction toolFunction;
36 }
37
38 @Data
39 @Builder
40 @NoArgsConstructor
41 @AllArgsConstructor
42 public static class ToolSpec {
43 private String name;
44 private String description;
45 private Parameters parameters;
46 }
47
48 @Data
49 @NoArgsConstructor
50 @AllArgsConstructor
51 public static class Parameters {
52 private Map<String, Property> properties;
53 private List<String> required = new ArrayList<>();
54
55 public static Parameters of(Map<String, Property> properties) {
56 Parameters params = new Parameters();
57 params.setProperties(properties);
58 // Optionally, populate required from properties' required flags
59 if (properties != null) {
60 for (Map.Entry<String, Property> entry : properties.entrySet()) {
61 if (entry.getValue() != null && entry.getValue().isRequired()) {
62 params.getRequired().add(entry.getKey());
63 }
64 }
65 }
66 return params;
67 }
68
69 @Override
70 public String toString() {
71 ObjectNode node =
72 com.fasterxml.jackson.databind.json.JsonMapper.builder()
73 .build()
74 .createObjectNode();
75 node.put("type", "object");
76 if (properties != null) {
77 ObjectNode propsNode = node.putObject("properties");
78 for (Map.Entry<String, Property> entry : properties.entrySet()) {
79 ObjectNode propNode = propsNode.putObject(entry.getKey());
80 Property prop = entry.getValue();
81 propNode.put("type", prop.getType());
82 propNode.put("description", prop.getDescription());
83 if (prop.getEnumValues() != null) {
84 propNode.putArray("enum")
85 .addAll(
86 prop.getEnumValues().stream()
87 .map(
88 com.fasterxml.jackson.databind.node.TextNode
89 ::new)
90 .collect(java.util.stream.Collectors.toList()));
91 }
92 }
93 }
94 if (required != null && !required.isEmpty()) {
95 node.putArray("required")
96 .addAll(
97 required.stream()
98 .map(com.fasterxml.jackson.databind.node.TextNode::new)
99 .collect(java.util.stream.Collectors.toList()));
100 }
101 return node.toPrettyString();
102 }
103 }
104
105 @Data
106 @Builder
107 @NoArgsConstructor
108 @AllArgsConstructor
109 public static class Property {
110 private String type;
111 private String description;
112
113 @JsonProperty("enum")
114 @JsonInclude(JsonInclude.Include.NON_NULL)
115 private List<String> enumValues;
116
117 @JsonIgnore private boolean required;
118 }
119}