mirror of
https://github.com/amithkoujalgi/ollama4j.git
synced 2025-10-14 09:28: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 {
|
try {
|
||||||
String prettyJson =
|
String prettyJson =
|
||||||
Utils.getObjectMapper()
|
Utils.toJSON(Utils.getObjectMapper().readValue(jsonData, Object.class));
|
||||||
.writerWithDefaultPrettyPrinter()
|
|
||||||
.writeValueAsString(
|
|
||||||
Utils.getObjectMapper().readValue(jsonData, Object.class));
|
|
||||||
LOG.debug("Asking model:\n{}", prettyJson);
|
LOG.debug("Asking model:\n{}", prettyJson);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LOG.debug("Asking model: {}", jsonData);
|
LOG.debug("Asking model: {}", jsonData);
|
||||||
@ -730,11 +727,7 @@ public class OllamaAPI {
|
|||||||
LOG.debug("Model response:\n{}", ollamaResult);
|
LOG.debug("Model response:\n{}", ollamaResult);
|
||||||
return ollamaResult;
|
return ollamaResult;
|
||||||
} else {
|
} else {
|
||||||
LOG.debug(
|
LOG.debug("Model response:\n{}", Utils.toJSON(responseBody));
|
||||||
"Model response:\n{}",
|
|
||||||
Utils.getObjectMapper()
|
|
||||||
.writerWithDefaultPrettyPrinter()
|
|
||||||
.writeValueAsString(responseBody));
|
|
||||||
throw new OllamaBaseException(statusCode + " - " + responseBody);
|
throw new OllamaBaseException(statusCode + " - " + responseBody);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
|
|||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
|
@SuppressWarnings("SpellCheckingInspection")
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class CustomModelFileContentsRequest {
|
public class CustomModelFileContentsRequest {
|
||||||
|
@ -9,6 +9,8 @@
|
|||||||
package io.github.ollama4j.utils;
|
package io.github.ollama4j.utils;
|
||||||
|
|
||||||
public final class Constants {
|
public final class Constants {
|
||||||
|
private Constants() {}
|
||||||
|
|
||||||
public static final class HttpConstants {
|
public static final class HttpConstants {
|
||||||
private HttpConstants() {}
|
private HttpConstants() {}
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
*/
|
*/
|
||||||
package io.github.ollama4j.utils;
|
package io.github.ollama4j.utils;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -22,6 +23,8 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
public class Utils {
|
public class Utils {
|
||||||
|
private Utils() {}
|
||||||
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(Utils.class);
|
private static final Logger LOG = LoggerFactory.getLogger(Utils.class);
|
||||||
|
|
||||||
private static ObjectMapper objectMapper;
|
private static ObjectMapper objectMapper;
|
||||||
@ -77,4 +80,8 @@ public class Utils {
|
|||||||
ClassLoader classLoader = Utils.class.getClassLoader();
|
ClassLoader classLoader = Utils.class.getClassLoader();
|
||||||
return new File(Objects.requireNonNull(classLoader.getResource(fileName)).getFile());
|
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
|
@Test
|
||||||
@Order(1)
|
@Order(1)
|
||||||
void testPing()
|
void testPing() throws OllamaBaseException {
|
||||||
throws URISyntaxException, IOException, OllamaBaseException, InterruptedException {
|
|
||||||
boolean pingResponse = api.ping();
|
boolean pingResponse = api.ping();
|
||||||
assertTrue(pingResponse, "Ping should return true");
|
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.ToolProperty;
|
||||||
import io.github.ollama4j.tools.annotations.ToolSpec;
|
import io.github.ollama4j.tools.annotations.ToolSpec;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
public class AnnotatedTool {
|
public class AnnotatedTool {
|
||||||
|
|
||||||
@ -18,7 +19,8 @@ public class AnnotatedTool {
|
|||||||
public String computeImportantConstant(
|
public String computeImportantConstant(
|
||||||
@ToolProperty(name = "noOfDigits", desc = "Number of digits that shall be returned")
|
@ToolProperty(name = "noOfDigits", desc = "Number of digits that shall be returned")
|
||||||
Integer noOfDigits) {
|
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!")
|
@ToolSpec(desc = "Says hello to a friend!")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user