Remove deprecated embedding and library model classes

Deleted deprecated classes related to embeddings and library models, including OllamaEmbeddingResponseModel, OllamaEmbeddingsRequestBuilder, OllamaEmbeddingsRequestModel, LibraryModel, LibraryModelDetail, LibraryModelTag, and OllamaModelType. Updated OllamaAPI to remove references to these classes and improve documentation, exception handling, and code clarity.
This commit is contained in:
amithkoujalgi 2025-09-17 19:47:07 +05:30
parent 2f83a5c98c
commit 4df4ea1930
No known key found for this signature in database
GPG Key ID: E29A37746AF94B70
11 changed files with 300 additions and 612 deletions

View File

@ -15,11 +15,11 @@ apply-formatting:
@mvn spotless:apply @mvn spotless:apply
# pre-commit run --all-files # pre-commit run --all-files
build: build: apply-formatting
@echo "\033[0;34mBuilding project (GPG skipped)...\033[0m" @echo "\033[0;34mBuilding project (GPG skipped)...\033[0m"
@mvn -B clean install -Dgpg.skip=true @mvn -B clean install -Dgpg.skip=true
full-build: full-build: apply-formatting
@echo "\033[0;34mPerforming full build...\033[0m" @echo "\033[0;34mPerforming full build...\033[0m"
@mvn -B clean install @mvn -B clean install

View File

