From f67f3b9eb558cf44a37b5706525b62449b73f425 Mon Sep 17 00:00:00 2001 From: Amith Koujalgi Date: Thu, 14 Dec 2023 16:40:15 +0530 Subject: [PATCH] updated readme --- README.md | 1 + .../models/OllamaAsyncResultCallback.java | 181 +++++++++++------- .../core/models/OllamaErrorResponseModel.java | 18 ++ .../ollama4j/core/models/OllamaResult.java | 47 +++-- 4 files changed, 170 insertions(+), 77 deletions(-) create mode 100644 src/main/java/io/github/amithkoujalgi/ollama4j/core/models/OllamaErrorResponseModel.java diff --git a/README.md b/README.md index 14ddb02..0a5ac9b 100644 --- a/README.md +++ b/README.md @@ -353,6 +353,7 @@ Find the full `Javadoc` (API specifications) [here](https://amithkoujalgi.github conversational memory - `stream`: Add support for streaming responses from the model - [x] Setup logging +- [ ] Use lombok - [ ] Add test cases - [ ] Handle exceptions better (maybe throw more appropriate exceptions) 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 0d92d5c..4683b61 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 @@ -2,7 +2,6 @@ package io.github.amithkoujalgi.ollama4j.core.models; import io.github.amithkoujalgi.ollama4j.core.exceptions.OllamaBaseException; import io.github.amithkoujalgi.ollama4j.core.utils.Utils; - import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; @@ -17,79 +16,129 @@ import java.util.Queue; @SuppressWarnings("unused") public class OllamaAsyncResultCallback extends Thread { - private final HttpClient client; - private final URI uri; - private final OllamaRequestModel ollamaRequestModel; - private final Queue queue = new LinkedList<>(); - private String result; - private boolean isDone; - private long responseTime = 0; + private final HttpClient client; + private final URI uri; + private final OllamaRequestModel ollamaRequestModel; + private final Queue queue = new LinkedList<>(); + private String result; + private boolean isDone; + private boolean succeeded; - public OllamaAsyncResultCallback(HttpClient client, URI uri, OllamaRequestModel ollamaRequestModel) { - this.client = client; - this.ollamaRequestModel = ollamaRequestModel; - this.uri = uri; - this.isDone = false; - this.result = ""; - this.queue.add(""); - } + private int httpStatusCode; + private long responseTime = 0; - @Override - public void run() { - try { - long startTime = System.currentTimeMillis(); - HttpRequest request = HttpRequest.newBuilder(uri).POST(HttpRequest.BodyPublishers.ofString(Utils.getObjectMapper().writeValueAsString(ollamaRequestModel))).header("Content-Type", "application/json").build(); - HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofInputStream()); - int statusCode = response.statusCode(); + public OllamaAsyncResultCallback( + HttpClient client, URI uri, OllamaRequestModel ollamaRequestModel) { + this.client = client; + this.ollamaRequestModel = ollamaRequestModel; + this.uri = uri; + this.isDone = false; + this.result = ""; + this.queue.add(""); + } - InputStream responseBodyStream = response.body(); - String responseString = ""; - try (BufferedReader reader = new BufferedReader(new InputStreamReader(responseBodyStream, StandardCharsets.UTF_8))) { - String line; - StringBuilder responseBuffer = new StringBuilder(); - while ((line = reader.readLine()) != null) { - OllamaResponseModel ollamaResponseModel = Utils.getObjectMapper().readValue(line, OllamaResponseModel.class); - queue.add(ollamaResponseModel.getResponse()); - if (!ollamaResponseModel.getDone()) { - responseBuffer.append(ollamaResponseModel.getResponse()); - } - } - reader.close(); - this.isDone = true; - this.result = responseBuffer.toString(); - long endTime = System.currentTimeMillis(); - responseTime = endTime - startTime; + @Override + public void run() { + try { + long startTime = System.currentTimeMillis(); + HttpRequest request = + HttpRequest.newBuilder(uri) + .POST( + HttpRequest.BodyPublishers.ofString( + Utils.getObjectMapper().writeValueAsString(ollamaRequestModel))) + .header("Content-Type", "application/json") + .build(); + HttpResponse response = + client.send(request, HttpResponse.BodyHandlers.ofInputStream()); + int statusCode = response.statusCode(); + this.httpStatusCode = statusCode; + + InputStream responseBodyStream = response.body(); + try (BufferedReader reader = + new BufferedReader(new InputStreamReader(responseBodyStream, StandardCharsets.UTF_8))) { + String line; + StringBuilder responseBuffer = new StringBuilder(); + while ((line = reader.readLine()) != null) { + if (statusCode == 404) { + OllamaErrorResponseModel ollamaResponseModel = + Utils.getObjectMapper().readValue(line, OllamaErrorResponseModel.class); + queue.add(ollamaResponseModel.getError()); + responseBuffer.append(ollamaResponseModel.getError()); + } else { + OllamaResponseModel ollamaResponseModel = + Utils.getObjectMapper().readValue(line, OllamaResponseModel.class); + queue.add(ollamaResponseModel.getResponse()); + if (!ollamaResponseModel.getDone()) { + responseBuffer.append(ollamaResponseModel.getResponse()); } - if (statusCode != 200) { - throw new OllamaBaseException(statusCode + " - " + responseString); - } - } catch (IOException | InterruptedException | OllamaBaseException e) { - this.isDone = true; - this.result = "FAILED! " + e.getMessage(); + } } - } + reader.close(); - public boolean isComplete() { - return isDone; + this.isDone = true; + this.succeeded = true; + this.result = responseBuffer.toString(); + long endTime = System.currentTimeMillis(); + responseTime = endTime - startTime; + } + if (statusCode != 200) { + throw new OllamaBaseException(this.result); + } + } catch (IOException | InterruptedException | OllamaBaseException e) { + this.isDone = true; + this.succeeded = false; + this.result = "[FAILED] " + e.getMessage(); } + } - /** - * Returns the final response when the execution completes. Does not return intermediate results. - * @return response text - */ - public String getResponse() { - return result; - } + /** + * Returns the status of the thread. This does not indicate that the request was successful or a + * failure, rather it is just a status flag to indicate if the thread is active or ended. + * + * @return boolean - status + */ + public boolean isComplete() { + return isDone; + } - public Queue getStream() { - return queue; - } + /** + * Returns the HTTP response status code for the request that was made to Ollama server. + * + * @return int - the status code for the request + */ + public int getHttpStatusCode() { + return httpStatusCode; + } - /** - * Returns the response time in seconds. - * @return response time in seconds - */ - public long getResponseTime() { - return responseTime; - } + /** + * Returns the status of the request. Indicates if the request was successful or a failure. If the + * request was a failure, the `getResponse()` method will return the error message. + * + * @return boolean - status + */ + public boolean isSucceeded() { + return succeeded; + } + + /** + * Returns the final response when the execution completes. Does not return intermediate results. + * + * @return String - response text + */ + public String getResponse() { + return result; + } + + public Queue getStream() { + return queue; + } + + /** + * Returns the response time in milliseconds. + * + * @return long - response time in milliseconds. + */ + public long getResponseTime() { + return responseTime; + } } diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/OllamaErrorResponseModel.java b/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/OllamaErrorResponseModel.java new file mode 100644 index 0000000..c5e829a --- /dev/null +++ b/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/OllamaErrorResponseModel.java @@ -0,0 +1,18 @@ +package io.github.amithkoujalgi.ollama4j.core.models; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import java.util.List; + +@JsonIgnoreProperties(ignoreUnknown = true) +public class OllamaErrorResponseModel { + private String error; + + public String getError() { + return error; + } + + public void setError(String error) { + this.error = error; + } +} diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/OllamaResult.java b/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/OllamaResult.java index ac5f67b..9bdb246 100644 --- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/OllamaResult.java +++ b/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/OllamaResult.java @@ -1,20 +1,45 @@ package io.github.amithkoujalgi.ollama4j.core.models; +import static io.github.amithkoujalgi.ollama4j.core.utils.Utils.getObjectMapper; + +import com.fasterxml.jackson.core.JsonProcessingException; + +/** The type Ollama result. */ @SuppressWarnings("unused") public class OllamaResult { - private String response; - private long responseTime = 0; + private final String response; - public OllamaResult(String response, long responseTime) { - this.response = response; - this.responseTime = responseTime; - } + private long responseTime = 0; - public String getResponse() { - return response; - } + public OllamaResult(String response, long responseTime) { + this.response = response; + this.responseTime = responseTime; + } - public long getResponseTime() { - return responseTime; + /** + * Get the response text + * + * @return String - response text + */ + public String getResponse() { + return response; + } + + /** + * Get the response time in milliseconds. + * + * @return long - response time in milliseconds + */ + public long getResponseTime() { + return responseTime; + } + + @Override + public String toString() { + try { + return getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); } + } }