### Verify Ollama Server Installation Source: https://ollama4j.github.io/ollama4j/blog/talk-to-your-data-on-couchbase-via-ollama4j Confirms that the Ollama server is installed and running. The Ollama server is essential for interacting with AI models. ```shell ollama --version ``` -------------------------------- ### Go Code Example for Reading and Printing File Contents Source: https://ollama4j.github.io/ollama4j/apis-generate/prompt-builder This Go code snippet, generated as a response to a prompt, demonstrates how to read a file and print its contents to standard output. It includes error handling for file reading operations. Note that this is an example response and might require adjustments for specific use cases. ```go package main import ( "fmt" "io/ioutil" ) func readFile(fileName string) { file, err := ioutil.ReadFile(fileName) if err != nil { fmt.Fprintln(os.Stderr, "Error reading file:", err.Error()) return } f, _ := ioutil.ReadFile("file.txt") if f != nil { fmt.Println(f.String()) } } ``` -------------------------------- ### Verify Java Installation Source: https://ollama4j.github.io/ollama4j/blog/talk-to-your-data-on-couchbase-via-ollama4j Checks if Java 11 or later is installed on the system. This is a prerequisite for running Ollama4j. ```shell java --version ``` -------------------------------- ### Verify Maven Installation Source: https://ollama4j.github.io/ollama4j/blog/talk-to-your-data-on-couchbase-via-ollama4j Verifies the installation of Apache Maven, a build automation tool used for Java projects. Maven is required to manage project dependencies and build the Ollama4j application. ```shell mvn --version ``` -------------------------------- ### OllamaChatRequestBuilder: Get Instance with Model Source: https://ollama4j.github.io/ollama4j/apidocs/io/github/ollama4j/models/chat/class-use/OllamaChatRequestBuilder Demonstrates how to get a static instance of OllamaChatRequestBuilder, specifying the Ollama model to be used. This is the entry point for building chat requests. ```java OllamaChatRequestBuilder.getInstance("model-name") ``` -------------------------------- ### Couchbase Capella Connection String Example Source: https://ollama4j.github.io/ollama4j/blog/talk-to-your-data-on-couchbase-via-ollama4j This is an example of a public connection string for a Couchbase Capella cluster. It provides the endpoint URL necessary for client applications to access the database. Ensure that the IP addresses are configured in the cluster settings for access. ```text couchbases://cb.uniqueclusteridentifer.cloud.couchbase.com ``` -------------------------------- ### Pull Model Source: https://ollama4j.github.io/ollama4j/apidocs/io/github/ollama4j/OllamaAPI Initiates the download and installation of a specified model from the Ollama server. ```APIDOC ## POST /api/pull ### Description Pulls a model from the Ollama server. ### Method POST ### Endpoint /api/pull ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **name** (String) - Required - The name of the model to pull. ### Request Example ```json { "name": "llama2" } ``` ### Response #### Success Response (200) - **status** (String) - The status of the pull operation. #### Response Example ```json { "status": "success" } ``` ``` -------------------------------- ### Java: Initialize OllamaGenerateRequestBuilder Source: https://ollama4j.github.io/ollama4j/doxygen/html/OllamaGenerateRequestBuilder_8java_source This snippet shows how to get an instance of OllamaGenerateRequestBuilder to start building an Ollama generation request. It requires the model name as input. ```java import io.github.ollama4j.models.generate.OllamaGenerateRequestBuilder; // ... String modelName = "llama2"; OllamaGenerateRequestBuilder requestBuilder = OllamaGenerateRequestBuilder.getInstance(modelName); ``` -------------------------------- ### GET /ps Source: https://ollama4j.github.io/ollama4j/doxygen/html/classio_1_1github_1_1ollama4j_1_1OllamaAPI Provides a list of running models and details about each model currently loaded into memory. ```APIDOC ## GET /ps ### Description Provides a list of running models and details about each model currently loaded into memory. ### Method GET ### Endpoint /ps ### Parameters None ### Request Example None ### Response #### Success Response (200) - **models** (array of objects) - A list of running models with their details. #### Response Example ```json { "models": [ { "name": "llama2:latest", "details": { "parent_model": "", "format": "gguf", "family": "llama", "parameter_size": "7B", "quantization_level": "Q4_0" }, "size": 3500000000, "digest": "f1b4c7b8a6b1d2c3e4f5a6b7c8d9e0f1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7", "memory_used": 3700000000, "modelfile": "FROM llama2\nPARAMETER temperature 1", "created_at": "2023-07-13T10:12:11.390154962Z", "expires_at": null, "expired": false } ] } ``` ``` -------------------------------- ### GET /roles Source: https://ollama4j.github.io/ollama4j/doxygen/html/classio_1_1github_1_1ollama4j_1_1OllamaAPI Lists all available roles. ```APIDOC ## GET /roles ### Description Lists all available roles. ### Method GET ### Endpoint /roles ### Parameters None ### Request Example None ### Response #### Success Response (200) - **roles** (array of strings) - A list of available OllamaChatMessageRole objects. #### Response Example ```json { "roles": [ "user", "assistant", "system" ] } ``` ``` -------------------------------- ### GET /models Source: https://ollama4j.github.io/ollama4j/doxygen/html/classio_1_1github_1_1ollama4j_1_1OllamaAPI Lists all available models from the Ollama server. ```APIDOC ## GET /models ### Description Lists available models from the Ollama server. ### Method GET ### Endpoint /models ### Parameters None ### Request Example None ### Response #### Success Response (200) - **models** (array of objects) - A list of models available on the server. Each object contains model details. #### Response Example ```json { "models": [ { "name": "llama2", "modified_at": "2023-07-13T10:12:11.390154962Z", "digest": "f1b4c7b8a6b1d2c3e4f5a6b7c8d9e0f1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7", "size": 3500000000, "details": { "parent_model": "", "format": "gguf", "family": "llama", "families": null, "parameter_size": "7B", "quantization_level": "Q4_0" } } ] } ``` ``` -------------------------------- ### Define Prompt for Ollama Model Source: https://ollama4j.github.io/ollama4j/blog/talk-to-your-data-on-couchbase-via-ollama4j Sets a prompt string to be sent to the Ollama model. This example asks for the call-sign of a specific airline. ```java String prompt = "What is the call-sign of Astraeus?"; ``` -------------------------------- ### Get Ollama Model Details in Java Source: https://ollama4j.github.io/ollama4j/apis-model-management/get-model-details Retrieves detailed information about a specific model hosted on an Ollama server. This example requires the ollama4j library and demonstrates pulling a model before fetching its details. ```java package io.github.ollama4j.examples; import io.github.ollama4j.OllamaAPI; import io.github.ollama4j.models.response.ModelDetail; import io.github.ollama4j.utils.Utilities; public class GetModelDetails { public static void main(String[] args) throws Exception { OllamaAPI ollamaAPI = Utilities.setUp(); String model = "mistral:7b"; ollamaAPI.pullModel(model); ModelDetail modelDetails = ollamaAPI.getModelDetails(model); System.out.println(modelDetails); } } ``` -------------------------------- ### Get OllamaGenerateRequestBuilder Instance (Java) Source: https://ollama4j.github.io/ollama4j/apidocs/io/github/ollama4j/models/generate/class-use/OllamaGenerateRequestBuilder Retrieves a static instance of OllamaGenerateRequestBuilder for a specified Ollama model. This is the starting point for building a request. ```java static OllamaGenerateRequestBuilder OllamaGenerateRequestBuilder.getInstance​(String model) ``` -------------------------------- ### OllamaAPI: Initialize Ollama Client Source: https://ollama4j.github.io/ollama4j/doxygen/html/OllamaAPI_8java_source Provides constructors for initializing the OllamaAPI client. One constructor takes a host URL, while the other uses default settings. This is the entry point for all interactions with the Ollama service. ```java io.github.ollama4j.OllamaAPI.OllamaAPI OllamaAPI(String host) **Definition** OllamaAPI.java:120 ``` ```java io.github.ollama4j.OllamaAPI.OllamaAPI OllamaAPI() **Definition** OllamaAPI.java:111 ``` -------------------------------- ### POST /api/pull Source: https://ollama4j.github.io/ollama4j/doxygen/html/OllamaAPI_8java_source Initiates the download and installation of a specified Ollama model. It handles retries with exponential backoff in case of transient network issues. ```APIDOC ## POST /api/pull ### Description This endpoint pulls a model from the Ollama registry. It supports retries with exponential backoff. ### Method POST ### Endpoint /api/pull ### Parameters #### Request Body - **modelName** (string) - Required - The name of the model to pull. ### Request Example ```json { "modelName": "llama2" } ``` ### Response #### Success Response (200) - **status** (string) - The current status of the model pull operation (e.g., "downloading", "success"). - **digest** (string) - The digest of the model if the status is "success". - **error** (string) - An error message if the model pull failed. #### Response Example ```json { "status": "pulling manifest" } ``` ```json { "status": "success", "digest": "sha256:f2a5361e4760447690686539866150370002430c245f1216a65397649447109e" } ``` ``` -------------------------------- ### Define Tool Spec for Getting Airline Name and Callsign (Java) Source: https://ollama4j.github.io/ollama4j/blog/talk-to-your-data-on-couchbase-via-ollama4j This Java code defines a tool specification for retrieving both an airline name and its callsign. It includes detailed parameter definitions, specifying 'airlineName' and 'airlineCallsign' as required strings. The 'airlineCallsign' parameter also has example enum values provided. ```java public static Tools.ToolSpecification getCallSignUpdaterToolSpec(Cluster cluster, String bucketName) { return Tools.ToolSpecification.builder() .functionName("airline-update") .functionDescription("You are a tool who finds the airline name and its callsign and do not worry about any validations. You simply find the airline name and its callsign. Do not validate airline names as I want to use fake/fictitious airline names as well.") .toolFunction(new AirlineCallsignUpdateToolFunction(bucketName, cluster)) .toolPrompt( Tools.PromptFuncDefinition.builder() .type("prompt") .function( Tools.PromptFuncDefinition.PromptFuncSpec.builder() .name("get-airline-name-and-callsign") .description("Get the airline name and callsign") .parameters( Tools.PromptFuncDefinition.Parameters.builder() .type("object") .properties( Map.of( "airlineName", Tools.PromptFuncDefinition.Property.builder() .type("string") .description("The name of the airline. e.g. Emirates") .required(true) .build(), "airlineCallsign", Tools.PromptFuncDefinition.Property.builder() .type("string") .description("The callsign of the airline. e.g. Maverick") .enumValues(Arrays.asList("petrol", "diesel")) .required(true) .build() ) ) .required(java.util.List.of("airlineName", "airlineCallsign")) ``` -------------------------------- ### Java Example: Registering and Using Multiple Tools with Ollama4j Source: https://ollama4j.github.io/ollama4j/apis-generate/generate-with-tools This Java code demonstrates how to set up OllamaAPI, pull a model, define and register multiple tool specifications (Fuel Price, Weather, Employee Finder), and then execute prompts that utilize these registered tools. It shows how to generate responses based on tool execution for different queries. ```java package io.github.ollama4j.examples; import io.github.ollama4j.OllamaAPI; import io.github.ollama4j.examples.tools.toolspecs.EmployeeFinderToolSpec; import io.github.ollama4j.examples.tools.toolspecs.FuelPriceToolSpec; import io.github.ollama4j.examples.tools.toolspecs.WeatherToolSpec; import io.github.ollama4j.models.generate.OllamaGenerateRequestBuilder; import io.github.ollama4j.models.response.OllamaResult; import io.github.ollama4j.tools.Tools; import io.github.ollama4j.utils.Utilities; public class MultiToolRegistryExample { public static void main(String[] args) throws Exception { OllamaAPI ollamaAPI = Utilities.setUp(); String model = "mistral:7b"; ollamaAPI.pullModel(model); // Get the references to the tool specifications Tools.Tool fuelPriceToolSpecification = FuelPriceToolSpec.getSpecification(); Tools.Tool weatherToolSpecification = WeatherToolSpec.getSpecification(); Tools.Tool employeeFinderToolSpecification = EmployeeFinderToolSpec.getSpecification(); // Register the tool specifications ollamaAPI.registerTool(fuelPriceToolSpecification); ollamaAPI.registerTool(weatherToolSpecification); ollamaAPI.registerTool(employeeFinderToolSpecification); // Use the fuel-price tool specifications in the prompt OllamaResult res = ollamaAPI .generate(OllamaGenerateRequestBuilder.builder().withModel(model).withUseTools(true) .withPrompt("How much does petrol cost in Bengaluru?").build(), null); // Use the weather tool specifications in the prompt OllamaResult res2 = ollamaAPI .generate(OllamaGenerateRequestBuilder.builder().withModel(model).withUseTools(true) .withPrompt("What is the current weather in Bengaluru?").build(), null); // Use the database query tool specifications in the prompt OllamaResult res3 = ollamaAPI .generate(OllamaGenerateRequestBuilder.builder().withModel(model).withUseTools(true) .withPrompt("Give me the details of the employee named Rahul Kumar and tell me if he would have like the weather in Bengaluru compared to his city and whether petrol is decently priced for him for his daily commute?") .build(), null); System.out.println("[Response 1]: " + res.getResponse()); System.out.println("[Response 2]: " + res2.getResponse()); System.out.println("[Response 3]: " + res3.getResponse()); } } ``` -------------------------------- ### Get Model Details Source: https://ollama4j.github.io/ollama4j/doxygen/html/classio_1_1github_1_1ollama4j_1_1OllamaAPI Gets model details from the Ollama server. ```APIDOC ## GET /api/models/{modelName} ### Description Gets model details from the Ollama server. ### Method GET ### Endpoint /api/models/{modelName} ### Parameters #### Path Parameters - **modelName** (String) - Required - The name of the model to retrieve details for. ### Response #### Success Response (200) - **modelDetails** (ModelDetail) - An object containing the model's details. #### Response Example ```json { "name": "llama2", "modifiedAt": "2023-10-20T14:30:00Z", "size": 3500000000, "digest": "sha256:a2b3c4d5e6f7..." } ``` ### Exceptions - **OllamaBaseException**: If the response indicates an error status. - **IOException**: If an I/O error occurs during the HTTP request. - **InterruptedException**: If the operation is interrupted. - **URISyntaxException**: If the URI for the request is malformed. ``` -------------------------------- ### Build and Execute Prompt with Ollama4j in Java Source: https://ollama4j.github.io/ollama4j/apis-generate/prompt-builder This Java code snippet demonstrates how to initialize OllamaAPI, set request timeouts, and use PromptBuilder to create a complex prompt for generating code. It includes instructions for the AI model and a specific question, then prints the generated response. Dependencies include ollama4j library. ```java import io.github.ollama4j.OllamaAPI; import io.github.ollama4j.models.response.OllamaResult; import io.github.ollama4j.types.OllamaModelType; import io.github.ollama4j.utils.OptionsBuilder; import io.github.ollama4j.utils.PromptBuilder; public class Main { public static void main(String[] args) throws Exception { String host = "http://localhost:11434/"; OllamaAPI ollamaAPI = new OllamaAPI(host); ollamaAPI.setRequestTimeoutSeconds(10); String model = OllamaModelType.PHI; PromptBuilder promptBuilder = new PromptBuilder() .addLine("You are an expert coder and understand different programming languages.") .addLine("Given a question, answer ONLY with code.") .addLine("Produce clean, formatted and indented code in markdown format.") .addLine( "DO NOT include ANY extra text apart from code. Follow this instruction very strictly!") .addLine("If there's any additional information you want to add, use comments within code.") .addLine("Answer only in the programming language that has been asked for.") .addSeparator() .addLine("Example: Sum 2 numbers in Python") .addLine("Answer:") .addLine("```python") .addLine("def sum(num1: int, num2: int) -> int:") .addLine(" return num1 + num2") .addLine("```") .addSeparator() .add("How do I read a file in Go and print its contents to stdout?"); boolean raw = false; OllamaResult response = ollamaAPI.generate(model, promptBuilder.build(), raw, new OptionsBuilder().build()); System.out.println(response.getResponse()); } } ``` -------------------------------- ### Get Model Details Source: https://ollama4j.github.io/ollama4j/apidocs/io/github/ollama4j/exceptions/class-use/OllamaBaseException Gets model details from the Ollama server. ```APIDOC ## GET /api/models/{modelName} ### Description Gets details for a specific model from the Ollama server. ### Method GET ### Endpoint /api/models/{modelName} ### Parameters #### Path Parameters - **modelName** (String) - Required - The name of the model to get details for. ### Response #### Success Response (200) - **ModelDetail** (Object) - Details about the specified model. #### Response Example ```json { "name": "llama3", "modified_at": "2024-05-01T10:00:00Z", "size": 4700000000, "digest": "sha256:...", "details": { "families": ["llama"], "parameters": "...", "template": "..." } } ``` ``` -------------------------------- ### GET /api/show - Get Model Details Source: https://ollama4j.github.io/ollama4j/doxygen/html/OllamaAPI_8java_source Retrieves details for a specific model. ```APIDOC ## GET /api/show ### Description Retrieves detailed information about a specific Ollama model. ### Method GET ### Endpoint /api/show ### Parameters #### Query Parameters - **name** (string) - Required - The name of the model for which to retrieve details. ### Request Example ``` GET /api/show?name=llama2:latest ``` ### Response #### Success Response (200) - **details** (object) - Contains detailed information about the model. - **parent_model** (string) - The parent model name, if any. - **format** (string) - The format of the model. - **family** (string) - The family the model belongs to. - **families** (array) - An array of model families. - **parameter_size** (string) - The size of the model parameters. - **quantization_level** (string) - The quantization level of the model. - **modelfile** (string) - The content of the Modelfile for this model. - **license** (string) - The license under which the model is distributed. - **system** (string) - The system prompt associated with the model. - **template** (string) - The template used for prompts. - **description** (string) - A description of the model. - **digest** (string) - The model's digest. - **quantization** (string) - Information about model quantization. #### Response Example ```json { "details": { "parent_model": "", "format": "ggufv2", "family": "llama", "families": [ "llama" ], "parameter_size": "7.0 B", "quantization_level": "Q4_0" }, "modelfile": "FROM llama2\n\nTEMPLATE \"{{.Prompt}}\n\n{{.System}} \n{{.Template}}\"\nSYSTEM \"You are a helpful AI assistant.\" PARAMETER temperature 0.8", "license": "", "system": "You are a helpful AI assistant.", "template": "{{.Prompt}}\n\n{{.System}} \n{{.Template}}", "description": "A causal decoder-only transformer decoder model.", "digest": "sha256:f011c45f572346308b0a0c6f72467e036588975c155e4c1f999e88785314d19c", "quantization": "Q4_0" } ``` ``` -------------------------------- ### Ollama API Initialization and Authentication Source: https://ollama4j.github.io/ollama4j/doxygen/html/classio_1_1github_1_1ollama4j_1_1OllamaAPI Demonstrates how to initialize the OllamaAPI with default or custom host, and set authentication credentials using basic or bearer tokens. ```java import io.ollamaai.ollama4j.OllamaAPI; // Initialize with default host OllamaAPI ollamaApi = new OllamaAPI(); // Initialize with custom host OllamaAPI ollamaApiCustomHost = new OllamaAPI("http://localhost:11434"); // Set basic authentication ollamaApi.setBasicAuth("username", "password"); // Set bearer token authentication ollamaApi.setBearerAuth("your_bearer_token"); ``` -------------------------------- ### Import Ollama4j Utils Options Source: https://ollama4j.github.io/ollama4j/doxygen/html/OllamaEmbeddingsRequestBuilder_8java This snippet demonstrates how to import the `Options` class from the `io.github.ollama4j.utils` package. This is typically used for configuring requests to the Ollama server. ```java import io.github.ollama4j.utils.Options; ``` -------------------------------- ### Ollama API Initialization and Core Functionality Source: https://ollama4j.github.io/ollama4j/doxygen/html/functions_func This section covers the initialization of the OllamaAPI client and essential utility methods for interacting with the Ollama service, such as pinging the server and pulling models. ```java io.github.ollama4j.OllamaAPI.OllamaAPI() io.github.ollama4j.OllamaAPI.ping() io.github.ollama4j.OllamaAPI.pullModel() io.github.ollama4j.OllamaAPI.ps() io.github.ollama4j.OllamaAPI.getVersion() ``` -------------------------------- ### Get Authentication Header Value Source: https://ollama4j.github.io/ollama4j/doxygen/html/OllamaAPI_8java_source Abstract method to get the authentication header value. Implementations like BasicAuth will provide concrete values for authentication. ```java String authHeader = auth.getAuthHeaderValue(); ``` -------------------------------- ### List Available Ollama Models Source: https://ollama4j.github.io/ollama4j/blog/talk-to-your-data-on-couchbase-via-ollama4j Displays all models currently available on the Ollama server. This helps in confirming that the desired models have been downloaded. ```shell ollama list ``` -------------------------------- ### Initialize and Configure Ollama API Client Source: https://ollama4j.github.io/ollama4j/blog/talk-to-your-data-on-couchbase-via-ollama4j Initializes the Ollama API client with the server host and sets a request timeout. This is the first step to interact with the Ollama service. ```java OllamaAPI ollamaAPI = new OllamaAPI(host); ollamaAPI.setRequestTimeoutSeconds(60); ``` -------------------------------- ### OllamaStreamHandler Implementation Example (Java) Source: https://ollama4j.github.io/ollama4j/doxygen/html/interfaceio_1_1github_1_1ollama4j_1_1models_1_1generate_1_1OllamaStreamHandler An example implementation of the `OllamaStreamHandler` interface. This class, `ConsoleOutputStreamHandler`, demonstrates how to process incoming messages from the Ollama stream by printing them to the console. ```java class ConsoleOutputStreamHandler implements OllamaStreamHandler { @Override public void accept(String message) { System.out.println(message); } } ``` -------------------------------- ### OllamaAPI Initialization Source: https://ollama4j.github.io/ollama4j/doxygen/html/classio_1_1github_1_1ollama4j_1_1OllamaAPI Initializes the Ollama API client. You can use the default host or specify a custom one. ```APIDOC ## OllamaAPI() ### Description Instantiates the Ollama API with default Ollama host: http://localhost:11434 ### Method Constructor ### Endpoint N/A ### Parameters None ### Request Example ```java OllamaAPI ollamaAPI = new OllamaAPI(); ``` ### Response N/A ``` ```APIDOC ## OllamaAPI(String host) ### Description Instantiates the Ollama API with specified Ollama host address. ### Method Constructor ### Endpoint N/A ### Parameters #### Path Parameters - **host** (String) - Required - The host address of Ollama server ### Request Example ```java OllamaAPI ollamaAPI = new OllamaAPI("http://custom-host:11434"); ``` ### Response N/A ``` -------------------------------- ### Get Role by Name (Java) Source: https://ollama4j.github.io/ollama4j/doxygen/html/classio_1_1github_1_1ollama4j_1_1OllamaAPI Retrieves an OllamaChatMessageRole object based on its string name. This method is used to get specific role configurations. It requires the role name. It throws a RoleNotFoundException if the role does not exist. ```java try { OllamaChatMessageRole systemRole = ollamaAPI.getRole("system"); } catch (RoleNotFoundException e) { System.err.println("Role not found: " + e.getMessage()); } ``` -------------------------------- ### ToolRegistry Constructor - Java Source: https://ollama4j.github.io/ollama4j/apidocs/io/github/ollama4j/tools/ToolRegistry Initializes a new instance of the ToolRegistry class. This constructor does not take any arguments and sets up the registry for managing tool specifications. ```java public ToolRegistry() ``` -------------------------------- ### Ollama4j: Constructor and Basic Methods for OllamaResult (Java) Source: https://ollama4j.github.io/ollama4j/doxygen/html/classio_1_1github_1_1ollama4j_1_1models_1_1response_1_1OllamaResult-members This snippet demonstrates the constructor and essential methods for the `OllamaResult` class within the Ollama4j library. It includes how to initialize an `OllamaResult` object with response details and access structured responses or string representations. This is useful for parsing and utilizing the results from Ollama server interactions. ```java OllamaResult(String response, String thinking, long responseTime, int httpStatusCode) String getStructuredResponse() String toString() ``` -------------------------------- ### Ollama4j API - Core Functionality Source: https://ollama4j.github.io/ollama4j/doxygen/html/classio_1_1github_1_1ollama4j_1_1OllamaAPI-members This snippet covers core functionalities of the Ollama4j API, including listing models, checking service status, and managing models. It demonstrates initialization and basic operations. ```java import io.github.ollama4j.OllamaAPI; // Initialize OllamaAPI OllamaAPI ollamaAPI = new OllamaAPI(); // Default host // OllamaAPI ollamaAPI = new OllamaAPI("http://localhost:11434"); // Custom host // List all available models in the library ollamaAPI.listModelsFromLibrary(); // List available roles (if any supported by Ollama) ollamaAPI.listRoles(); // Check if the Ollama service is reachable ollamaAPI.ping(); // List running Ollama services/models ollamaAPI.ps(); // Pull a model from the library by name // ollamaAPI.pullModel("llama2"); // Pull a model using a LibraryModelTag object (if applicable) // LibraryModelTag tag = new LibraryModelTag("llama2", "latest"); // ollamaAPI.pullModel(tag); ``` -------------------------------- ### Java: Integrate Ollama4j with Couchbase for Tool Calling Source: https://ollama4j.github.io/ollama4j/blog/talk-to-your-data-on-couchbase-via-ollama4j This Java code demonstrates the core implementation of integrating ollama4j with Couchbase for tool calling. It sets up Couchbase connections, initializes OllamaAPI, registers custom tools for airline lookups and updates, and executes prompts to interact with these tools. Dependencies include ollama4j, Couchbase client, and Lombok. ```java package io.github.ollama4j.examples; import com.couchbase.client.java.Bucket; import com.couchbase.client.java.Cluster; import com.couchbase.client.java.ClusterOptions; import com.couchbase.client.java.Scope; import com.couchbase.client.java.json.JsonObject; import com.couchbase.client.java.query.QueryResult; import io.github.ollama4j.OllamaAPI; import io.github.ollama4j.exceptions.OllamaBaseException; import io.github.ollama4j.exceptions.ToolInvocationException; import io.github.ollama4j.tools.OllamaToolsResult; import io.github.ollama4j.tools.ToolFunction; import io.github.ollama4j.tools.Tools; import io.github.ollama4j.utils.OptionsBuilder; import io.github.ollama4j.utils.Utilities; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.io.IOException; import java.time.Duration; import java.util.Arrays; import java.util.Map; public class CouchbaseToolCallingExample { public static void main(String[] args) throws IOException, ToolInvocationException, OllamaBaseException, InterruptedException { String connectionString = Utilities.getFromEnvVar("CB_CLUSTER_URL"); String username = Utilities.getFromEnvVar("CB_CLUSTER_USERNAME"); String password = Utilities.getFromEnvVar("CB_CLUSTER_PASSWORD"); String bucketName = "travel-sample"; Cluster cluster = Cluster.connect( connectionString, ClusterOptions.clusterOptions(username, password).environment(env -> { env.applyProfile("wan-development"); }) ); String host = Utilities.getFromConfig("host"); String modelName = Utilities.getFromConfig("tools_model_mistral"); OllamaAPI ollamaAPI = new OllamaAPI(host); ollamaAPI.setVerbose(false); ollamaAPI.setRequestTimeoutSeconds(60); Tools.ToolSpecification callSignFinderToolSpec = getCallSignFinderToolSpec(cluster, bucketName); Tools.ToolSpecification callSignUpdaterToolSpec = getCallSignUpdaterToolSpec(cluster, bucketName); ollamaAPI.registerTool(callSignFinderToolSpec); ollamaAPI.registerTool(callSignUpdaterToolSpec); String prompt1 = "What is the call-sign of Astraeus?"; for (OllamaToolsResult.ToolResult r : ollamaAPI.generateWithTools(modelName, new Tools.PromptBuilder() .withToolSpecification(callSignFinderToolSpec) .withPrompt(prompt1) .build(), new OptionsBuilder().build()).getToolResults()) { AirlineDetail airlineDetail = (AirlineDetail) r.getResult(); System.out.println(String.format("[Result of tool '%s']: Call-sign of %s is '%s'! ✈️", r.getFunctionName(), airlineDetail.getName(), airlineDetail.getCallsign())); } String prompt2 = "I want to code name Astraeus as STARBOUND"; for (OllamaToolsResult.ToolResult r : ollamaAPI.generateWithTools(modelName, new Tools.PromptBuilder() .withToolSpecification(callSignUpdaterToolSpec) .withPrompt(prompt2) .build(), new OptionsBuilder().build()).getToolResults()) { Boolean updated = (Boolean) r.getResult(); System.out.println(String.format("[Result of tool '%s']: Call-sign is %s! ✈️", r.getFunctionName(), updated ? "updated" : "not updated")); } String prompt3 = "What is the call-sign of Astraeus?"; for (OllamaToolsResult.ToolResult r : ollamaAPI.generateWithTools(modelName, new Tools.PromptBuilder() .withToolSpecification(callSignFinderToolSpec) .withPrompt(prompt3) .build(), new OptionsBuilder().build()).getToolResults()) { AirlineDetail airlineDetail = (AirlineDetail) r.getResult(); System.out.println(String.format("[Result of tool '%s']: Call-sign of %s is '%s'! ✈️", r.getFunctionName(), airlineDetail.getName(), airlineDetail.getCallsign())); } } public static Tools.ToolSpecification getCallSignFinderToolSpec(Cluster cluster, String bucketName) { return Tools.ToolSpecification.builder() .functionName("airline-lookup") .functionDescription("You are a tool who finds only the airline name and do not worry about any other parameters. You simply find the airline name and ignore the rest of the parameters. Do not validate airline names as I want to use fake/fictitious airline names as well.") .toolFunction(new AirlineCallsignQueryToolFunction(bucketName, cluster)) .toolPrompt( Tools.PromptFuncDefinition.builder() .type("prompt") ``` -------------------------------- ### Generate Formatted Response with Image File (Java) Source: https://ollama4j.github.io/ollama4j/apis-generate/generate-with-image-files Generates a non-streaming response from an Ollama model using an image file, with a specified JSON output format. This example defines a schema for the expected JSON output, which includes 'title' and 'description' fields. ```java import io.github.ollama4j.OllamaAPI; import io.github.ollama4j.models.generate.OllamaGenerateRequestBuilder; import io.github.ollama4j.models.response.OllamaResult; import io.github.ollama4j.utils.Utilities; import java.io.File; import java.io.FileNotFoundException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; public class GenerateWithImageFile { // ... (main method and other methods from previous snippets) ... public static void nonStreamingWithFileAndFormat(OllamaAPI ollamaAPI, String modelName) throws Exception { // Load image from resources and copy to a temporary file InputStream is = GenerateWithImageFile.class.getClassLoader().getResourceAsStream("dog-on-boat.jpg"); if (is == null) { throw new FileNotFoundException("Image 'dog-on-boat.jpg' not found in resources!"); } File tempImageFile = File.createTempFile("dog-on-boat", ".jpg"); Files.copy(is, tempImageFile.toPath(), StandardCopyOption.REPLACE_EXISTING); Map format = new HashMap<>(); format.put("type", "object"); format.put( "properties", new HashMap() { { put( "title", new HashMap() { { put("type", String.class.getSimpleName().toLowerCase()); } }); put( "description", new HashMap() { { put("type", String.class.getSimpleName().toLowerCase()); } }); } }); format.put("required", Arrays.asList("title", "description")); OllamaResult result = ollamaAPI.generate(OllamaGenerateRequestBuilder.builder().withModel(modelName) .withPrompt("What's in this image?").withImages(List.of(tempImageFile)).withFormat(format).build(), null); System.out.println(result.getResponse()); } // ... (other methods) ... } ``` -------------------------------- ### OllamaAPI Constructor Source: https://ollama4j.github.io/ollama4j/doxygen/html/classio_1_1github_1_1ollama4j_1_1OllamaAPI Constructs an instance of the OllamaAPI. It can be initialized with default settings or a custom host address for the Ollama server. ```java OllamaAPI() // Instantiates the Ollama API with default Ollama host: http://localhost:11434 OllamaAPI(String _host_) // Instantiates the Ollama API with specified Ollama host address. // Parameters: host| the host address of Ollama server ``` -------------------------------- ### GET / Source: https://ollama4j.github.io/ollama4j/doxygen/html/classio_1_1github_1_1ollama4j_1_1OllamaAPI API to check the reachability of Ollama server. ```APIDOC ## GET / ### Description API to check the reachability of Ollama server. ### Method GET ### Endpoint / ### Parameters None ### Request Example None ### Response #### Success Response (200) - **status** (boolean) - True if the server is reachable, false otherwise. #### Response Example ```json { "status": true } ``` ``` -------------------------------- ### List Running Ollama Models (Java) Source: https://ollama4j.github.io/ollama4j/apis-extras/ps This Java code snippet demonstrates how to use the Ollama4j library to call the PS API. It initializes the OllamaAPI with a local Ollama instance and retrieves a list of running models. The response object containing model details is then printed to the console. This requires the ollama4j library and a running Ollama instance. ```java package io.github.ollama4j.localtests; import io.github.ollama4j.OllamaAPI; import io.github.ollama4j.exceptions.OllamaBaseException; import io.github.ollama4j.models.ps.ModelsProcessResponse; import java.io.IOException; public class Main { public static void main(String[] args) { OllamaAPI ollamaAPI = new OllamaAPI("http://localhost:11434"); ModelsProcessResponse response = ollamaAPI.ps(); System.out.println(response); } } ``` -------------------------------- ### Java WeatherTool Class Methods Source: https://ollama4j.github.io/ollama4j/doxygen/html/classio_1_1github_1_1ollama4j_1_1tools_1_1sampletools_1_1WeatherTool-members Provides the method signatures for the WeatherTool class, a sample tool for Ollama4j. It includes a method for getting current weather based on arguments and a method to retrieve the tool's specification. The constructor is also listed. ```java getCurrentWeather(Map< String, Object > arguments) getSpecification() WeatherTool() ``` -------------------------------- ### GET /version Source: https://ollama4j.github.io/ollama4j/doxygen/html/classio_1_1github_1_1ollama4j_1_1OllamaAPI Retrieves the version of the Ollama API. ```APIDOC ## GET /version ### Description Retrieves the version of the Ollama API. ### Method GET ### Endpoint /version ### Parameters None ### Request Example None ### Response #### Success Response (200) - **version** (string) - The version of the Ollama API. #### Response Example ```json { "version": "1.2.3" } ``` ``` -------------------------------- ### Get Role Source: https://ollama4j.github.io/ollama4j/doxygen/html/classio_1_1github_1_1ollama4j_1_1OllamaAPI Retrieves a specific role by name. ```APIDOC ## GET /api/roles/{roleName} ### Description Retrieves a specific role by name. ### Method GET ### Endpoint /api/roles/{roleName} ### Parameters #### Path Parameters - **roleName** (String) - Required - The name of the role to retrieve. ### Response #### Success Response (200) - **role** (OllamaChatMessageRole) - The OllamaChatMessageRole associated with the given name. #### Response Example ```json { "name": "user", "description": "The user initiating the conversation." } ``` ### Exceptions - **RoleNotFoundException**: If the role with the specified name does not exist. ``` -------------------------------- ### GET /library/models Source: https://ollama4j.github.io/ollama4j/doxygen/html/classio_1_1github_1_1ollama4j_1_1OllamaAPI Retrieves a list of models from the Ollama library. ```APIDOC ## GET /library/models ### Description Retrieves a list of models from the Ollama library. This method fetches the available models directly from Ollama library page, including model details such as the name, pull count, popular tags, tag count, and the time when model was updated. ### Method GET ### Endpoint /library/models ### Parameters None ### Request Example None ### Response #### Success Response (200) - **models** (array of objects) - A list of `LibraryModel` objects representing the models available in the Ollama library. #### Response Example ```json { "models": [ { "name": "llama2", "modified_at": "2023-07-13T10:12:11.390154962Z", "digest": "f1b4c7b8a6b1d2c3e4f5a6b7c8d9e0f1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7", "size": 3500000000, "details": { "parent_model": "", "format": "gguf", "family": "llama", "families": null, "parameter_size": "7B", "quantization_level": "Q4_0" } } ] } ``` ``` -------------------------------- ### OllamaGenerateEndpointCaller Constructor Source: https://ollama4j.github.io/ollama4j/apidocs/io/github/ollama4j/models/request/OllamaGenerateEndpointCaller Initializes an OllamaGenerateEndpointCaller instance. It requires the host address, authentication details, request timeout, and a verbose flag for debugging. ```java public OllamaGenerateEndpointCaller( String host, Auth basicAuth, long requestTimeoutSeconds, boolean verbose ) ``` -------------------------------- ### Java Options Builder Build Method Source: https://ollama4j.github.io/ollama4j/apidocs/io/github/ollama4j/utils/class-use/Options Illustrates the `build()` method of the `OptionsBuilder` class in `io.github.ollama4j.utils`, which is used to construct the `Options` map for configuring Ollama requests. ```java Options OptionsBuilder.build() ``` -------------------------------- ### GET /api/version Source: https://ollama4j.github.io/ollama4j/doxygen/html/OllamaAPI_8java_source Retrieves the current version of the Ollama server. ```APIDOC ## GET /api/version ### Description This endpoint retrieves the Ollama server version. ### Method GET ### Endpoint /api/version ### Parameters None ### Request Example ``` GET /api/version ``` ### Response #### Success Response (200) - **version** (string) - The Ollama server version. #### Response Example ```json { "version": "0.1.30" } ``` ``` -------------------------------- ### Get Specific Role Source: https://ollama4j.github.io/ollama4j/apidocs/io/github/ollama4j/OllamaAPI Retrieves a specific role by its name. ```APIDOC ## GET /roles/{roleName} ### Description Retrieves a specific role by name. ### Method GET ### Endpoint /roles/{roleName} ### Parameters #### Path Parameters - **roleName** (String) - Required - The name of the role to retrieve. ### Returns - **OllamaChatMessageRole** - The OllamaChatMessageRole associated with the given name. ### Throws - `RoleNotFoundException` - if the role with the specified name does not exist. ``` -------------------------------- ### OllamaToolService - providers() Source: https://ollama4j.github.io/ollama4j/doxygen/html/interfaceio_1_1github_1_1ollama4j_1_1tools_1_1annotations_1_1OllamaToolService This method is part of the OllamaToolService annotation, which helps in auto-registering tools for the Ollama API. It returns an array of classes that have a no-arg constructor and will be used for tool registration. ```APIDOC ## GET /tools/annotations/OllamaToolService/providers ### Description Retrieves an array of classes with no-arg constructors to be used for auto-registering tools with the Ollama API. ### Method GET ### Endpoint /tools/annotations/OllamaToolService/providers ### Parameters #### Query Parameters None #### Request Body None ### Response #### Success Response (200) - **classes** (Class[]) - An array of classes eligible for tool registration. #### Response Example { "classes": [ "com.example.ToolClass1", "com.example.ToolClass2" ] } ``` -------------------------------- ### Get Ollama Server Version in Java Source: https://ollama4j.github.io/ollama4j/doxygen/html/OllamaAPI_8java_source Retrieves the version of the Ollama server by making an HTTP GET request to the '/api/version' endpoint. It uses Java's HttpClient to send the request and handles the response, parsing the JSON body to extract the version string. If the status code is not 200, it throws an OllamaBaseException with the status and response body. ```java public String getVersion() throws URISyntaxException, IOException, InterruptedException, OllamaBaseException { String url = this.host + "/api/version"; HttpClient httpClient = HttpClient.newHttpClient(); HttpRequest httpRequest = getRequestBuilderDefault(new URI(url)) .header(Constants.HttpConstants.HEADER_KEY_ACCEPT, Constants.HttpConstants.APPLICATION_JSON) .header(Constants.HttpConstants.HEADER_KEY_CONTENT_TYPE, Constants.HttpConstants.APPLICATION_JSON).GET() .build(); HttpResponse response = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString()); int statusCode = response.statusCode(); String responseString = response.body(); if (statusCode == 200) { return Utils.getObjectMapper().readValue(responseString, OllamaVersion.class).getVersion(); } else { throw new OllamaBaseException(statusCode + " - " + responseString); } } ``` -------------------------------- ### Java PromptBuilder Example Usage Source: https://ollama4j.github.io/ollama4j/doxygen/html/classio_1_1github_1_1ollama4j_1_1utils_1_1PromptBuilder Demonstrates how to use the PromptBuilder class to construct a prompt for language models. It shows chaining of methods like add, addLine, and addSeparator to build the final prompt string. ```java PromptBuilder promptBuilder = new PromptBuilder(); promptBuilder.add("This is a sample prompt for language models.") .addLine("You can add lines to provide context.") .addSeparator() .add("Feel free to customize as needed."); String finalPrompt = promptBuilder.build(); System.out.println(finalPrompt); ```