From c379e9457114b6f228d85290e95a6f6a1f55868c Mon Sep 17 00:00:00 2001 From: Amith Koujalgi Date: Wed, 8 Nov 2023 14:27:50 +0530 Subject: [PATCH] added Javadocs --- README.md | 1 + .../ollama4j/core/OllamaAPI.java | 70 +++++++++++++++++++ .../models/OllamaAsyncResultCallback.java | 4 +- 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6ef1f2b..2b81640 100644 --- a/README.md +++ b/README.md @@ -196,6 +196,7 @@ Find the full `Javadoc` (API specifications) [here](https://amithkoujalgi.github - Use Java-naming conventions for attributes in the request/response models instead of the snake-case conventions. ( possibly with Jackson-mapper's `@JsonProperty`) - Setup logging +- Handle exceptions better (maybe throw more appropriate exceptions) #### Get Involved diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/OllamaAPI.java b/src/main/java/io/github/amithkoujalgi/ollama4j/core/OllamaAPI.java index 6b28b97..bdde73e 100644 --- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/OllamaAPI.java +++ b/src/main/java/io/github/amithkoujalgi/ollama4j/core/OllamaAPI.java @@ -23,12 +23,20 @@ import java.net.URL; import java.util.List; import java.util.stream.Collectors; +/** + * The base Ollama API class. + */ @SuppressWarnings({"DuplicatedCode", "ExtractMethodRecommender"}) public class OllamaAPI { private final String host; + /** + * Instantiates the Ollama API. + * + * @param host the host address of Ollama server + */ public OllamaAPI(String host) { if (host.endsWith("/")) { this.host = host.substring(0, host.length() - 1); @@ -37,6 +45,14 @@ public class OllamaAPI { } } + /** + * List available models from Ollama server. + * + * @return the list + * @throws IOException + * @throws OllamaBaseException + * @throws ParseException + */ public List listModels() throws IOException, OllamaBaseException, ParseException { String url = this.host + "/api/tags"; final HttpGet httpGet = new HttpGet(url); @@ -58,6 +74,15 @@ public class OllamaAPI { } } + /** + * Gets model details from the Ollama server. + * + * @param model the model + * @return the model details + * @throws IOException + * @throws OllamaBaseException + * @throws ParseException + */ public ModelDetail getModelDetails(Model model) throws IOException, OllamaBaseException, ParseException { String url = this.host + "/api/show"; String jsonData = String.format("{\"name\": \"%s\"}", model.getName()); @@ -81,6 +106,14 @@ public class OllamaAPI { } } + /** + * Pull a model on the Ollama server from the list of available models. + * + * @param model the name of the model + * @throws IOException + * @throws ParseException + * @throws OllamaBaseException + */ public void pullModel(String model) throws IOException, ParseException, OllamaBaseException { List models = listModels().stream().filter(m -> m.getModelName().split(":")[0].equals(model)).collect(Collectors.toList()); if (!models.isEmpty()) { @@ -106,6 +139,16 @@ public class OllamaAPI { } } + /** + * Create a custom model from a model file. + * Read more about custom model file creation here. + * + * @param name the name of the custom model to be created + * @param modelFilePath the path to model file that exists on the Ollama server. + * @throws IOException + * @throws ParseException + * @throws OllamaBaseException + */ public void createModel(String name, String modelFilePath) throws IOException, ParseException, OllamaBaseException { String url = this.host + "/api/create"; String jsonData = String.format("{\"name\": \"%s\", \"path\": \"%s\"}", name, modelFilePath); @@ -127,6 +170,15 @@ public class OllamaAPI { } } + /** + * Delete a model from Ollama server. + * + * @param name the name of the model to be deleted. + * @param ignoreIfNotPresent - ignore errors if the specified model is not present on Ollama server. + * @throws IOException + * @throws ParseException + * @throws OllamaBaseException + */ public void deleteModel(String name, boolean ignoreIfNotPresent) throws IOException, ParseException, OllamaBaseException { String url = this.host + "/api/delete"; String jsonData = String.format("{\"name\": \"%s\"}", name); @@ -152,6 +204,15 @@ public class OllamaAPI { } + /** + * Ask a question to a model running on Ollama server. This is a sync/blocking call. + * + * @param ollamaModelType the ollama model to ask the question to + * @param promptText the prompt/question text + * @return the response text from the model + * @throws OllamaBaseException + * @throws IOException + */ public String ask(String ollamaModelType, String promptText) throws OllamaBaseException, IOException { Gson gson = new Gson(); OllamaRequestModel ollamaRequestModel = new OllamaRequestModel(ollamaModelType, promptText); @@ -182,6 +243,15 @@ public class OllamaAPI { } } + /** + * Ask a question to a model running on Ollama server and get a callback handle that can be used to check for status and get the response from the model later. + * This would be a async/non-blocking call. + * + * @param ollamaModelType the ollama model type + * @param promptText the prompt text + * @return the ollama async result callback + * @throws IOException + */ public OllamaAsyncResultCallback askAsync(String ollamaModelType, String promptText) throws IOException { OllamaRequestModel ollamaRequestModel = new OllamaRequestModel(ollamaModelType, promptText); URL obj = new URL(this.host + "/api/generate"); diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/OllamaAsyncResultCallback.java b/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/OllamaAsyncResultCallback.java index 35e86e6..536dbe7 100644 --- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/OllamaAsyncResultCallback.java +++ b/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/OllamaAsyncResultCallback.java @@ -13,8 +13,8 @@ public class OllamaAsyncResultCallback extends Thread { private String result; private boolean isDone; - public OllamaAsyncResultCallback(HttpURLConnection con) { - this.connection = con; + public OllamaAsyncResultCallback(HttpURLConnection connection) { + this.connection = connection; this.isDone = false; this.result = ""; }