mirror of
https://github.com/amithkoujalgi/ollama4j.git
synced 2025-05-15 11:57:12 +02:00
updated readme
This commit is contained in:
parent
a3c59c32ef
commit
f67f3b9eb5
@ -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)
|
||||
|
||||
|
@ -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;
|
||||
@ -23,9 +22,13 @@ public class OllamaAsyncResultCallback extends Thread {
|
||||
private final Queue<String> queue = new LinkedList<>();
|
||||
private String result;
|
||||
private boolean isDone;
|
||||
private boolean succeeded;
|
||||
|
||||
private int httpStatusCode;
|
||||
private long responseTime = 0;
|
||||
|
||||
public OllamaAsyncResultCallback(HttpClient client, URI uri, OllamaRequestModel ollamaRequestModel) {
|
||||
public OllamaAsyncResultCallback(
|
||||
HttpClient client, URI uri, OllamaRequestModel ollamaRequestModel) {
|
||||
this.client = client;
|
||||
this.ollamaRequestModel = ollamaRequestModel;
|
||||
this.uri = uri;
|
||||
@ -38,44 +41,89 @@ public class OllamaAsyncResultCallback extends Thread {
|
||||
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<InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
|
||||
HttpRequest request =
|
||||
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();
|
||||
this.httpStatusCode = statusCode;
|
||||
|
||||
InputStream responseBodyStream = response.body();
|
||||
String responseString = "";
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(responseBodyStream, StandardCharsets.UTF_8))) {
|
||||
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);
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
reader.close();
|
||||
|
||||
this.isDone = true;
|
||||
this.succeeded = true;
|
||||
this.result = responseBuffer.toString();
|
||||
long endTime = System.currentTimeMillis();
|
||||
responseTime = endTime - startTime;
|
||||
}
|
||||
if (statusCode != 200) {
|
||||
throw new OllamaBaseException(statusCode + " - " + responseString);
|
||||
throw new OllamaBaseException(this.result);
|
||||
}
|
||||
} catch (IOException | InterruptedException | OllamaBaseException e) {
|
||||
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() {
|
||||
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.
|
||||
* @return response text
|
||||
*
|
||||
* @return String - response text
|
||||
*/
|
||||
public String getResponse() {
|
||||
return result;
|
||||
@ -86,8 +134,9 @@ public class OllamaAsyncResultCallback extends Thread {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the response time in seconds.
|
||||
* @return response time in seconds
|
||||
* Returns the response time in milliseconds.
|
||||
*
|
||||
* @return long - response time in milliseconds.
|
||||
*/
|
||||
public long getResponseTime() {
|
||||
return responseTime;
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -1,8 +1,14 @@
|
||||
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 final String response;
|
||||
|
||||
private long responseTime = 0;
|
||||
|
||||
public OllamaResult(String response, long responseTime) {
|
||||
@ -10,11 +16,30 @@ public class OllamaResult {
|
||||
this.responseTime = 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user