mirror of
https://github.com/amithkoujalgi/ollama4j.git
synced 2025-05-15 20:07:10 +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
|
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)
|
||||||
|
|
||||||
|
@ -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;
|
||||||
@ -17,79 +16,129 @@ import java.util.Queue;
|
|||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
public class OllamaAsyncResultCallback extends Thread {
|
public class OllamaAsyncResultCallback extends Thread {
|
||||||
private final HttpClient client;
|
private final HttpClient client;
|
||||||
private final URI uri;
|
private final URI uri;
|
||||||
private final OllamaRequestModel ollamaRequestModel;
|
private final OllamaRequestModel ollamaRequestModel;
|
||||||
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 long responseTime = 0;
|
private boolean succeeded;
|
||||||
|
|
||||||
public OllamaAsyncResultCallback(HttpClient client, URI uri, OllamaRequestModel ollamaRequestModel) {
|
private int httpStatusCode;
|
||||||
this.client = client;
|
private long responseTime = 0;
|
||||||
this.ollamaRequestModel = ollamaRequestModel;
|
|
||||||
this.uri = uri;
|
|
||||||
this.isDone = false;
|
|
||||||
this.result = "";
|
|
||||||
this.queue.add("");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
public OllamaAsyncResultCallback(
|
||||||
public void run() {
|
HttpClient client, URI uri, OllamaRequestModel ollamaRequestModel) {
|
||||||
try {
|
this.client = client;
|
||||||
long startTime = System.currentTimeMillis();
|
this.ollamaRequestModel = ollamaRequestModel;
|
||||||
HttpRequest request = HttpRequest.newBuilder(uri).POST(HttpRequest.BodyPublishers.ofString(Utils.getObjectMapper().writeValueAsString(ollamaRequestModel))).header("Content-Type", "application/json").build();
|
this.uri = uri;
|
||||||
HttpResponse<InputStream> response = client.send(request, HttpResponse.BodyHandlers.ofInputStream());
|
this.isDone = false;
|
||||||
int statusCode = response.statusCode();
|
this.result = "";
|
||||||
|
this.queue.add("");
|
||||||
|
}
|
||||||
|
|
||||||
InputStream responseBodyStream = response.body();
|
@Override
|
||||||
String responseString = "";
|
public void run() {
|
||||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(responseBodyStream, StandardCharsets.UTF_8))) {
|
try {
|
||||||
String line;
|
long startTime = System.currentTimeMillis();
|
||||||
StringBuilder responseBuffer = new StringBuilder();
|
HttpRequest request =
|
||||||
while ((line = reader.readLine()) != null) {
|
HttpRequest.newBuilder(uri)
|
||||||
OllamaResponseModel ollamaResponseModel = Utils.getObjectMapper().readValue(line, OllamaResponseModel.class);
|
.POST(
|
||||||
queue.add(ollamaResponseModel.getResponse());
|
HttpRequest.BodyPublishers.ofString(
|
||||||
if (!ollamaResponseModel.getDone()) {
|
Utils.getObjectMapper().writeValueAsString(ollamaRequestModel)))
|
||||||
responseBuffer.append(ollamaResponseModel.getResponse());
|
.header("Content-Type", "application/json")
|
||||||
}
|
.build();
|
||||||
}
|
HttpResponse<InputStream> response =
|
||||||
reader.close();
|
client.send(request, HttpResponse.BodyHandlers.ofInputStream());
|
||||||
this.isDone = true;
|
int statusCode = response.statusCode();
|
||||||
this.result = responseBuffer.toString();
|
this.httpStatusCode = statusCode;
|
||||||
long endTime = System.currentTimeMillis();
|
|
||||||
responseTime = endTime - startTime;
|
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() {
|
this.isDone = true;
|
||||||
return isDone;
|
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.
|
* Returns the status of the thread. This does not indicate that the request was successful or a
|
||||||
* @return response text
|
* failure, rather it is just a status flag to indicate if the thread is active or ended.
|
||||||
*/
|
*
|
||||||
public String getResponse() {
|
* @return boolean - status
|
||||||
return result;
|
*/
|
||||||
}
|
public boolean isComplete() {
|
||||||
|
return isDone;
|
||||||
|
}
|
||||||
|
|
||||||
public Queue<String> 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.
|
* Returns the status of the request. Indicates if the request was successful or a failure. If the
|
||||||
* @return response time in seconds
|
* request was a failure, the `getResponse()` method will return the error message.
|
||||||
*/
|
*
|
||||||
public long getResponseTime() {
|
* @return boolean - status
|
||||||
return responseTime;
|
*/
|
||||||
}
|
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<String> getStream() {
|
||||||
|
return queue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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,20 +1,45 @@
|
|||||||
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;
|
|
||||||
|
|
||||||
public OllamaResult(String response, long responseTime) {
|
private long responseTime = 0;
|
||||||
this.response = response;
|
|
||||||
this.responseTime = responseTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getResponse() {
|
public OllamaResult(String response, long responseTime) {
|
||||||
return response;
|
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);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user