@ -49,10 +49,8 @@ import lombok.Setter;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
/** /** The base Ollama API class. */
* The base Ollama API class. @SuppressWarnings({"DuplicatedCode", "resource", "SpellCheckingInspection"})
*/
@SuppressWarnings({"DuplicatedCode", "resource"})
public class OllamaAPI { public class OllamaAPI {
private static final Logger LOG = LoggerFactory.getLogger(OllamaAPI.class); private static final Logger LOG = LoggerFactory.getLogger(OllamaAPI.class);
@ -63,9 +61,8 @@ public class OllamaAPI {
/** /**
* The request timeout in seconds for API calls. * The request timeout in seconds for API calls.
* <p> *
* Default is 10 seconds. This value determines how long the client will wait * <p>Default is 10 seconds. This value determines how long the client will wait for a response
* for a response
* from the Ollama server before timing out. * from the Ollama server before timing out.
*/ */
@Setter private long requestTimeoutSeconds = 10; @Setter private long requestTimeoutSeconds = 10;
@ -76,38 +73,36 @@ public class OllamaAPI {
/** /**
* The maximum number of retries for tool calls during chat interactions. * The maximum number of retries for tool calls during chat interactions.
* <p> *
* This value controls how many times the API will attempt to call a tool in the * <p>This value controls how many times the API will attempt to call a tool in the event of a
* event of a failure. * failure. Default is 3.
* Default is 3.
*/ */
@Setter private int maxChatToolCallRetries = 3; @Setter private int maxChatToolCallRetries = 3;
/** /**
* The number of retries to attempt when pulling a model from the Ollama server. * The number of retries to attempt when pulling a model from the Ollama server.
* <p> *
* If set to 0, no retries will be performed. If greater than 0, the API will * <p>If set to 0, no retries will be performed. If greater than 0, the API will retry pulling
* retry pulling the model * the model up to the specified number of times in case of failure.
* up to the specified number of times in case of failure. *
* <p> * <p>Default is 0 (no retries).
* Default is 0 (no retries).
*/ */
@Setter @Setter
@SuppressWarnings({"FieldMayBeFinal", "FieldCanBeLocal"}) @SuppressWarnings({"FieldMayBeFinal", "FieldCanBeLocal"})
private int numberOfRetriesForModelPull = 0; private int numberOfRetriesForModelPull = 0;
/** /**
* When set to true, tools will not be automatically executed by the library. * When set to true, tools will not be automatically executed by the library. Instead, tool
* Instead, tool calls will be returned to the client for manual handling. * calls will be returned to the client for manual handling.
* <p> *
* Default is false for backward compatibility. * <p>Default is false for backward compatibility.
*/ */
@Setter private boolean clientHandlesTools = false; @Setter private boolean clientHandlesTools = false;
/** /**
* Instantiates the Ollama API with default Ollama host: * Instantiates the Ollama API with default Ollama host: <a
* <a href="http://localhost:11434">http://localhost:11434</a> * href="http://localhost:11434">http://localhost:11434</a>
**/ */
public OllamaAPI() { public OllamaAPI() {
this.host = "http://localhost:11434"; this.host = "http://localhost:11434";
} }
@ -127,8 +122,7 @@ public class OllamaAPI {
} }
/** /**
* Set basic authentication for accessing Ollama server that's behind a * Set basic authentication for accessing Ollama server that's behind a reverse-proxy/gateway.
* reverse-proxy/gateway.
* *
* @param username the username * @param username the username
* @param password the password * @param password the password
@ -138,8 +132,7 @@ public class OllamaAPI {
} }
/** /**
* Set Bearer authentication for accessing Ollama server that's behind a * Set Bearer authentication for accessing Ollama server that's behind a reverse-proxy/gateway.
* reverse-proxy/gateway.
* *
* @param bearerToken the Bearer authentication token to provide * @param bearerToken the Bearer authentication token to provide
*/ */
@ -152,7 +145,7 @@ public class OllamaAPI {
* *
* @return true if the server is reachable, false otherwise. * @return true if the server is reachable, false otherwise.
*/ */
public boolean ping() { public boolean ping() throws OllamaBaseException {
String url = this.host + "/api/tags"; String url = this.host + "/api/tags";
HttpClient httpClient = HttpClient.newHttpClient(); HttpClient httpClient = HttpClient.newHttpClient();
HttpRequest httpRequest; HttpRequest httpRequest;
@ -168,23 +161,25 @@ public class OllamaAPI {
.GET() .GET()
.build(); .build();
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
throw new RuntimeException(e); throw new OllamaBaseException(e.getMessage());
} }
HttpResponse<String> response; HttpResponse<String> response;
try { try {
response = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString()); response = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
} catch (HttpConnectTimeoutException e) { } catch (HttpConnectTimeoutException e) {
return false; return false;
} catch (IOException | InterruptedException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new OllamaBaseException(e.getMessage());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new OllamaBaseException(e.getMessage());
} }
int statusCode = response.statusCode(); int statusCode = response.statusCode();
return statusCode == 200; return statusCode == 200;
} }
/** /**
* Provides a list of running models and details about each model currently * Provides a list of running models and details about each model currently loaded into memory.
* loaded into memory.
* *
* @return ModelsProcessResponse containing details about the running models * @return ModelsProcessResponse containing details about the running models
* @throws IOException if an I/O error occurs during the HTTP request * @throws IOException if an I/O error occurs during the HTTP request
@ -208,7 +203,7 @@ public class OllamaAPI {
.GET() .GET()
.build(); .build();
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
throw new RuntimeException(e); throw new OllamaBaseException(e.getMessage());
} }
HttpResponse<String> response = null; HttpResponse<String> response = null;
response = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString()); response = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
@ -257,56 +252,7 @@ public class OllamaAPI {
} }
} }
/** /** Handles retry backoff for pullModel. */
* Pull a model on the Ollama server from the list of <a
* href="https://ollama.ai/library">available models</a>.
* <p>
* If {@code numberOfRetriesForModelPull} is greater than 0, this method will
* retry pulling the model
* up to the specified number of times if an {@link OllamaBaseException} occurs,
* using exponential backoff
* between retries (delay doubles after each failed attempt, starting at 1
* second).
* <p>
* The backoff is only applied between retries, not after the final attempt.
*
* @param modelName the name of the model
* @throws OllamaBaseException if the response indicates an error status or all
* retries fail
* @throws IOException if an I/O error occurs during the HTTP request
* @throws InterruptedException if the operation is interrupted or the thread is
* interrupted during backoff
* @throws URISyntaxException if the URI for the request is malformed
*/
public void pullModel(String modelName)
throws OllamaBaseException, IOException, URISyntaxException, InterruptedException {
if (numberOfRetriesForModelPull == 0) {
this.doPullModel(modelName);
return;
}
int numberOfRetries = 0;
long baseDelayMillis = 3000L; // 1 second base delay
while (numberOfRetries < numberOfRetriesForModelPull) {
try {
this.doPullModel(modelName);
return;
} catch (OllamaBaseException e) {
handlePullRetry(
modelName, numberOfRetries, numberOfRetriesForModelPull, baseDelayMillis);
numberOfRetries++;
}
}
throw new OllamaBaseException(
"Failed to pull model "
+ modelName
+ " after "
+ numberOfRetriesForModelPull
+ " retries");
}
/**
* Handles retry backoff for pullModel.
*/
private void handlePullRetry( private void handlePullRetry(
String modelName, int currentRetry, int maxRetries, long baseDelayMillis) String modelName, int currentRetry, int maxRetries, long baseDelayMillis)
throws InterruptedException { throws InterruptedException {
@ -354,6 +300,7 @@ public class OllamaAPI {
InputStream responseBodyStream = response.body(); InputStream responseBodyStream = response.body();
String responseString = ""; String responseString = "";
boolean success = false; // Flag to check the pull success. boolean success = false; // Flag to check the pull success.
try (BufferedReader reader = try (BufferedReader reader =
new BufferedReader( new BufferedReader(
new InputStreamReader(responseBodyStream, StandardCharsets.UTF_8))) { new InputStreamReader(responseBodyStream, StandardCharsets.UTF_8))) {
@ -361,26 +308,10 @@ public class OllamaAPI {
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
ModelPullResponse modelPullResponse = ModelPullResponse modelPullResponse =
Utils.getObjectMapper().readValue(line, ModelPullResponse.class); Utils.getObjectMapper().readValue(line, ModelPullResponse.class);
if (modelPullResponse != null) { success = processModelPullResponse(modelPullResponse, modelName) || success;
// Check for error in response body first }
if (modelPullResponse.getError() != null
&& !modelPullResponse.getError().trim().isEmpty()) {
throw new OllamaBaseException(
"Model pull failed: " + modelPullResponse.getError());
} }
if (modelPullResponse.getStatus() != null) {
LOG.info("{}: {}", modelName, modelPullResponse.getStatus());
// Check if status is "success" and set success flag to true.
if ("success".equalsIgnoreCase(modelPullResponse.getStatus())) {
success = true;
}
}
} else {
LOG.error("Received null response for model pull.");
}
}
}
if (!success) { if (!success) {
LOG.error("Model pull failed or returned invalid status."); LOG.error("Model pull failed or returned invalid status.");
throw new OllamaBaseException("Model pull failed or returned invalid status."); throw new OllamaBaseException("Model pull failed or returned invalid status.");
@ -390,6 +321,31 @@ public class OllamaAPI {
} }
} }
/**
* Processes a single ModelPullResponse, handling errors and logging status. Returns true if the
* response indicates a successful pull.
*/
@SuppressWarnings("RedundantIfStatement")
private boolean processModelPullResponse(ModelPullResponse modelPullResponse, String modelName)
throws OllamaBaseException {
if (modelPullResponse == null) {
LOG.error("Received null response for model pull.");
return false;
}
String error = modelPullResponse.getError();
if (error != null && !error.trim().isEmpty()) {
throw new OllamaBaseException("Model pull failed: " + error);
}
String status = modelPullResponse.getStatus();
if (status != null) {
LOG.info("{}: {}", modelName, status);
if ("success".equalsIgnoreCase(status)) {
return true;
}
}
return false;
}
public String getVersion() public String getVersion()
throws URISyntaxException, IOException, InterruptedException, OllamaBaseException { throws URISyntaxException, IOException, InterruptedException, OllamaBaseException {
String url = this.host + "/api/version"; String url = this.host + "/api/version";
@ -418,24 +374,40 @@ public class OllamaAPI {
} }
/** /**
* Pulls a model using the specified Ollama library model tag. * Pulls a model using the specified Ollama library model tag. The model is identified by a name
* The model is identified by a name and a tag, which are combined into a single * and a tag, which are combined into a single identifier in the format "name:tag" to pull the
* identifier * corresponding model.
* in the format "name:tag" to pull the corresponding model.
* *
* @param libraryModelTag the {@link LibraryModelTag} object containing the name * @param modelName the name/tag of the model to be pulled. Ex: llama3:latest
* and tag
* of the model to be pulled.
* @throws OllamaBaseException if the response indicates an error status * @throws OllamaBaseException if the response indicates an error status
* @throws IOException if an I/O error occurs during the HTTP request * @throws IOException if an I/O error occurs during the HTTP request
* @throws InterruptedException if the operation is interrupted * @throws InterruptedException if the operation is interrupted
* @throws URISyntaxException if the URI for the request is malformed * @throws URISyntaxException if the URI for the request is malformed
*/ */
public void pullModel(LibraryModelTag libraryModelTag) public void pullModel(String modelName)
throws OllamaBaseException, IOException, URISyntaxException, InterruptedException { throws OllamaBaseException, IOException, URISyntaxException, InterruptedException {
String tagToPull = if (numberOfRetriesForModelPull == 0) {
String.format("%s:%s", libraryModelTag.getName(), libraryModelTag.getTag()); this.doPullModel(modelName);
pullModel(tagToPull); return;
}
int numberOfRetries = 0;
long baseDelayMillis = 3000L; // 1 second base delay
while (numberOfRetries < numberOfRetriesForModelPull) {
try {
this.doPullModel(modelName);
return;
} catch (OllamaBaseException e) {
handlePullRetry(
modelName, numberOfRetries, numberOfRetriesForModelPull, baseDelayMillis);
numberOfRetries++;
}
}
throw new OllamaBaseException(
"Failed to pull model "
+ modelName
+ " after "
+ numberOfRetriesForModelPull
+ " retries");
} }
/** /**
@ -474,8 +446,7 @@ public class OllamaAPI {
} }
/** /**
* Create a custom model. Read more about custom model creation <a * Create a custom model. Read more about custom model creation <a href=
* href=
* "https://github.com/ollama/ollama/blob/main/docs/api.md#create-a-model">here</a>. * "https://github.com/ollama/ollama/blob/main/docs/api.md#create-a-model">here</a>.
* *
* @param customModelRequest custom model spec * @param customModelRequest custom model spec
@ -515,8 +486,8 @@ public class OllamaAPI {
* Delete a model from Ollama server. * Delete a model from Ollama server.
* *
* @param modelName the name of the model to be deleted. * @param modelName the name of the model to be deleted.
* @param ignoreIfNotPresent ignore errors if the specified model is not present * @param ignoreIfNotPresent ignore errors if the specified model is not present on Ollama
* on Ollama server. * server.
* @throws OllamaBaseException if the response indicates an error status * @throws OllamaBaseException if the response indicates an error status
* @throws IOException if an I/O error occurs during the HTTP request * @throws IOException if an I/O error occurs during the HTTP request
* @throws InterruptedException if the operation is interrupted * @throws InterruptedException if the operation is interrupted
@ -589,26 +560,19 @@ public class OllamaAPI {
} }
/** /**
* Generate response for a question to a model running on Ollama server. This is * Generate response for a question to a model running on Ollama server. This is a sync/blocking
* a sync/blocking call. This API does not support "thinking" models. * call. This API does not support "thinking" models.
* *
* @param model the ollama model to ask the question to * @param model the ollama model to ask the question to
* @param prompt the prompt/question text * @param prompt the prompt/question text
* @param raw if true no formatting will be applied to the * @param raw if true no formatting will be applied to the prompt. You may choose to use the raw
* prompt. You * parameter if you are specifying a full templated prompt in your request to the API
* may choose to use the raw parameter if you are * @param options the Options object - <a href=
* specifying a full templated prompt in your
* request to
* the API
* @param options the Options object - <a
* href=
* "https://github.com/jmorganca/ollama/blob/main/docs/modelfile.md#valid-parameters-and-values">More * "https://github.com/jmorganca/ollama/blob/main/docs/modelfile.md#valid-parameters-and-values">More
* details on the options</a> * details on the options</a>
* @param responseStreamHandler optional callback consumer that will be applied * @param responseStreamHandler optional callback consumer that will be applied every time a
* every * streamed response is received. If not set, the stream parameter of the request is set to
* time a streamed response is received. If not * false.
* set, the
* stream parameter of the request is set to false.
* @return OllamaResult that includes response text and time taken for response * @return OllamaResult that includes response text and time taken for response
* @throws OllamaBaseException if the response indicates an error status * @throws OllamaBaseException if the response indicates an error status
* @throws IOException if an I/O error occurs during the HTTP request * @throws IOException if an I/O error occurs during the HTTP request
@ -629,27 +593,19 @@ public class OllamaAPI {
} }
/** /**
* Generate thinking and response tokens for a question to a thinking model * Generate thinking and response tokens for a question to a thinking model running on Ollama
* running on Ollama server. This is * server. This is a sync/blocking call.
* a sync/blocking call.
* *
* @param model the ollama model to ask the question to * @param model the ollama model to ask the question to
* @param prompt the prompt/question text * @param prompt the prompt/question text
* @param raw if true no formatting will be applied to the * @param raw if true no formatting will be applied to the prompt. You may choose to use the raw
* prompt. You * parameter if you are specifying a full templated prompt in your request to the API
* may choose to use the raw parameter if you are * @param options the Options object - <a href=
* specifying a full templated prompt in your
* request to
* the API
* @param options the Options object - <a
* href=
* "https://github.com/jmorganca/ollama/blob/main/docs/modelfile.md#valid-parameters-and-values">More * "https://github.com/jmorganca/ollama/blob/main/docs/modelfile.md#valid-parameters-and-values">More
* details on the options</a> * details on the options</a>
* @param responseStreamHandler optional callback consumer that will be applied * @param responseStreamHandler optional callback consumer that will be applied every time a
* every * streamed response is received. If not set, the stream parameter of the request is set to
* time a streamed response is received. If not * false.
* set, the
* stream parameter of the request is set to false.
* @return OllamaResult that includes response text and time taken for response * @return OllamaResult that includes response text and time taken for response
* @throws OllamaBaseException if the response indicates an error status * @throws OllamaBaseException if the response indicates an error status
* @throws IOException if an I/O error occurs during the HTTP request * @throws IOException if an I/O error occurs during the HTTP request
@ -672,23 +628,17 @@ public class OllamaAPI {
} }
/** /**
* Generates response using the specified AI model and prompt (in blocking * Generates response using the specified AI model and prompt (in blocking mode).
* mode).
* <p>
* Uses
* {@link #generate(String, String, boolean, Options, OllamaStreamHandler)}
* *
* @param model The name or identifier of the AI model to use for generating * <p>Uses {@link #generate(String, String, boolean, Options, OllamaStreamHandler)}
* the response. *
* @param model The name or identifier of the AI model to use for generating the response.
* @param prompt The input text or prompt to provide to the AI model. * @param prompt The input text or prompt to provide to the AI model.
* @param raw In some cases, you may wish to bypass the templating system * @param raw In some cases, you may wish to bypass the templating system and provide a full
* and provide a full prompt. In this case, you can use the raw * prompt. In this case, you can use the raw parameter to disable templating. Also note that
* parameter to disable templating. Also note that raw mode will * raw mode will not return a context.
* not return a context. * @param options Additional options or configurations to use when generating the response.
* @param options Additional options or configurations to use when generating * @param think if true the model will "think" step-by-step before generating the final response
* the response.
* @param think if true the model will "think" step-by-step before
* generating the final response
* @return {@link OllamaResult} * @return {@link OllamaResult}
* @throws OllamaBaseException if the response indicates an error status * @throws OllamaBaseException if the response indicates an error status
* @throws IOException if an I/O error occurs during the HTTP request * @throws IOException if an I/O error occurs during the HTTP request
@ -706,16 +656,13 @@ public class OllamaAPI {
/** /**
* Generates structured output from the specified AI model and prompt. * Generates structured output from the specified AI model and prompt.
* <p>
* Note: When formatting is specified, the 'think' parameter is not allowed.
* *
* @param model The name or identifier of the AI model to use for generating * <p>Note: When formatting is specified, the 'think' parameter is not allowed.
* the response. *
* @param model The name or identifier of the AI model to use for generating the response.
* @param prompt The input text or prompt to provide to the AI model. * @param prompt The input text or prompt to provide to the AI model.
* @param format A map containing the format specification for the structured * @param format A map containing the format specification for the structured output.
* output. * @return An instance of {@link OllamaResult} containing the structured response.
* @return An instance of {@link OllamaResult} containing the structured
* response.
* @throws OllamaBaseException if the response indicates an error status. * @throws OllamaBaseException if the response indicates an error status.
* @throws IOException if an I/O error occurs during the HTTP request. * @throws IOException if an I/O error occurs during the HTTP request.
* @throws InterruptedException if the operation is interrupted. * @throws InterruptedException if the operation is interrupted.
@ -769,7 +716,6 @@ public class OllamaAPI {
structuredResult.getThinking(), structuredResult.getThinking(),
structuredResult.getResponseTime(), structuredResult.getResponseTime(),
statusCode); statusCode);
ollamaResult.setModel(structuredResult.getModel()); ollamaResult.setModel(structuredResult.getModel());
ollamaResult.setCreatedAt(structuredResult.getCreatedAt()); ollamaResult.setCreatedAt(structuredResult.getCreatedAt());
ollamaResult.setDone(structuredResult.isDone()); ollamaResult.setDone(structuredResult.isDone());
@ -794,17 +740,15 @@ public class OllamaAPI {
} }
/** /**
* Generates a response using the specified AI model and prompt, then automatically * Generates a response using the specified AI model and prompt, then automatically detects and
* detects and invokes any tool calls present in the model's output. * invokes any tool calls present in the model's output.
* <p> *
* This method operates in blocking mode. It first augments the prompt with all * <p>This method operates in blocking mode. It first augments the prompt with all registered
* registered tool specifications (unless the prompt already begins with * tool specifications (unless the prompt already begins with {@code [AVAILABLE_TOOLS]}), sends
* {@code [AVAILABLE_TOOLS]}), sends the prompt to the model, and parses the model's * the prompt to the model, and parses the model's response for tool call instructions. If tool
* response for tool call instructions. If tool calls are found, each is invoked * calls are found, each is invoked using the registered tool implementations, and their results
* using the registered tool implementations, and their results are collected. * are collected. <b>Typical usage:</b>
* </p>
* *
* <b>Typical usage:</b>
* <pre>{@code * <pre>{@code
* OllamaToolsResult result = ollamaAPI.generateWithTools( * OllamaToolsResult result = ollamaAPI.generateWithTools(
* "my-model", * "my-model",
@ -820,8 +764,9 @@ public class OllamaAPI {
* @param prompt the input text or prompt to provide to the AI model * @param prompt the input text or prompt to provide to the AI model
* @param options additional options or configurations to use when generating the response * @param options additional options or configurations to use when generating the response
* @param streamHandler handler for streaming responses; if {@code null}, streaming is disabled * @param streamHandler handler for streaming responses; if {@code null}, streaming is disabled
* @return an {@link OllamaToolsResult} containing the model's response and the results of any invoked tools. * @return an {@link OllamaToolsResult} containing the model's response and the results of any
* If the model does not request any tool calls, the tool results map will be empty. * invoked tools. If the model does not request any tool calls, the tool results map will be
* empty.
* @throws OllamaBaseException if the Ollama API returns an error status * @throws OllamaBaseException if the Ollama API returns an error status
* @throws IOException if an I/O error occurs during the HTTP request * @throws IOException if an I/O error occurs during the HTTP request
* @throws InterruptedException if the operation is interrupted * @throws InterruptedException if the operation is interrupted
@ -880,18 +825,13 @@ public class OllamaAPI {
} }
/** /**
* Asynchronously generates a response for a prompt using a model running on the * Asynchronously generates a response for a prompt using a model running on the Ollama server.
* Ollama server.
* <p>
* This method returns an {@link OllamaAsyncResultStreamer} handle that can be
* used to poll for
* status and retrieve streamed "thinking" and response tokens from the model.
* The call is non-blocking.
* </p>
* *
* <p> * <p>This method returns an {@link OllamaAsyncResultStreamer} handle that can be used to poll
* <b>Example usage:</b> * for status and retrieve streamed "thinking" and response tokens from the model. The call is
* </p> * non-blocking.
*
* <p><b>Example usage:</b>
* *
* <pre>{@code * <pre>{@code
* OllamaAsyncResultStreamer resultStreamer = ollamaAPI.generate("gpt-oss:20b", "Who are you", false, true); * OllamaAsyncResultStreamer resultStreamer = ollamaAPI.generate("gpt-oss:20b", "Who are you", false, true);
@ -912,10 +852,9 @@ public class OllamaAPI {
* @param model the Ollama model to use for generating the response * @param model the Ollama model to use for generating the response
* @param prompt the prompt or question text to send to the model * @param prompt the prompt or question text to send to the model
* @param raw if {@code true}, returns the raw response from the model * @param raw if {@code true}, returns the raw response from the model
* @param think if {@code true}, streams "thinking" tokens as well as response * @param think if {@code true}, streams "thinking" tokens as well as response tokens
* tokens * @return an {@link OllamaAsyncResultStreamer} handle for polling and retrieving streamed
* @return an {@link OllamaAsyncResultStreamer} handle for polling and * results
* retrieving streamed results
*/ */
public OllamaAsyncResultStreamer generate( public OllamaAsyncResultStreamer generate(
String model, String prompt, boolean raw, boolean think) { String model, String prompt, boolean raw, boolean think) {
@ -931,28 +870,30 @@ public class OllamaAPI {
} }
/** /**
* Generates a response from a model running on the Ollama server using one or more images as input. * Generates a response from a model running on the Ollama server using one or more images as
* <p> * input.
* This method allows you to provide images (as {@link File}, {@code byte[]}, or image URL {@link String})
* along with a prompt to the specified model. The images are automatically encoded as base64 before being sent.
* Additional model options can be specified via the {@link Options} parameter.
* </p>
* *
* <p> * <p>This method allows you to provide images (as {@link File}, {@code byte[]}, or image URL
* If a {@code streamHandler} is provided, the response will be streamed and the handler will be called * {@link String}) along with a prompt to the specified model. The images are automatically
* for each streamed response chunk. If {@code streamHandler} is {@code null}, streaming is disabled and * encoded as base64 before being sent. Additional model options can be specified via the {@link
* the full response is returned synchronously. * Options} parameter.
* </p> *
* <p>If a {@code streamHandler} is provided, the response will be streamed and the handler will
* be called for each streamed response chunk. If {@code streamHandler} is {@code null},
* streaming is disabled and the full response is returned synchronously.
* *
* @param model the name of the Ollama model to use for generating the response * @param model the name of the Ollama model to use for generating the response
* @param prompt the prompt or question text to send to the model * @param prompt the prompt or question text to send to the model
* @param images a list of images to use for the question; each element must be a {@link File}, {@code byte[]}, or a URL {@link String} * @param images a list of images to use for the question; each element must be a {@link File},
* @param options the {@link Options} object containing model parameters; * {@code byte[]}, or a URL {@link String}
* see <a href="https://github.com/jmorganca/ollama/blob/main/docs/modelfile.md#valid-parameters-and-values">Ollama model options documentation</a> * @param options the {@link Options} object containing model parameters; see <a
* href="https://github.com/jmorganca/ollama/blob/main/docs/modelfile.md#valid-parameters-and-values">Ollama
* model options documentation</a>
* @param streamHandler an optional callback that is invoked for each streamed response chunk; * @param streamHandler an optional callback that is invoked for each streamed response chunk;
* if {@code null}, disables streaming and returns the full response synchronously * if {@code null}, disables streaming and returns the full response synchronously
* @return an {@link OllamaResult} containing the response text and time taken for the response * @return an {@link OllamaResult} containing the response text and time taken for the response
* @throws OllamaBaseException if the response indicates an error status or an invalid image type is provided * @throws OllamaBaseException if the response indicates an error status or an invalid image
* type is provided
* @throws IOException if an I/O error occurs during the HTTP request * @throws IOException if an I/O error occurs during the HTTP request
* @throws InterruptedException if the operation is interrupted * @throws InterruptedException if the operation is interrupted
* @throws URISyntaxException if an image URL is malformed * @throws URISyntaxException if an image URL is malformed
@ -971,7 +912,7 @@ public class OllamaAPI {
LOG.debug("Using image file: {}", ((File) image).getAbsolutePath()); LOG.debug("Using image file: {}", ((File) image).getAbsolutePath());
encodedImages.add(encodeFileToBase64((File) image)); encodedImages.add(encodeFileToBase64((File) image));
} else if (image instanceof byte[]) { } else if (image instanceof byte[]) {
LOG.debug("Using image bytes: {}", ((byte[]) image).length + " bytes"); LOG.debug("Using image bytes: {} bytes", ((byte[]) image).length);
encodedImages.add(encodeByteArrayToBase64((byte[]) image)); encodedImages.add(encodeByteArrayToBase64((byte[]) image));
} else if (image instanceof String) { } else if (image instanceof String) {
LOG.debug("Using image URL: {}", image); LOG.debug("Using image URL: {}", image);
@ -996,20 +937,18 @@ public class OllamaAPI {
} }
/** /**
* Ask a question to a model using an {@link OllamaChatRequest} and set up streaming response. This can be * Ask a question to a model using an {@link OllamaChatRequest} and set up streaming response.
* constructed using an {@link OllamaChatRequestBuilder}. * This can be constructed using an {@link OllamaChatRequestBuilder}.
* <p> *
* Hint: the OllamaChatRequestModel#getStream() property is not implemented. * <p>Hint: the OllamaChatRequestModel#getStream() property is not implemented.
* *
* @param request request object to be sent to the server * @param request request object to be sent to the server
* @param tokenHandler callback handler to handle the last token from stream * @param tokenHandler callback handler to handle the last token from stream (caution: the
* (caution: the previous tokens from stream will not be * previous tokens from stream will not be concatenated)
* concatenated)
* @return {@link OllamaChatResult} * @return {@link OllamaChatResult}
* @throws OllamaBaseException any response code than 200 has been returned * @throws OllamaBaseException any response code than 200 has been returned
* @throws IOException in case the responseStream can not be read * @throws IOException in case the responseStream can not be read
* @throws InterruptedException in case the server is not reachable or network * @throws InterruptedException in case the server is not reachable or network issues happen
* issues happen
* @throws OllamaBaseException if the response indicates an error status * @throws OllamaBaseException if the response indicates an error status
* @throws IOException if an I/O error occurs during the HTTP request * @throws IOException if an I/O error occurs during the HTTP request
* @throws InterruptedException if the operation is interrupted * @throws InterruptedException if the operation is interrupted
@ -1081,12 +1020,10 @@ public class OllamaAPI {
} }
/** /**
* Registers a single tool in the tool registry using the provided tool * Registers a single tool in the tool registry using the provided tool specification.
* specification.
* *
* @param toolSpecification the specification of the tool to register. It * @param toolSpecification the specification of the tool to register. It contains the tool's
* contains the * function name and other relevant information.
* tool's function name and other relevant information.
*/ */
public void registerTool(Tools.ToolSpecification toolSpecification) { public void registerTool(Tools.ToolSpecification toolSpecification) {
toolRegistry.addTool(toolSpecification.getFunctionName(), toolSpecification); toolRegistry.addTool(toolSpecification.getFunctionName(), toolSpecification);
@ -1094,14 +1031,11 @@ public class OllamaAPI {
} }
/** /**
* Registers multiple tools in the tool registry using a list of tool * Registers multiple tools in the tool registry using a list of tool specifications. Iterates
* specifications. * over the list and adds each tool specification to the registry.
* Iterates over the list and adds each tool specification to the registry.
* *
* @param toolSpecifications a list of tool specifications to register. Each * @param toolSpecifications a list of tool specifications to register. Each specification
* specification * contains information about a tool, such as its function name.
* contains information about a tool, such as its
* function name.
*/ */
public void registerTools(List<Tools.ToolSpecification> toolSpecifications) { public void registerTools(List<Tools.ToolSpecification> toolSpecifications) {
for (Tools.ToolSpecification toolSpecification : toolSpecifications) { for (Tools.ToolSpecification toolSpecification : toolSpecifications) {
@ -1110,8 +1044,8 @@ public class OllamaAPI {
} }
/** /**
* Deregisters all tools from the tool registry. * Deregisters all tools from the tool registry. This method removes all registered tools,
* This method removes all registered tools, effectively clearing the registry. * effectively clearing the registry.
*/ */
public void deregisterTools() { public void deregisterTools() {
toolRegistry.clear(); toolRegistry.clear();
@ -1119,25 +1053,22 @@ public class OllamaAPI {
} }
/** /**
* 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 class and its
* class and its providers. * providers. This method scans the caller's class for the {@link OllamaToolService} annotation
* This method scans the caller's class for the {@link OllamaToolService} * and recursively registers annotated tools from all the providers specified in the annotation.
* annotation and recursively registers
* annotated tools from all the providers specified in the annotation.
* *
* @throws IllegalStateException if the caller's class is not annotated with * @throws IllegalStateException if the caller's class is not annotated with {@link
* {@link OllamaToolService}. * OllamaToolService}.
* @throws RuntimeException if any reflection-based instantiation or * @throws RuntimeException if any reflection-based instantiation or invocation fails.
* invocation fails.
*/ */
public void registerAnnotatedTools() { public void registerAnnotatedTools() throws OllamaBaseException {
try { try {
Class<?> callerClass = null; Class<?> callerClass = null;
try { try {
callerClass = callerClass =
Class.forName(Thread.currentThread().getStackTrace()[2].getClassName()); Class.forName(Thread.currentThread().getStackTrace()[2].getClassName());
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
throw new RuntimeException(e); throw new OllamaBaseException(e.getMessage());
} }
OllamaToolService ollamaToolServiceAnnotation = OllamaToolService ollamaToolServiceAnnotation =
@ -1155,22 +1086,18 @@ public class OllamaAPI {
| NoSuchMethodException | NoSuchMethodException
| IllegalAccessException | IllegalAccessException
| InvocationTargetException e) { | InvocationTargetException e) {
throw new RuntimeException(e); throw new OllamaBaseException(e.getMessage());
} }
} }
/** /**
* Registers tools based on the annotations found on the methods of the provided * Registers tools based on the annotations found on the methods of the provided object. This
* object. * method scans the methods of the given object and registers tools using the {@link ToolSpec}
* This method scans the methods of the given object and registers tools using * annotation and associated {@link ToolProperty} annotations. It constructs tool specifications
* the {@link ToolSpec} annotation * and stores them in a tool registry.
* and associated {@link ToolProperty} annotations. It constructs tool
* specifications and stores them in a tool registry.
* *
* @param object the object whose methods are to be inspected for annotated * @param object the object whose methods are to be inspected for annotated tools.
* tools. * @throws RuntimeException if any reflection-based instantiation or invocation fails.
* @throws RuntimeException if any reflection-based instantiation or invocation
* fails.
*/ */
public void registerAnnotatedTools(Object object) { public void registerAnnotatedTools(Object object) {
Class<?> objectClass = object.getClass(); Class<?> objectClass = object.getClass();
@ -1267,8 +1194,7 @@ public class OllamaAPI {
* *
* @param roleName the name of the role to retrieve * @param roleName the name of the role to retrieve
* @return the OllamaChatMessageRole associated with the given name * @return the OllamaChatMessageRole associated with the given name
* @throws RoleNotFoundException if the role with the specified name does not * @throws RoleNotFoundException if the role with the specified name does not exist
* exist
*/ */
public OllamaChatMessageRole getRole(String roleName) throws RoleNotFoundException { public OllamaChatMessageRole getRole(String roleName) throws RoleNotFoundException {
return OllamaChatMessageRole.getRole(roleName); return OllamaChatMessageRole.getRole(roleName);
@ -1298,23 +1224,17 @@ public class OllamaAPI {
} }
/** /**
* Generates a request for the Ollama API and returns the result. * Generates a request for the Ollama API and returns the result. This method synchronously
* This method synchronously calls the Ollama API. If a stream handler is * calls the Ollama API. If a stream handler is provided, the request will be streamed;
* provided, * otherwise, a regular synchronous request will be made.
* the request will be streamed; otherwise, a regular synchronous request will
* be made.
* *
* @param ollamaRequestModel the request model containing necessary * @param ollamaRequestModel the request model containing necessary parameters for the Ollama
* parameters * API request.
* for the Ollama API request. * @param responseStreamHandler the stream handler to process streaming responses, or null for
* @param responseStreamHandler the stream handler to process streaming * non-streaming requests.
* responses,
* or null for non-streaming requests.
* @return the result of the Ollama API request. * @return the result of the Ollama API request.
* @throws OllamaBaseException if the request fails due to an issue with the * @throws OllamaBaseException if the request fails due to an issue with the Ollama API.
* Ollama API. * @throws IOException if an I/O error occurs during the request process.
* @throws IOException if an I/O error occurs during the request
* process.
* @throws InterruptedException if the thread is interrupted during the request. * @throws InterruptedException if the thread is interrupted during the request.
*/ */
private OllamaResult generateSyncForOllamaRequestModel( private OllamaResult generateSyncForOllamaRequestModel(

View File

@ -1,21 +0,0 @@
/*
* Ollama4j - Java library for interacting with Ollama server.
* Copyright (c) 2025 Amith Koujalgi and contributors.
*
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
*
*/
package io.github.ollama4j.models.embeddings;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
import lombok.Data;
@SuppressWarnings("unused")
@Data
@Deprecated(since = "1.0.90")
public class OllamaEmbeddingResponseModel {
@JsonProperty("embedding")
private List<Double> embedding;
}

View File

@ -1,39 +0,0 @@
/*
* Ollama4j - Java library for interacting with Ollama server.
* Copyright (c) 2025 Amith Koujalgi and contributors.
*
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
*
*/
package io.github.ollama4j.models.embeddings;
import io.github.ollama4j.utils.Options;
@Deprecated(since = "1.0.90")
public class OllamaEmbeddingsRequestBuilder {
private OllamaEmbeddingsRequestBuilder(String model, String prompt) {
request = new OllamaEmbeddingsRequestModel(model, prompt);
}
private OllamaEmbeddingsRequestModel request;
public static OllamaEmbeddingsRequestBuilder getInstance(String model, String prompt) {
return new OllamaEmbeddingsRequestBuilder(model, prompt);
}
public OllamaEmbeddingsRequestModel build() {
return request;
}
public OllamaEmbeddingsRequestBuilder withOptions(Options options) {
this.request.setOptions(options.getOptionsMap());
return this;
}
public OllamaEmbeddingsRequestBuilder withKeepAlive(String keepAlive) {
this.request.setKeepAlive(keepAlive);
return this;
}
}

View File

@ -1,42 +0,0 @@
/*
* Ollama4j - Java library for interacting with Ollama server.
* Copyright (c) 2025 Amith Koujalgi and contributors.
*
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
*
*/
package io.github.ollama4j.models.embeddings;
import static io.github.ollama4j.utils.Utils.getObjectMapper;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import java.util.Map;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
@Data
@RequiredArgsConstructor
@NoArgsConstructor
@Deprecated(since = "1.0.90")
public class OllamaEmbeddingsRequestModel {
@NonNull private String model;
@NonNull private String prompt;
protected Map<String, Object> options;
@JsonProperty(value = "keep_alive")
private String keepAlive;
@Override
public String toString() {
try {
return getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -1,24 +0,0 @@
/*
* Ollama4j - Java library for interacting with Ollama server.
* Copyright (c) 2025 Amith Koujalgi and contributors.
*
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
*
*/
package io.github.ollama4j.models.response;
import java.util.ArrayList;
import java.util.List;
import lombok.Data;
@Data
public class LibraryModel {
private String name;
private String description;
private String pullCount;
private int totalTags;
private List<String> popularTags = new ArrayList<>();
private String lastUpdated;
}

View File

@ -1,19 +0,0 @@
/*
* Ollama4j - Java library for interacting with Ollama server.
* Copyright (c) 2025 Amith Koujalgi and contributors.
*
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
*
*/
package io.github.ollama4j.models.response;
import java.util.List;
import lombok.Data;
@Data
public class LibraryModelDetail {
private LibraryModel model;
private List<LibraryModelTag> tags;
}

View File

@ -1,19 +0,0 @@
/*
* Ollama4j - Java library for interacting with Ollama server.
* Copyright (c) 2025 Amith Koujalgi and contributors.
*
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
*
*/
package io.github.ollama4j.models.response;
import lombok.Data;
@Data
public class LibraryModelTag {
private String name;
private String tag;
private String size;
private String lastUpdated;
}

View File

@ -1,95 +0,0 @@
/*
* Ollama4j - Java library for interacting with Ollama server.
* Copyright (c) 2025 Amith Koujalgi and contributors.
*
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
*
*/
package io.github.ollama4j.types;
/**
* A class to provide constants for all the supported models by Ollama.
*
* <p>Refer to the full list of models and the details here: <a
* href="https://ollama.ai/library">https://ollama.ai/library</a>
*/
@SuppressWarnings("ALL")
public class OllamaModelType {
public static final String GEMMA = "gemma";
public static final String GEMMA2 = "gemma2";
public static final String LLAMA2 = "llama2";
public static final String LLAMA3 = "llama3";
public static final String LLAMA3_1 = "llama3.1";
public static final String MISTRAL = "mistral";
public static final String MIXTRAL = "mixtral";
public static final String DEEPSEEK_R1 = "deepseek-r1";
public static final String LLAVA = "llava";
public static final String LLAVA_PHI3 = "llava-phi3";
public static final String NEURAL_CHAT = "neural-chat";
public static final String CODELLAMA = "codellama";
public static final String DOLPHIN_MIXTRAL = "dolphin-mixtral";
public static final String MISTRAL_OPENORCA = "mistral-openorca";
public static final String LLAMA2_UNCENSORED = "llama2-uncensored";
public static final String PHI = "phi";
public static final String PHI3 = "phi3";
public static final String ORCA_MINI = "orca-mini";
public static final String DEEPSEEK_CODER = "deepseek-coder";
public static final String DOLPHIN_MISTRAL = "dolphin-mistral";
public static final String VICUNA = "vicuna";
public static final String WIZARD_VICUNA_UNCENSORED = "wizard-vicuna-uncensored";
public static final String ZEPHYR = "zephyr";
public static final String OPENHERMES = "openhermes";
public static final String QWEN = "qwen";
public static final String QWEN2 = "qwen2";
public static final String WIZARDCODER = "wizardcoder";
public static final String LLAMA2_CHINESE = "llama2-chinese";
public static final String TINYLLAMA = "tinyllama";
public static final String PHIND_CODELLAMA = "phind-codellama";
public static final String OPENCHAT = "openchat";
public static final String ORCA2 = "orca2";
public static final String FALCON = "falcon";
public static final String WIZARD_MATH = "wizard-math";
public static final String TINYDOLPHIN = "tinydolphin";
public static final String NOUS_HERMES = "nous-hermes";
public static final String YI = "yi";
public static final String DOLPHIN_PHI = "dolphin-phi";
public static final String STARLING_LM = "starling-lm";
public static final String STARCODER = "starcoder";
public static final String CODEUP = "codeup";
public static final String MEDLLAMA2 = "medllama2";
public static final String STABLE_CODE = "stable-code";
public static final String WIZARDLM_UNCENSORED = "wizardlm-uncensored";
public static final String BAKLLAVA = "bakllava";
public static final String EVERYTHINGLM = "everythinglm";
public static final String SOLAR = "solar";
public static final String STABLE_BELUGA = "stable-beluga";
public static final String SQLCODER = "sqlcoder";
public static final String YARN_MISTRAL = "yarn-mistral";
public static final String NOUS_HERMES2_MIXTRAL = "nous-hermes2-mixtral";
public static final String SAMANTHA_MISTRAL = "samantha-mistral";
public static final String STABLELM_ZEPHYR = "stablelm-zephyr";
public static final String MEDITRON = "meditron";
public static final String WIZARD_VICUNA = "wizard-vicuna";
public static final String STABLELM2 = "stablelm2";
public static final String MAGICODER = "magicoder";
public static final String YARN_LLAMA2 = "yarn-llama2";
public static final String NOUS_HERMES2 = "nous-hermes2";
public static final String DEEPSEEK_LLM = "deepseek-llm";
public static final String LLAMA_PRO = "llama-pro";
public static final String OPEN_ORCA_PLATYPUS2 = "open-orca-platypus2";
public static final String CODEBOOGA = "codebooga";
public static final String MISTRALLITE = "mistrallite";
public static final String NEXUSRAVEN = "nexusraven";
public static final String GOLIATH = "goliath";
public static final String NOMIC_EMBED_TEXT = "nomic-embed-text";
public static final String NOTUX = "notux";
public static final String ALFRED = "alfred";
public static final String MEGADOLPHIN = "megadolphin";
public static final String WIZARDLM = "wizardlm";
public static final String XWINLM = "xwinlm";
public static final String NOTUS = "notus";
public static final String DUCKDB_NSQL = "duckdb-nsql";
public static final String ALL_MINILM = "all-minilm";
public static final String CODESTRAL = "codestral";
}

View File

@ -148,18 +148,30 @@ public class WithAuth {
@Order(1) @Order(1)
void testOllamaBehindProxy() { void testOllamaBehindProxy() {
api.setBearerAuth(BEARER_AUTH_TOKEN); api.setBearerAuth(BEARER_AUTH_TOKEN);
try {
assertTrue( assertTrue(
api.ping(), api.ping(),
"Expected OllamaAPI to successfully ping through NGINX with valid auth token."); "Expected OllamaAPI to successfully ping through NGINX with valid auth token.");
} catch (Exception e) {
fail("Exception occurred while pinging OllamaAPI through NGINX: " + e.getMessage(), e);
}
} }
@Test @Test
@Order(1) @Order(1)
void testWithWrongToken() { void testWithWrongToken() {
api.setBearerAuth("wrong-token"); api.setBearerAuth("wrong-token");
try {
assertFalse( assertFalse(
api.ping(), api.ping(),
"Expected OllamaAPI ping to fail through NGINX with an invalid auth token."); "Expected OllamaAPI ping to fail through NGINX with an invalid auth token.");
} catch (Exception e) {
// If an exception is thrown, that's also an expected failure for a wrong token
// (e.g., OllamaBaseException or IOException)
// Optionally, you can assert the type/message of the exception if needed
// For now, we treat any exception as a pass for this negative test
return;
}
} }
@Test @Test

View File

@ -22,7 +22,8 @@ import io.github.ollama4j.models.request.CustomModelRequest;
import io.github.ollama4j.models.response.ModelDetail; import io.github.ollama4j.models.response.ModelDetail;
import io.github.ollama4j.models.response.OllamaAsyncResultStreamer; import io.github.ollama4j.models.response.OllamaAsyncResultStreamer;
import io.github.ollama4j.models.response.OllamaResult; import io.github.ollama4j.models.response.OllamaResult;
import io.github.ollama4j.types.OllamaModelType; import io.github.ollama4j.tools.Tools;
import io.github.ollama4j.tools.sampletools.WeatherTool;
import io.github.ollama4j.utils.OptionsBuilder; import io.github.ollama4j.utils.OptionsBuilder;
import java.io.IOException; import java.io.IOException;
import java.net.URISyntaxException; import java.net.URISyntaxException;
@ -36,7 +37,7 @@ class TestMockedAPIs {
@Test @Test
void testPullModel() { void testPullModel() {
OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class); OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class);
String model = OllamaModelType.LLAMA2; String model = "llama2";
try { try {
doNothing().when(ollamaAPI).pullModel(model); doNothing().when(ollamaAPI).pullModel(model);
ollamaAPI.pullModel(model); ollamaAPI.pullModel(model);
@ -79,7 +80,7 @@ class TestMockedAPIs {
@Test @Test
void testDeleteModel() { void testDeleteModel() {
OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class); OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class);
String model = OllamaModelType.LLAMA2; String model = "llama2";
try { try {
doNothing().when(ollamaAPI).deleteModel(model, true); doNothing().when(ollamaAPI).deleteModel(model, true);
ollamaAPI.deleteModel(model, true); ollamaAPI.deleteModel(model, true);
@ -89,10 +90,24 @@ class TestMockedAPIs {
} }
} }
@Test
void testRegisteredTools() {
OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class);
doNothing().when(ollamaAPI).registerTools(Collections.emptyList());
ollamaAPI.registerTools(Collections.emptyList());
verify(ollamaAPI, times(1)).registerTools(Collections.emptyList());
List<Tools.ToolSpecification> toolSpecifications = new ArrayList<>();
toolSpecifications.add(new WeatherTool().getSpecification());
doNothing().when(ollamaAPI).registerTools(toolSpecifications);
ollamaAPI.registerTools(toolSpecifications);
verify(ollamaAPI, times(1)).registerTools(toolSpecifications);
}
@Test @Test
void testGetModelDetails() { void testGetModelDetails() {
OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class); OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class);
String model = OllamaModelType.LLAMA2; String model = "llama2";
try { try {
when(ollamaAPI.getModelDetails(model)).thenReturn(new ModelDetail()); when(ollamaAPI.getModelDetails(model)).thenReturn(new ModelDetail());
ollamaAPI.getModelDetails(model); ollamaAPI.getModelDetails(model);
@ -105,7 +120,7 @@ class TestMockedAPIs {
@Test @Test
void testGenerateEmbeddings() { void testGenerateEmbeddings() {
OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class); OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class);
String model = OllamaModelType.LLAMA2; String model = "llama2";
String prompt = "some prompt text"; String prompt = "some prompt text";
try { try {
OllamaEmbedRequestModel m = new OllamaEmbedRequestModel(); OllamaEmbedRequestModel m = new OllamaEmbedRequestModel();
@ -122,7 +137,7 @@ class TestMockedAPIs {
@Test @Test
void testEmbed() { void testEmbed() {
OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class); OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class);
String model = OllamaModelType.LLAMA2; String model = "llama2";
List<String> inputs = List.of("some prompt text"); List<String> inputs = List.of("some prompt text");
try { try {
OllamaEmbedRequestModel m = new OllamaEmbedRequestModel(model, inputs); OllamaEmbedRequestModel m = new OllamaEmbedRequestModel(model, inputs);
@ -137,7 +152,7 @@ class TestMockedAPIs {
@Test @Test
void testEmbedWithEmbedRequestModel() { void testEmbedWithEmbedRequestModel() {
OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class); OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class);
String model = OllamaModelType.LLAMA2; String model = "llama2";
List<String> inputs = List.of("some prompt text"); List<String> inputs = List.of("some prompt text");
try { try {
when(ollamaAPI.embed(new OllamaEmbedRequestModel(model, inputs))) when(ollamaAPI.embed(new OllamaEmbedRequestModel(model, inputs)))
@ -152,7 +167,7 @@ class TestMockedAPIs {
@Test @Test
void testAsk() { void testAsk() {
OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class); OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class);
String model = OllamaModelType.LLAMA2; String model = "llama2";
String prompt = "some prompt text"; String prompt = "some prompt text";
OptionsBuilder optionsBuilder = new OptionsBuilder(); OptionsBuilder optionsBuilder = new OptionsBuilder();
try { try {
@ -169,7 +184,7 @@ class TestMockedAPIs {
@Test @Test
void testAskWithImageFiles() { void testAskWithImageFiles() {
OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class); OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class);
String model = OllamaModelType.LLAMA2; String model = "llama2";
String prompt = "some prompt text"; String prompt = "some prompt text";
try { try {
when(ollamaAPI.generateWithImages( when(ollamaAPI.generateWithImages(
@ -203,7 +218,7 @@ class TestMockedAPIs {
@Test @Test
void testAskWithImageURLs() { void testAskWithImageURLs() {
OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class); OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class);
String model = OllamaModelType.LLAMA2; String model = "llama2";
String prompt = "some prompt text"; String prompt = "some prompt text";
try { try {
when(ollamaAPI.generateWithImages( when(ollamaAPI.generateWithImages(
@ -237,7 +252,7 @@ class TestMockedAPIs {
@Test @Test
void testAskAsync() { void testAskAsync() {
OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class); OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class);
String model = OllamaModelType.LLAMA2; String model = "llama2";
String prompt = "some prompt text"; String prompt = "some prompt text";
when(ollamaAPI.generate(model, prompt, false, false)) when(ollamaAPI.generate(model, prompt, false, false))
.thenReturn(new OllamaAsyncResultStreamer(null, null, 3)); .thenReturn(new OllamaAsyncResultStreamer(null, null, 3));