Ollama4j
A Java library (wrapper/binding) for Ollama server.
Loading...
Searching...
No Matches
OllamaChatRequest.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.models.chat;
10
11import io.github.ollama4j.models.request.OllamaCommonRequest;
12import io.github.ollama4j.tools.Tools;
13import io.github.ollama4j.utils.OllamaRequestBody;
14import io.github.ollama4j.utils.Options;
15import java.io.File;
16import java.util.ArrayList;
17import java.util.Collections;
18import java.util.List;
19import lombok.Getter;
20import lombok.Setter;
21
29@Getter
30@Setter
32
33 private List<OllamaChatMessage> messages = Collections.emptyList();
34
35 private List<Tools.Tool> tools;
36
37 private boolean think;
38
52 private boolean useTools = true;
53
55
56 public OllamaChatRequest(String model, boolean think, List<OllamaChatMessage> messages) {
57 this.model = model;
58 this.messages = messages;
59 this.think = think;
60 }
61
62 @Override
63 public boolean equals(Object o) {
64 if (!(o instanceof OllamaChatRequest)) {
65 return false;
66 }
67 return this.toString().equals(o.toString());
68 }
69
70 // --- Builder-like fluent API methods ---
71
72 public static OllamaChatRequest builder() {
74 req.setMessages(new ArrayList<>());
75 return req;
76 }
77
79 this.setModel(model);
80 return this;
81 }
82
84 return withMessage(role, content, Collections.emptyList());
85 }
86
88 OllamaChatMessageRole role, String content, List<OllamaChatToolCalls> toolCalls) {
89 if (this.messages == null || this.messages == Collections.EMPTY_LIST) {
90 this.messages = new ArrayList<>();
91 }
92 this.messages.add(new OllamaChatMessage(role, content, null, toolCalls, null));
93 return this;
94 }
95
98 String content,
99 List<OllamaChatToolCalls> toolCalls,
100 List<File> images) {
101 if (this.messages == null || this.messages == Collections.EMPTY_LIST) {
102 this.messages = new ArrayList<>();
103 }
104
105 List<byte[]> imagesAsBytes = new ArrayList<>();
106 if (images != null) {
107 for (File image : images) {
108 try {
109 imagesAsBytes.add(java.nio.file.Files.readAllBytes(image.toPath()));
110 } catch (java.io.IOException e) {
111 throw new RuntimeException(
112 "Failed to read image file: " + image.getAbsolutePath(), e);
113 }
114 }
115 }
116 this.messages.add(new OllamaChatMessage(role, content, null, toolCalls, imagesAsBytes));
117 return this;
118 }
119
120 public OllamaChatRequest withMessages(List<OllamaChatMessage> messages) {
121 this.setMessages(messages);
122 return this;
123 }
124
126 if (options != null) {
127 this.setOptions(options.getOptionsMap());
128 }
129 return this;
130 }
131
133 this.setFormat("json");
134 return this;
135 }
136
138 this.setTemplate(template);
139 return this;
140 }
141
143 this.setStream(true);
144 return this;
145 }
146
148 this.setKeepAlive(keepAlive);
149 return this;
150 }
151
152 public OllamaChatRequest withThinking(boolean think) {
153 this.setThink(think);
154 return this;
155 }
156
157 public OllamaChatRequest withUseTools(boolean useTools) {
158 this.setUseTools(useTools);
159 return this;
160 }
161
162 public OllamaChatRequest withTools(List<Tools.Tool> tools) {
163 this.setTools(tools);
164 return this;
165 }
166
168 return this;
169 }
170
171 public void reset() {
172 // Only clear the messages, keep model and think as is
173 if (this.messages == null || this.messages == Collections.EMPTY_LIST) {
174 this.messages = new ArrayList<>();
175 } else {
176 this.messages.clear();
177 }
178 }
179}
OllamaChatRequest withMessage(OllamaChatMessageRole role, String content, List< OllamaChatToolCalls > toolCalls, List< File > images)
OllamaChatRequest withTemplate(String template)
OllamaChatRequest(String model, boolean think, List< OllamaChatMessage > messages)
OllamaChatRequest withTools(List< Tools.Tool > tools)
OllamaChatRequest withUseTools(boolean useTools)
OllamaChatRequest withMessage(OllamaChatMessageRole role, String content)
OllamaChatRequest withMessage(OllamaChatMessageRole role, String content, List< OllamaChatToolCalls > toolCalls)
OllamaChatRequest withMessages(List< OllamaChatMessage > messages)
OllamaChatRequest withKeepAlive(String keepAlive)