forked from Mirror/ollama4j
		
	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:
		@@ -373,7 +373,6 @@ public class CouchbaseToolCallingExample {
 | 
			
		||||
        String modelName = Utilities.getFromConfig("tools_model_mistral");
 | 
			
		||||
 | 
			
		||||
        OllamaAPI ollamaAPI = new OllamaAPI(host);
 | 
			
		||||
        ollamaAPI.setVerbose(false);
 | 
			
		||||
        ollamaAPI.setRequestTimeoutSeconds(60);
 | 
			
		||||
 | 
			
		||||
        Tools.ToolSpecification callSignFinderToolSpec = getCallSignFinderToolSpec(cluster, bucketName);
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
```
 | 
			
		||||
@@ -139,8 +139,6 @@ public class OllamaAPITest {
 | 
			
		||||
 | 
			
		||||
        OllamaAPI ollamaAPI = new OllamaAPI(host);
 | 
			
		||||
 | 
			
		||||
        ollamaAPI.setVerbose(true);
 | 
			
		||||
 | 
			
		||||
        boolean isOllamaServerReachable = ollamaAPI.ping();
 | 
			
		||||
 | 
			
		||||
        System.out.println("Is Ollama server running: " + isOllamaServerReachable);
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,6 @@
 | 
			
		||||
package io.github.ollama4j;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.core.JsonParseException;
 | 
			
		||||
import com.fasterxml.jackson.databind.JsonNode;
 | 
			
		||||
import com.fasterxml.jackson.databind.ObjectMapper;
 | 
			
		||||
import io.github.ollama4j.exceptions.OllamaBaseException;
 | 
			
		||||
import io.github.ollama4j.exceptions.RoleNotFoundException;
 | 
			
		||||
@@ -71,16 +70,6 @@ public class OllamaAPI {
 | 
			
		||||
    @Setter
 | 
			
		||||
    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.
 | 
			
		||||
     * <p>
 | 
			
		||||
@@ -123,9 +112,7 @@ public class OllamaAPI {
 | 
			
		||||
        } else {
 | 
			
		||||
            this.host = host;
 | 
			
		||||
        }
 | 
			
		||||
        if (this.verbose) {
 | 
			
		||||
            logger.info("Ollama API initialized with host: {}", this.host);
 | 
			
		||||
        }
 | 
			
		||||
        logger.info("Ollama API initialized with host: {}", this.host);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -502,9 +489,7 @@ public class OllamaAPI {
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    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.
 | 
			
		||||
                        if ("success".equalsIgnoreCase(modelPullResponse.getStatus())) {
 | 
			
		||||
                            success = true;
 | 
			
		||||
@@ -625,9 +610,7 @@ public class OllamaAPI {
 | 
			
		||||
        if (responseString.contains("error")) {
 | 
			
		||||
            throw new OllamaBaseException(responseString);
 | 
			
		||||
        }
 | 
			
		||||
        if (verbose) {
 | 
			
		||||
            logger.info(responseString);
 | 
			
		||||
        }
 | 
			
		||||
        logger.debug(responseString);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -663,9 +646,7 @@ public class OllamaAPI {
 | 
			
		||||
        if (responseString.contains("error")) {
 | 
			
		||||
            throw new OllamaBaseException(responseString);
 | 
			
		||||
        }
 | 
			
		||||
        if (verbose) {
 | 
			
		||||
            logger.info(responseString);
 | 
			
		||||
        }
 | 
			
		||||
        logger.debug(responseString);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -697,9 +678,7 @@ public class OllamaAPI {
 | 
			
		||||
        if (responseString.contains("error")) {
 | 
			
		||||
            throw new OllamaBaseException(responseString);
 | 
			
		||||
        }
 | 
			
		||||
        if (verbose) {
 | 
			
		||||
            logger.info(responseString);
 | 
			
		||||
        }
 | 
			
		||||
        logger.debug(responseString);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -967,15 +946,14 @@ public class OllamaAPI {
 | 
			
		||||
                .header(Constants.HttpConstants.HEADER_KEY_CONTENT_TYPE, Constants.HttpConstants.APPLICATION_JSON)
 | 
			
		||||
                .POST(HttpRequest.BodyPublishers.ofString(jsonData)).build();
 | 
			
		||||
 | 
			
		||||
        if (verbose) {
 | 
			
		||||
            try {
 | 
			
		||||
                String prettyJson = Utils.getObjectMapper().writerWithDefaultPrettyPrinter()
 | 
			
		||||
                        .writeValueAsString(Utils.getObjectMapper().readValue(jsonData, Object.class));
 | 
			
		||||
                logger.info("Asking model:\n{}", prettyJson);
 | 
			
		||||
            } catch (Exception e) {
 | 
			
		||||
                logger.info("Asking model: {}", jsonData);
 | 
			
		||||
            }
 | 
			
		||||
        try {
 | 
			
		||||
            String prettyJson = Utils.getObjectMapper().writerWithDefaultPrettyPrinter()
 | 
			
		||||
                    .writeValueAsString(Utils.getObjectMapper().readValue(jsonData, Object.class));
 | 
			
		||||
            logger.debug("Asking model:\n{}", prettyJson);
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            logger.debug("Asking model: {}", jsonData);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
 | 
			
		||||
        int statusCode = response.statusCode();
 | 
			
		||||
        String responseBody = response.body();
 | 
			
		||||
@@ -996,15 +974,11 @@ public class OllamaAPI {
 | 
			
		||||
            ollamaResult.setPromptEvalDuration(structuredResult.getPromptEvalDuration());
 | 
			
		||||
            ollamaResult.setEvalCount(structuredResult.getEvalCount());
 | 
			
		||||
            ollamaResult.setEvalDuration(structuredResult.getEvalDuration());
 | 
			
		||||
            if (verbose) {
 | 
			
		||||
                logger.info("Model response:\n{}", ollamaResult);
 | 
			
		||||
            }
 | 
			
		||||
            logger.debug("Model response:\n{}", ollamaResult);
 | 
			
		||||
            return ollamaResult;
 | 
			
		||||
        } else {
 | 
			
		||||
            if (verbose) {
 | 
			
		||||
                logger.info("Model response:\n{}",
 | 
			
		||||
                        Utils.getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(responseBody));
 | 
			
		||||
            }
 | 
			
		||||
            logger.debug("Model response:\n{}",
 | 
			
		||||
                    Utils.getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(responseBody));
 | 
			
		||||
            throw new OllamaBaseException(statusCode + " - " + responseBody);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
@@ -1055,7 +1029,7 @@ public class OllamaAPI {
 | 
			
		||||
        if (!toolsResponse.isEmpty()) {
 | 
			
		||||
            try {
 | 
			
		||||
                // Try to parse the string to see if it's a valid JSON
 | 
			
		||||
                JsonNode jsonNode = objectMapper.readTree(toolsResponse);
 | 
			
		||||
                objectMapper.readTree(toolsResponse);
 | 
			
		||||
            } catch (JsonParseException e) {
 | 
			
		||||
                logger.warn("Response from model does not contain any tool calls. Returning the response as is.");
 | 
			
		||||
                return toolResult;
 | 
			
		||||
@@ -1361,8 +1335,7 @@ public class OllamaAPI {
 | 
			
		||||
     */
 | 
			
		||||
    public OllamaChatResult chatStreaming(OllamaChatRequest request, OllamaTokenHandler tokenHandler)
 | 
			
		||||
            throws OllamaBaseException, IOException, InterruptedException, ToolInvocationException {
 | 
			
		||||
        OllamaChatEndpointCaller requestCaller = new OllamaChatEndpointCaller(host, auth, requestTimeoutSeconds,
 | 
			
		||||
                verbose);
 | 
			
		||||
        OllamaChatEndpointCaller requestCaller = new OllamaChatEndpointCaller(host, auth, requestTimeoutSeconds);
 | 
			
		||||
        OllamaChatResult result;
 | 
			
		||||
 | 
			
		||||
        // add all registered tools to Request
 | 
			
		||||
@@ -1417,9 +1390,7 @@ public class OllamaAPI {
 | 
			
		||||
     */
 | 
			
		||||
    public void registerTool(Tools.ToolSpecification toolSpecification) {
 | 
			
		||||
        toolRegistry.addTool(toolSpecification.getFunctionName(), toolSpecification);
 | 
			
		||||
        if (this.verbose) {
 | 
			
		||||
            logger.debug("Registered tool: {}", toolSpecification.getFunctionName());
 | 
			
		||||
        }
 | 
			
		||||
        logger.debug("Registered tool: {}", toolSpecification.getFunctionName());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -1444,9 +1415,7 @@ public class OllamaAPI {
 | 
			
		||||
     */
 | 
			
		||||
    public void deregisterTools() {
 | 
			
		||||
        toolRegistry.clear();
 | 
			
		||||
        if (this.verbose) {
 | 
			
		||||
            logger.debug("All tools have been deregistered.");
 | 
			
		||||
        }
 | 
			
		||||
        logger.debug("All tools have been deregistered.");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -1621,8 +1590,7 @@ public class OllamaAPI {
 | 
			
		||||
    private OllamaResult generateSyncForOllamaRequestModel(OllamaGenerateRequest ollamaRequestModel,
 | 
			
		||||
                                                           OllamaStreamHandler thinkingStreamHandler, OllamaStreamHandler responseStreamHandler)
 | 
			
		||||
            throws OllamaBaseException, IOException, InterruptedException {
 | 
			
		||||
        OllamaGenerateEndpointCaller requestCaller = new OllamaGenerateEndpointCaller(host, auth, requestTimeoutSeconds,
 | 
			
		||||
                verbose);
 | 
			
		||||
        OllamaGenerateEndpointCaller requestCaller = new OllamaGenerateEndpointCaller(host, auth, requestTimeoutSeconds);
 | 
			
		||||
        OllamaResult result;
 | 
			
		||||
        if (responseStreamHandler != null) {
 | 
			
		||||
            ollamaRequestModel.setStream(true);
 | 
			
		||||
@@ -1663,9 +1631,7 @@ public class OllamaAPI {
 | 
			
		||||
            String methodName = toolFunctionCallSpec.getName();
 | 
			
		||||
            Map<String, Object> arguments = toolFunctionCallSpec.getArguments();
 | 
			
		||||
            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) {
 | 
			
		||||
                throw new ToolNotFoundException(
 | 
			
		||||
                        "No such tool: " + methodName + ". Please register the tool before invoking it.");
 | 
			
		||||
 
 | 
			
		||||
@@ -31,8 +31,8 @@ public class OllamaChatEndpointCaller extends OllamaEndpointCaller {
 | 
			
		||||
 | 
			
		||||
    private OllamaTokenHandler tokenHandler;
 | 
			
		||||
 | 
			
		||||
    public OllamaChatEndpointCaller(String host, Auth auth, long requestTimeoutSeconds, boolean verbose) {
 | 
			
		||||
        super(host, auth, requestTimeoutSeconds, verbose);
 | 
			
		||||
    public OllamaChatEndpointCaller(String host, Auth auth, long requestTimeoutSeconds) {
 | 
			
		||||
        super(host, auth, requestTimeoutSeconds);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
@@ -91,7 +91,7 @@ public class OllamaChatEndpointCaller extends OllamaEndpointCaller {
 | 
			
		||||
                        .POST(
 | 
			
		||||
                                body.getBodyPublisher());
 | 
			
		||||
        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());
 | 
			
		||||
 | 
			
		||||
@@ -150,7 +150,7 @@ public class OllamaChatEndpointCaller extends OllamaEndpointCaller {
 | 
			
		||||
            }
 | 
			
		||||
            OllamaChatResult ollamaResult =
 | 
			
		||||
                    new OllamaChatResult(ollamaChatResponseModel, body.getMessages());
 | 
			
		||||
            if (isVerbose()) LOG.info("Model response: " + ollamaResult);
 | 
			
		||||
            LOG.debug("Model response: {}", ollamaResult);
 | 
			
		||||
            return ollamaResult;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,10 +1,7 @@
 | 
			
		||||
package io.github.ollama4j.models.request;
 | 
			
		||||
 | 
			
		||||
import io.github.ollama4j.OllamaAPI;
 | 
			
		||||
import io.github.ollama4j.utils.Constants;
 | 
			
		||||
import lombok.Getter;
 | 
			
		||||
import org.slf4j.Logger;
 | 
			
		||||
import org.slf4j.LoggerFactory;
 | 
			
		||||
 | 
			
		||||
import java.net.URI;
 | 
			
		||||
import java.net.http.HttpRequest;
 | 
			
		||||
@@ -16,18 +13,14 @@ import java.time.Duration;
 | 
			
		||||
@Getter
 | 
			
		||||
public abstract class OllamaEndpointCaller {
 | 
			
		||||
 | 
			
		||||
    private static final Logger LOG = LoggerFactory.getLogger(OllamaAPI.class);
 | 
			
		||||
 | 
			
		||||
    private final String host;
 | 
			
		||||
    private final Auth auth;
 | 
			
		||||
    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.auth = auth;
 | 
			
		||||
        this.requestTimeoutSeconds = requestTimeoutSeconds;
 | 
			
		||||
        this.verbose = verbose;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected abstract String getEndpointSuffix();
 | 
			
		||||
 
 | 
			
		||||
@@ -29,8 +29,8 @@ public class OllamaGenerateEndpointCaller extends OllamaEndpointCaller {
 | 
			
		||||
 | 
			
		||||
    private OllamaGenerateStreamObserver responseStreamObserver;
 | 
			
		||||
 | 
			
		||||
    public OllamaGenerateEndpointCaller(String host, Auth basicAuth, long requestTimeoutSeconds, boolean verbose) {
 | 
			
		||||
        super(host, basicAuth, requestTimeoutSeconds, verbose);
 | 
			
		||||
    public OllamaGenerateEndpointCaller(String host, Auth basicAuth, long requestTimeoutSeconds) {
 | 
			
		||||
        super(host, basicAuth, requestTimeoutSeconds);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
@@ -80,7 +80,7 @@ public class OllamaGenerateEndpointCaller extends OllamaEndpointCaller {
 | 
			
		||||
        URI uri = URI.create(getHost() + getEndpointSuffix());
 | 
			
		||||
        HttpRequest.Builder requestBuilder = getRequestBuilderDefault(uri).POST(body.getBodyPublisher());
 | 
			
		||||
        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());
 | 
			
		||||
 | 
			
		||||
        int statusCode = response.statusCode();
 | 
			
		||||
@@ -132,7 +132,7 @@ public class OllamaGenerateEndpointCaller extends OllamaEndpointCaller {
 | 
			
		||||
            ollamaResult.setEvalCount(ollamaGenerateResponseModel.getEvalCount());
 | 
			
		||||
            ollamaResult.setEvalDuration(ollamaGenerateResponseModel.getEvalDuration());
 | 
			
		||||
 | 
			
		||||
            if (isVerbose()) LOG.info("Model response: {}", ollamaResult);
 | 
			
		||||
            LOG.debug("Model response: {}", ollamaResult);
 | 
			
		||||
            return ollamaResult;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -75,7 +75,6 @@ class OllamaAPIIntegrationTest {
 | 
			
		||||
            api = new OllamaAPI("http://" + ollama.getHost() + ":" + ollama.getMappedPort(internalPort));
 | 
			
		||||
        }
 | 
			
		||||
        api.setRequestTimeoutSeconds(120);
 | 
			
		||||
        api.setVerbose(true);
 | 
			
		||||
        api.setNumberOfRetriesForModelPull(5);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -61,7 +61,6 @@ public class WithAuth {
 | 
			
		||||
 | 
			
		||||
        api = new OllamaAPI("http://" + nginx.getHost() + ":" + nginx.getMappedPort(NGINX_PORT));
 | 
			
		||||
        api.setRequestTimeoutSeconds(120);
 | 
			
		||||
        api.setVerbose(true);
 | 
			
		||||
        api.setNumberOfRetriesForModelPull(3);
 | 
			
		||||
 | 
			
		||||
        String ollamaUrl = "http://" + ollama.getHost() + ":" + ollama.getMappedPort(OLLAMA_INTERNAL_PORT);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user