Ollama4j
A Java library (wrapper/binding) for Ollama server.
Loading...
Searching...
No Matches
OllamaChatRequestBuilder.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.utils.Options;
12import io.github.ollama4j.utils.Utils;
13import java.io.File;
14import java.io.IOException;
15import java.nio.file.Files;
16import java.util.ArrayList;
17import java.util.Collections;
18import java.util.List;
19import java.util.stream.Collectors;
20import lombok.Setter;
21import org.slf4j.Logger;
22import org.slf4j.LoggerFactory;
23
26
27 private static final Logger LOG = LoggerFactory.getLogger(OllamaChatRequestBuilder.class);
28
29 private int imageURLConnectTimeoutSeconds = 10;
30 private int imageURLReadTimeoutSeconds = 10;
31 private OllamaChatRequest request;
32 @Setter private boolean useTools = true;
33
34 private OllamaChatRequestBuilder() {
35 request = new OllamaChatRequest();
36 request.setMessages(new ArrayList<>());
37 }
38
40 return new OllamaChatRequestBuilder();
41 }
42
44 int imageURLConnectTimeoutSeconds) {
45 this.imageURLConnectTimeoutSeconds = imageURLConnectTimeoutSeconds;
46 return this;
47 }
48
49 public OllamaChatRequestBuilder withImageURLReadTimeoutSeconds(int imageURLReadTimeoutSeconds) {
50 this.imageURLReadTimeoutSeconds = imageURLReadTimeoutSeconds;
51 return this;
52 }
53
54 public OllamaChatRequestBuilder withModel(String model) {
55 request.setModel(model);
56 return this;
57 }
58
59 public void reset() {
60 request = new OllamaChatRequest(request.getModel(), request.isThink(), new ArrayList<>());
61 }
62
64 return withMessage(role, content, Collections.emptyList());
65 }
66
68 OllamaChatMessageRole role, String content, List<OllamaChatToolCalls> toolCalls) {
69 List<OllamaChatMessage> messages = this.request.getMessages();
70 messages.add(new OllamaChatMessage(role, content, null, toolCalls, null));
71 return this;
72 }
73
76 String content,
77 List<OllamaChatToolCalls> toolCalls,
78 List<File> images) {
79 List<OllamaChatMessage> messages = this.request.getMessages();
80 List<byte[]> binaryImages =
81 images.stream()
82 .map(
83 file -> {
84 try {
85 return Files.readAllBytes(file.toPath());
86 } catch (IOException e) {
87 LOG.warn(
88 "File '{}' could not be accessed, will not add to"
89 + " message!",
90 file.toPath(),
91 e);
92 return new byte[0];
93 }
94 })
95 .collect(Collectors.toList());
96 messages.add(new OllamaChatMessage(role, content, null, toolCalls, binaryImages));
97 return this;
98 }
99
102 String content,
103 List<OllamaChatToolCalls> toolCalls,
104 String... imageUrls)
105 throws IOException, InterruptedException {
106 List<OllamaChatMessage> messages = this.request.getMessages();
107 List<byte[]> binaryImages = null;
108 if (imageUrls.length > 0) {
109 binaryImages = new ArrayList<>();
110 for (String imageUrl : imageUrls) {
111 try {
112 binaryImages.add(
114 imageUrl,
115 imageURLConnectTimeoutSeconds,
116 imageURLReadTimeoutSeconds));
117 } catch (InterruptedException e) {
118 LOG.error("Failed to load image from URL: '{}'. Cause: {}", imageUrl, e);
119 Thread.currentThread().interrupt();
120 throw new InterruptedException(
121 "Interrupted while loading image from URL: " + imageUrl);
122 } catch (IOException e) {
123 LOG.error(
124 "IOException occurred while loading image from URL '{}'. Cause: {}",
125 imageUrl,
126 e.getMessage(),
127 e);
128 throw new IOException(
129 "IOException while loading image from URL: " + imageUrl, e);
130 }
131 }
132 }
133 messages.add(new OllamaChatMessage(role, content, null, toolCalls, binaryImages));
134 return this;
135 }
136
137 public OllamaChatRequestBuilder withMessages(List<OllamaChatMessage> messages) {
138 request.setMessages(messages);
139 return this;
140 }
141
143 this.request.setOptions(options.getOptionsMap());
144 return this;
145 }
146
148 this.request.setFormat("json");
149 return this;
150 }
151
152 public OllamaChatRequestBuilder withTemplate(String template) {
153 this.request.setTemplate(template);
154 return this;
155 }
156
158 this.request.setStream(true);
159 return this;
160 }
161
162 public OllamaChatRequestBuilder withKeepAlive(String keepAlive) {
163 this.request.setKeepAlive(keepAlive);
164 return this;
165 }
166
168 this.request.setThink(think);
169 return this;
170 }
171
173 request.setUseTools(useTools);
174 return request;
175 }
176}
OllamaChatRequestBuilder withMessage(OllamaChatMessageRole role, String content, List< OllamaChatToolCalls > toolCalls, List< File > images)
OllamaChatRequestBuilder withMessage(OllamaChatMessageRole role, String content, List< OllamaChatToolCalls > toolCalls)
OllamaChatRequestBuilder withMessages(List< OllamaChatMessage > messages)
OllamaChatRequestBuilder withMessage(OllamaChatMessageRole role, String content)
OllamaChatRequestBuilder withMessage(OllamaChatMessageRole role, String content, List< OllamaChatToolCalls > toolCalls, String... imageUrls)
OllamaChatRequestBuilder withImageURLReadTimeoutSeconds(int imageURLReadTimeoutSeconds)
OllamaChatRequestBuilder withImageURLConnectTimeoutSeconds(int imageURLConnectTimeoutSeconds)
static byte[] loadImageBytesFromUrl(String imageUrl, int connectTimeoutSeconds, int readTimeoutSeconds)
Definition Utils.java:40