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.models.request.ThinkMode;
13import io.github.ollama4j.models.request.ThinkModeSerializer;
14import io.github.ollama4j.tools.Tools;
15import io.github.ollama4j.utils.OllamaRequestBody;
16import io.github.ollama4j.utils.Options;
17import java.io.File;
18import java.util.ArrayList;
19import java.util.Collections;
20import java.util.List;
21import lombok.Getter;
22import lombok.Setter;
23
31@Getter
32@Setter
34
35 private List<OllamaChatMessage> messages = new ArrayList<>();
36
37 private List<Tools.Tool> tools = new ArrayList<>();
38
39 @com.fasterxml.jackson.databind.annotation.JsonSerialize(using = ThinkModeSerializer.class)
40 private ThinkMode think;
41
51 private boolean useTools = true;
52
54
55 public OllamaChatRequest(String model, ThinkMode think, List<OllamaChatMessage> messages) {
56 this.model = model;
57 this.messages = messages;
58 this.think = think;
59 }
60
61 @Override
62 public boolean equals(Object o) {
63 if (!(o instanceof OllamaChatRequest)) {
64 return false;
65 }
66 return this.toString().equals(o.toString());
67 }
68
69 // --- Builder-like fluent API methods ---
70
71 public static OllamaChatRequest builder() {
73 req.setMessages(new ArrayList<>());
74 return req;
75 }
76
78 this.setModel(model);
79 return this;
80 }
81
83 return withMessage(role, content, new ArrayList<>());
84 }
85
87 OllamaChatMessageRole role, String content, List<OllamaChatToolCalls> toolCalls) {
88 if (this.messages == null || this.messages == Collections.EMPTY_LIST) {
89 this.messages = new ArrayList<>();
90 }
91 this.messages.add(new OllamaChatMessage(role, content, null, toolCalls, null));
92 return this;
93 }
94
97 String content,
98 List<OllamaChatToolCalls> toolCalls,
99 List<File> images) {
100 if (this.messages == null || this.messages == Collections.EMPTY_LIST) {
101 this.messages = new ArrayList<>();
102 }
103
104 List<byte[]> imagesAsBytes = new ArrayList<>();
105 if (images != null) {
106 for (File image : images) {
107 try {
108 imagesAsBytes.add(java.nio.file.Files.readAllBytes(image.toPath()));
109 } catch (java.io.IOException e) {
110 throw new RuntimeException(
111 "Failed to read image file: " + image.getAbsolutePath(), e);
112 }
113 }
114 }
115 this.messages.add(new OllamaChatMessage(role, content, null, toolCalls, imagesAsBytes));
116 return this;
117 }
118
119 public OllamaChatRequest withMessages(List<OllamaChatMessage> messages) {
120 this.setMessages(messages);
121 return this;
122 }
123
125 if (options != null) {
126 this.setOptions(options.getOptionsMap());
127 }
128 return this;
129 }
130
132 this.setFormat("json");
133 return this;
134 }
135
137 this.setTemplate(template);
138 return this;
139 }
140
142 this.setStream(true);
143 return this;
144 }
145
147 this.setKeepAlive(keepAlive);
148 return this;
149 }
150
152 this.setThink(think);
153 return this;
154 }
155
156 public OllamaChatRequest withUseTools(boolean useTools) {
157 this.setUseTools(useTools);
158 return this;
159 }
160
161 public OllamaChatRequest withTools(List<Tools.Tool> tools) {
162 this.setTools(tools);
163 return this;
164 }
165
167 return this;
168 }
169
170 public void reset() {
171 // Only clear the messages, keep model and think as is
172 if (this.messages == null || this.messages == Collections.EMPTY_LIST) {
173 this.messages = new ArrayList<>();
174 } else {
175 this.messages.clear();
176 }
177 }
178}
OllamaChatRequest withMessage(OllamaChatMessageRole role, String content, List< OllamaChatToolCalls > toolCalls, List< File > images)
OllamaChatRequest withTemplate(String template)
OllamaChatRequest withTools(List< Tools.Tool > tools)
OllamaChatRequest withThinking(ThinkMode think)
OllamaChatRequest withUseTools(boolean useTools)
OllamaChatRequest(String model, ThinkMode think, List< OllamaChatMessage > messages)
OllamaChatRequest withMessage(OllamaChatMessageRole role, String content)
OllamaChatRequest withMessage(OllamaChatMessageRole role, String content, List< OllamaChatToolCalls > toolCalls)
OllamaChatRequest withMessages(List< OllamaChatMessage > messages)
OllamaChatRequest withKeepAlive(String keepAlive)