Refactor imports and add fields to OllamaStructuredResult

Reorders and deduplicates import statements across multiple files for consistency and clarity. Adds additional fields to OllamaStructuredResult and ensures OllamaResult is populated with these fields. Updates tests and minor code style improvements throughout the codebase.
This commit is contained in:
amithkoujalgi 2025-08-31 16:46:32 +05:30
parent 4de3d98b79
commit b216d1b647
No known key found for this signature in database
GPG Key ID: E29A37746AF94B70
36 changed files with 136 additions and 136 deletions

View File

@ -52,7 +52,7 @@ import java.util.stream.Collectors;
/**
* The base Ollama API class.
*/
@SuppressWarnings({ "DuplicatedCode", "resource" })
@SuppressWarnings({"DuplicatedCode", "resource"})
public class OllamaAPI {
private static final Logger logger = LoggerFactory.getLogger(OllamaAPI.class);
@ -101,7 +101,7 @@ public class OllamaAPI {
* Default is 0 (no retries).
*/
@Setter
@SuppressWarnings({ "FieldMayBeFinal", "FieldCanBeLocal" })
@SuppressWarnings({"FieldMayBeFinal", "FieldCanBeLocal"})
private int numberOfRetriesForModelPull = 0;
/**
@ -984,6 +984,18 @@ public class OllamaAPI {
OllamaStructuredResult.class);
OllamaResult ollamaResult = new OllamaResult(structuredResult.getResponse(), structuredResult.getThinking(),
structuredResult.getResponseTime(), statusCode);
ollamaResult.setModel(structuredResult.getModel());
ollamaResult.setCreatedAt(structuredResult.getCreatedAt());
ollamaResult.setDone(structuredResult.isDone());
ollamaResult.setDoneReason(structuredResult.getDoneReason());
ollamaResult.setContext(structuredResult.getContext());
ollamaResult.setTotalDuration(structuredResult.getTotalDuration());
ollamaResult.setLoadDuration(structuredResult.getLoadDuration());
ollamaResult.setPromptEvalCount(structuredResult.getPromptEvalCount());
ollamaResult.setPromptEvalDuration(structuredResult.getPromptEvalDuration());
ollamaResult.setEvalCount(structuredResult.getEvalCount());
ollamaResult.setEvalDuration(structuredResult.getEvalDuration());
if (verbose) {
logger.info("Model response:\n{}", ollamaResult);
}

View File

@ -1,21 +1,15 @@
package io.github.ollama4j.models.chat;
import static io.github.ollama4j.utils.Utils.getObjectMapper;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.github.ollama4j.utils.FileToBase64Serializer;
import lombok.*;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import static io.github.ollama4j.utils.Utils.getObjectMapper;
/**
* Defines a single Message to be used inside a chat request against the ollama /api/chat endpoint.

View File

@ -1,14 +1,13 @@
package io.github.ollama4j.models.chat;
import java.util.List;
import io.github.ollama4j.models.request.OllamaCommonRequest;
import io.github.ollama4j.tools.Tools;
import io.github.ollama4j.utils.OllamaRequestBody;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
/**
* Defines a Request to use against the ollama /api/chat endpoint.
*

View File

@ -1,10 +1,10 @@
package io.github.ollama4j.models.chat;
import java.util.List;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.Getter;
import java.util.List;
import static io.github.ollama4j.utils.Utils.getObjectMapper;
/**

View File

@ -1,9 +1,9 @@
package io.github.ollama4j.models.embeddings;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import java.util.List;
import lombok.Data;
@SuppressWarnings("unused")
@Data

View File

@ -1,7 +1,5 @@
package io.github.ollama4j.models.embeddings;
import static io.github.ollama4j.utils.Utils.getObjectMapper;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.Data;
@ -9,6 +7,10 @@ import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import java.util.Map;
import static io.github.ollama4j.utils.Utils.getObjectMapper;
@Data
@RequiredArgsConstructor
@NoArgsConstructor

View File

@ -3,12 +3,11 @@ package io.github.ollama4j.models.generate;
import io.github.ollama4j.models.request.OllamaCommonRequest;
import io.github.ollama4j.utils.OllamaRequestBody;
import java.util.List;
import lombok.Getter;
import lombok.Setter;
import java.util.List;
@Getter
@Setter
public class OllamaGenerateRequest extends OllamaCommonRequest implements OllamaRequestBody{

View File

@ -2,11 +2,10 @@ package io.github.ollama4j.models.generate;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import java.util.List;
import lombok.Data;
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class OllamaGenerateResponseModel {

View File

@ -1,11 +1,11 @@
package io.github.ollama4j.models.request;
import java.util.Base64;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.util.Base64;
@Data
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)

View File

@ -1,11 +1,11 @@
package io.github.ollama4j.models.request;
import static io.github.ollama4j.utils.Utils.getObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.AllArgsConstructor;
import lombok.Data;
import static io.github.ollama4j.utils.Utils.getObjectMapper;
@Data
@AllArgsConstructor
public class CustomModelFileContentsRequest {

View File

@ -1,11 +1,11 @@
package io.github.ollama4j.models.request;
import static io.github.ollama4j.utils.Utils.getObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.AllArgsConstructor;
import lombok.Data;
import static io.github.ollama4j.utils.Utils.getObjectMapper;
@Data
@AllArgsConstructor
public class CustomModelFilePathRequest {

View File

@ -1,17 +1,15 @@
package io.github.ollama4j.models.request;
import static io.github.ollama4j.utils.Utils.getObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Data;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import java.util.List;
import java.util.Map;
import static io.github.ollama4j.utils.Utils.getObjectMapper;
@Data
@AllArgsConstructor

View File

@ -1,11 +1,11 @@
package io.github.ollama4j.models.request;
import static io.github.ollama4j.utils.Utils.getObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.AllArgsConstructor;
import lombok.Data;
import static io.github.ollama4j.utils.Utils.getObjectMapper;
@Data
@AllArgsConstructor
public class ModelRequest {

View File

@ -1,15 +1,15 @@
package io.github.ollama4j.models.request;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import io.github.ollama4j.utils.BooleanToJsonFormatFlagSerializer;
import io.github.ollama4j.utils.Utils;
import lombok.Data;
import java.util.Map;
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public abstract class OllamaCommonRequest {

View File

@ -1,16 +1,15 @@
package io.github.ollama4j.models.request;
import io.github.ollama4j.OllamaAPI;
import io.github.ollama4j.utils.Constants;
import lombok.Getter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.URI;
import java.net.http.HttpRequest;
import java.time.Duration;
import io.github.ollama4j.utils.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.github.ollama4j.OllamaAPI;
import lombok.Getter;
/**
* Abstract helperclass to call the ollama api server.
*/

View File

@ -2,11 +2,11 @@ package io.github.ollama4j.models.request;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.github.ollama4j.exceptions.OllamaBaseException;
import io.github.ollama4j.models.response.OllamaErrorResponse;
import io.github.ollama4j.models.response.OllamaResult;
import io.github.ollama4j.models.generate.OllamaGenerateResponseModel;
import io.github.ollama4j.models.generate.OllamaGenerateStreamObserver;
import io.github.ollama4j.models.generate.OllamaStreamHandler;
import io.github.ollama4j.models.response.OllamaErrorResponse;
import io.github.ollama4j.models.response.OllamaResult;
import io.github.ollama4j.utils.OllamaRequestBody;
import io.github.ollama4j.utils.Utils;
import org.slf4j.Logger;

View File

@ -1,9 +1,10 @@
package io.github.ollama4j.models.response;
import java.util.ArrayList;
import java.util.List;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
@Data
public class LibraryModel {

View File

@ -2,8 +2,6 @@ package io.github.ollama4j.models.response;
import lombok.Data;
import java.util.List;
@Data
public class LibraryModelTag {
private String name;

View File

@ -1,9 +1,9 @@
package io.github.ollama4j.models.response;
import java.util.List;
import lombok.Data;
import java.util.List;
@Data
public class ListModelsResponse {
private List<Model> models;

View File

@ -1,13 +1,13 @@
package io.github.ollama4j.models.response;
import java.time.OffsetDateTime;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.github.ollama4j.utils.Utils;
import lombok.Data;
import java.time.OffsetDateTime;
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Model {

View File

@ -3,12 +3,12 @@ package io.github.ollama4j.models.response;
import io.github.ollama4j.exceptions.OllamaBaseException;
import io.github.ollama4j.models.generate.OllamaGenerateRequest;
import io.github.ollama4j.models.generate.OllamaGenerateResponseModel;
import io.github.ollama4j.utils.Constants;
import io.github.ollama4j.utils.Utils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import io.github.ollama4j.utils.Constants;
import java.io.BufferedReader;
import java.io.IOException;

View File

@ -1,20 +1,17 @@
package io.github.ollama4j.models.response;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import io.github.ollama4j.models.generate.OllamaGenerateResponseModel;
import lombok.Data;
import lombok.Getter;
import static io.github.ollama4j.utils.Utils.getObjectMapper;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static io.github.ollama4j.utils.Utils.getObjectMapper;
/**
* The type Ollama result.
*/

View File

@ -1,19 +1,18 @@
package io.github.ollama4j.models.response;
import static io.github.ollama4j.utils.Utils.getObjectMapper;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import java.util.List;
import java.util.Map;
import static io.github.ollama4j.utils.Utils.getObjectMapper;
@Getter
@SuppressWarnings("unused")
@Data
@ -22,13 +21,21 @@ import lombok.NoArgsConstructor;
public class OllamaStructuredResult {
private String response;
private String thinking;
private int httpStatusCode;
private long responseTime = 0;
private String model;
private @JsonProperty("created_at") String createdAt;
private boolean done;
private @JsonProperty("done_reason") String doneReason;
private List<Integer> context;
private @JsonProperty("total_duration") Long totalDuration;
private @JsonProperty("load_duration") Long loadDuration;
private @JsonProperty("prompt_eval_count") Integer promptEvalCount;
private @JsonProperty("prompt_eval_duration") Long promptEvalDuration;
private @JsonProperty("eval_count") Integer evalCount;
private @JsonProperty("eval_duration") Long evalDuration;
public OllamaStructuredResult(String response, long responseTime, int httpStatusCode) {
this.response = response;
this.responseTime = responseTime;

View File

@ -2,8 +2,6 @@ package io.github.ollama4j.models.response;
import lombok.Data;
import java.util.List;
@Data
public class OllamaVersion {
private String version;

View File

@ -1,11 +1,11 @@
package io.github.ollama4j.utils;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
public class BooleanToJsonFormatFlagSerializer extends JsonSerializer<Boolean>{
@Override

View File

@ -1,13 +1,13 @@
package io.github.ollama4j.utils;
import java.io.IOException;
import java.util.Base64;
import java.util.Collection;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import java.util.Base64;
import java.util.Collection;
public class FileToBase64Serializer extends JsonSerializer<Collection<byte[]>> {
@Override

View File

@ -1,11 +1,11 @@
package io.github.ollama4j.utils;
import java.net.http.HttpRequest.BodyPublisher;
import java.net.http.HttpRequest.BodyPublishers;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import java.net.http.HttpRequest.BodyPublisher;
import java.net.http.HttpRequest.BodyPublishers;
/**
* Interface to represent a OllamaRequest as HTTP-Request Body via {@link BodyPublishers}.
*/

View File

@ -1,8 +1,9 @@
package io.github.ollama4j.utils;
import java.util.Map;
import lombok.Data;
import java.util.Map;
/** Class for options for Ollama model. */
@Data
public class Options {

View File

@ -1,6 +1,5 @@
package io.github.ollama4j.utils;
import java.io.IOException;
import java.util.HashMap;
/** Builder class for creating options for Ollama model. */

View File

@ -1,5 +1,8 @@
package io.github.ollama4j.utils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
@ -9,9 +12,6 @@ import java.net.URISyntaxException;
import java.net.URL;
import java.util.Objects;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
public class Utils {
private static ObjectMapper objectMapper;

View File

@ -24,7 +24,6 @@ import java.io.FileWriter;
import java.io.IOException;
import java.net.URISyntaxException;
import java.time.Duration;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -42,9 +41,8 @@ public class WithAuth {
private static final String OLLAMA_VERSION = "0.6.1";
private static final String NGINX_VERSION = "nginx:1.23.4-alpine";
private static final String BEARER_AUTH_TOKEN = "secret-token";
private static final String GENERAL_PURPOSE_MODEL = "gemma3:270m";
private static final String THINKING_MODEL = "gpt-oss:20b";
// private static final String THINKING_MODEL = "gpt-oss:20b";
private static OllamaContainer ollama;
@ -52,7 +50,7 @@ public class WithAuth {
private static OllamaAPI api;
@BeforeAll
public static void setUp() {
static void setUp() {
ollama = createOllamaContainer();
ollama.start();
@ -135,14 +133,14 @@ public class WithAuth {
@Test
@Order(1)
void testOllamaBehindProxy() throws InterruptedException {
void testOllamaBehindProxy() {
api.setBearerAuth(BEARER_AUTH_TOKEN);
assertTrue(api.ping(), "Expected OllamaAPI to successfully ping through NGINX with valid auth token.");
}
@Test
@Order(1)
void testWithWrongToken() throws InterruptedException {
void testWithWrongToken() {
api.setBearerAuth("wrong-token");
assertFalse(api.ping(), "Expected OllamaAPI ping to fail through NGINX with an invalid auth token.");
}
@ -152,8 +150,8 @@ public class WithAuth {
void testAskModelWithStructuredOutput()
throws OllamaBaseException, IOException, InterruptedException, URISyntaxException {
api.setBearerAuth(BEARER_AUTH_TOKEN);
api.pullModel(THINKING_MODEL);
String model = GENERAL_PURPOSE_MODEL;
api.pullModel(model);
String prompt = "The sun is shining brightly and is directly overhead at the zenith, casting my shadow over my foot, so it must be noon.";
@ -170,7 +168,7 @@ public class WithAuth {
});
format.put("required", List.of("isNoon"));
OllamaResult result = api.generate(THINKING_MODEL, prompt, format);
OllamaResult result = api.generate(model, prompt, format);
assertNotNull(result);
assertNotNull(result.getResponse());

View File

@ -21,7 +21,8 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.*;
class TestMockedAPIs {

View File

@ -1,11 +1,12 @@
package io.github.ollama4j.unittests.jackson;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.ollama4j.utils.Utils;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
public abstract class AbstractSerializationTest<T> {
protected ObjectMapper mapper = Utils.getObjectMapper();

View File

@ -1,20 +1,19 @@
package io.github.ollama4j.unittests.jackson;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
import io.github.ollama4j.models.chat.OllamaChatMessageRole;
import io.github.ollama4j.models.chat.OllamaChatRequest;
import io.github.ollama4j.models.chat.OllamaChatRequestBuilder;
import io.github.ollama4j.utils.OptionsBuilder;
import org.json.JSONObject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.util.Collections;
import java.util.List;
import io.github.ollama4j.models.chat.OllamaChatRequest;
import org.json.JSONObject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import io.github.ollama4j.models.chat.OllamaChatMessageRole;
import io.github.ollama4j.models.chat.OllamaChatRequestBuilder;
import io.github.ollama4j.utils.OptionsBuilder;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
public class TestChatRequestSerialization extends AbstractSerializationTest<OllamaChatRequest> {

View File

@ -1,12 +1,12 @@
package io.github.ollama4j.unittests.jackson;
import static org.junit.jupiter.api.Assertions.assertEquals;
import io.github.ollama4j.models.embeddings.OllamaEmbedRequestBuilder;
import io.github.ollama4j.models.embeddings.OllamaEmbedRequestModel;
import io.github.ollama4j.utils.OptionsBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import io.github.ollama4j.utils.OptionsBuilder;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TestEmbedRequestSerialization extends AbstractSerializationTest<OllamaEmbedRequestModel> {

View File

@ -1,15 +1,13 @@
package io.github.ollama4j.unittests.jackson;
import static org.junit.jupiter.api.Assertions.assertEquals;
import io.github.ollama4j.models.generate.OllamaGenerateRequest;
import io.github.ollama4j.models.generate.OllamaGenerateRequestBuilder;
import io.github.ollama4j.utils.OptionsBuilder;
import org.json.JSONObject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import io.github.ollama4j.models.generate.OllamaGenerateRequestBuilder;
import io.github.ollama4j.utils.OptionsBuilder;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TestGenerateRequestSerialization extends AbstractSerializationTest<OllamaGenerateRequest> {