forked from Mirror/ollama4j
- updated non-primitives Long
and Boolean
to primitive types
This commit is contained in:
parent
7da4a7ffd4
commit
c5f3bb5d31
@ -10,7 +10,6 @@ import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.http.HttpClient;
|
||||
@ -22,6 +21,7 @@ import java.util.List;
|
||||
/**
|
||||
* The base Ollama API class.
|
||||
*/
|
||||
@SuppressWarnings("DuplicatedCode")
|
||||
public class OllamaAPI {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(OllamaAPI.class);
|
||||
@ -205,12 +205,23 @@ public class OllamaAPI {
|
||||
HttpClient httpClient = HttpClient.newHttpClient();
|
||||
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();
|
||||
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
if (response.statusCode() == HttpURLConnection.HTTP_OK) {
|
||||
OllamaResponseModel ollamaResponseModel = Utils.getObjectMapper().readValue(response.body(), OllamaResponseModel.class);
|
||||
return ollamaResponseModel.getResponse();
|
||||
HttpResponse<InputStream> response = httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
|
||||
int statusCode = response.statusCode();
|
||||
InputStream responseBodyStream = response.body();
|
||||
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 {
|
||||
throw new OllamaBaseException(response.statusCode() + " - " + response.body());
|
||||
return responseBuffer.toString().trim();
|
||||
}
|
||||
}
|
||||
|
||||
@ -222,7 +233,7 @@ public class OllamaAPI {
|
||||
* @param promptText the prompt/question text
|
||||
* @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);
|
||||
HttpClient httpClient = HttpClient.newHttpClient();
|
||||
URI uri = URI.create(this.host + "/api/generate");
|
||||
|
@ -7,7 +7,7 @@ public class Model {
|
||||
@JsonProperty("modified_at")
|
||||
private String modifiedAt;
|
||||
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 `:`
|
||||
@ -53,11 +53,11 @@ public class Model {
|
||||
this.digest = digest;
|
||||
}
|
||||
|
||||
public Long getSize() {
|
||||
public long getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public void setSize(Long size) {
|
||||
public void setSize(long size) {
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
|
@ -7,8 +7,8 @@ public class ModelPullResponse {
|
||||
private String status;
|
||||
|
||||
private String digest;
|
||||
private Long total;
|
||||
private Long completed;
|
||||
private long total;
|
||||
private long completed;
|
||||
|
||||
public String getStatus() {
|
||||
return status;
|
||||
@ -26,19 +26,19 @@ public class ModelPullResponse {
|
||||
this.digest = digest;
|
||||
}
|
||||
|
||||
public Long getTotal() {
|
||||
public long getTotal() {
|
||||
return total;
|
||||
}
|
||||
|
||||
public void setTotal(Long total) {
|
||||
public void setTotal(long total) {
|
||||
this.total = total;
|
||||
}
|
||||
|
||||
public Long getCompleted() {
|
||||
public long getCompleted() {
|
||||
return completed;
|
||||
}
|
||||
|
||||
public void setCompleted(Long completed) {
|
||||
public void setCompleted(long completed) {
|
||||
this.completed = completed;
|
||||
}
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ public class OllamaResponseModel {
|
||||
private String model;
|
||||
private @JsonProperty("created_at") String createdAt;
|
||||
private String response;
|
||||
private Boolean done;
|
||||
private boolean done;
|
||||
private List<Integer> context;
|
||||
private @JsonProperty("total_duration") Long totalDuration;
|
||||
private @JsonProperty("load_duration") Long loadDuration;
|
||||
@ -43,11 +43,11 @@ public class OllamaResponseModel {
|
||||
this.response = response;
|
||||
}
|
||||
|
||||
public Boolean getDone() {
|
||||
public boolean getDone() {
|
||||
return done;
|
||||
}
|
||||
|
||||
public void setDone(Boolean done) {
|
||||
public void setDone(boolean done) {
|
||||
this.done = done;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user