mirror of
https://github.com/amithkoujalgi/ollama4j.git
synced 2025-10-14 01:18:58 +02:00
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:
parent
4df4ea1930
commit
b1d3ee54a5
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
@SuppressWarnings("SpellCheckingInspection")
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class CustomModelFileContentsRequest {
|
||||
|
@ -9,6 +9,8 @@
|
||||
package io.github.ollama4j.utils;
|
||||
|
||||
public final class Constants {
|
||||
private Constants() {}
|
||||
|
||||
public static final class HttpConstants {
|
||||
private HttpConstants() {}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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");
|
||||
}
|
||||
|
@ -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!")
|
||||
|
Loading…
x
Reference in New Issue
Block a user