updated readme

This commit is contained in:
Amith Koujalgi 2023-12-14 16:40:15 +05:30
parent a3c59c32ef
commit f67f3b9eb5
4 changed files with 170 additions and 77 deletions

View File

@ -353,6 +353,7 @@ Find the full `Javadoc` (API specifications) [here](https://amithkoujalgi.github
conversational memory conversational memory
- `stream`: Add support for streaming responses from the model - `stream`: Add support for streaming responses from the model
- [x] Setup logging - [x] Setup logging
- [ ] Use lombok
- [ ] Add test cases - [ ] Add test cases
- [ ] Handle exceptions better (maybe throw more appropriate exceptions) - [ ] Handle exceptions better (maybe throw more appropriate exceptions)

View File

@ -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.exceptions.OllamaBaseException;
import io.github.amithkoujalgi.ollama4j.core.utils.Utils; import io.github.amithkoujalgi.ollama4j.core.utils.Utils;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -23,9 +22,13 @@ public class OllamaAsyncResultCallback extends Thread {
private final Queue<String> queue = new LinkedList<>(); private final Queue<String> queue = new LinkedList<>();
private String result; private String result;
private boolean isDone; private boolean isDone;
private boolean succeeded;
private int httpStatusCode;
private long responseTime = 0; private long responseTime = 0;
public OllamaAsyncResultCallback(HttpClient client, URI uri, OllamaRequestModel ollamaRequestModel) { public OllamaAsyncResultCallback(
HttpClient client, URI uri, OllamaRequestModel ollamaRequestModel) {
this.client = client; this.client = client;
this.ollamaRequestModel = ollamaRequestModel; this.ollamaRequestModel = ollamaRequestModel;
this.uri = uri; this.uri = uri;
@ -38,44 +41,89 @@ public class OllamaAsyncResultCallback extends Thread {
public void run() { public void run() {
try { try {
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
HttpRequest request = HttpRequest.newBuilder(uri).POST(HttpRequest.BodyPublishers.ofString(Utils.getObjectMapper().writeValueAsString(ollamaRequestModel))).header("Content-Type", "application/json").build(); HttpRequest request =
HttpResponse<InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream()); HttpRequest.newBuilder(uri)
.POST(
HttpRequest.BodyPublishers.ofString(
Utils.getObjectMapper().writeValueAsString(ollamaRequestModel)))
.header("Content-Type", "application/json")
.build();
HttpResponse<InputStream> response =
client.send(request, HttpResponse.BodyHandlers.ofInputStream());
int statusCode = response.statusCode(); int statusCode = response.statusCode();
this.httpStatusCode = statusCode;
InputStream responseBodyStream = response.body(); InputStream responseBodyStream = response.body();
String responseString = ""; try (BufferedReader reader =
try (BufferedReader reader = new BufferedReader(new InputStreamReader(responseBodyStream, StandardCharsets.UTF_8))) { new BufferedReader(new InputStreamReader(responseBodyStream, StandardCharsets.UTF_8))) {
String line; String line;
StringBuilder responseBuffer = new StringBuilder(); StringBuilder responseBuffer = new StringBuilder();
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
OllamaResponseModel ollamaResponseModel = Utils.getObjectMapper().readValue(line, OllamaResponseModel.class); 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()); queue.add(ollamaResponseModel.getResponse());
if (!ollamaResponseModel.getDone()) { if (!ollamaResponseModel.getDone()) {
responseBuffer.append(ollamaResponseModel.getResponse()); responseBuffer.append(ollamaResponseModel.getResponse());
} }
} }
}
reader.close(); reader.close();
this.isDone = true; this.isDone = true;
this.succeeded = true;
this.result = responseBuffer.toString(); this.result = responseBuffer.toString();
long endTime = System.currentTimeMillis(); long endTime = System.currentTimeMillis();
responseTime = endTime - startTime; responseTime = endTime - startTime;
} }
if (statusCode != 200) { if (statusCode != 200) {
throw new OllamaBaseException(statusCode + " - " + responseString); throw new OllamaBaseException(this.result);
} }
} catch (IOException | InterruptedException | OllamaBaseException e) { } catch (IOException | InterruptedException | OllamaBaseException e) {
this.isDone = true; this.isDone = true;
this.result = "FAILED! " + e.getMessage(); this.succeeded = false;
this.result = "[FAILED] " + e.getMessage();
} }
} }
/**
* 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() { public boolean isComplete() {
return isDone; return isDone;
} }
/**
* 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 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. * Returns the final response when the execution completes. Does not return intermediate results.
* @return response text *
* @return String - response text
*/ */
public String getResponse() { public String getResponse() {
return result; return result;
@ -86,8 +134,9 @@ public class OllamaAsyncResultCallback extends Thread {
} }
/** /**
* Returns the response time in seconds. * Returns the response time in milliseconds.
* @return response time in seconds *
* @return long - response time in milliseconds.
*/ */
public long getResponseTime() { public long getResponseTime() {
return responseTime; return responseTime;

View File

@ -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;
}
}

View File

@ -1,8 +1,14 @@
package io.github.amithkoujalgi.ollama4j.core.models; 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") @SuppressWarnings("unused")
public class OllamaResult { public class OllamaResult {
private String response; private final String response;
private long responseTime = 0; private long responseTime = 0;
public OllamaResult(String response, long responseTime) { public OllamaResult(String response, long responseTime) {
@ -10,11 +16,30 @@ public class OllamaResult {
this.responseTime = responseTime; this.responseTime = responseTime;
} }
/**
* Get the response text
*
* @return String - response text
*/
public String getResponse() { public String getResponse() {
return response; return response;
} }
/**
* Get the response time in milliseconds.
*
* @return long - response time in milliseconds
*/
public long getResponseTime() { public long getResponseTime() {
return responseTime; return responseTime;
} }
@Override
public String toString() {
try {
return getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
} }