mirror of
https://github.com/amithkoujalgi/ollama4j.git
synced 2025-05-15 11:57:12 +02:00
- removed returning JSON string from toString methods of POJOs
- moved the code to get the `ObjectMapper` instance to `Utils` so that any changes to ObjectMapper could be handled at one place. - removed duplicate ObjectMapper instantiation code
This commit is contained in:
parent
0d6af4b4a5
commit
d2f405dc64
@ -1,9 +1,8 @@
|
||||
package io.github.amithkoujalgi.ollama4j.core;
|
||||
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.amithkoujalgi.ollama4j.core.exceptions.OllamaBaseException;
|
||||
import io.github.amithkoujalgi.ollama4j.core.models.*;
|
||||
import io.github.amithkoujalgi.ollama4j.core.utils.Utils;
|
||||
import org.apache.hc.client5.http.classic.methods.HttpDelete;
|
||||
import org.apache.hc.client5.http.classic.methods.HttpGet;
|
||||
import org.apache.hc.client5.http.classic.methods.HttpPost;
|
||||
@ -37,9 +36,6 @@ public class OllamaAPI {
|
||||
private final String host;
|
||||
private boolean verbose = false;
|
||||
|
||||
private final ObjectMapper objectMapper = new ObjectMapper()
|
||||
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
|
||||
/**
|
||||
* Instantiates the Ollama API.
|
||||
*
|
||||
@ -82,7 +78,7 @@ public class OllamaAPI {
|
||||
responseString = EntityUtils.toString(responseEntity, "UTF-8");
|
||||
}
|
||||
if (statusCode == 200) {
|
||||
return objectMapper.readValue(responseString, ListModelsResponse.class).getModels();
|
||||
return Utils.getObjectMapper().readValue(responseString, ListModelsResponse.class).getModels();
|
||||
} else {
|
||||
throw new OllamaBaseException(statusCode + " - " + responseString);
|
||||
}
|
||||
@ -114,7 +110,7 @@ public class OllamaAPI {
|
||||
responseString = EntityUtils.toString(responseEntity, "UTF-8");
|
||||
}
|
||||
if (statusCode == 200) {
|
||||
return objectMapper.readValue(responseString, ModelDetail.class);
|
||||
return Utils.getObjectMapper().readValue(responseString, ModelDetail.class);
|
||||
} else {
|
||||
throw new OllamaBaseException(statusCode + " - " + responseString);
|
||||
}
|
||||
@ -245,9 +241,9 @@ public class OllamaAPI {
|
||||
con.setRequestMethod("POST");
|
||||
con.setDoOutput(true);
|
||||
con.setRequestProperty("Content-Type", "application/json");
|
||||
System.out.println(ollamaRequestModel.toString());
|
||||
String jsonReq = Utils.getObjectMapper().writeValueAsString(ollamaRequestModel);
|
||||
try (OutputStream out = con.getOutputStream()) {
|
||||
out.write(ollamaRequestModel.toString().getBytes(StandardCharsets.UTF_8));
|
||||
out.write(jsonReq.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
int responseCode = con.getResponseCode();
|
||||
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||
@ -255,7 +251,7 @@ public class OllamaAPI {
|
||||
String inputLine;
|
||||
StringBuilder response = new StringBuilder();
|
||||
while ((inputLine = in.readLine()) != null) {
|
||||
OllamaResponseModel ollamaResponseModel = objectMapper.readValue(inputLine, OllamaResponseModel.class);
|
||||
OllamaResponseModel ollamaResponseModel = Utils.getObjectMapper().readValue(inputLine, OllamaResponseModel.class);
|
||||
if (!ollamaResponseModel.getDone()) {
|
||||
response.append(ollamaResponseModel.getResponse());
|
||||
}
|
||||
@ -284,8 +280,9 @@ public class OllamaAPI {
|
||||
con.setRequestMethod("POST");
|
||||
con.setDoOutput(true);
|
||||
con.setRequestProperty("Content-Type", "application/json");
|
||||
String jsonReq = Utils.getObjectMapper().writeValueAsString(ollamaRequestModel);
|
||||
try (OutputStream out = con.getOutputStream()) {
|
||||
out.write(ollamaRequestModel.toString().getBytes(StandardCharsets.UTF_8));
|
||||
out.write(jsonReq.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
OllamaAsyncResultCallback ollamaAsyncResultCallback = new OllamaAsyncResultCallback(con);
|
||||
ollamaAsyncResultCallback.start();
|
||||
@ -316,7 +313,7 @@ public class OllamaAPI {
|
||||
String responseString = "";
|
||||
if (responseEntity != null) {
|
||||
responseString = EntityUtils.toString(responseEntity, "UTF-8");
|
||||
EmbeddingResponse embeddingResponse = objectMapper.readValue(responseString, EmbeddingResponse.class);
|
||||
EmbeddingResponse embeddingResponse = Utils.getObjectMapper().readValue(responseString, EmbeddingResponse.class);
|
||||
return embeddingResponse.getEmbedding();
|
||||
} else {
|
||||
throw new OllamaBaseException(statusCode + " - " + responseString);
|
||||
|
@ -1,8 +1,6 @@
|
||||
package io.github.amithkoujalgi.ollama4j.core.models;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class ModelDetail {
|
||||
private String license;
|
||||
@ -41,16 +39,4 @@ public class ModelDetail {
|
||||
public void setTemplate(String template) {
|
||||
this.template = template;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
try {
|
||||
return new ObjectMapper()
|
||||
.writer()
|
||||
.withDefaultPrettyPrinter()
|
||||
.writeValueAsString(this);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
package io.github.amithkoujalgi.ollama4j.core.models;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.amithkoujalgi.ollama4j.core.exceptions.OllamaBaseException;
|
||||
import io.github.amithkoujalgi.ollama4j.core.utils.Utils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
@ -10,12 +10,12 @@ import java.net.HttpURLConnection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
|
||||
@SuppressWarnings("DuplicatedCode")
|
||||
public class OllamaAsyncResultCallback extends Thread {
|
||||
private final HttpURLConnection connection;
|
||||
private String result;
|
||||
private boolean isDone;
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
private final Queue<String> queue = new LinkedList<>();
|
||||
|
||||
public OllamaAsyncResultCallback(HttpURLConnection connection) {
|
||||
@ -35,7 +35,7 @@ public class OllamaAsyncResultCallback extends Thread {
|
||||
String inputLine;
|
||||
StringBuilder response = new StringBuilder();
|
||||
while ((inputLine = in.readLine()) != null) {
|
||||
OllamaResponseModel ollamaResponseModel = objectMapper.readValue(inputLine, OllamaResponseModel.class);
|
||||
OllamaResponseModel ollamaResponseModel = Utils.getObjectMapper().readValue(inputLine, OllamaResponseModel.class);
|
||||
queue.add(ollamaResponseModel.getResponse());
|
||||
if (!ollamaResponseModel.getDone()) {
|
||||
response.append(ollamaResponseModel.getResponse());
|
||||
|
@ -1,9 +1,6 @@
|
||||
package io.github.amithkoujalgi.ollama4j.core.models;
|
||||
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class OllamaRequestModel {
|
||||
private String model;
|
||||
private String prompt;
|
||||
@ -28,16 +25,4 @@ public class OllamaRequestModel {
|
||||
public void setPrompt(String prompt) {
|
||||
this.prompt = prompt;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
try {
|
||||
return new ObjectMapper()
|
||||
.writer()
|
||||
.withDefaultPrettyPrinter()
|
||||
.writeValueAsString(this);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,9 @@
|
||||
package io.github.amithkoujalgi.ollama4j.core.utils;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
public class Utils {
|
||||
public static ObjectMapper getObjectMapper() {
|
||||
return new ObjectMapper();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user