Ollama4j
A Java library (wrapper/binding) for Ollama server.
Loading...
Searching...
No Matches
OllamaGenerateRequest.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.generate;
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.io.IOException;
19import java.nio.file.Files;
20import java.util.ArrayList;
21import java.util.Base64;
22import java.util.List;
23import java.util.Map;
24import lombok.Getter;
25import lombok.Setter;
26
27@Getter
28@Setter
30
31 private String prompt;
32 private List<String> images;
33 private String system;
34 private String context;
35 private boolean raw;
36
37 @com.fasterxml.jackson.databind.annotation.JsonSerialize(using = ThinkModeSerializer.class)
38 private ThinkMode think;
39
40 private boolean useTools;
41 private List<Tools.Tool> tools;
42
44
45 public OllamaGenerateRequest(String model, String prompt) {
46 this.model = model;
47 this.prompt = prompt;
48 }
49
50 public OllamaGenerateRequest(String model, String prompt, List<String> images) {
51 this.model = model;
52 this.prompt = prompt;
53 this.images = images;
54 }
55
56 // --- Builder-style methods ---
57
59 return new OllamaGenerateRequest();
60 }
61
62 public OllamaGenerateRequest withPrompt(String prompt) {
63 this.setPrompt(prompt);
64 return this;
65 }
66
67 public OllamaGenerateRequest withTools(List<Tools.Tool> tools) {
68 this.setTools(tools);
69 return this;
70 }
71
73 this.setModel(model);
74 return this;
75 }
76
78 this.setFormat("json");
79 return this;
80 }
81
83 this.setOptions(options.getOptionsMap());
84 return this;
85 }
86
88 this.setTemplate(template);
89 return this;
90 }
91
92 public OllamaGenerateRequest withStreaming(boolean streaming) {
93 this.setStream(streaming);
94 return this;
95 }
96
98 this.setKeepAlive(keepAlive);
99 return this;
100 }
101
102 public OllamaGenerateRequest withRaw(boolean raw) {
103 this.setRaw(raw);
104 return this;
105 }
106
108 this.setThink(think);
109 return this;
110 }
111
112 public OllamaGenerateRequest withUseTools(boolean useTools) {
113 this.setUseTools(useTools);
114 return this;
115 }
116
117 public OllamaGenerateRequest withFormat(Map<String, Object> format) {
118 this.setFormat(format);
119 return this;
120 }
121
122 public OllamaGenerateRequest withSystem(String system) {
123 this.setSystem(system);
124 return this;
125 }
126
127 public OllamaGenerateRequest withContext(String context) {
128 this.setContext(context);
129 return this;
130 }
131
132 public OllamaGenerateRequest withImagesBase64(List<String> images) {
133 this.setImages(images);
134 return this;
135 }
136
137 public OllamaGenerateRequest withImages(List<File> imageFiles) throws IOException {
138 List<String> images = new ArrayList<>();
139 for (File imageFile : imageFiles) {
140 images.add(Base64.getEncoder().encodeToString(Files.readAllBytes(imageFile.toPath())));
141 }
142 this.setImages(images);
143 return this;
144 }
145
147 return this;
148 }
149
150 @Override
151 public boolean equals(Object o) {
152 if (!(o instanceof OllamaGenerateRequest)) {
153 return false;
154 }
155 return this.toString().equals(o.toString());
156 }
157}
OllamaGenerateRequest withImages(List< File > imageFiles)
OllamaGenerateRequest withTools(List< Tools.Tool > tools)
OllamaGenerateRequest(String model, String prompt, List< String > images)
OllamaGenerateRequest withFormat(Map< String, Object > format)
OllamaGenerateRequest withImagesBase64(List< String > images)