diff --git a/pom.xml b/pom.xml
index 2c9ac67..8483ce3 100644
--- a/pom.xml
+++ b/pom.xml
@@ -275,6 +275,15 @@
slf4j-api2.0.17
+
+
+
+
+
+
+
+
+
org.junit.jupiterjunit-jupiter-api
diff --git a/src/main/java/io/github/ollama4j/OllamaAPI.java b/src/main/java/io/github/ollama4j/OllamaAPI.java
index 6e95ee5..3bd55c1 100644
--- a/src/main/java/io/github/ollama4j/OllamaAPI.java
+++ b/src/main/java/io/github/ollama4j/OllamaAPI.java
@@ -8,7 +8,6 @@
*/
package io.github.ollama4j;
-import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.ollama4j.exceptions.OllamaBaseException;
import io.github.ollama4j.exceptions.RoleNotFoundException;
@@ -20,7 +19,6 @@ import io.github.ollama4j.models.chat.OllamaChatTokenHandler;
import io.github.ollama4j.models.embeddings.OllamaEmbedRequestModel;
import io.github.ollama4j.models.embeddings.OllamaEmbedResponseModel;
import io.github.ollama4j.models.generate.OllamaGenerateRequest;
-import io.github.ollama4j.models.generate.OllamaGenerateRequestBuilder;
import io.github.ollama4j.models.generate.OllamaGenerateStreamObserver;
import io.github.ollama4j.models.generate.OllamaGenerateTokenHandler;
import io.github.ollama4j.models.ps.ModelsProcessResponse;
@@ -31,7 +29,6 @@ import io.github.ollama4j.tools.annotations.OllamaToolService;
import io.github.ollama4j.tools.annotations.ToolProperty;
import io.github.ollama4j.tools.annotations.ToolSpec;
import io.github.ollama4j.utils.Constants;
-import io.github.ollama4j.utils.Options;
import io.github.ollama4j.utils.Utils;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
@@ -730,39 +727,6 @@ public class OllamaAPI {
}
}
- /**
- * Generates a response from a model using the specified parameters and stream observer.
- *
- * @param model the model name
- * @param prompt the prompt to send
- * @param raw whether to return the raw response
- * @param think whether to stream "thinking" tokens
- * @param options additional options
- * @param streamObserver the stream observer for handling streamed responses
- * @return the OllamaResult containing the response
- * @throws OllamaBaseException if the request fails
- */
- @Deprecated
- private OllamaResult generate(
- String model,
- String prompt,
- boolean raw,
- boolean think,
- Options options,
- OllamaGenerateStreamObserver streamObserver)
- throws OllamaBaseException {
- OllamaGenerateRequest request =
- OllamaGenerateRequestBuilder.builder()
- .withModel(model)
- .withPrompt(prompt)
- .withRaw(raw)
- .withThink(think)
- .withOptions(options)
- .withKeepAlive("0m")
- .build();
- return generate(request, streamObserver);
- }
-
/**
* Generates a response from a model using the specified parameters and stream observer. If
* {@code streamObserver} is provided, streaming is enabled; otherwise, a synchronous call is
@@ -796,179 +760,34 @@ public class OllamaAPI {
private OllamaResult generateWithToolsInternal(
OllamaGenerateRequest request, OllamaGenerateStreamObserver streamObserver)
throws OllamaBaseException {
- try {
- boolean raw = true;
- OllamaToolsResult toolResult = new OllamaToolsResult();
- Map toolResults = new HashMap<>();
-
- String prompt = request.getPrompt();
- if (!prompt.startsWith("[AVAILABLE_TOOLS]")) {
- final Tools.PromptBuilder promptBuilder = new Tools.PromptBuilder();
- for (Tools.ToolSpecification spec : toolRegistry.getRegisteredSpecs()) {
- promptBuilder.withToolSpecification(spec);
- }
- promptBuilder.withPrompt(prompt);
- prompt = promptBuilder.build();
- }
-
- request.setPrompt(prompt);
- request.setRaw(raw);
- request.setThink(false);
-
- OllamaResult result =
- generate(
- request,
- new OllamaGenerateStreamObserver(
- null,
- streamObserver != null
- ? streamObserver.getResponseStreamHandler()
- : null));
- toolResult.setModelResult(result);
-
- String toolsResponse = result.getResponse();
- if (toolsResponse.contains("[TOOL_CALLS]")) {
- toolsResponse = toolsResponse.replace("[TOOL_CALLS]", "");
- }
-
- List toolFunctionCallSpecs = new ArrayList<>();
- ObjectMapper objectMapper = Utils.getObjectMapper();
-
- if (!toolsResponse.isEmpty()) {
- try {
- objectMapper.readTree(toolsResponse);
- } catch (JsonParseException e) {
- return result;
- }
- toolFunctionCallSpecs =
- objectMapper.readValue(
- toolsResponse,
- objectMapper
- .getTypeFactory()
- .constructCollectionType(
- List.class, ToolFunctionCallSpec.class));
- }
- for (ToolFunctionCallSpec toolFunctionCallSpec : toolFunctionCallSpecs) {
- toolResults.put(toolFunctionCallSpec, invokeTool(toolFunctionCallSpec));
- }
- toolResult.setToolResults(toolResults);
- return result;
- } catch (Exception e) {
- throw new OllamaBaseException(e.getMessage(), e);
+ List tools = new ArrayList<>();
+ for (Tools.ToolSpecification spec : toolRegistry.getRegisteredSpecs()) {
+ tools.add(spec.getToolPrompt());
}
- }
-
- /**
- * Generates structured output from the specified AI model and prompt.
- *
- *
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 the response.
- * @param prompt The input text or prompt to provide to the AI model.
- * @param format A map containing the format specification for the structured output.
- * @return An instance of {@link OllamaResult} containing the structured response.
- * @throws OllamaBaseException if the response indicates an error status.
- */
- @Deprecated
- @SuppressWarnings("LoggingSimilarMessage")
- private OllamaResult generateWithFormat(String model, String prompt, Map format)
- throws OllamaBaseException {
- OllamaGenerateRequest request =
- OllamaGenerateRequestBuilder.builder()
- .withModel(model)
- .withPrompt(prompt)
- .withFormat(format)
- .withThink(false)
- .build();
- return generate(request, null);
- }
-
- /**
- * Generates a response using the specified AI model and prompt, then automatically detects and
- * invokes any tool calls present in the model's output.
- *
- *
This method operates in blocking mode. It first augments the prompt with all registered
- * tool specifications (unless the prompt already begins with {@code [AVAILABLE_TOOLS]}), sends
- * the prompt to the model, and parses the model's response for tool call instructions. If tool
- * calls are found, each is invoked using the registered tool implementations, and their results
- * are collected.
- *
- *
Typical usage:
- *
- *
{@code
- * OllamaToolsResult result = ollamaAPI.generateWithTools(
- * "my-model",
- * "What is the weather in Bengaluru?",
- * Options.defaultOptions(),
- * null // or a custom OllamaStreamHandler for streaming
- * );
- * String modelResponse = result.getModelResult().getResponse();
- * Map toolResults = result.getToolResults();
- * }
- *
- * @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 options additional options or configurations to use when generating the response
- * @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. 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
- */
- @Deprecated
- private OllamaToolsResult generateWithTools(
- String model, String prompt, Options options, OllamaGenerateTokenHandler streamHandler)
- throws OllamaBaseException {
- OllamaGenerateRequest request =
- OllamaGenerateRequestBuilder.builder()
- .withModel(model)
- .withPrompt(prompt)
- .withOptions(options)
- .withUseTools(true)
- .build();
- // Execute unified path, but also return tools result by re-parsing
- OllamaResult res = generate(request, new OllamaGenerateStreamObserver(null, streamHandler));
- OllamaToolsResult tr = new OllamaToolsResult();
- tr.setModelResult(res);
- return tr;
- }
-
- /**
- * Asynchronously generates a response for a prompt using a model running on the Ollama server.
- *
- *
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.
- *
- *
- *
- * @param model the Ollama model to use for generating the response
- * @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 think if {@code true}, streams "thinking" tokens as well as response tokens
- * @return an {@link OllamaAsyncResultStreamer} handle for polling and retrieving streamed
- * results
- * @throws OllamaBaseException if the request fails
- */
- @Deprecated
- private OllamaAsyncResultStreamer generate(
- String model, String prompt, boolean raw, boolean think) throws OllamaBaseException {
- return generateAsync(model, prompt, raw, think);
+ ArrayList msgs = new ArrayList<>();
+ OllamaChatRequest chatRequest = new OllamaChatRequest();
+ chatRequest.setModel(request.getModel());
+ OllamaChatMessage ocm = new OllamaChatMessage();
+ ocm.setRole(OllamaChatMessageRole.USER);
+ ocm.setResponse(request.getPrompt());
+ chatRequest.setMessages(msgs);
+ msgs.add(ocm);
+ OllamaChatTokenHandler hdlr = null;
+ chatRequest.setTools(tools);
+ if (streamObserver != null) {
+ chatRequest.setStream(true);
+ hdlr =
+ chatResponseModel ->
+ streamObserver
+ .getResponseStreamHandler()
+ .accept(chatResponseModel.getMessage().getResponse());
+ }
+ OllamaChatResult res = chat(chatRequest, hdlr);
+ return new OllamaResult(
+ res.getResponseModel().getMessage().getResponse(),
+ res.getResponseModel().getMessage().getThinking(),
+ res.getResponseModel().getTotalDuration(),
+ -1);
}
public OllamaAsyncResultStreamer generateAsync(
@@ -996,83 +815,6 @@ public class OllamaAPI {
}
}
- /**
- * Generates a response from a model running on the Ollama server using one or more images as
- * 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.
- *
- *
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 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 options the {@link Options} object containing model parameters; see Ollama
- * model options documentation
- * @param format a map specifying the output format, or null for default
- * @param streamHandler an optional callback that is invoked for each streamed response chunk;
- * 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
- * @throws OllamaBaseException if the response indicates an error status or an invalid image
- * type is provided
- */
- @Deprecated
- private OllamaResult generateWithImages(
- String model,
- String prompt,
- List