From dd1022a990ca5b55ed1a04a78fd027ce6b95786c Mon Sep 17 00:00:00 2001 From: amithkoujalgi Date: Sun, 28 Sep 2025 22:46:37 +0530 Subject: [PATCH] Add Javadoc generation to Makefile and refactor model classes - Introduced a new `javadoc` target in the Makefile to generate Javadocs. - Refactored model classes: renamed `ModelsProcessResponse` to `ModelProcessesResponse` and updated related references. - Updated `OllamaEmbedRequestModel` and `OllamaEmbedResponseModel` to `OllamaEmbedRequest` and `OllamaEmbedResponse`, respectively, across the codebase. - Added new classes for `OllamaEmbedRequest` and `OllamaEmbedResponse` to improve clarity and maintainability. --- Makefile | 10 ++++++++++ docs/docs/apis-extras/ps.md | 4 ++-- .../java/io/github/ollama4j/OllamaAPI.java | 18 ++++++++--------- ...uestModel.java => OllamaEmbedRequest.java} | 2 +- .../embed/OllamaEmbedRequestBuilder.java | 6 +++--- ...nseModel.java => OllamaEmbedResponse.java} | 2 +- ...ponse.java => ModelProcessesResponse.java} | 2 +- .../OllamaAPIIntegrationTest.java | 12 +++++------ .../ollama4j/unittests/TestMockedAPIs.java | 20 +++++++++---------- .../unittests/TestOptionsAndUtils.java | 5 ++++- .../TestEmbedRequestSerialization.java | 14 ++++++------- 11 files changed, 52 insertions(+), 43 deletions(-) rename src/main/java/io/github/ollama4j/models/embed/{OllamaEmbedRequestModel.java => OllamaEmbedRequest.java} (96%) rename src/main/java/io/github/ollama4j/models/embed/{OllamaEmbedResponseModel.java => OllamaEmbedResponse.java} (95%) rename src/main/java/io/github/ollama4j/models/ps/{ModelsProcessResponse.java => ModelProcessesResponse.java} (97%) diff --git a/Makefile b/Makefile index b6beff8..7b5ad0c 100644 --- a/Makefile +++ b/Makefile @@ -38,6 +38,16 @@ doxygen: @echo "\033[0;34mGenerating documentation with Doxygen...\033[0m" @doxygen Doxyfile +javadoc: + @echo "\033[0;34mGenerating Javadocs into '$(javadocfolder)'...\033[0m" + @mvn clean javadoc:javadoc + @if [ -f "target/reports/apidocs/index.html" ]; then \ + echo "\033[0;32mJavadocs generated in target/reports/apidocs/index.html\033[0m"; \ + else \ + echo "\033[0;31mFailed to generate Javadocs in target/reports/apidocs\033[0m"; \ + exit 1; \ + fi + list-releases: @echo "\033[0;34mListing latest releases...\033[0m" @curl 'https://central.sonatype.com/api/internal/browse/component/versions?sortField=normalizedVersion&sortDirection=desc&page=0&size=20&filter=namespace%3Aio.github.ollama4j%2Cname%3Aollama4j' \ diff --git a/docs/docs/apis-extras/ps.md b/docs/docs/apis-extras/ps.md index b4822f2..d8641a0 100644 --- a/docs/docs/apis-extras/ps.md +++ b/docs/docs/apis-extras/ps.md @@ -12,14 +12,14 @@ This API corresponds to the [PS](https://github.com/ollama/ollama/blob/main/docs package io.github.ollama4j.localtests; import io.github.ollama4j.OllamaAPI; -import io.github.ollama4j.models.ps.ModelsProcessResponse; +import io.github.ollama4j.models.ps.ModelProcessesResponse; public class Main { public static void main(String[] args) { OllamaAPI ollamaAPI = new OllamaAPI("http://localhost:11434"); - ModelsProcessResponse response = ollamaAPI.ps(); + ModelProcessesResponse response = ollamaAPI.ps(); System.out.println(response); } diff --git a/src/main/java/io/github/ollama4j/OllamaAPI.java b/src/main/java/io/github/ollama4j/OllamaAPI.java index 401228c..68931e1 100644 --- a/src/main/java/io/github/ollama4j/OllamaAPI.java +++ b/src/main/java/io/github/ollama4j/OllamaAPI.java @@ -15,12 +15,12 @@ import io.github.ollama4j.exceptions.ToolInvocationException; import io.github.ollama4j.metrics.MetricsRecorder; import io.github.ollama4j.models.chat.*; import io.github.ollama4j.models.chat.OllamaChatTokenHandler; -import io.github.ollama4j.models.embed.OllamaEmbedRequestModel; -import io.github.ollama4j.models.embed.OllamaEmbedResponseModel; +import io.github.ollama4j.models.embed.OllamaEmbedRequest; +import io.github.ollama4j.models.embed.OllamaEmbedResponse; import io.github.ollama4j.models.generate.OllamaGenerateRequest; import io.github.ollama4j.models.generate.OllamaGenerateStreamObserver; import io.github.ollama4j.models.generate.OllamaGenerateTokenHandler; -import io.github.ollama4j.models.ps.ModelsProcessResponse; +import io.github.ollama4j.models.ps.ModelProcessesResponse; import io.github.ollama4j.models.request.*; import io.github.ollama4j.models.response.*; import io.github.ollama4j.tools.*; @@ -189,7 +189,7 @@ public class OllamaAPI { * @return ModelsProcessResponse containing details about the running models * @throws OllamaException if the response indicates an error status */ - public ModelsProcessResponse ps() throws OllamaException { + public ModelProcessesResponse ps() throws OllamaException { long startTime = System.currentTimeMillis(); String url = "/api/ps"; int statusCode = -1; @@ -217,7 +217,7 @@ public class OllamaAPI { String responseString = response.body(); if (statusCode == 200) { return Utils.getObjectMapper() - .readValue(responseString, ModelsProcessResponse.class); + .readValue(responseString, ModelProcessesResponse.class); } else { throw new OllamaException(statusCode + " - " + responseString); } @@ -713,14 +713,13 @@ public class OllamaAPI { } /** - * Generate embeddings using a {@link OllamaEmbedRequestModel}. + * Generate embeddings using a {@link OllamaEmbedRequest}. * * @param modelRequest request for '/api/embed' endpoint * @return embeddings * @throws OllamaException if the response indicates an error status */ - public OllamaEmbedResponseModel embed(OllamaEmbedRequestModel modelRequest) - throws OllamaException { + public OllamaEmbedResponse embed(OllamaEmbedRequest modelRequest) throws OllamaException { long startTime = System.currentTimeMillis(); String url = "/api/embed"; int statusCode = -1; @@ -740,8 +739,7 @@ public class OllamaAPI { statusCode = response.statusCode(); String responseBody = response.body(); if (statusCode == 200) { - return Utils.getObjectMapper() - .readValue(responseBody, OllamaEmbedResponseModel.class); + return Utils.getObjectMapper().readValue(responseBody, OllamaEmbedResponse.class); } else { throw new OllamaException(statusCode + " - " + responseBody); } diff --git a/src/main/java/io/github/ollama4j/models/embed/OllamaEmbedRequestModel.java b/src/main/java/io/github/ollama4j/models/embed/OllamaEmbedRequest.java similarity index 96% rename from src/main/java/io/github/ollama4j/models/embed/OllamaEmbedRequestModel.java rename to src/main/java/io/github/ollama4j/models/embed/OllamaEmbedRequest.java index 1bf815a..8c2fae8 100644 --- a/src/main/java/io/github/ollama4j/models/embed/OllamaEmbedRequestModel.java +++ b/src/main/java/io/github/ollama4j/models/embed/OllamaEmbedRequest.java @@ -20,7 +20,7 @@ import lombok.*; @Data @RequiredArgsConstructor @NoArgsConstructor -public class OllamaEmbedRequestModel { +public class OllamaEmbedRequest { @NonNull private String model; @NonNull private List input; diff --git a/src/main/java/io/github/ollama4j/models/embed/OllamaEmbedRequestBuilder.java b/src/main/java/io/github/ollama4j/models/embed/OllamaEmbedRequestBuilder.java index 910891c..8e551ca 100644 --- a/src/main/java/io/github/ollama4j/models/embed/OllamaEmbedRequestBuilder.java +++ b/src/main/java/io/github/ollama4j/models/embed/OllamaEmbedRequestBuilder.java @@ -16,10 +16,10 @@ import java.util.List; */ public class OllamaEmbedRequestBuilder { - private final OllamaEmbedRequestModel request; + private final OllamaEmbedRequest request; private OllamaEmbedRequestBuilder(String model, List input) { - this.request = new OllamaEmbedRequestModel(model, input); + this.request = new OllamaEmbedRequest(model, input); } public static OllamaEmbedRequestBuilder getInstance(String model, String... input) { @@ -41,7 +41,7 @@ public class OllamaEmbedRequestBuilder { return this; } - public OllamaEmbedRequestModel build() { + public OllamaEmbedRequest build() { return this.request; } } diff --git a/src/main/java/io/github/ollama4j/models/embed/OllamaEmbedResponseModel.java b/src/main/java/io/github/ollama4j/models/embed/OllamaEmbedResponse.java similarity index 95% rename from src/main/java/io/github/ollama4j/models/embed/OllamaEmbedResponseModel.java rename to src/main/java/io/github/ollama4j/models/embed/OllamaEmbedResponse.java index 742af9f..060b4c6 100644 --- a/src/main/java/io/github/ollama4j/models/embed/OllamaEmbedResponseModel.java +++ b/src/main/java/io/github/ollama4j/models/embed/OllamaEmbedResponse.java @@ -14,7 +14,7 @@ import lombok.Data; @SuppressWarnings("unused") @Data -public class OllamaEmbedResponseModel { +public class OllamaEmbedResponse { @JsonProperty("model") private String model; diff --git a/src/main/java/io/github/ollama4j/models/ps/ModelsProcessResponse.java b/src/main/java/io/github/ollama4j/models/ps/ModelProcessesResponse.java similarity index 97% rename from src/main/java/io/github/ollama4j/models/ps/ModelsProcessResponse.java rename to src/main/java/io/github/ollama4j/models/ps/ModelProcessesResponse.java index 96cb971..518205e 100644 --- a/src/main/java/io/github/ollama4j/models/ps/ModelsProcessResponse.java +++ b/src/main/java/io/github/ollama4j/models/ps/ModelProcessesResponse.java @@ -17,7 +17,7 @@ import lombok.NoArgsConstructor; @Data @NoArgsConstructor @JsonIgnoreProperties(ignoreUnknown = true) -public class ModelsProcessResponse { +public class ModelProcessesResponse { @JsonProperty("models") private List models; diff --git a/src/test/java/io/github/ollama4j/integrationtests/OllamaAPIIntegrationTest.java b/src/test/java/io/github/ollama4j/integrationtests/OllamaAPIIntegrationTest.java index 5a5bf39..638f32c 100644 --- a/src/test/java/io/github/ollama4j/integrationtests/OllamaAPIIntegrationTest.java +++ b/src/test/java/io/github/ollama4j/integrationtests/OllamaAPIIntegrationTest.java @@ -15,8 +15,8 @@ import io.github.ollama4j.exceptions.OllamaException; import io.github.ollama4j.impl.ConsoleOutputChatTokenHandler; import io.github.ollama4j.impl.ConsoleOutputGenerateTokenHandler; import io.github.ollama4j.models.chat.*; -import io.github.ollama4j.models.embed.OllamaEmbedRequestModel; -import io.github.ollama4j.models.embed.OllamaEmbedResponseModel; +import io.github.ollama4j.models.embed.OllamaEmbedRequest; +import io.github.ollama4j.models.embed.OllamaEmbedResponse; import io.github.ollama4j.models.generate.OllamaGenerateRequest; import io.github.ollama4j.models.generate.OllamaGenerateRequestBuilder; import io.github.ollama4j.models.generate.OllamaGenerateStreamObserver; @@ -231,10 +231,10 @@ class OllamaAPIIntegrationTest { @Order(5) void shouldReturnEmbeddings() throws Exception { api.pullModel(EMBEDDING_MODEL); - OllamaEmbedRequestModel m = new OllamaEmbedRequestModel(); + OllamaEmbedRequest m = new OllamaEmbedRequest(); m.setModel(EMBEDDING_MODEL); m.setInput(Arrays.asList("Why is the sky blue?", "Why is the grass green?")); - OllamaEmbedResponseModel embeddings = api.embed(m); + OllamaEmbedResponse embeddings = api.embed(m); assertNotNull(embeddings, "Embeddings should not be null"); assertFalse(embeddings.getEmbeddings().isEmpty(), "Embeddings should not be empty"); } @@ -1328,12 +1328,12 @@ class OllamaAPIIntegrationTest { void shouldReturnEmbeddingsForSingleInput() throws Exception { api.pullModel(EMBEDDING_MODEL); - OllamaEmbedRequestModel requestModel = new OllamaEmbedRequestModel(); + OllamaEmbedRequest requestModel = new OllamaEmbedRequest(); requestModel.setModel(EMBEDDING_MODEL); requestModel.setInput( Collections.singletonList("This is a single test sentence for embedding.")); - OllamaEmbedResponseModel embeddings = api.embed(requestModel); + OllamaEmbedResponse embeddings = api.embed(requestModel); assertNotNull(embeddings); assertFalse(embeddings.getEmbeddings().isEmpty()); diff --git a/src/test/java/io/github/ollama4j/unittests/TestMockedAPIs.java b/src/test/java/io/github/ollama4j/unittests/TestMockedAPIs.java index 1b944d5..7140146 100644 --- a/src/test/java/io/github/ollama4j/unittests/TestMockedAPIs.java +++ b/src/test/java/io/github/ollama4j/unittests/TestMockedAPIs.java @@ -16,8 +16,8 @@ import io.github.ollama4j.OllamaAPI; import io.github.ollama4j.exceptions.OllamaException; import io.github.ollama4j.exceptions.RoleNotFoundException; import io.github.ollama4j.models.chat.OllamaChatMessageRole; -import io.github.ollama4j.models.embed.OllamaEmbedRequestModel; -import io.github.ollama4j.models.embed.OllamaEmbedResponseModel; +import io.github.ollama4j.models.embed.OllamaEmbedRequest; +import io.github.ollama4j.models.embed.OllamaEmbedResponse; import io.github.ollama4j.models.generate.OllamaGenerateRequest; import io.github.ollama4j.models.generate.OllamaGenerateRequestBuilder; import io.github.ollama4j.models.generate.OllamaGenerateStreamObserver; @@ -109,10 +109,10 @@ class TestMockedAPIs { String model = "llama2"; String prompt = "some prompt text"; try { - OllamaEmbedRequestModel m = new OllamaEmbedRequestModel(); + OllamaEmbedRequest m = new OllamaEmbedRequest(); m.setModel(model); m.setInput(List.of(prompt)); - when(ollamaAPI.embed(m)).thenReturn(new OllamaEmbedResponseModel()); + when(ollamaAPI.embed(m)).thenReturn(new OllamaEmbedResponse()); ollamaAPI.embed(m); verify(ollamaAPI, times(1)).embed(m); } catch (OllamaException e) { @@ -126,8 +126,8 @@ class TestMockedAPIs { String model = "llama2"; List inputs = List.of("some prompt text"); try { - OllamaEmbedRequestModel m = new OllamaEmbedRequestModel(model, inputs); - when(ollamaAPI.embed(m)).thenReturn(new OllamaEmbedResponseModel()); + OllamaEmbedRequest m = new OllamaEmbedRequest(model, inputs); + when(ollamaAPI.embed(m)).thenReturn(new OllamaEmbedResponse()); ollamaAPI.embed(m); verify(ollamaAPI, times(1)).embed(m); } catch (OllamaException e) { @@ -141,10 +141,10 @@ class TestMockedAPIs { String model = "llama2"; List inputs = List.of("some prompt text"); try { - when(ollamaAPI.embed(new OllamaEmbedRequestModel(model, inputs))) - .thenReturn(new OllamaEmbedResponseModel()); - ollamaAPI.embed(new OllamaEmbedRequestModel(model, inputs)); - verify(ollamaAPI, times(1)).embed(new OllamaEmbedRequestModel(model, inputs)); + when(ollamaAPI.embed(new OllamaEmbedRequest(model, inputs))) + .thenReturn(new OllamaEmbedResponse()); + ollamaAPI.embed(new OllamaEmbedRequest(model, inputs)); + verify(ollamaAPI, times(1)).embed(new OllamaEmbedRequest(model, inputs)); } catch (OllamaException e) { throw new RuntimeException(e); } diff --git a/src/test/java/io/github/ollama4j/unittests/TestOptionsAndUtils.java b/src/test/java/io/github/ollama4j/unittests/TestOptionsAndUtils.java index 45fefff..3973a08 100644 --- a/src/test/java/io/github/ollama4j/unittests/TestOptionsAndUtils.java +++ b/src/test/java/io/github/ollama4j/unittests/TestOptionsAndUtils.java @@ -69,7 +69,10 @@ class TestOptionsAndUtils { void testOptionsBuilderRejectsUnsupportedCustomType() { assertThrows( IllegalArgumentException.class, - () -> new OptionsBuilder().setCustomOption("bad", new Object())); + () -> { + OptionsBuilder builder = new OptionsBuilder(); + builder.setCustomOption("bad", new Object()); + }); } @Test diff --git a/src/test/java/io/github/ollama4j/unittests/jackson/TestEmbedRequestSerialization.java b/src/test/java/io/github/ollama4j/unittests/jackson/TestEmbedRequestSerialization.java index 7cd1808..b4d7a7e 100644 --- a/src/test/java/io/github/ollama4j/unittests/jackson/TestEmbedRequestSerialization.java +++ b/src/test/java/io/github/ollama4j/unittests/jackson/TestEmbedRequestSerialization.java @@ -10,13 +10,13 @@ package io.github.ollama4j.unittests.jackson; import static org.junit.jupiter.api.Assertions.assertEquals; +import io.github.ollama4j.models.embed.OllamaEmbedRequest; import io.github.ollama4j.models.embed.OllamaEmbedRequestBuilder; -import io.github.ollama4j.models.embed.OllamaEmbedRequestModel; import io.github.ollama4j.utils.OptionsBuilder; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -class TestEmbedRequestSerialization extends AbstractSerializationTest { +class TestEmbedRequestSerialization extends AbstractSerializationTest { private OllamaEmbedRequestBuilder builder; @@ -27,20 +27,18 @@ class TestEmbedRequestSerialization extends AbstractSerializationTest