Refactor verbosity handling in OllamaAPI

- Removed the verbose logging feature from the OllamaAPI class and related classes.
- Updated logging statements to use debug level instead of info for model requests and responses.
- Cleaned up related test cases to reflect the removal of verbosity settings.
This commit is contained in:
amithkoujalgi 2025-09-10 18:02:28 +05:30
parent 9036d9e7c6
commit 773cb4af78
No known key found for this signature in database
GPG Key ID: E29A37746AF94B70
9 changed files with 30 additions and 101 deletions

View File

@ -373,7 +373,6 @@ public class CouchbaseToolCallingExample {
String modelName = Utilities.getFromConfig("tools_model_mistral"); String modelName = Utilities.getFromConfig("tools_model_mistral");
OllamaAPI ollamaAPI = new OllamaAPI(host); OllamaAPI ollamaAPI = new OllamaAPI(host);
ollamaAPI.setVerbose(false);
ollamaAPI.setRequestTimeoutSeconds(60); ollamaAPI.setRequestTimeoutSeconds(60);
Tools.ToolSpecification callSignFinderToolSpec = getCallSignFinderToolSpec(cluster, bucketName); Tools.ToolSpecification callSignFinderToolSpec = getCallSignFinderToolSpec(cluster, bucketName);

View File

@ -1,25 +0,0 @@
---
sidebar_position: 1
---
# Set Verbosity
This API lets you set the verbosity of the Ollama client.
## Try asking a question about the model.
```java
import io.github.ollama4j.OllamaAPI;
public class Main {
public static void main(String[] args) {
String host = "http://localhost:11434/";
OllamaAPI ollamaAPI = new OllamaAPI(host);
ollamaAPI.setVerbose(true);
}
}
```

View File

@ -139,8 +139,6 @@ public class OllamaAPITest {
OllamaAPI ollamaAPI = new OllamaAPI(host); OllamaAPI ollamaAPI = new OllamaAPI(host);
ollamaAPI.setVerbose(true);
boolean isOllamaServerReachable = ollamaAPI.ping(); boolean isOllamaServerReachable = ollamaAPI.ping();
System.out.println("Is Ollama server running: " + isOllamaServerReachable); System.out.println("Is Ollama server running: " + isOllamaServerReachable);

View File

@ -1,7 +1,6 @@
package io.github.ollama4j; package io.github.ollama4j;
import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.ollama4j.exceptions.OllamaBaseException; import io.github.ollama4j.exceptions.OllamaBaseException;
import io.github.ollama4j.exceptions.RoleNotFoundException; import io.github.ollama4j.exceptions.RoleNotFoundException;
@ -71,16 +70,6 @@ public class OllamaAPI {
@Setter @Setter
private long requestTimeoutSeconds = 10; private long requestTimeoutSeconds = 10;
/**
* Enables or disables verbose logging of responses.
* <p>
* If set to {@code true}, the API will log detailed information about requests
* and responses.
* Default is {@code true}.
*/
@Setter
private boolean verbose = true;
/** /**
* The maximum number of retries for tool calls during chat interactions. * The maximum number of retries for tool calls during chat interactions.
* <p> * <p>
@ -123,10 +112,8 @@ public class OllamaAPI {
} else { } else {
this.host = host; this.host = host;
} }
if (this.verbose) {
logger.info("Ollama API initialized with host: {}", this.host); logger.info("Ollama API initialized with host: {}", this.host);
} }
}
/** /**
* Set basic authentication for accessing Ollama server that's behind a * Set basic authentication for accessing Ollama server that's behind a
@ -502,9 +489,7 @@ public class OllamaAPI {
} }
if (modelPullResponse.getStatus() != null) { if (modelPullResponse.getStatus() != null) {
if (verbose) {
logger.info("{}: {}", modelName, modelPullResponse.getStatus()); logger.info("{}: {}", modelName, modelPullResponse.getStatus());
}
// Check if status is "success" and set success flag to true. // Check if status is "success" and set success flag to true.
if ("success".equalsIgnoreCase(modelPullResponse.getStatus())) { if ("success".equalsIgnoreCase(modelPullResponse.getStatus())) {
success = true; success = true;
@ -625,9 +610,7 @@ public class OllamaAPI {
if (responseString.contains("error")) { if (responseString.contains("error")) {
throw new OllamaBaseException(responseString); throw new OllamaBaseException(responseString);
} }
if (verbose) { logger.debug(responseString);
logger.info(responseString);
}
} }
/** /**
@ -663,9 +646,7 @@ public class OllamaAPI {
if (responseString.contains("error")) { if (responseString.contains("error")) {
throw new OllamaBaseException(responseString); throw new OllamaBaseException(responseString);
} }
if (verbose) { logger.debug(responseString);
logger.info(responseString);
}
} }
/** /**
@ -697,9 +678,7 @@ public class OllamaAPI {
if (responseString.contains("error")) { if (responseString.contains("error")) {
throw new OllamaBaseException(responseString); throw new OllamaBaseException(responseString);
} }
if (verbose) { logger.debug(responseString);
logger.info(responseString);
}
} }
/** /**
@ -967,15 +946,14 @@ public class OllamaAPI {
.header(Constants.HttpConstants.HEADER_KEY_CONTENT_TYPE, Constants.HttpConstants.APPLICATION_JSON) .header(Constants.HttpConstants.HEADER_KEY_CONTENT_TYPE, Constants.HttpConstants.APPLICATION_JSON)
.POST(HttpRequest.BodyPublishers.ofString(jsonData)).build(); .POST(HttpRequest.BodyPublishers.ofString(jsonData)).build();
if (verbose) {
try { try {
String prettyJson = Utils.getObjectMapper().writerWithDefaultPrettyPrinter() String prettyJson = Utils.getObjectMapper().writerWithDefaultPrettyPrinter()
.writeValueAsString(Utils.getObjectMapper().readValue(jsonData, Object.class)); .writeValueAsString(Utils.getObjectMapper().readValue(jsonData, Object.class));
logger.info("Asking model:\n{}", prettyJson); logger.debug("Asking model:\n{}", prettyJson);
} catch (Exception e) { } catch (Exception e) {
logger.info("Asking model: {}", jsonData); logger.debug("Asking model: {}", jsonData);
}
} }
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
int statusCode = response.statusCode(); int statusCode = response.statusCode();
String responseBody = response.body(); String responseBody = response.body();
@ -996,15 +974,11 @@ public class OllamaAPI {
ollamaResult.setPromptEvalDuration(structuredResult.getPromptEvalDuration()); ollamaResult.setPromptEvalDuration(structuredResult.getPromptEvalDuration());
ollamaResult.setEvalCount(structuredResult.getEvalCount()); ollamaResult.setEvalCount(structuredResult.getEvalCount());
ollamaResult.setEvalDuration(structuredResult.getEvalDuration()); ollamaResult.setEvalDuration(structuredResult.getEvalDuration());
if (verbose) { logger.debug("Model response:\n{}", ollamaResult);
logger.info("Model response:\n{}", ollamaResult);
}
return ollamaResult; return ollamaResult;
} else { } else {
if (verbose) { logger.debug("Model response:\n{}",
logger.info("Model response:\n{}",
Utils.getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(responseBody)); Utils.getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(responseBody));
}
throw new OllamaBaseException(statusCode + " - " + responseBody); throw new OllamaBaseException(statusCode + " - " + responseBody);
} }
} }
@ -1055,7 +1029,7 @@ public class OllamaAPI {
if (!toolsResponse.isEmpty()) { if (!toolsResponse.isEmpty()) {
try { try {
// Try to parse the string to see if it's a valid JSON // Try to parse the string to see if it's a valid JSON
JsonNode jsonNode = objectMapper.readTree(toolsResponse); objectMapper.readTree(toolsResponse);
} catch (JsonParseException e) { } catch (JsonParseException e) {
logger.warn("Response from model does not contain any tool calls. Returning the response as is."); logger.warn("Response from model does not contain any tool calls. Returning the response as is.");
return toolResult; return toolResult;
@ -1361,8 +1335,7 @@ public class OllamaAPI {
*/ */
public OllamaChatResult chatStreaming(OllamaChatRequest request, OllamaTokenHandler tokenHandler) public OllamaChatResult chatStreaming(OllamaChatRequest request, OllamaTokenHandler tokenHandler)
throws OllamaBaseException, IOException, InterruptedException, ToolInvocationException { throws OllamaBaseException, IOException, InterruptedException, ToolInvocationException {
OllamaChatEndpointCaller requestCaller = new OllamaChatEndpointCaller(host, auth, requestTimeoutSeconds, OllamaChatEndpointCaller requestCaller = new OllamaChatEndpointCaller(host, auth, requestTimeoutSeconds);
verbose);
OllamaChatResult result; OllamaChatResult result;
// add all registered tools to Request // add all registered tools to Request
@ -1417,10 +1390,8 @@ public class OllamaAPI {
*/ */
public void registerTool(Tools.ToolSpecification toolSpecification) { public void registerTool(Tools.ToolSpecification toolSpecification) {
toolRegistry.addTool(toolSpecification.getFunctionName(), toolSpecification); toolRegistry.addTool(toolSpecification.getFunctionName(), toolSpecification);
if (this.verbose) {
logger.debug("Registered tool: {}", toolSpecification.getFunctionName()); logger.debug("Registered tool: {}", toolSpecification.getFunctionName());
} }
}
/** /**
* Registers multiple tools in the tool registry using a list of tool * Registers multiple tools in the tool registry using a list of tool
@ -1444,10 +1415,8 @@ public class OllamaAPI {
*/ */
public void deregisterTools() { public void deregisterTools() {
toolRegistry.clear(); toolRegistry.clear();
if (this.verbose) {
logger.debug("All tools have been deregistered."); logger.debug("All tools have been deregistered.");
} }
}
/** /**
* Registers tools based on the annotations found on the methods of the caller's * Registers tools based on the annotations found on the methods of the caller's
@ -1621,8 +1590,7 @@ public class OllamaAPI {
private OllamaResult generateSyncForOllamaRequestModel(OllamaGenerateRequest ollamaRequestModel, private OllamaResult generateSyncForOllamaRequestModel(OllamaGenerateRequest ollamaRequestModel,
OllamaStreamHandler thinkingStreamHandler, OllamaStreamHandler responseStreamHandler) OllamaStreamHandler thinkingStreamHandler, OllamaStreamHandler responseStreamHandler)
throws OllamaBaseException, IOException, InterruptedException { throws OllamaBaseException, IOException, InterruptedException {
OllamaGenerateEndpointCaller requestCaller = new OllamaGenerateEndpointCaller(host, auth, requestTimeoutSeconds, OllamaGenerateEndpointCaller requestCaller = new OllamaGenerateEndpointCaller(host, auth, requestTimeoutSeconds);
verbose);
OllamaResult result; OllamaResult result;
if (responseStreamHandler != null) { if (responseStreamHandler != null) {
ollamaRequestModel.setStream(true); ollamaRequestModel.setStream(true);
@ -1663,9 +1631,7 @@ public class OllamaAPI {
String methodName = toolFunctionCallSpec.getName(); String methodName = toolFunctionCallSpec.getName();
Map<String, Object> arguments = toolFunctionCallSpec.getArguments(); Map<String, Object> arguments = toolFunctionCallSpec.getArguments();
ToolFunction function = toolRegistry.getToolFunction(methodName); ToolFunction function = toolRegistry.getToolFunction(methodName);
if (verbose) {
logger.debug("Invoking function {} with arguments {}", methodName, arguments); logger.debug("Invoking function {} with arguments {}", methodName, arguments);
}
if (function == null) { if (function == null) {
throw new ToolNotFoundException( throw new ToolNotFoundException(
"No such tool: " + methodName + ". Please register the tool before invoking it."); "No such tool: " + methodName + ". Please register the tool before invoking it.");

View File

@ -31,8 +31,8 @@ public class OllamaChatEndpointCaller extends OllamaEndpointCaller {
private OllamaTokenHandler tokenHandler; private OllamaTokenHandler tokenHandler;
public OllamaChatEndpointCaller(String host, Auth auth, long requestTimeoutSeconds, boolean verbose) { public OllamaChatEndpointCaller(String host, Auth auth, long requestTimeoutSeconds) {
super(host, auth, requestTimeoutSeconds, verbose); super(host, auth, requestTimeoutSeconds);
} }
@Override @Override
@ -91,7 +91,7 @@ public class OllamaChatEndpointCaller extends OllamaEndpointCaller {
.POST( .POST(
body.getBodyPublisher()); body.getBodyPublisher());
HttpRequest request = requestBuilder.build(); HttpRequest request = requestBuilder.build();
if (isVerbose()) LOG.info("Asking model: {}", body); LOG.debug("Asking model: {}", body);
HttpResponse<InputStream> response = HttpResponse<InputStream> response =
httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream()); httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
@ -150,7 +150,7 @@ public class OllamaChatEndpointCaller extends OllamaEndpointCaller {
} }
OllamaChatResult ollamaResult = OllamaChatResult ollamaResult =
new OllamaChatResult(ollamaChatResponseModel, body.getMessages()); new OllamaChatResult(ollamaChatResponseModel, body.getMessages());
if (isVerbose()) LOG.info("Model response: " + ollamaResult); LOG.debug("Model response: {}", ollamaResult);
return ollamaResult; return ollamaResult;
} }
} }

View File

@ -1,10 +1,7 @@
package io.github.ollama4j.models.request; package io.github.ollama4j.models.request;
import io.github.ollama4j.OllamaAPI;
import io.github.ollama4j.utils.Constants; import io.github.ollama4j.utils.Constants;
import lombok.Getter; import lombok.Getter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.URI; import java.net.URI;
import java.net.http.HttpRequest; import java.net.http.HttpRequest;
@ -16,18 +13,14 @@ import java.time.Duration;
@Getter @Getter
public abstract class OllamaEndpointCaller { public abstract class OllamaEndpointCaller {
private static final Logger LOG = LoggerFactory.getLogger(OllamaAPI.class);
private final String host; private final String host;
private final Auth auth; private final Auth auth;
private final long requestTimeoutSeconds; private final long requestTimeoutSeconds;
private final boolean verbose;
public OllamaEndpointCaller(String host, Auth auth, long requestTimeoutSeconds, boolean verbose) { public OllamaEndpointCaller(String host, Auth auth, long requestTimeoutSeconds) {
this.host = host; this.host = host;
this.auth = auth; this.auth = auth;
this.requestTimeoutSeconds = requestTimeoutSeconds; this.requestTimeoutSeconds = requestTimeoutSeconds;
this.verbose = verbose;
} }
protected abstract String getEndpointSuffix(); protected abstract String getEndpointSuffix();

View File

@ -29,8 +29,8 @@ public class OllamaGenerateEndpointCaller extends OllamaEndpointCaller {
private OllamaGenerateStreamObserver responseStreamObserver; private OllamaGenerateStreamObserver responseStreamObserver;
public OllamaGenerateEndpointCaller(String host, Auth basicAuth, long requestTimeoutSeconds, boolean verbose) { public OllamaGenerateEndpointCaller(String host, Auth basicAuth, long requestTimeoutSeconds) {
super(host, basicAuth, requestTimeoutSeconds, verbose); super(host, basicAuth, requestTimeoutSeconds);
} }
@Override @Override
@ -80,7 +80,7 @@ public class OllamaGenerateEndpointCaller extends OllamaEndpointCaller {
URI uri = URI.create(getHost() + getEndpointSuffix()); URI uri = URI.create(getHost() + getEndpointSuffix());
HttpRequest.Builder requestBuilder = getRequestBuilderDefault(uri).POST(body.getBodyPublisher()); HttpRequest.Builder requestBuilder = getRequestBuilderDefault(uri).POST(body.getBodyPublisher());
HttpRequest request = requestBuilder.build(); HttpRequest request = requestBuilder.build();
if (isVerbose()) LOG.info("Asking model: {}", body); LOG.debug("Asking model: {}", body);
HttpResponse<InputStream> response = httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream()); HttpResponse<InputStream> response = httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
int statusCode = response.statusCode(); int statusCode = response.statusCode();
@ -132,7 +132,7 @@ public class OllamaGenerateEndpointCaller extends OllamaEndpointCaller {
ollamaResult.setEvalCount(ollamaGenerateResponseModel.getEvalCount()); ollamaResult.setEvalCount(ollamaGenerateResponseModel.getEvalCount());
ollamaResult.setEvalDuration(ollamaGenerateResponseModel.getEvalDuration()); ollamaResult.setEvalDuration(ollamaGenerateResponseModel.getEvalDuration());
if (isVerbose()) LOG.info("Model response: {}", ollamaResult); LOG.debug("Model response: {}", ollamaResult);
return ollamaResult; return ollamaResult;
} }
} }

View File

@ -75,7 +75,6 @@ class OllamaAPIIntegrationTest {
api = new OllamaAPI("http://" + ollama.getHost() + ":" + ollama.getMappedPort(internalPort)); api = new OllamaAPI("http://" + ollama.getHost() + ":" + ollama.getMappedPort(internalPort));
} }
api.setRequestTimeoutSeconds(120); api.setRequestTimeoutSeconds(120);
api.setVerbose(true);
api.setNumberOfRetriesForModelPull(5); api.setNumberOfRetriesForModelPull(5);
} }

View File

@ -61,7 +61,6 @@ public class WithAuth {
api = new OllamaAPI("http://" + nginx.getHost() + ":" + nginx.getMappedPort(NGINX_PORT)); api = new OllamaAPI("http://" + nginx.getHost() + ":" + nginx.getMappedPort(NGINX_PORT));
api.setRequestTimeoutSeconds(120); api.setRequestTimeoutSeconds(120);
api.setVerbose(true);
api.setNumberOfRetriesForModelPull(3); api.setNumberOfRetriesForModelPull(3);
String ollamaUrl = "http://" + ollama.getHost() + ":" + ollama.getMappedPort(OLLAMA_INTERNAL_PORT); String ollamaUrl = "http://" + ollama.getHost() + ":" + ollama.getMappedPort(OLLAMA_INTERNAL_PORT);