- updated non-primitives Long and Boolean to primitive types

This commit is contained in:
Amith Koujalgi 2023-11-13 11:45:53 +05:30
parent 7da4a7ffd4
commit c5f3bb5d31
4 changed files with 30 additions and 19 deletions

View File

@ -10,7 +10,6 @@ import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.http.HttpClient; import java.net.http.HttpClient;
@ -22,6 +21,7 @@ import java.util.List;
/** /**
* The base Ollama API class. * The base Ollama API class.
*/ */
@SuppressWarnings("DuplicatedCode")
public class OllamaAPI { public class OllamaAPI {
private static final Logger logger = LoggerFactory.getLogger(OllamaAPI.class); private static final Logger logger = LoggerFactory.getLogger(OllamaAPI.class);
@ -205,12 +205,23 @@ public class OllamaAPI {
HttpClient httpClient = HttpClient.newHttpClient(); HttpClient httpClient = HttpClient.newHttpClient();
URI uri = URI.create(this.host + "/api/generate"); URI uri = URI.create(this.host + "/api/generate");
HttpRequest request = HttpRequest.newBuilder(uri).POST(HttpRequest.BodyPublishers.ofString(Utils.getObjectMapper().writeValueAsString(ollamaRequestModel))).header("Content-Type", "application/json").build(); HttpRequest request = HttpRequest.newBuilder(uri).POST(HttpRequest.BodyPublishers.ofString(Utils.getObjectMapper().writeValueAsString(ollamaRequestModel))).header("Content-Type", "application/json").build();
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); HttpResponse<InputStream> response = httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
if (response.statusCode() == HttpURLConnection.HTTP_OK) { int statusCode = response.statusCode();
OllamaResponseModel ollamaResponseModel = Utils.getObjectMapper().readValue(response.body(), OllamaResponseModel.class); InputStream responseBodyStream = response.body();
return ollamaResponseModel.getResponse(); StringBuilder responseBuffer = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(responseBodyStream, StandardCharsets.UTF_8))) {
String line;
while ((line = reader.readLine()) != null) {
OllamaResponseModel ollamaResponseModel = Utils.getObjectMapper().readValue(line, OllamaResponseModel.class);
if (!ollamaResponseModel.getDone()) {
responseBuffer.append(ollamaResponseModel.getResponse());
}
}
}
if (statusCode != 200) {
throw new OllamaBaseException(statusCode + " - " + responseBuffer);
} else { } else {
throw new OllamaBaseException(response.statusCode() + " - " + response.body()); return responseBuffer.toString().trim();
} }
} }
@ -222,7 +233,7 @@ public class OllamaAPI {
* @param promptText the prompt/question text * @param promptText the prompt/question text
* @return the ollama async result callback handle * @return the ollama async result callback handle
*/ */
public OllamaAsyncResultCallback askAsyncNew(String ollamaModelType, String promptText) { public OllamaAsyncResultCallback askAsync(String ollamaModelType, String promptText) {
OllamaRequestModel ollamaRequestModel = new OllamaRequestModel(ollamaModelType, promptText); OllamaRequestModel ollamaRequestModel = new OllamaRequestModel(ollamaModelType, promptText);
HttpClient httpClient = HttpClient.newHttpClient(); HttpClient httpClient = HttpClient.newHttpClient();
URI uri = URI.create(this.host + "/api/generate"); URI uri = URI.create(this.host + "/api/generate");

View File

@ -7,7 +7,7 @@ public class Model {
@JsonProperty("modified_at") @JsonProperty("modified_at")
private String modifiedAt; private String modifiedAt;
private String digest; private String digest;
private Long size; private long size;
/** /**
* Returns the model's tag. This includes model name and its version separated by a colon character `:` * Returns the model's tag. This includes model name and its version separated by a colon character `:`
@ -53,11 +53,11 @@ public class Model {
this.digest = digest; this.digest = digest;
} }
public Long getSize() { public long getSize() {
return size; return size;
} }
public void setSize(Long size) { public void setSize(long size) {
this.size = size; this.size = size;
} }

View File

@ -7,8 +7,8 @@ public class ModelPullResponse {
private String status; private String status;
private String digest; private String digest;
private Long total; private long total;
private Long completed; private long completed;
public String getStatus() { public String getStatus() {
return status; return status;
@ -26,19 +26,19 @@ public class ModelPullResponse {
this.digest = digest; this.digest = digest;
} }
public Long getTotal() { public long getTotal() {
return total; return total;
} }
public void setTotal(Long total) { public void setTotal(long total) {
this.total = total; this.total = total;
} }
public Long getCompleted() { public long getCompleted() {
return completed; return completed;
} }
public void setCompleted(Long completed) { public void setCompleted(long completed) {
this.completed = completed; this.completed = completed;
} }
} }

View File

@ -10,7 +10,7 @@ public class OllamaResponseModel {
private String model; private String model;
private @JsonProperty("created_at") String createdAt; private @JsonProperty("created_at") String createdAt;
private String response; private String response;
private Boolean done; private boolean done;
private List<Integer> context; private List<Integer> context;
private @JsonProperty("total_duration") Long totalDuration; private @JsonProperty("total_duration") Long totalDuration;
private @JsonProperty("load_duration") Long loadDuration; private @JsonProperty("load_duration") Long loadDuration;
@ -43,11 +43,11 @@ public class OllamaResponseModel {
this.response = response; this.response = response;
} }
public Boolean getDone() { public boolean getDone() {
return done; return done;
} }
public void setDone(Boolean done) { public void setDone(boolean done) {
this.done = done; this.done = done;
} }