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