Refactor JSON logging and utility methods

Replaced manual pretty-printing of JSON in OllamaAPI with a new Utils.toJSON method for cleaner logging. Added private constructors to utility classes to prevent instantiation. Updated test and sample code for improved clarity and randomness.
This commit is contained in:
amithkoujalgi 2025-09-17 20:05:18 +05:30
parent 4df4ea1930
commit b1d3ee54a5
No known key found for this signature in database
GPG Key ID: E29A37746AF94B70
6 changed files with 16 additions and 12 deletions

View File

@ -694,10 +694,7 @@ public class OllamaAPI {
try {
String prettyJson =
Utils.getObjectMapper()
.writerWithDefaultPrettyPrinter()
.writeValueAsString(
Utils.getObjectMapper().readValue(jsonData, Object.class));
Utils.toJSON(Utils.getObjectMapper().readValue(jsonData, Object.class));
LOG.debug("Asking model:\n{}", prettyJson);
} catch (Exception e) {
LOG.debug("Asking model: {}", jsonData);
@ -730,11 +727,7 @@ public class OllamaAPI {
LOG.debug("Model response:\n{}", ollamaResult);
return ollamaResult;
} else {
LOG.debug(
"Model response:\n{}",
Utils.getObjectMapper()
.writerWithDefaultPrettyPrinter()
.writeValueAsString(responseBody));
LOG.debug("Model response:\n{}", Utils.toJSON(responseBody));
throw new OllamaBaseException(statusCode + " - " + responseBody);
}
}

View File

@ -14,6 +14,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.AllArgsConstructor;
import lombok.Data;
@SuppressWarnings("SpellCheckingInspection")
@Data
@AllArgsConstructor
public class CustomModelFileContentsRequest {

View File

@ -9,6 +9,8 @@
package io.github.ollama4j.utils;
public final class Constants {
private Constants() {}
public static final class HttpConstants {
private HttpConstants() {}

View File

@ -8,6 +8,7 @@
*/
package io.github.ollama4j.utils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.io.File;
@ -22,6 +23,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Utils {
private Utils() {}
private static final Logger LOG = LoggerFactory.getLogger(Utils.class);
private static ObjectMapper objectMapper;
@ -77,4 +80,8 @@ public class Utils {
ClassLoader classLoader = Utils.class.getClassLoader();
return new File(Objects.requireNonNull(classLoader.getResource(fileName)).getFile());
}
public static String toJSON(Object object) throws JsonProcessingException {
return Utils.getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(object);
}
}

View File

@ -114,8 +114,7 @@ class OllamaAPIIntegrationTest {
@Test
@Order(1)
void testPing()
throws URISyntaxException, IOException, OllamaBaseException, InterruptedException {
void testPing() throws OllamaBaseException {
boolean pingResponse = api.ping();
assertTrue(pingResponse, "Ping should return true");
}

View File

@ -11,6 +11,7 @@ package io.github.ollama4j.samples;
import io.github.ollama4j.tools.annotations.ToolProperty;
import io.github.ollama4j.tools.annotations.ToolSpec;
import java.math.BigDecimal;
import java.util.Random;
public class AnnotatedTool {
@ -18,7 +19,8 @@ public class AnnotatedTool {
public String computeImportantConstant(
@ToolProperty(name = "noOfDigits", desc = "Number of digits that shall be returned")
Integer noOfDigits) {
return BigDecimal.valueOf((long) (Math.random() * 1000000L), noOfDigits).toString();
return BigDecimal.valueOf((long) (new Random().nextLong() * 1000000L), noOfDigits)
.toString();
}
@ToolSpec(desc = "Says hello to a friend!")