response = null;
+ response = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
+ int statusCode = response.statusCode();
+ String responseString = response.body();
+ if (statusCode == 200) {
+ return Utils.getObjectMapper()
+ .readValue(responseString, ModelsProcessResponse.class);
+ } else {
+ throw new OllamaBaseException(statusCode + " - " + responseString);
+ }
+ }
+
/**
* List available models from Ollama server.
*
@@ -339,6 +375,7 @@ public class OllamaAPI {
}
}
+
/**
* Generate response for a question to a model running on Ollama server. This is a sync/blocking
* call.
@@ -351,23 +388,67 @@ public class OllamaAPI {
* @param streamHandler optional callback consumer that will be applied every time a streamed response is received. If not set, the stream parameter of the request is set to false.
* @return OllamaResult that includes response text and time taken for response
*/
- public OllamaResult generate(String model, String prompt, Options options, OllamaStreamHandler streamHandler)
+ public OllamaResult generate(String model, String prompt, boolean raw, Options options, OllamaStreamHandler streamHandler)
throws OllamaBaseException, IOException, InterruptedException {
- OllamaGenerateRequestModel ollamaRequestModel = new OllamaGenerateRequestModel(model, prompt);
+ OllamaGenerateRequest ollamaRequestModel = new OllamaGenerateRequest(model, prompt);
+ ollamaRequestModel.setRaw(raw);
ollamaRequestModel.setOptions(options.getOptionsMap());
return generateSyncForOllamaRequestModel(ollamaRequestModel, streamHandler);
}
/**
- * Convenience method to call Ollama API without streaming responses.
+ * Generates response using the specified AI model and prompt (in blocking mode).
*
- * Uses {@link #generate(String, String, Options, OllamaStreamHandler)}
+ * Uses {@link #generate(String, String, boolean, Options, OllamaStreamHandler)}
+ *
+ * @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 raw In some cases, you may wish to bypass the templating system and provide a full prompt. In this case, you can use the raw parameter to disable templating. Also note that raw mode will not return a context.
+ * @param options Additional options or configurations to use when generating the response.
+ * @return {@link OllamaResult}
*/
- public OllamaResult generate(String model, String prompt, Options options)
+ public OllamaResult generate(String model, String prompt, boolean raw, Options options)
throws OllamaBaseException, IOException, InterruptedException {
- return generate(model, prompt, options, null);
+ return generate(model, prompt, raw, options, null);
}
+
+ /**
+ * Generates response using the specified AI model and prompt (in blocking mode), and then invokes a set of tools
+ * on the generated 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 options Additional options or configurations to use when generating the response.
+ * @return {@link OllamaToolsResult} An OllamaToolsResult object containing the response from the AI model and the results of invoking the tools on that output.
+ * @throws OllamaBaseException If there is an error related to the Ollama API or service.
+ * @throws IOException If there is an error related to input/output operations.
+ * @throws InterruptedException If the method is interrupted while waiting for the AI model
+ * to generate the response or for the tools to be invoked.
+ */
+ public OllamaToolsResult generateWithTools(String model, String prompt, Options options)
+ throws OllamaBaseException, IOException, InterruptedException, ToolInvocationException {
+ boolean raw = true;
+ OllamaToolsResult toolResult = new OllamaToolsResult();
+ Map toolResults = new HashMap<>();
+
+ OllamaResult result = generate(model, prompt, raw, options, null);
+ toolResult.setModelResult(result);
+
+ String toolsResponse = result.getResponse();
+ if (toolsResponse.contains("[TOOL_CALLS]")) {
+ toolsResponse = toolsResponse.replace("[TOOL_CALLS]", "");
+ }
+
+ List toolFunctionCallSpecs = Utils.getObjectMapper().readValue(toolsResponse, Utils.getObjectMapper().getTypeFactory().constructCollectionType(List.class, ToolFunctionCallSpec.class));
+ for (ToolFunctionCallSpec toolFunctionCallSpec : toolFunctionCallSpecs) {
+ toolResults.put(toolFunctionCallSpec, invokeTool(toolFunctionCallSpec));
+ }
+ toolResult.setToolResults(toolResults);
+ return toolResult;
+ }
+
+
/**
* Generate response for a question to a model running on Ollama server and get a callback handle
* that can be used to check for status and get the response from the model later. This would be
@@ -377,15 +458,15 @@ public class OllamaAPI {
* @param prompt the prompt/question text
* @return the ollama async result callback handle
*/
- public OllamaAsyncResultCallback generateAsync(String model, String prompt) {
- OllamaGenerateRequestModel ollamaRequestModel = new OllamaGenerateRequestModel(model, prompt);
-
+ public OllamaAsyncResultStreamer generateAsync(String model, String prompt, boolean raw) {
+ OllamaGenerateRequest ollamaRequestModel = new OllamaGenerateRequest(model, prompt);
+ ollamaRequestModel.setRaw(raw);
URI uri = URI.create(this.host + "/api/generate");
- OllamaAsyncResultCallback ollamaAsyncResultCallback =
- new OllamaAsyncResultCallback(
+ OllamaAsyncResultStreamer ollamaAsyncResultStreamer =
+ new OllamaAsyncResultStreamer(
getRequestBuilderDefault(uri), ollamaRequestModel, requestTimeoutSeconds);
- ollamaAsyncResultCallback.start();
- return ollamaAsyncResultCallback;
+ ollamaAsyncResultStreamer.start();
+ return ollamaAsyncResultStreamer;
}
/**
@@ -408,7 +489,7 @@ public class OllamaAPI {
for (File imageFile : imageFiles) {
images.add(encodeFileToBase64(imageFile));
}
- OllamaGenerateRequestModel ollamaRequestModel = new OllamaGenerateRequestModel(model, prompt, images);
+ OllamaGenerateRequest ollamaRequestModel = new OllamaGenerateRequest(model, prompt, images);
ollamaRequestModel.setOptions(options.getOptionsMap());
return generateSyncForOllamaRequestModel(ollamaRequestModel, streamHandler);
}
@@ -444,7 +525,7 @@ public class OllamaAPI {
for (String imageURL : imageURLs) {
images.add(encodeByteArrayToBase64(Utils.loadImageBytesFromUrl(imageURL)));
}
- OllamaGenerateRequestModel ollamaRequestModel = new OllamaGenerateRequestModel(model, prompt, images);
+ OllamaGenerateRequest ollamaRequestModel = new OllamaGenerateRequest(model, prompt, images);
ollamaRequestModel.setOptions(options.getOptionsMap());
return generateSyncForOllamaRequestModel(ollamaRequestModel, streamHandler);
}
@@ -478,33 +559,33 @@ public class OllamaAPI {
}
/**
- * Ask a question to a model using an {@link OllamaChatRequestModel}. This can be constructed using an {@link OllamaChatRequestBuilder}.
+ * Ask a question to a model using an {@link OllamaChatRequest}. This can be constructed using an {@link OllamaChatRequestBuilder}.
*
* Hint: the OllamaChatRequestModel#getStream() property is not implemented.
*
* @param request request object to be sent to the server
- * @return
+ * @return {@link OllamaChatResult}
* @throws OllamaBaseException any response code than 200 has been returned
* @throws IOException in case the responseStream can not be read
* @throws InterruptedException in case the server is not reachable or network issues happen
*/
- public OllamaChatResult chat(OllamaChatRequestModel request) throws OllamaBaseException, IOException, InterruptedException {
+ public OllamaChatResult chat(OllamaChatRequest request) throws OllamaBaseException, IOException, InterruptedException {
return chat(request, null);
}
/**
- * Ask a question to a model using an {@link OllamaChatRequestModel}. This can be constructed using an {@link OllamaChatRequestBuilder}.
+ * Ask a question to a model using an {@link OllamaChatRequest}. This can be constructed using an {@link OllamaChatRequestBuilder}.
*
* Hint: the OllamaChatRequestModel#getStream() property is not implemented.
*
* @param request request object to be sent to the server
* @param streamHandler callback handler to handle the last message from stream (caution: all previous messages from stream will be concatenated)
- * @return
+ * @return {@link OllamaChatResult}
* @throws OllamaBaseException any response code than 200 has been returned
* @throws IOException in case the responseStream can not be read
* @throws InterruptedException in case the server is not reachable or network issues happen
*/
- public OllamaChatResult chat(OllamaChatRequestModel request, OllamaStreamHandler streamHandler) throws OllamaBaseException, IOException, InterruptedException {
+ public OllamaChatResult chat(OllamaChatRequest request, OllamaStreamHandler streamHandler) throws OllamaBaseException, IOException, InterruptedException {
OllamaChatEndpointCaller requestCaller = new OllamaChatEndpointCaller(host, basicAuth, requestTimeoutSeconds, verbose);
OllamaResult result;
if (streamHandler != null) {
@@ -516,6 +597,10 @@ public class OllamaAPI {
return new OllamaChatResult(result.getResponse(), result.getResponseTime(), result.getHttpStatusCode(), request.getMessages());
}
+ public void registerTool(Tools.ToolSpecification toolSpecification) {
+ toolRegistry.addFunction(toolSpecification.getFunctionName(), toolSpecification.getToolDefinition());
+ }
+
// technical private methods //
private static String encodeFileToBase64(File file) throws IOException {
@@ -527,7 +612,7 @@ public class OllamaAPI {
}
private OllamaResult generateSyncForOllamaRequestModel(
- OllamaGenerateRequestModel ollamaRequestModel, OllamaStreamHandler streamHandler)
+ OllamaGenerateRequest ollamaRequestModel, OllamaStreamHandler streamHandler)
throws OllamaBaseException, IOException, InterruptedException {
OllamaGenerateEndpointCaller requestCaller =
new OllamaGenerateEndpointCaller(host, basicAuth, requestTimeoutSeconds, verbose);
@@ -576,4 +661,22 @@ public class OllamaAPI {
private boolean isBasicAuthCredentialsSet() {
return basicAuth != null;
}
+
+
+ private Object invokeTool(ToolFunctionCallSpec toolFunctionCallSpec) throws ToolInvocationException {
+ try {
+ String methodName = toolFunctionCallSpec.getName();
+ Map arguments = toolFunctionCallSpec.getArguments();
+ ToolFunction function = toolRegistry.getFunction(methodName);
+ if (verbose) {
+ logger.debug("Invoking function {} with arguments {}", methodName, arguments);
+ }
+ if (function == null) {
+ throw new ToolNotFoundException("No such tool: " + methodName);
+ }
+ return function.apply(arguments);
+ } catch (Exception e) {
+ throw new ToolInvocationException("Failed to invoke tool: " + toolFunctionCallSpec.getName(), e);
+ }
+ }
}
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/exceptions/OllamaBaseException.java b/src/main/java/io/github/ollama4j/exceptions/OllamaBaseException.java
similarity index 68%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/exceptions/OllamaBaseException.java
rename to src/main/java/io/github/ollama4j/exceptions/OllamaBaseException.java
index 7c8612f..9474d72 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/exceptions/OllamaBaseException.java
+++ b/src/main/java/io/github/ollama4j/exceptions/OllamaBaseException.java
@@ -1,4 +1,4 @@
-package io.github.amithkoujalgi.ollama4j.core.exceptions;
+package io.github.ollama4j.exceptions;
public class OllamaBaseException extends Exception {
diff --git a/src/main/java/io/github/ollama4j/exceptions/ToolInvocationException.java b/src/main/java/io/github/ollama4j/exceptions/ToolInvocationException.java
new file mode 100644
index 0000000..ea81bb9
--- /dev/null
+++ b/src/main/java/io/github/ollama4j/exceptions/ToolInvocationException.java
@@ -0,0 +1,8 @@
+package io.github.ollama4j.exceptions;
+
+public class ToolInvocationException extends Exception {
+
+ public ToolInvocationException(String s, Exception e) {
+ super(s, e);
+ }
+}
diff --git a/src/main/java/io/github/ollama4j/exceptions/ToolNotFoundException.java b/src/main/java/io/github/ollama4j/exceptions/ToolNotFoundException.java
new file mode 100644
index 0000000..bd3e007
--- /dev/null
+++ b/src/main/java/io/github/ollama4j/exceptions/ToolNotFoundException.java
@@ -0,0 +1,8 @@
+package io.github.ollama4j.exceptions;
+
+public class ToolNotFoundException extends Exception {
+
+ public ToolNotFoundException(String s) {
+ super(s);
+ }
+}
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/impl/ConsoleOutputStreamHandler.java b/src/main/java/io/github/ollama4j/impl/ConsoleOutputStreamHandler.java
similarity index 73%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/impl/ConsoleOutputStreamHandler.java
rename to src/main/java/io/github/ollama4j/impl/ConsoleOutputStreamHandler.java
index 6807019..c9f8e36 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/impl/ConsoleOutputStreamHandler.java
+++ b/src/main/java/io/github/ollama4j/impl/ConsoleOutputStreamHandler.java
@@ -1,6 +1,6 @@
-package io.github.amithkoujalgi.ollama4j.core.impl;
+package io.github.ollama4j.impl;
-import io.github.amithkoujalgi.ollama4j.core.OllamaStreamHandler;
+import io.github.ollama4j.models.generate.OllamaStreamHandler;
public class ConsoleOutputStreamHandler implements OllamaStreamHandler {
private final StringBuffer response = new StringBuffer();
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/chat/OllamaChatMessage.java b/src/main/java/io/github/ollama4j/models/chat/OllamaChatMessage.java
similarity index 83%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/models/chat/OllamaChatMessage.java
rename to src/main/java/io/github/ollama4j/models/chat/OllamaChatMessage.java
index 0b14315..d4fe195 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/chat/OllamaChatMessage.java
+++ b/src/main/java/io/github/ollama4j/models/chat/OllamaChatMessage.java
@@ -1,11 +1,11 @@
-package io.github.amithkoujalgi.ollama4j.core.models.chat;
+package io.github.ollama4j.models.chat;
-import static io.github.amithkoujalgi.ollama4j.core.utils.Utils.getObjectMapper;
+import static io.github.ollama4j.utils.Utils.getObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
-import io.github.amithkoujalgi.ollama4j.core.utils.FileToBase64Serializer;
+import io.github.ollama4j.utils.FileToBase64Serializer;
import java.util.List;
import lombok.AllArgsConstructor;
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/chat/OllamaChatMessageRole.java b/src/main/java/io/github/ollama4j/models/chat/OllamaChatMessageRole.java
similarity index 85%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/models/chat/OllamaChatMessageRole.java
rename to src/main/java/io/github/ollama4j/models/chat/OllamaChatMessageRole.java
index cbecb00..3986135 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/chat/OllamaChatMessageRole.java
+++ b/src/main/java/io/github/ollama4j/models/chat/OllamaChatMessageRole.java
@@ -1,4 +1,4 @@
-package io.github.amithkoujalgi.ollama4j.core.models.chat;
+package io.github.ollama4j.models.chat;
import com.fasterxml.jackson.annotation.JsonValue;
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/chat/OllamaChatRequestModel.java b/src/main/java/io/github/ollama4j/models/chat/OllamaChatRequest.java
similarity index 53%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/models/chat/OllamaChatRequestModel.java
rename to src/main/java/io/github/ollama4j/models/chat/OllamaChatRequest.java
index e55bf6a..e6e528d 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/chat/OllamaChatRequestModel.java
+++ b/src/main/java/io/github/ollama4j/models/chat/OllamaChatRequest.java
@@ -1,8 +1,9 @@
-package io.github.amithkoujalgi.ollama4j.core.models.chat;
+package io.github.ollama4j.models.chat;
import java.util.List;
-import io.github.amithkoujalgi.ollama4j.core.models.OllamaCommonRequestModel;
-import io.github.amithkoujalgi.ollama4j.core.utils.OllamaRequestBody;
+
+import io.github.ollama4j.models.request.OllamaCommonRequest;
+import io.github.ollama4j.utils.OllamaRequestBody;
import lombok.Getter;
import lombok.Setter;
@@ -16,20 +17,20 @@ import lombok.Setter;
*/
@Getter
@Setter
-public class OllamaChatRequestModel extends OllamaCommonRequestModel implements OllamaRequestBody {
+public class OllamaChatRequest extends OllamaCommonRequest implements OllamaRequestBody {
private List messages;
- public OllamaChatRequestModel() {}
+ public OllamaChatRequest() {}
- public OllamaChatRequestModel(String model, List messages) {
+ public OllamaChatRequest(String model, List messages) {
this.model = model;
this.messages = messages;
}
@Override
public boolean equals(Object o) {
- if (!(o instanceof OllamaChatRequestModel)) {
+ if (!(o instanceof OllamaChatRequest)) {
return false;
}
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/chat/OllamaChatRequestBuilder.java b/src/main/java/io/github/ollama4j/models/chat/OllamaChatRequestBuilder.java
similarity index 61%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/models/chat/OllamaChatRequestBuilder.java
rename to src/main/java/io/github/ollama4j/models/chat/OllamaChatRequestBuilder.java
index e07722f..7cdf879 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/chat/OllamaChatRequestBuilder.java
+++ b/src/main/java/io/github/ollama4j/models/chat/OllamaChatRequestBuilder.java
@@ -1,4 +1,9 @@
-package io.github.amithkoujalgi.ollama4j.core.models.chat;
+package io.github.ollama4j.models.chat;
+
+import io.github.ollama4j.utils.Options;
+import io.github.ollama4j.utils.Utils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
@@ -8,101 +13,92 @@ import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import io.github.amithkoujalgi.ollama4j.core.utils.Options;
-import io.github.amithkoujalgi.ollama4j.core.utils.Utils;
-
/**
- * Helper class for creating {@link OllamaChatRequestModel} objects using the builder-pattern.
+ * Helper class for creating {@link OllamaChatRequest} objects using the builder-pattern.
*/
public class OllamaChatRequestBuilder {
private static final Logger LOG = LoggerFactory.getLogger(OllamaChatRequestBuilder.class);
- private OllamaChatRequestBuilder(String model, List messages){
- request = new OllamaChatRequestModel(model, messages);
+ private OllamaChatRequestBuilder(String model, List messages) {
+ request = new OllamaChatRequest(model, messages);
}
- private OllamaChatRequestModel request;
+ private OllamaChatRequest request;
- public static OllamaChatRequestBuilder getInstance(String model){
+ public static OllamaChatRequestBuilder getInstance(String model) {
return new OllamaChatRequestBuilder(model, new ArrayList<>());
}
- public OllamaChatRequestModel build(){
+ public OllamaChatRequest build() {
return request;
}
- public void reset(){
- request = new OllamaChatRequestModel(request.getModel(), new ArrayList<>());
+ public void reset() {
+ request = new OllamaChatRequest(request.getModel(), new ArrayList<>());
}
- public OllamaChatRequestBuilder withMessage(OllamaChatMessageRole role, String content, List images){
+ public OllamaChatRequestBuilder withMessage(OllamaChatMessageRole role, String content, List images) {
List messages = this.request.getMessages();
List binaryImages = images.stream().map(file -> {
try {
return Files.readAllBytes(file.toPath());
} catch (IOException e) {
- LOG.warn(String.format("File '%s' could not be accessed, will not add to message!",file.toPath()), e);
+ LOG.warn(String.format("File '%s' could not be accessed, will not add to message!", file.toPath()), e);
return new byte[0];
}
}).collect(Collectors.toList());
- messages.add(new OllamaChatMessage(role,content,binaryImages));
+ messages.add(new OllamaChatMessage(role, content, binaryImages));
return this;
}
- public OllamaChatRequestBuilder withMessage(OllamaChatMessageRole role, String content, String... imageUrls){
+ public OllamaChatRequestBuilder withMessage(OllamaChatMessageRole role, String content, String... imageUrls) {
List messages = this.request.getMessages();
List binaryImages = null;
- if(imageUrls.length>0){
+ if (imageUrls.length > 0) {
binaryImages = new ArrayList<>();
for (String imageUrl : imageUrls) {
- try{
+ try {
binaryImages.add(Utils.loadImageBytesFromUrl(imageUrl));
- }
- catch (URISyntaxException e){
- LOG.warn(String.format("URL '%s' could not be accessed, will not add to message!",imageUrl), e);
- }
- catch (IOException e){
- LOG.warn(String.format("Content of URL '%s' could not be read, will not add to message!",imageUrl), e);
+ } catch (URISyntaxException e) {
+ LOG.warn(String.format("URL '%s' could not be accessed, will not add to message!", imageUrl), e);
+ } catch (IOException e) {
+ LOG.warn(String.format("Content of URL '%s' could not be read, will not add to message!", imageUrl), e);
}
}
}
-
- messages.add(new OllamaChatMessage(role,content,binaryImages));
+
+ messages.add(new OllamaChatMessage(role, content, binaryImages));
return this;
}
- public OllamaChatRequestBuilder withMessages(List messages){
- this.request.getMessages().addAll(messages);
- return this;
+ public OllamaChatRequestBuilder withMessages(List messages) {
+ return new OllamaChatRequestBuilder(request.getModel(), messages);
}
- public OllamaChatRequestBuilder withOptions(Options options){
+ public OllamaChatRequestBuilder withOptions(Options options) {
this.request.setOptions(options.getOptionsMap());
return this;
}
- public OllamaChatRequestBuilder withGetJsonResponse(){
+ public OllamaChatRequestBuilder withGetJsonResponse() {
this.request.setReturnFormatJson(true);
return this;
}
- public OllamaChatRequestBuilder withTemplate(String template){
+ public OllamaChatRequestBuilder withTemplate(String template) {
this.request.setTemplate(template);
return this;
}
- public OllamaChatRequestBuilder withStreaming(){
+ public OllamaChatRequestBuilder withStreaming() {
this.request.setStream(true);
return this;
}
- public OllamaChatRequestBuilder withKeepAlive(String keepAlive){
+ public OllamaChatRequestBuilder withKeepAlive(String keepAlive) {
this.request.setKeepAlive(keepAlive);
return this;
}
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/chat/OllamaChatResponseModel.java b/src/main/java/io/github/ollama4j/models/chat/OllamaChatResponseModel.java
similarity index 93%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/models/chat/OllamaChatResponseModel.java
rename to src/main/java/io/github/ollama4j/models/chat/OllamaChatResponseModel.java
index 418338f..2ccc731 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/chat/OllamaChatResponseModel.java
+++ b/src/main/java/io/github/ollama4j/models/chat/OllamaChatResponseModel.java
@@ -1,4 +1,4 @@
-package io.github.amithkoujalgi.ollama4j.core.models.chat;
+package io.github.ollama4j.models.chat;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/chat/OllamaChatResult.java b/src/main/java/io/github/ollama4j/models/chat/OllamaChatResult.java
similarity index 88%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/models/chat/OllamaChatResult.java
rename to src/main/java/io/github/ollama4j/models/chat/OllamaChatResult.java
index 6ac6578..b105e81 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/chat/OllamaChatResult.java
+++ b/src/main/java/io/github/ollama4j/models/chat/OllamaChatResult.java
@@ -1,8 +1,8 @@
-package io.github.amithkoujalgi.ollama4j.core.models.chat;
+package io.github.ollama4j.models.chat;
import java.util.List;
-import io.github.amithkoujalgi.ollama4j.core.models.OllamaResult;
+import io.github.ollama4j.models.response.OllamaResult;
/**
* Specific chat-API result that contains the chat history sent to the model and appends the answer as {@link OllamaChatResult} given by the
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/chat/OllamaChatStreamObserver.java b/src/main/java/io/github/ollama4j/models/chat/OllamaChatStreamObserver.java
similarity index 82%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/models/chat/OllamaChatStreamObserver.java
rename to src/main/java/io/github/ollama4j/models/chat/OllamaChatStreamObserver.java
index ea4b4d8..9f1bf7f 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/chat/OllamaChatStreamObserver.java
+++ b/src/main/java/io/github/ollama4j/models/chat/OllamaChatStreamObserver.java
@@ -1,10 +1,10 @@
-package io.github.amithkoujalgi.ollama4j.core.models.chat;
+package io.github.ollama4j.models.chat;
+
+import io.github.ollama4j.models.generate.OllamaStreamHandler;
import java.util.ArrayList;
import java.util.List;
-import io.github.amithkoujalgi.ollama4j.core.OllamaStreamHandler;
-
public class OllamaChatStreamObserver {
private OllamaStreamHandler streamHandler;
@@ -17,12 +17,12 @@ public class OllamaChatStreamObserver {
this.streamHandler = streamHandler;
}
- public void notify(OllamaChatResponseModel currentResponsePart){
+ public void notify(OllamaChatResponseModel currentResponsePart) {
responseParts.add(currentResponsePart);
handleCurrentResponsePart(currentResponsePart);
}
-
- protected void handleCurrentResponsePart(OllamaChatResponseModel currentResponsePart){
+
+ protected void handleCurrentResponsePart(OllamaChatResponseModel currentResponsePart) {
message = message + currentResponsePart.getMessage().getContent();
streamHandler.accept(message);
}
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/embeddings/OllamaEmbeddingResponseModel.java b/src/main/java/io/github/ollama4j/models/embeddings/OllamaEmbeddingResponseModel.java
similarity index 79%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/models/embeddings/OllamaEmbeddingResponseModel.java
rename to src/main/java/io/github/ollama4j/models/embeddings/OllamaEmbeddingResponseModel.java
index 85dba31..24d95bc 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/embeddings/OllamaEmbeddingResponseModel.java
+++ b/src/main/java/io/github/ollama4j/models/embeddings/OllamaEmbeddingResponseModel.java
@@ -1,4 +1,4 @@
-package io.github.amithkoujalgi.ollama4j.core.models.embeddings;
+package io.github.ollama4j.models.embeddings;
import com.fasterxml.jackson.annotation.JsonProperty;
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/embeddings/OllamaEmbeddingsRequestBuilder.java b/src/main/java/io/github/ollama4j/models/embeddings/OllamaEmbeddingsRequestBuilder.java
similarity index 86%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/models/embeddings/OllamaEmbeddingsRequestBuilder.java
rename to src/main/java/io/github/ollama4j/models/embeddings/OllamaEmbeddingsRequestBuilder.java
index ef7a84e..b542931 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/embeddings/OllamaEmbeddingsRequestBuilder.java
+++ b/src/main/java/io/github/ollama4j/models/embeddings/OllamaEmbeddingsRequestBuilder.java
@@ -1,6 +1,6 @@
-package io.github.amithkoujalgi.ollama4j.core.models.embeddings;
+package io.github.ollama4j.models.embeddings;
-import io.github.amithkoujalgi.ollama4j.core.utils.Options;
+import io.github.ollama4j.utils.Options;
public class OllamaEmbeddingsRequestBuilder {
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/embeddings/OllamaEmbeddingsRequestModel.java b/src/main/java/io/github/ollama4j/models/embeddings/OllamaEmbeddingsRequestModel.java
similarity index 83%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/models/embeddings/OllamaEmbeddingsRequestModel.java
rename to src/main/java/io/github/ollama4j/models/embeddings/OllamaEmbeddingsRequestModel.java
index a369124..d700b91 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/embeddings/OllamaEmbeddingsRequestModel.java
+++ b/src/main/java/io/github/ollama4j/models/embeddings/OllamaEmbeddingsRequestModel.java
@@ -1,6 +1,6 @@
-package io.github.amithkoujalgi.ollama4j.core.models.embeddings;
+package io.github.ollama4j.models.embeddings;
-import static io.github.amithkoujalgi.ollama4j.core.utils.Utils.getObjectMapper;
+import static io.github.ollama4j.utils.Utils.getObjectMapper;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
diff --git a/src/main/java/io/github/ollama4j/models/generate/OllamaGenerateRequest.java b/src/main/java/io/github/ollama4j/models/generate/OllamaGenerateRequest.java
new file mode 100644
index 0000000..de767dc
--- /dev/null
+++ b/src/main/java/io/github/ollama4j/models/generate/OllamaGenerateRequest.java
@@ -0,0 +1,46 @@
+package io.github.ollama4j.models.generate;
+
+
+import io.github.ollama4j.models.request.OllamaCommonRequest;
+import io.github.ollama4j.utils.OllamaRequestBody;
+
+import java.util.List;
+
+import lombok.Getter;
+import lombok.Setter;
+
+@Getter
+@Setter
+public class OllamaGenerateRequest extends OllamaCommonRequest implements OllamaRequestBody{
+
+ private String prompt;
+ private List images;
+
+ private String system;
+ private String context;
+ private boolean raw;
+
+ public OllamaGenerateRequest() {
+ }
+
+ public OllamaGenerateRequest(String model, String prompt) {
+ this.model = model;
+ this.prompt = prompt;
+ }
+
+ public OllamaGenerateRequest(String model, String prompt, List images) {
+ this.model = model;
+ this.prompt = prompt;
+ this.images = images;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (!(o instanceof OllamaGenerateRequest)) {
+ return false;
+ }
+
+ return this.toString().equals(o.toString());
+ }
+
+}
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/generate/OllamaGenerateRequestBuilder.java b/src/main/java/io/github/ollama4j/models/generate/OllamaGenerateRequestBuilder.java
similarity index 74%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/models/generate/OllamaGenerateRequestBuilder.java
rename to src/main/java/io/github/ollama4j/models/generate/OllamaGenerateRequestBuilder.java
index 48b4d18..f802094 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/generate/OllamaGenerateRequestBuilder.java
+++ b/src/main/java/io/github/ollama4j/models/generate/OllamaGenerateRequestBuilder.java
@@ -1,24 +1,24 @@
-package io.github.amithkoujalgi.ollama4j.core.models.generate;
+package io.github.ollama4j.models.generate;
-import io.github.amithkoujalgi.ollama4j.core.utils.Options;
+import io.github.ollama4j.utils.Options;
/**
- * Helper class for creating {@link io.github.amithkoujalgi.ollama4j.core.models.generate.OllamaGenerateRequestModel}
+ * Helper class for creating {@link OllamaGenerateRequest}
* objects using the builder-pattern.
*/
public class OllamaGenerateRequestBuilder {
private OllamaGenerateRequestBuilder(String model, String prompt){
- request = new OllamaGenerateRequestModel(model, prompt);
+ request = new OllamaGenerateRequest(model, prompt);
}
- private OllamaGenerateRequestModel request;
+ private OllamaGenerateRequest request;
public static OllamaGenerateRequestBuilder getInstance(String model){
return new OllamaGenerateRequestBuilder(model,"");
}
- public OllamaGenerateRequestModel build(){
+ public OllamaGenerateRequest build(){
return request;
}
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/generate/OllamaGenerateResponseModel.java b/src/main/java/io/github/ollama4j/models/generate/OllamaGenerateResponseModel.java
similarity index 92%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/models/generate/OllamaGenerateResponseModel.java
rename to src/main/java/io/github/ollama4j/models/generate/OllamaGenerateResponseModel.java
index a575a7a..9fb975e 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/generate/OllamaGenerateResponseModel.java
+++ b/src/main/java/io/github/ollama4j/models/generate/OllamaGenerateResponseModel.java
@@ -1,4 +1,4 @@
-package io.github.amithkoujalgi.ollama4j.core.models.generate;
+package io.github.ollama4j.models.generate;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/generate/OllamaGenerateStreamObserver.java b/src/main/java/io/github/ollama4j/models/generate/OllamaGenerateStreamObserver.java
similarity index 80%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/models/generate/OllamaGenerateStreamObserver.java
rename to src/main/java/io/github/ollama4j/models/generate/OllamaGenerateStreamObserver.java
index a166bac..bc47fa0 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/generate/OllamaGenerateStreamObserver.java
+++ b/src/main/java/io/github/ollama4j/models/generate/OllamaGenerateStreamObserver.java
@@ -1,10 +1,8 @@
-package io.github.amithkoujalgi.ollama4j.core.models.generate;
+package io.github.ollama4j.models.generate;
import java.util.ArrayList;
import java.util.List;
-import io.github.amithkoujalgi.ollama4j.core.OllamaStreamHandler;
-
public class OllamaGenerateStreamObserver {
private OllamaStreamHandler streamHandler;
@@ -17,12 +15,12 @@ public class OllamaGenerateStreamObserver {
this.streamHandler = streamHandler;
}
- public void notify(OllamaGenerateResponseModel currentResponsePart){
+ public void notify(OllamaGenerateResponseModel currentResponsePart) {
responseParts.add(currentResponsePart);
handleCurrentResponsePart(currentResponsePart);
}
-
- protected void handleCurrentResponsePart(OllamaGenerateResponseModel currentResponsePart){
+
+ protected void handleCurrentResponsePart(OllamaGenerateResponseModel currentResponsePart) {
message = message + currentResponsePart.getResponse();
streamHandler.accept(message);
}
diff --git a/src/main/java/io/github/ollama4j/models/generate/OllamaStreamHandler.java b/src/main/java/io/github/ollama4j/models/generate/OllamaStreamHandler.java
new file mode 100644
index 0000000..e2da640
--- /dev/null
+++ b/src/main/java/io/github/ollama4j/models/generate/OllamaStreamHandler.java
@@ -0,0 +1,7 @@
+package io.github.ollama4j.models.generate;
+
+import java.util.function.Consumer;
+
+public interface OllamaStreamHandler extends Consumer {
+ void accept(String message);
+}
diff --git a/src/main/java/io/github/ollama4j/models/ps/ModelsProcessResponse.java b/src/main/java/io/github/ollama4j/models/ps/ModelsProcessResponse.java
new file mode 100644
index 0000000..490d362
--- /dev/null
+++ b/src/main/java/io/github/ollama4j/models/ps/ModelsProcessResponse.java
@@ -0,0 +1,63 @@
+package io.github.ollama4j.models.ps;
+
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.util.List;
+
+@Data
+@NoArgsConstructor
+@JsonIgnoreProperties(ignoreUnknown = true)
+public class ModelsProcessResponse {
+ @JsonProperty("models")
+ private List models;
+
+ @Data
+ @NoArgsConstructor
+ public static class ModelProcess {
+ @JsonProperty("name")
+ private String name;
+
+ @JsonProperty("model")
+ private String model;
+
+ @JsonProperty("size")
+ private long size;
+
+ @JsonProperty("digest")
+ private String digest;
+
+ @JsonProperty("details")
+ private ModelDetails details;
+
+ @JsonProperty("expires_at")
+ private String expiresAt; // Consider using LocalDateTime if you need to process date/time
+
+ @JsonProperty("size_vram")
+ private long sizeVram;
+ }
+
+ @Data
+ @NoArgsConstructor
+ public static class ModelDetails {
+ @JsonProperty("parent_model")
+ private String parentModel;
+
+ @JsonProperty("format")
+ private String format;
+
+ @JsonProperty("family")
+ private String family;
+
+ @JsonProperty("families")
+ private List families;
+
+ @JsonProperty("parameter_size")
+ private String parameterSize;
+
+ @JsonProperty("quantization_level")
+ private String quantizationLevel;
+ }
+}
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/BasicAuth.java b/src/main/java/io/github/ollama4j/models/request/BasicAuth.java
similarity index 79%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/models/BasicAuth.java
rename to src/main/java/io/github/ollama4j/models/request/BasicAuth.java
index dbcf8a7..f3372a9 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/BasicAuth.java
+++ b/src/main/java/io/github/ollama4j/models/request/BasicAuth.java
@@ -1,4 +1,4 @@
-package io.github.amithkoujalgi.ollama4j.core.models;
+package io.github.ollama4j.models.request;
import lombok.AllArgsConstructor;
import lombok.Data;
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/request/CustomModelFileContentsRequest.java b/src/main/java/io/github/ollama4j/models/request/CustomModelFileContentsRequest.java
similarity index 76%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/models/request/CustomModelFileContentsRequest.java
rename to src/main/java/io/github/ollama4j/models/request/CustomModelFileContentsRequest.java
index 9e606d3..6841476 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/request/CustomModelFileContentsRequest.java
+++ b/src/main/java/io/github/ollama4j/models/request/CustomModelFileContentsRequest.java
@@ -1,6 +1,6 @@
-package io.github.amithkoujalgi.ollama4j.core.models.request;
+package io.github.ollama4j.models.request;
-import static io.github.amithkoujalgi.ollama4j.core.utils.Utils.getObjectMapper;
+import static io.github.ollama4j.utils.Utils.getObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.AllArgsConstructor;
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/request/CustomModelFilePathRequest.java b/src/main/java/io/github/ollama4j/models/request/CustomModelFilePathRequest.java
similarity index 76%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/models/request/CustomModelFilePathRequest.java
rename to src/main/java/io/github/ollama4j/models/request/CustomModelFilePathRequest.java
index ea08dbf..2fcda43 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/request/CustomModelFilePathRequest.java
+++ b/src/main/java/io/github/ollama4j/models/request/CustomModelFilePathRequest.java
@@ -1,6 +1,6 @@
-package io.github.amithkoujalgi.ollama4j.core.models.request;
+package io.github.ollama4j.models.request;
-import static io.github.amithkoujalgi.ollama4j.core.utils.Utils.getObjectMapper;
+import static io.github.ollama4j.utils.Utils.getObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.AllArgsConstructor;
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/request/ModelRequest.java b/src/main/java/io/github/ollama4j/models/request/ModelRequest.java
similarity index 74%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/models/request/ModelRequest.java
rename to src/main/java/io/github/ollama4j/models/request/ModelRequest.java
index d3fdec4..923cd87 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/request/ModelRequest.java
+++ b/src/main/java/io/github/ollama4j/models/request/ModelRequest.java
@@ -1,6 +1,6 @@
-package io.github.amithkoujalgi.ollama4j.core.models.request;
+package io.github.ollama4j.models.request;
-import static io.github.amithkoujalgi.ollama4j.core.utils.Utils.getObjectMapper;
+import static io.github.ollama4j.utils.Utils.getObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.AllArgsConstructor;
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/request/OllamaChatEndpointCaller.java b/src/main/java/io/github/ollama4j/models/request/OllamaChatEndpointCaller.java
similarity index 72%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/models/request/OllamaChatEndpointCaller.java
rename to src/main/java/io/github/ollama4j/models/request/OllamaChatEndpointCaller.java
index cc6c7f8..b875bf8 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/request/OllamaChatEndpointCaller.java
+++ b/src/main/java/io/github/ollama4j/models/request/OllamaChatEndpointCaller.java
@@ -1,14 +1,13 @@
-package io.github.amithkoujalgi.ollama4j.core.models.request;
+package io.github.ollama4j.models.request;
import com.fasterxml.jackson.core.JsonProcessingException;
-import io.github.amithkoujalgi.ollama4j.core.OllamaStreamHandler;
-import io.github.amithkoujalgi.ollama4j.core.exceptions.OllamaBaseException;
-import io.github.amithkoujalgi.ollama4j.core.models.BasicAuth;
-import io.github.amithkoujalgi.ollama4j.core.models.OllamaResult;
-import io.github.amithkoujalgi.ollama4j.core.models.chat.OllamaChatResponseModel;
-import io.github.amithkoujalgi.ollama4j.core.models.chat.OllamaChatStreamObserver;
-import io.github.amithkoujalgi.ollama4j.core.utils.OllamaRequestBody;
-import io.github.amithkoujalgi.ollama4j.core.utils.Utils;
+import io.github.ollama4j.exceptions.OllamaBaseException;
+import io.github.ollama4j.models.response.OllamaResult;
+import io.github.ollama4j.models.chat.OllamaChatResponseModel;
+import io.github.ollama4j.models.chat.OllamaChatStreamObserver;
+import io.github.ollama4j.models.generate.OllamaStreamHandler;
+import io.github.ollama4j.utils.OllamaRequestBody;
+import io.github.ollama4j.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/OllamaCommonRequestModel.java b/src/main/java/io/github/ollama4j/models/request/OllamaCommonRequest.java
similarity index 78%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/models/OllamaCommonRequestModel.java
rename to src/main/java/io/github/ollama4j/models/request/OllamaCommonRequest.java
index 6f985ab..2e6ed7e 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/OllamaCommonRequestModel.java
+++ b/src/main/java/io/github/ollama4j/models/request/OllamaCommonRequest.java
@@ -1,4 +1,4 @@
-package io.github.amithkoujalgi.ollama4j.core.models;
+package io.github.ollama4j.models.request;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonInclude;
@@ -6,13 +6,13 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
-import io.github.amithkoujalgi.ollama4j.core.utils.BooleanToJsonFormatFlagSerializer;
-import io.github.amithkoujalgi.ollama4j.core.utils.Utils;
+import io.github.ollama4j.utils.BooleanToJsonFormatFlagSerializer;
+import io.github.ollama4j.utils.Utils;
import lombok.Data;
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
-public abstract class OllamaCommonRequestModel {
+public abstract class OllamaCommonRequest {
protected String model;
@JsonSerialize(using = BooleanToJsonFormatFlagSerializer.class)
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/request/OllamaEndpointCaller.java b/src/main/java/io/github/ollama4j/models/request/OllamaEndpointCaller.java
similarity index 85%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/models/request/OllamaEndpointCaller.java
rename to src/main/java/io/github/ollama4j/models/request/OllamaEndpointCaller.java
index 350200a..8529c18 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/request/OllamaEndpointCaller.java
+++ b/src/main/java/io/github/ollama4j/models/request/OllamaEndpointCaller.java
@@ -1,12 +1,11 @@
-package io.github.amithkoujalgi.ollama4j.core.models.request;
+package io.github.ollama4j.models.request;
-import io.github.amithkoujalgi.ollama4j.core.OllamaAPI;
-import io.github.amithkoujalgi.ollama4j.core.exceptions.OllamaBaseException;
-import io.github.amithkoujalgi.ollama4j.core.models.BasicAuth;
-import io.github.amithkoujalgi.ollama4j.core.models.OllamaErrorResponseModel;
-import io.github.amithkoujalgi.ollama4j.core.models.OllamaResult;
-import io.github.amithkoujalgi.ollama4j.core.utils.OllamaRequestBody;
-import io.github.amithkoujalgi.ollama4j.core.utils.Utils;
+import io.github.ollama4j.OllamaAPI;
+import io.github.ollama4j.exceptions.OllamaBaseException;
+import io.github.ollama4j.models.response.OllamaErrorResponse;
+import io.github.ollama4j.models.response.OllamaResult;
+import io.github.ollama4j.utils.OllamaRequestBody;
+import io.github.ollama4j.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -78,19 +77,19 @@ public abstract class OllamaEndpointCaller {
while ((line = reader.readLine()) != null) {
if (statusCode == 404) {
LOG.warn("Status code: 404 (Not Found)");
- OllamaErrorResponseModel ollamaResponseModel =
- Utils.getObjectMapper().readValue(line, OllamaErrorResponseModel.class);
+ OllamaErrorResponse ollamaResponseModel =
+ Utils.getObjectMapper().readValue(line, OllamaErrorResponse.class);
responseBuffer.append(ollamaResponseModel.getError());
} else if (statusCode == 401) {
LOG.warn("Status code: 401 (Unauthorized)");
- OllamaErrorResponseModel ollamaResponseModel =
+ OllamaErrorResponse ollamaResponseModel =
Utils.getObjectMapper()
- .readValue("{\"error\":\"Unauthorized\"}", OllamaErrorResponseModel.class);
+ .readValue("{\"error\":\"Unauthorized\"}", OllamaErrorResponse.class);
responseBuffer.append(ollamaResponseModel.getError());
} else if (statusCode == 400) {
LOG.warn("Status code: 400 (Bad Request)");
- OllamaErrorResponseModel ollamaResponseModel = Utils.getObjectMapper().readValue(line,
- OllamaErrorResponseModel.class);
+ OllamaErrorResponse ollamaResponseModel = Utils.getObjectMapper().readValue(line,
+ OllamaErrorResponse.class);
responseBuffer.append(ollamaResponseModel.getError());
} else {
boolean finished = parseResponseAndAddToBuffer(line, responseBuffer);
diff --git a/src/main/java/io/github/ollama4j/models/request/OllamaGenerateEndpointCaller.java b/src/main/java/io/github/ollama4j/models/request/OllamaGenerateEndpointCaller.java
new file mode 100644
index 0000000..f4afb2c
--- /dev/null
+++ b/src/main/java/io/github/ollama4j/models/request/OllamaGenerateEndpointCaller.java
@@ -0,0 +1,51 @@
+package io.github.ollama4j.models.request;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import io.github.ollama4j.exceptions.OllamaBaseException;
+import io.github.ollama4j.models.response.OllamaResult;
+import io.github.ollama4j.models.generate.OllamaGenerateResponseModel;
+import io.github.ollama4j.models.generate.OllamaGenerateStreamObserver;
+import io.github.ollama4j.models.generate.OllamaStreamHandler;
+import io.github.ollama4j.utils.OllamaRequestBody;
+import io.github.ollama4j.utils.Utils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+
+public class OllamaGenerateEndpointCaller extends OllamaEndpointCaller {
+
+ private static final Logger LOG = LoggerFactory.getLogger(OllamaGenerateEndpointCaller.class);
+
+ private OllamaGenerateStreamObserver streamObserver;
+
+ public OllamaGenerateEndpointCaller(String host, BasicAuth basicAuth, long requestTimeoutSeconds, boolean verbose) {
+ super(host, basicAuth, requestTimeoutSeconds, verbose);
+ }
+
+ @Override
+ protected String getEndpointSuffix() {
+ return "/api/generate";
+ }
+
+ @Override
+ protected boolean parseResponseAndAddToBuffer(String line, StringBuilder responseBuffer) {
+ try {
+ OllamaGenerateResponseModel ollamaResponseModel = Utils.getObjectMapper().readValue(line, OllamaGenerateResponseModel.class);
+ responseBuffer.append(ollamaResponseModel.getResponse());
+ if (streamObserver != null) {
+ streamObserver.notify(ollamaResponseModel);
+ }
+ return ollamaResponseModel.isDone();
+ } catch (JsonProcessingException e) {
+ LOG.error("Error parsing the Ollama chat response!", e);
+ return true;
+ }
+ }
+
+ public OllamaResult call(OllamaRequestBody body, OllamaStreamHandler streamHandler)
+ throws OllamaBaseException, IOException, InterruptedException {
+ streamObserver = new OllamaGenerateStreamObserver(streamHandler);
+ return super.callSync(body);
+ }
+}
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/ListModelsResponse.java b/src/main/java/io/github/ollama4j/models/response/ListModelsResponse.java
similarity index 68%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/models/ListModelsResponse.java
rename to src/main/java/io/github/ollama4j/models/response/ListModelsResponse.java
index db6f413..62f151b 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/ListModelsResponse.java
+++ b/src/main/java/io/github/ollama4j/models/response/ListModelsResponse.java
@@ -1,6 +1,7 @@
-package io.github.amithkoujalgi.ollama4j.core.models;
+package io.github.ollama4j.models.response;
import java.util.List;
+
import lombok.Data;
@Data
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/Model.java b/src/main/java/io/github/ollama4j/models/response/Model.java
similarity index 87%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/models/Model.java
rename to src/main/java/io/github/ollama4j/models/response/Model.java
index 15efd70..e03049e 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/Model.java
+++ b/src/main/java/io/github/ollama4j/models/response/Model.java
@@ -1,11 +1,10 @@
-package io.github.amithkoujalgi.ollama4j.core.models;
+package io.github.ollama4j.models.response;
-import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
-import io.github.amithkoujalgi.ollama4j.core.utils.Utils;
+import io.github.ollama4j.utils.Utils;
import lombok.Data;
@Data
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/ModelDetail.java b/src/main/java/io/github/ollama4j/models/response/ModelDetail.java
similarity index 86%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/models/ModelDetail.java
rename to src/main/java/io/github/ollama4j/models/response/ModelDetail.java
index e81a20e..cf7e6bb 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/ModelDetail.java
+++ b/src/main/java/io/github/ollama4j/models/response/ModelDetail.java
@@ -1,9 +1,9 @@
-package io.github.amithkoujalgi.ollama4j.core.models;
+package io.github.ollama4j.models.response;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
-import io.github.amithkoujalgi.ollama4j.core.utils.Utils;
+import io.github.ollama4j.utils.Utils;
import lombok.Data;
@Data
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/ModelMeta.java b/src/main/java/io/github/ollama4j/models/response/ModelMeta.java
similarity index 87%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/models/ModelMeta.java
rename to src/main/java/io/github/ollama4j/models/response/ModelMeta.java
index e534832..eb7f176 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/ModelMeta.java
+++ b/src/main/java/io/github/ollama4j/models/response/ModelMeta.java
@@ -1,9 +1,9 @@
-package io.github.amithkoujalgi.ollama4j.core.models;
+package io.github.ollama4j.models.response;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
-import io.github.amithkoujalgi.ollama4j.core.utils.Utils;
+import io.github.ollama4j.utils.Utils;
import lombok.Data;
@Data
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/ModelPullResponse.java b/src/main/java/io/github/ollama4j/models/response/ModelPullResponse.java
similarity index 83%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/models/ModelPullResponse.java
rename to src/main/java/io/github/ollama4j/models/response/ModelPullResponse.java
index d9db5c1..dc8349e 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/ModelPullResponse.java
+++ b/src/main/java/io/github/ollama4j/models/response/ModelPullResponse.java
@@ -1,4 +1,4 @@
-package io.github.amithkoujalgi.ollama4j.core.models;
+package io.github.ollama4j.models.response;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
diff --git a/src/main/java/io/github/ollama4j/models/response/OllamaAsyncResultStreamer.java b/src/main/java/io/github/ollama4j/models/response/OllamaAsyncResultStreamer.java
new file mode 100644
index 0000000..11c726c
--- /dev/null
+++ b/src/main/java/io/github/ollama4j/models/response/OllamaAsyncResultStreamer.java
@@ -0,0 +1,123 @@
+package io.github.ollama4j.models.response;
+
+import io.github.ollama4j.exceptions.OllamaBaseException;
+import io.github.ollama4j.models.generate.OllamaGenerateRequest;
+import io.github.ollama4j.models.generate.OllamaGenerateResponseModel;
+import io.github.ollama4j.utils.Utils;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.Getter;
+import lombok.Setter;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
+import java.nio.charset.StandardCharsets;
+import java.time.Duration;
+
+@Data
+@EqualsAndHashCode(callSuper = true)
+@SuppressWarnings("unused")
+public class OllamaAsyncResultStreamer extends Thread {
+ private final HttpRequest.Builder requestBuilder;
+ private final OllamaGenerateRequest ollamaRequestModel;
+ private final OllamaResultStream stream = new OllamaResultStream();
+ private String completeResponse;
+
+
+ /**
+ * -- GETTER -- Returns the status of the request. Indicates if the request was successful or a
+ * failure. If the request was a failure, the `getResponse()` method will return the error
+ * message.
+ */
+ @Getter
+ private boolean succeeded;
+
+ @Setter
+ private long requestTimeoutSeconds;
+
+ /**
+ * -- GETTER -- Returns the HTTP response status code for the request that was made to Ollama
+ * server.
+ */
+ @Getter
+ private int httpStatusCode;
+
+ /**
+ * -- GETTER -- Returns the response time in milliseconds.
+ */
+ @Getter
+ private long responseTime = 0;
+
+ public OllamaAsyncResultStreamer(
+ HttpRequest.Builder requestBuilder,
+ OllamaGenerateRequest ollamaRequestModel,
+ long requestTimeoutSeconds) {
+ this.requestBuilder = requestBuilder;
+ this.ollamaRequestModel = ollamaRequestModel;
+ this.completeResponse = "";
+ this.stream.add("");
+ this.requestTimeoutSeconds = requestTimeoutSeconds;
+ }
+
+ @Override
+ public void run() {
+ ollamaRequestModel.setStream(true);
+ HttpClient httpClient = HttpClient.newHttpClient();
+ try {
+ long startTime = System.currentTimeMillis();
+ HttpRequest request =
+ requestBuilder
+ .POST(
+ HttpRequest.BodyPublishers.ofString(
+ Utils.getObjectMapper().writeValueAsString(ollamaRequestModel)))
+ .header("Content-Type", "application/json")
+ .timeout(Duration.ofSeconds(requestTimeoutSeconds))
+ .build();
+ HttpResponse response =
+ httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
+ int statusCode = response.statusCode();
+ this.httpStatusCode = statusCode;
+
+ InputStream responseBodyStream = response.body();
+ try (BufferedReader reader =
+ new BufferedReader(new InputStreamReader(responseBodyStream, StandardCharsets.UTF_8))) {
+ String line;
+ StringBuilder responseBuffer = new StringBuilder();
+ while ((line = reader.readLine()) != null) {
+ if (statusCode == 404) {
+ OllamaErrorResponse ollamaResponseModel =
+ Utils.getObjectMapper().readValue(line, OllamaErrorResponse.class);
+ stream.add(ollamaResponseModel.getError());
+ responseBuffer.append(ollamaResponseModel.getError());
+ } else {
+ OllamaGenerateResponseModel ollamaResponseModel =
+ Utils.getObjectMapper().readValue(line, OllamaGenerateResponseModel.class);
+ String res = ollamaResponseModel.getResponse();
+ stream.add(res);
+ if (!ollamaResponseModel.isDone()) {
+ responseBuffer.append(res);
+ }
+ }
+ }
+
+ this.succeeded = true;
+ this.completeResponse = responseBuffer.toString();
+ long endTime = System.currentTimeMillis();
+ responseTime = endTime - startTime;
+ }
+ if (statusCode != 200) {
+ throw new OllamaBaseException(this.completeResponse);
+ }
+ } catch (IOException | InterruptedException | OllamaBaseException e) {
+ this.succeeded = false;
+ this.completeResponse = "[FAILED] " + e.getMessage();
+ }
+ }
+
+}
+
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/OllamaErrorResponseModel.java b/src/main/java/io/github/ollama4j/models/response/OllamaErrorResponse.java
similarity index 63%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/models/OllamaErrorResponseModel.java
rename to src/main/java/io/github/ollama4j/models/response/OllamaErrorResponse.java
index be3d8e4..bbc78c1 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/OllamaErrorResponseModel.java
+++ b/src/main/java/io/github/ollama4j/models/response/OllamaErrorResponse.java
@@ -1,11 +1,11 @@
-package io.github.amithkoujalgi.ollama4j.core.models;
+package io.github.ollama4j.models.response;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
-public class OllamaErrorResponseModel {
+public class OllamaErrorResponse {
private String error;
}
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/OllamaResult.java b/src/main/java/io/github/ollama4j/models/response/OllamaResult.java
similarity index 88%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/models/OllamaResult.java
rename to src/main/java/io/github/ollama4j/models/response/OllamaResult.java
index 1276f5f..beb01ec 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/models/OllamaResult.java
+++ b/src/main/java/io/github/ollama4j/models/response/OllamaResult.java
@@ -1,6 +1,6 @@
-package io.github.amithkoujalgi.ollama4j.core.models;
+package io.github.ollama4j.models.response;
-import static io.github.amithkoujalgi.ollama4j.core.utils.Utils.getObjectMapper;
+import static io.github.ollama4j.utils.Utils.getObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.Data;
diff --git a/src/main/java/io/github/ollama4j/models/response/OllamaResultStream.java b/src/main/java/io/github/ollama4j/models/response/OllamaResultStream.java
new file mode 100644
index 0000000..de44d63
--- /dev/null
+++ b/src/main/java/io/github/ollama4j/models/response/OllamaResultStream.java
@@ -0,0 +1,18 @@
+package io.github.ollama4j.models.response;
+
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.Queue;
+
+public class OllamaResultStream extends LinkedList implements Queue {
+ @Override
+ public String poll() {
+ StringBuilder tokens = new StringBuilder();
+ Iterator iterator = this.listIterator();
+ while (iterator.hasNext()) {
+ tokens.append(iterator.next());
+ iterator.remove();
+ }
+ return tokens.toString();
+ }
+}
diff --git a/src/main/java/io/github/ollama4j/tools/OllamaToolsResult.java b/src/main/java/io/github/ollama4j/tools/OllamaToolsResult.java
new file mode 100644
index 0000000..c855bd2
--- /dev/null
+++ b/src/main/java/io/github/ollama4j/tools/OllamaToolsResult.java
@@ -0,0 +1,35 @@
+package io.github.ollama4j.tools;
+
+import io.github.ollama4j.models.response.OllamaResult;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+public class OllamaToolsResult {
+ private OllamaResult modelResult;
+ private Map toolResults;
+
+ public List getToolResults() {
+ List results = new ArrayList<>();
+ for (Map.Entry r : this.toolResults.entrySet()) {
+ results.add(new ToolResult(r.getKey().getName(), r.getKey().getArguments(), r.getValue()));
+ }
+ return results;
+ }
+
+ @Data
+ @NoArgsConstructor
+ @AllArgsConstructor
+ public static class ToolResult {
+ private String functionName;
+ private Map functionArguments;
+ private Object result;
+ }
+}
diff --git a/src/main/java/io/github/ollama4j/tools/ToolFunction.java b/src/main/java/io/github/ollama4j/tools/ToolFunction.java
new file mode 100644
index 0000000..51ab8c5
--- /dev/null
+++ b/src/main/java/io/github/ollama4j/tools/ToolFunction.java
@@ -0,0 +1,8 @@
+package io.github.ollama4j.tools;
+
+import java.util.Map;
+
+@FunctionalInterface
+public interface ToolFunction {
+ Object apply(Map arguments);
+}
diff --git a/src/main/java/io/github/ollama4j/tools/ToolFunctionCallSpec.java b/src/main/java/io/github/ollama4j/tools/ToolFunctionCallSpec.java
new file mode 100644
index 0000000..7a4f7fd
--- /dev/null
+++ b/src/main/java/io/github/ollama4j/tools/ToolFunctionCallSpec.java
@@ -0,0 +1,16 @@
+package io.github.ollama4j.tools;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.util.Map;
+
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+public class ToolFunctionCallSpec {
+ private String name;
+ private Map arguments;
+}
+
diff --git a/src/main/java/io/github/ollama4j/tools/ToolRegistry.java b/src/main/java/io/github/ollama4j/tools/ToolRegistry.java
new file mode 100644
index 0000000..2ead13a
--- /dev/null
+++ b/src/main/java/io/github/ollama4j/tools/ToolRegistry.java
@@ -0,0 +1,16 @@
+package io.github.ollama4j.tools;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class ToolRegistry {
+ private final Map functionMap = new HashMap<>();
+
+ public ToolFunction getFunction(String name) {
+ return functionMap.get(name);
+ }
+
+ public void addFunction(String name, ToolFunction function) {
+ functionMap.put(name, function);
+ }
+}
diff --git a/src/main/java/io/github/ollama4j/tools/Tools.java b/src/main/java/io/github/ollama4j/tools/Tools.java
new file mode 100644
index 0000000..986302f
--- /dev/null
+++ b/src/main/java/io/github/ollama4j/tools/Tools.java
@@ -0,0 +1,113 @@
+package io.github.ollama4j.tools;
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import io.github.ollama4j.utils.Utils;
+import lombok.Builder;
+import lombok.Data;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class Tools {
+ @Data
+ @Builder
+ public static class ToolSpecification {
+ private String functionName;
+ private String functionDescription;
+ private Map properties;
+ private ToolFunction toolDefinition;
+ }
+
+ @Data
+ @JsonIgnoreProperties(ignoreUnknown = true)
+ public static class PromptFuncDefinition {
+ private String type;
+ private PromptFuncSpec function;
+
+ @Data
+ public static class PromptFuncSpec {
+ private String name;
+ private String description;
+ private Parameters parameters;
+ }
+
+ @Data
+ public static class Parameters {
+ private String type;
+ private Map properties;
+ private List required;
+ }
+
+ @Data
+ @Builder
+ public static class Property {
+ private String type;
+ private String description;
+ @JsonProperty("enum")
+ @JsonInclude(JsonInclude.Include.NON_NULL)
+ private List enumValues;
+ @JsonIgnore
+ private boolean required;
+ }
+ }
+
+ public static class PropsBuilder {
+ private final Map props = new HashMap<>();
+
+ public PropsBuilder withProperty(String key, PromptFuncDefinition.Property property) {
+ props.put(key, property);
+ return this;
+ }
+
+ public Map build() {
+ return props;
+ }
+ }
+
+ public static class PromptBuilder {
+ private final List tools = new ArrayList<>();
+
+ private String promptText;
+
+ public String build() throws JsonProcessingException {
+ return "[AVAILABLE_TOOLS] " + Utils.getObjectMapper().writeValueAsString(tools) + "[/AVAILABLE_TOOLS][INST] " + promptText + " [/INST]";
+ }
+
+ public PromptBuilder withPrompt(String prompt) throws JsonProcessingException {
+ promptText = prompt;
+ return this;
+ }
+
+ public PromptBuilder withToolSpecification(ToolSpecification spec) {
+ PromptFuncDefinition def = new PromptFuncDefinition();
+ def.setType("function");
+
+ PromptFuncDefinition.PromptFuncSpec functionDetail = new PromptFuncDefinition.PromptFuncSpec();
+ functionDetail.setName(spec.getFunctionName());
+ functionDetail.setDescription(spec.getFunctionDescription());
+
+ PromptFuncDefinition.Parameters parameters = new PromptFuncDefinition.Parameters();
+ parameters.setType("object");
+ parameters.setProperties(spec.getProperties());
+
+ List requiredValues = new ArrayList<>();
+ for (Map.Entry p : spec.getProperties().entrySet()) {
+ if (p.getValue().isRequired()) {
+ requiredValues.add(p.getKey());
+ }
+ }
+ parameters.setRequired(requiredValues);
+ functionDetail.setParameters(parameters);
+ def.setFunction(functionDetail);
+
+ tools.add(def);
+ return this;
+ }
+ }
+}
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/types/OllamaModelType.java b/src/main/java/io/github/ollama4j/types/OllamaModelType.java
similarity index 95%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/types/OllamaModelType.java
rename to src/main/java/io/github/ollama4j/types/OllamaModelType.java
index d7984d0..dc7b447 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/types/OllamaModelType.java
+++ b/src/main/java/io/github/ollama4j/types/OllamaModelType.java
@@ -1,4 +1,4 @@
-package io.github.amithkoujalgi.ollama4j.core.types;
+package io.github.ollama4j.types;
/**
* A class to provide constants for all the supported models by Ollama.
@@ -9,6 +9,9 @@ package io.github.amithkoujalgi.ollama4j.core.types;
@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 MISTRAL = "mistral";
@@ -30,6 +33,8 @@ public class OllamaModelType {
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";
@@ -79,4 +84,5 @@ public class OllamaModelType {
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";
}
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/utils/BooleanToJsonFormatFlagSerializer.java b/src/main/java/io/github/ollama4j/utils/BooleanToJsonFormatFlagSerializer.java
similarity index 91%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/utils/BooleanToJsonFormatFlagSerializer.java
rename to src/main/java/io/github/ollama4j/utils/BooleanToJsonFormatFlagSerializer.java
index f4d4ab3..a94e4d1 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/utils/BooleanToJsonFormatFlagSerializer.java
+++ b/src/main/java/io/github/ollama4j/utils/BooleanToJsonFormatFlagSerializer.java
@@ -1,4 +1,4 @@
-package io.github.amithkoujalgi.ollama4j.core.utils;
+package io.github.ollama4j.utils;
import java.io.IOException;
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/utils/FileToBase64Serializer.java b/src/main/java/io/github/ollama4j/utils/FileToBase64Serializer.java
similarity index 92%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/utils/FileToBase64Serializer.java
rename to src/main/java/io/github/ollama4j/utils/FileToBase64Serializer.java
index 8e862ab..235ebde 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/utils/FileToBase64Serializer.java
+++ b/src/main/java/io/github/ollama4j/utils/FileToBase64Serializer.java
@@ -1,4 +1,4 @@
-package io.github.amithkoujalgi.ollama4j.core.utils;
+package io.github.ollama4j.utils;
import java.io.IOException;
import java.util.Base64;
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/utils/OllamaRequestBody.java b/src/main/java/io/github/ollama4j/utils/OllamaRequestBody.java
similarity index 94%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/utils/OllamaRequestBody.java
rename to src/main/java/io/github/ollama4j/utils/OllamaRequestBody.java
index f787cee..5a2dfab 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/utils/OllamaRequestBody.java
+++ b/src/main/java/io/github/ollama4j/utils/OllamaRequestBody.java
@@ -1,4 +1,4 @@
-package io.github.amithkoujalgi.ollama4j.core.utils;
+package io.github.ollama4j.utils;
import java.net.http.HttpRequest.BodyPublisher;
import java.net.http.HttpRequest.BodyPublishers;
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/utils/Options.java b/src/main/java/io/github/ollama4j/utils/Options.java
similarity index 75%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/utils/Options.java
rename to src/main/java/io/github/ollama4j/utils/Options.java
index 2339969..c6e5e53 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/utils/Options.java
+++ b/src/main/java/io/github/ollama4j/utils/Options.java
@@ -1,4 +1,4 @@
-package io.github.amithkoujalgi.ollama4j.core.utils;
+package io.github.ollama4j.utils;
import java.util.Map;
import lombok.Data;
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/utils/OptionsBuilder.java b/src/main/java/io/github/ollama4j/utils/OptionsBuilder.java
similarity index 99%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/utils/OptionsBuilder.java
rename to src/main/java/io/github/ollama4j/utils/OptionsBuilder.java
index d605f81..c71818a 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/utils/OptionsBuilder.java
+++ b/src/main/java/io/github/ollama4j/utils/OptionsBuilder.java
@@ -1,4 +1,4 @@
-package io.github.amithkoujalgi.ollama4j.core.utils;
+package io.github.ollama4j.utils;
import java.util.HashMap;
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/utils/PromptBuilder.java b/src/main/java/io/github/ollama4j/utils/PromptBuilder.java
similarity index 97%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/utils/PromptBuilder.java
rename to src/main/java/io/github/ollama4j/utils/PromptBuilder.java
index be487d9..bb24ef8 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/utils/PromptBuilder.java
+++ b/src/main/java/io/github/ollama4j/utils/PromptBuilder.java
@@ -1,4 +1,4 @@
-package io.github.amithkoujalgi.ollama4j.core.utils;
+package io.github.ollama4j.utils;
/**
* The {@code PromptBuilder} class is used to construct prompt texts for language models (LLMs). It
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/utils/SamplePrompts.java b/src/main/java/io/github/ollama4j/utils/SamplePrompts.java
similarity index 88%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/utils/SamplePrompts.java
rename to src/main/java/io/github/ollama4j/utils/SamplePrompts.java
index 1e5dfdc..89a7f83 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/utils/SamplePrompts.java
+++ b/src/main/java/io/github/ollama4j/utils/SamplePrompts.java
@@ -1,6 +1,6 @@
-package io.github.amithkoujalgi.ollama4j.core.utils;
+package io.github.ollama4j.utils;
-import io.github.amithkoujalgi.ollama4j.core.OllamaAPI;
+import io.github.ollama4j.OllamaAPI;
import java.io.InputStream;
import java.util.Scanner;
diff --git a/src/main/java/io/github/amithkoujalgi/ollama4j/core/utils/Utils.java b/src/main/java/io/github/ollama4j/utils/Utils.java
similarity index 95%
rename from src/main/java/io/github/amithkoujalgi/ollama4j/core/utils/Utils.java
rename to src/main/java/io/github/ollama4j/utils/Utils.java
index 96b07ae..d854df1 100644
--- a/src/main/java/io/github/amithkoujalgi/ollama4j/core/utils/Utils.java
+++ b/src/main/java/io/github/ollama4j/utils/Utils.java
@@ -1,4 +1,4 @@
-package io.github.amithkoujalgi.ollama4j.core.utils;
+package io.github.ollama4j.utils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
diff --git a/src/test/java/io/github/amithkoujalgi/ollama4j/integrationtests/TestRealAPIs.java b/src/test/java/io/github/amithkoujalgi/ollama4j/integrationtests/TestRealAPIs.java
deleted file mode 100644
index d822077..0000000
--- a/src/test/java/io/github/amithkoujalgi/ollama4j/integrationtests/TestRealAPIs.java
+++ /dev/null
@@ -1,393 +0,0 @@
-package io.github.amithkoujalgi.ollama4j.integrationtests;
-
-import static org.junit.jupiter.api.Assertions.*;
-
-import io.github.amithkoujalgi.ollama4j.core.OllamaAPI;
-import io.github.amithkoujalgi.ollama4j.core.exceptions.OllamaBaseException;
-import io.github.amithkoujalgi.ollama4j.core.models.ModelDetail;
-import io.github.amithkoujalgi.ollama4j.core.models.OllamaResult;
-import io.github.amithkoujalgi.ollama4j.core.models.chat.OllamaChatMessageRole;
-import io.github.amithkoujalgi.ollama4j.core.models.chat.OllamaChatRequestBuilder;
-import io.github.amithkoujalgi.ollama4j.core.models.chat.OllamaChatRequestModel;
-import io.github.amithkoujalgi.ollama4j.core.models.chat.OllamaChatResult;
-import io.github.amithkoujalgi.ollama4j.core.models.embeddings.OllamaEmbeddingsRequestModel;
-import io.github.amithkoujalgi.ollama4j.core.models.embeddings.OllamaEmbeddingsRequestBuilder;
-import io.github.amithkoujalgi.ollama4j.core.utils.OptionsBuilder;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.ConnectException;
-import java.net.URISyntaxException;
-import java.net.http.HttpConnectTimeoutException;
-import java.util.List;
-import java.util.Objects;
-import java.util.Properties;
-import lombok.Data;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Order;
-import org.junit.jupiter.api.Test;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-class TestRealAPIs {
-
- private static final Logger LOG = LoggerFactory.getLogger(TestRealAPIs.class);
-
- OllamaAPI ollamaAPI;
- Config config;
-
- private File getImageFileFromClasspath(String fileName) {
- ClassLoader classLoader = getClass().getClassLoader();
- return new File(Objects.requireNonNull(classLoader.getResource(fileName)).getFile());
- }
-
- @BeforeEach
- void setUp() {
- config = new Config();
- ollamaAPI = new OllamaAPI(config.getOllamaURL());
- ollamaAPI.setRequestTimeoutSeconds(config.getRequestTimeoutSeconds());
- }
-
- @Test
- @Order(1)
- void testWrongEndpoint() {
- OllamaAPI ollamaAPI = new OllamaAPI("http://wrong-host:11434");
- assertThrows(ConnectException.class, ollamaAPI::listModels);
- }
-
- @Test
- @Order(1)
- void testEndpointReachability() {
- try {
- assertNotNull(ollamaAPI.listModels());
- } catch (HttpConnectTimeoutException e) {
- fail(e.getMessage());
- } catch (Exception e) {
- fail(e);
- }
- }
-
- @Test
- @Order(2)
- void testListModels() {
- testEndpointReachability();
- try {
- assertNotNull(ollamaAPI.listModels());
- ollamaAPI.listModels().forEach(System.out::println);
- } catch (IOException | OllamaBaseException | InterruptedException | URISyntaxException e) {
- fail(e);
- }
- }
-
- @Test
- @Order(2)
- void testPullModel() {
- testEndpointReachability();
- try {
- ollamaAPI.pullModel(config.getModel());
- boolean found =
- ollamaAPI.listModels().stream()
- .anyMatch(model -> model.getModel().equalsIgnoreCase(config.getModel()));
- assertTrue(found);
- } catch (IOException | OllamaBaseException | InterruptedException | URISyntaxException e) {
- fail(e);
- }
- }
-
- @Test
- @Order(3)
- void testListDtails() {
- testEndpointReachability();
- try {
- ModelDetail modelDetails = ollamaAPI.getModelDetails(config.getModel());
- assertNotNull(modelDetails);
- System.out.println(modelDetails);
- } catch (IOException | OllamaBaseException | InterruptedException | URISyntaxException e) {
- fail(e);
- }
- }
-
- @Test
- @Order(3)
- void testAskModelWithDefaultOptions() {
- testEndpointReachability();
- try {
- OllamaResult result =
- ollamaAPI.generate(
- config.getModel(),
- "What is the capital of France? And what's France's connection with Mona Lisa?",
- new OptionsBuilder().build());
- assertNotNull(result);
- assertNotNull(result.getResponse());
- assertFalse(result.getResponse().isEmpty());
- } catch (IOException | OllamaBaseException | InterruptedException e) {
- fail(e);
- }
- }
-
- @Test
- @Order(3)
- void testAskModelWithDefaultOptionsStreamed() {
- testEndpointReachability();
- try {
-
- StringBuffer sb = new StringBuffer("");
-
- OllamaResult result = ollamaAPI.generate(config.getModel(),
- "What is the capital of France? And what's France's connection with Mona Lisa?",
- new OptionsBuilder().build(), (s) -> {
- LOG.info(s);
- String substring = s.substring(sb.toString().length(), s.length());
- LOG.info(substring);
- sb.append(substring);
- });
-
- assertNotNull(result);
- assertNotNull(result.getResponse());
- assertFalse(result.getResponse().isEmpty());
- assertEquals(sb.toString().trim(), result.getResponse().trim());
- } catch (IOException | OllamaBaseException | InterruptedException e) {
- fail(e);
- }
- }
-
- @Test
- @Order(3)
- void testAskModelWithOptions() {
- testEndpointReachability();
- try {
- OllamaResult result =
- ollamaAPI.generate(
- config.getModel(),
- "What is the capital of France? And what's France's connection with Mona Lisa?",
- new OptionsBuilder().setTemperature(0.9f).build());
- assertNotNull(result);
- assertNotNull(result.getResponse());
- assertFalse(result.getResponse().isEmpty());
- } catch (IOException | OllamaBaseException | InterruptedException e) {
- fail(e);
- }
- }
-
- @Test
- @Order(3)
- void testChat() {
- testEndpointReachability();
- try {
- OllamaChatRequestBuilder builder = OllamaChatRequestBuilder.getInstance(config.getModel());
- OllamaChatRequestModel requestModel = builder.withMessage(OllamaChatMessageRole.USER, "What is the capital of France?")
- .withMessage(OllamaChatMessageRole.ASSISTANT, "Should be Paris!")
- .withMessage(OllamaChatMessageRole.USER,"And what is the second larges city?")
- .build();
-
- OllamaChatResult chatResult = ollamaAPI.chat(requestModel);
- assertNotNull(chatResult);
- assertFalse(chatResult.getResponse().isBlank());
- assertEquals(4,chatResult.getChatHistory().size());
- } catch (IOException | OllamaBaseException | InterruptedException e) {
- fail(e);
- }
- }
-
- @Test
- @Order(3)
- void testChatWithSystemPrompt() {
- testEndpointReachability();
- try {
- OllamaChatRequestBuilder builder = OllamaChatRequestBuilder.getInstance(config.getModel());
- OllamaChatRequestModel requestModel = builder.withMessage(OllamaChatMessageRole.SYSTEM,
- "You are a silent bot that only says 'NI'. Do not say anything else under any circumstances!")
- .withMessage(OllamaChatMessageRole.USER,
- "What is the capital of France? And what's France's connection with Mona Lisa?")
- .build();
-
- OllamaChatResult chatResult = ollamaAPI.chat(requestModel);
- assertNotNull(chatResult);
- assertFalse(chatResult.getResponse().isBlank());
- assertTrue(chatResult.getResponse().startsWith("NI"));
- assertEquals(3, chatResult.getChatHistory().size());
- } catch (IOException | OllamaBaseException | InterruptedException e) {
- fail(e);
- }
- }
-
- @Test
- @Order(3)
- void testChatWithStream() {
- testEndpointReachability();
- try {
- OllamaChatRequestBuilder builder = OllamaChatRequestBuilder.getInstance(config.getModel());
- OllamaChatRequestModel requestModel = builder.withMessage(OllamaChatMessageRole.USER,
- "What is the capital of France? And what's France's connection with Mona Lisa?")
- .build();
-
- StringBuffer sb = new StringBuffer("");
-
- OllamaChatResult chatResult = ollamaAPI.chat(requestModel,(s) -> {
- LOG.info(s);
- String substring = s.substring(sb.toString().length(), s.length());
- LOG.info(substring);
- sb.append(substring);
- });
- assertNotNull(chatResult);
- assertEquals(sb.toString().trim(), chatResult.getResponse().trim());
- } catch (IOException | OllamaBaseException | InterruptedException e) {
- fail(e);
- }
- }
-
- @Test
- @Order(3)
- void testChatWithImageFromFileWithHistoryRecognition() {
- testEndpointReachability();
- try {
- OllamaChatRequestBuilder builder =
- OllamaChatRequestBuilder.getInstance(config.getImageModel());
- OllamaChatRequestModel requestModel =
- builder.withMessage(OllamaChatMessageRole.USER, "What's in the picture?",
- List.of(getImageFileFromClasspath("dog-on-a-boat.jpg"))).build();
-
- OllamaChatResult chatResult = ollamaAPI.chat(requestModel);
- assertNotNull(chatResult);
- assertNotNull(chatResult.getResponse());
-
- builder.reset();
-
- requestModel =
- builder.withMessages(chatResult.getChatHistory())
- .withMessage(OllamaChatMessageRole.USER, "What's the dogs breed?").build();
-
- chatResult = ollamaAPI.chat(requestModel);
- assertNotNull(chatResult);
- assertNotNull(chatResult.getResponse());
-
-
- } catch (IOException | OllamaBaseException | InterruptedException e) {
- fail(e);
- }
- }
-
- @Test
- @Order(3)
- void testChatWithImageFromURL() {
- testEndpointReachability();
- try {
- OllamaChatRequestBuilder builder = OllamaChatRequestBuilder.getInstance(config.getImageModel());
- OllamaChatRequestModel requestModel = builder.withMessage(OllamaChatMessageRole.USER, "What's in the picture?",
- "https://t3.ftcdn.net/jpg/02/96/63/80/360_F_296638053_0gUVA4WVBKceGsIr7LNqRWSnkusi07dq.jpg")
- .build();
-
- OllamaChatResult chatResult = ollamaAPI.chat(requestModel);
- assertNotNull(chatResult);
- } catch (IOException | OllamaBaseException | InterruptedException e) {
- fail(e);
- }
- }
-
- @Test
- @Order(3)
- void testAskModelWithOptionsAndImageFiles() {
- testEndpointReachability();
- File imageFile = getImageFileFromClasspath("dog-on-a-boat.jpg");
- try {
- OllamaResult result =
- ollamaAPI.generateWithImageFiles(
- config.getImageModel(),
- "What is in this image?",
- List.of(imageFile),
- new OptionsBuilder().build());
- assertNotNull(result);
- assertNotNull(result.getResponse());
- assertFalse(result.getResponse().isEmpty());
- } catch (IOException | OllamaBaseException | InterruptedException e) {
- fail(e);
- }
- }
-
- @Test
- @Order(3)
- void testAskModelWithOptionsAndImageFilesStreamed() {
- testEndpointReachability();
- File imageFile = getImageFileFromClasspath("dog-on-a-boat.jpg");
- try {
- StringBuffer sb = new StringBuffer("");
-
- OllamaResult result = ollamaAPI.generateWithImageFiles(config.getImageModel(),
- "What is in this image?", List.of(imageFile), new OptionsBuilder().build(), (s) -> {
- LOG.info(s);
- String substring = s.substring(sb.toString().length(), s.length());
- LOG.info(substring);
- sb.append(substring);
- });
- assertNotNull(result);
- assertNotNull(result.getResponse());
- assertFalse(result.getResponse().isEmpty());
- assertEquals(sb.toString().trim(), result.getResponse().trim());
- } catch (IOException | OllamaBaseException | InterruptedException e) {
- fail(e);
- }
- }
-
- @Test
- @Order(3)
- void testAskModelWithOptionsAndImageURLs() {
- testEndpointReachability();
- try {
- OllamaResult result =
- ollamaAPI.generateWithImageURLs(
- config.getImageModel(),
- "What is in this image?",
- List.of(
- "https://t3.ftcdn.net/jpg/02/96/63/80/360_F_296638053_0gUVA4WVBKceGsIr7LNqRWSnkusi07dq.jpg"),
- new OptionsBuilder().build());
- assertNotNull(result);
- assertNotNull(result.getResponse());
- assertFalse(result.getResponse().isEmpty());
- } catch (IOException | OllamaBaseException | InterruptedException | URISyntaxException e) {
- fail(e);
- }
- }
-
- @Test
- @Order(3)
- public void testEmbedding() {
- testEndpointReachability();
- try {
- OllamaEmbeddingsRequestModel request = OllamaEmbeddingsRequestBuilder
- .getInstance(config.getModel(), "What is the capital of France?").build();
-
- List embeddings = ollamaAPI.generateEmbeddings(request);
-
- assertNotNull(embeddings);
- assertFalse(embeddings.isEmpty());
- } catch (IOException | OllamaBaseException | InterruptedException e) {
- fail(e);
- }
- }
-}
-
-@Data
-class Config {
- private String ollamaURL;
- private String model;
- private String imageModel;
- private int requestTimeoutSeconds;
-
- public Config() {
- Properties properties = new Properties();
- try (InputStream input =
- getClass().getClassLoader().getResourceAsStream("test-config.properties")) {
- if (input == null) {
- throw new RuntimeException("Sorry, unable to find test-config.properties");
- }
- properties.load(input);
- this.ollamaURL = properties.getProperty("ollama.url");
- this.model = properties.getProperty("ollama.model");
- this.imageModel = properties.getProperty("ollama.model.image");
- this.requestTimeoutSeconds =
- Integer.parseInt(properties.getProperty("ollama.request-timeout-seconds"));
- } catch (IOException e) {
- throw new RuntimeException("Error loading properties", e);
- }
- }
-}
diff --git a/src/test/java/io/github/amithkoujalgi/ollama4j/unittests/TestMockedAPIs.java b/src/test/java/io/github/amithkoujalgi/ollama4j/unittests/TestMockedAPIs.java
deleted file mode 100644
index 879c67c..0000000
--- a/src/test/java/io/github/amithkoujalgi/ollama4j/unittests/TestMockedAPIs.java
+++ /dev/null
@@ -1,163 +0,0 @@
-package io.github.amithkoujalgi.ollama4j.unittests;
-
-import static org.mockito.Mockito.*;
-
-import io.github.amithkoujalgi.ollama4j.core.OllamaAPI;
-import io.github.amithkoujalgi.ollama4j.core.exceptions.OllamaBaseException;
-import io.github.amithkoujalgi.ollama4j.core.models.ModelDetail;
-import io.github.amithkoujalgi.ollama4j.core.models.OllamaAsyncResultCallback;
-import io.github.amithkoujalgi.ollama4j.core.models.OllamaResult;
-import io.github.amithkoujalgi.ollama4j.core.types.OllamaModelType;
-import io.github.amithkoujalgi.ollama4j.core.utils.OptionsBuilder;
-import java.io.IOException;
-import java.net.URISyntaxException;
-import java.util.ArrayList;
-import java.util.Collections;
-import org.junit.jupiter.api.Test;
-import org.mockito.Mockito;
-
-class TestMockedAPIs {
- @Test
- void testPullModel() {
- OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class);
- String model = OllamaModelType.LLAMA2;
- try {
- doNothing().when(ollamaAPI).pullModel(model);
- ollamaAPI.pullModel(model);
- verify(ollamaAPI, times(1)).pullModel(model);
- } catch (IOException | OllamaBaseException | InterruptedException | URISyntaxException e) {
- throw new RuntimeException(e);
- }
- }
-
- @Test
- void testListModels() {
- OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class);
- try {
- when(ollamaAPI.listModels()).thenReturn(new ArrayList<>());
- ollamaAPI.listModels();
- verify(ollamaAPI, times(1)).listModels();
- } catch (IOException | OllamaBaseException | InterruptedException | URISyntaxException e) {
- throw new RuntimeException(e);
- }
- }
-
- @Test
- void testCreateModel() {
- OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class);
- String model = OllamaModelType.LLAMA2;
- String modelFilePath = "FROM llama2\nSYSTEM You are mario from Super Mario Bros.";
- try {
- doNothing().when(ollamaAPI).createModelWithModelFileContents(model, modelFilePath);
- ollamaAPI.createModelWithModelFileContents(model, modelFilePath);
- verify(ollamaAPI, times(1)).createModelWithModelFileContents(model, modelFilePath);
- } catch (IOException | OllamaBaseException | InterruptedException | URISyntaxException e) {
- throw new RuntimeException(e);
- }
- }
-
- @Test
- void testDeleteModel() {
- OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class);
- String model = OllamaModelType.LLAMA2;
- try {
- doNothing().when(ollamaAPI).deleteModel(model, true);
- ollamaAPI.deleteModel(model, true);
- verify(ollamaAPI, times(1)).deleteModel(model, true);
- } catch (IOException | OllamaBaseException | InterruptedException | URISyntaxException e) {
- throw new RuntimeException(e);
- }
- }
-
- @Test
- void testGetModelDetails() {
- OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class);
- String model = OllamaModelType.LLAMA2;
- try {
- when(ollamaAPI.getModelDetails(model)).thenReturn(new ModelDetail());
- ollamaAPI.getModelDetails(model);
- verify(ollamaAPI, times(1)).getModelDetails(model);
- } catch (IOException | OllamaBaseException | InterruptedException | URISyntaxException e) {
- throw new RuntimeException(e);
- }
- }
-
- @Test
- void testGenerateEmbeddings() {
- OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class);
- String model = OllamaModelType.LLAMA2;
- String prompt = "some prompt text";
- try {
- when(ollamaAPI.generateEmbeddings(model, prompt)).thenReturn(new ArrayList<>());
- ollamaAPI.generateEmbeddings(model, prompt);
- verify(ollamaAPI, times(1)).generateEmbeddings(model, prompt);
- } catch (IOException | OllamaBaseException | InterruptedException e) {
- throw new RuntimeException(e);
- }
- }
-
- @Test
- void testAsk() {
- OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class);
- String model = OllamaModelType.LLAMA2;
- String prompt = "some prompt text";
- OptionsBuilder optionsBuilder = new OptionsBuilder();
- try {
- when(ollamaAPI.generate(model, prompt, optionsBuilder.build()))
- .thenReturn(new OllamaResult("", 0, 200));
- ollamaAPI.generate(model, prompt, optionsBuilder.build());
- verify(ollamaAPI, times(1)).generate(model, prompt, optionsBuilder.build());
- } catch (IOException | OllamaBaseException | InterruptedException e) {
- throw new RuntimeException(e);
- }
- }
-
- @Test
- void testAskWithImageFiles() {
- OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class);
- String model = OllamaModelType.LLAMA2;
- String prompt = "some prompt text";
- try {
- when(ollamaAPI.generateWithImageFiles(
- model, prompt, Collections.emptyList(), new OptionsBuilder().build()))
- .thenReturn(new OllamaResult("", 0, 200));
- ollamaAPI.generateWithImageFiles(
- model, prompt, Collections.emptyList(), new OptionsBuilder().build());
- verify(ollamaAPI, times(1))
- .generateWithImageFiles(
- model, prompt, Collections.emptyList(), new OptionsBuilder().build());
- } catch (IOException | OllamaBaseException | InterruptedException e) {
- throw new RuntimeException(e);
- }
- }
-
- @Test
- void testAskWithImageURLs() {
- OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class);
- String model = OllamaModelType.LLAMA2;
- String prompt = "some prompt text";
- try {
- when(ollamaAPI.generateWithImageURLs(
- model, prompt, Collections.emptyList(), new OptionsBuilder().build()))
- .thenReturn(new OllamaResult("", 0, 200));
- ollamaAPI.generateWithImageURLs(
- model, prompt, Collections.emptyList(), new OptionsBuilder().build());
- verify(ollamaAPI, times(1))
- .generateWithImageURLs(
- model, prompt, Collections.emptyList(), new OptionsBuilder().build());
- } catch (IOException | OllamaBaseException | InterruptedException | URISyntaxException e) {
- throw new RuntimeException(e);
- }
- }
-
- @Test
- void testAskAsync() {
- OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class);
- String model = OllamaModelType.LLAMA2;
- String prompt = "some prompt text";
- when(ollamaAPI.generateAsync(model, prompt))
- .thenReturn(new OllamaAsyncResultCallback(null, null, 3));
- ollamaAPI.generateAsync(model, prompt);
- verify(ollamaAPI, times(1)).generateAsync(model, prompt);
- }
-}
diff --git a/src/test/java/io/github/ollama4j/integrationtests/TestRealAPIs.java b/src/test/java/io/github/ollama4j/integrationtests/TestRealAPIs.java
new file mode 100644
index 0000000..d584747
--- /dev/null
+++ b/src/test/java/io/github/ollama4j/integrationtests/TestRealAPIs.java
@@ -0,0 +1,395 @@
+package io.github.ollama4j.integrationtests;
+
+import io.github.ollama4j.OllamaAPI;
+import io.github.ollama4j.exceptions.OllamaBaseException;
+import io.github.ollama4j.models.response.ModelDetail;
+import io.github.ollama4j.models.chat.OllamaChatRequest;
+import io.github.ollama4j.models.response.OllamaResult;
+import io.github.ollama4j.models.chat.OllamaChatMessageRole;
+import io.github.ollama4j.models.chat.OllamaChatRequestBuilder;
+import io.github.ollama4j.models.chat.OllamaChatResult;
+import io.github.ollama4j.models.embeddings.OllamaEmbeddingsRequestBuilder;
+import io.github.ollama4j.models.embeddings.OllamaEmbeddingsRequestModel;
+import io.github.ollama4j.utils.OptionsBuilder;
+import lombok.Data;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Order;
+import org.junit.jupiter.api.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.ConnectException;
+import java.net.URISyntaxException;
+import java.net.http.HttpConnectTimeoutException;
+import java.util.List;
+import java.util.Objects;
+import java.util.Properties;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+class TestRealAPIs {
+
+ private static final Logger LOG = LoggerFactory.getLogger(TestRealAPIs.class);
+
+ OllamaAPI ollamaAPI;
+ Config config;
+
+ private File getImageFileFromClasspath(String fileName) {
+ ClassLoader classLoader = getClass().getClassLoader();
+ return new File(Objects.requireNonNull(classLoader.getResource(fileName)).getFile());
+ }
+
+ @BeforeEach
+ void setUp() {
+ config = new Config();
+ ollamaAPI = new OllamaAPI(config.getOllamaURL());
+ ollamaAPI.setRequestTimeoutSeconds(config.getRequestTimeoutSeconds());
+ }
+
+ @Test
+ @Order(1)
+ void testWrongEndpoint() {
+ OllamaAPI ollamaAPI = new OllamaAPI("http://wrong-host:11434");
+ assertThrows(ConnectException.class, ollamaAPI::listModels);
+ }
+
+ @Test
+ @Order(1)
+ void testEndpointReachability() {
+ try {
+ assertNotNull(ollamaAPI.listModels());
+ } catch (HttpConnectTimeoutException e) {
+ fail(e.getMessage());
+ } catch (Exception e) {
+ fail(e);
+ }
+ }
+
+ @Test
+ @Order(2)
+ void testListModels() {
+ testEndpointReachability();
+ try {
+ assertNotNull(ollamaAPI.listModels());
+ ollamaAPI.listModels().forEach(System.out::println);
+ } catch (IOException | OllamaBaseException | InterruptedException | URISyntaxException e) {
+ fail(e);
+ }
+ }
+
+ @Test
+ @Order(2)
+ void testPullModel() {
+ testEndpointReachability();
+ try {
+ ollamaAPI.pullModel(config.getModel());
+ boolean found =
+ ollamaAPI.listModels().stream()
+ .anyMatch(model -> model.getModel().equalsIgnoreCase(config.getModel()));
+ assertTrue(found);
+ } catch (IOException | OllamaBaseException | InterruptedException | URISyntaxException e) {
+ fail(e);
+ }
+ }
+
+ @Test
+ @Order(3)
+ void testListDtails() {
+ testEndpointReachability();
+ try {
+ ModelDetail modelDetails = ollamaAPI.getModelDetails(config.getModel());
+ assertNotNull(modelDetails);
+ System.out.println(modelDetails);
+ } catch (IOException | OllamaBaseException | InterruptedException | URISyntaxException e) {
+ fail(e);
+ }
+ }
+
+ @Test
+ @Order(3)
+ void testAskModelWithDefaultOptions() {
+ testEndpointReachability();
+ try {
+ OllamaResult result =
+ ollamaAPI.generate(
+ config.getModel(),
+ "What is the capital of France? And what's France's connection with Mona Lisa?",
+ false,
+ new OptionsBuilder().build());
+ assertNotNull(result);
+ assertNotNull(result.getResponse());
+ assertFalse(result.getResponse().isEmpty());
+ } catch (IOException | OllamaBaseException | InterruptedException e) {
+ fail(e);
+ }
+ }
+
+ @Test
+ @Order(3)
+ void testAskModelWithDefaultOptionsStreamed() {
+ testEndpointReachability();
+ try {
+ StringBuffer sb = new StringBuffer("");
+ OllamaResult result = ollamaAPI.generate(config.getModel(),
+ "What is the capital of France? And what's France's connection with Mona Lisa?",
+ false,
+ new OptionsBuilder().build(), (s) -> {
+ LOG.info(s);
+ String substring = s.substring(sb.toString().length(), s.length());
+ LOG.info(substring);
+ sb.append(substring);
+ });
+
+ assertNotNull(result);
+ assertNotNull(result.getResponse());
+ assertFalse(result.getResponse().isEmpty());
+ assertEquals(sb.toString().trim(), result.getResponse().trim());
+ } catch (IOException | OllamaBaseException | InterruptedException e) {
+ fail(e);
+ }
+ }
+
+ @Test
+ @Order(3)
+ void testAskModelWithOptions() {
+ testEndpointReachability();
+ try {
+ OllamaResult result =
+ ollamaAPI.generate(
+ config.getModel(),
+ "What is the capital of France? And what's France's connection with Mona Lisa?",
+ true,
+ new OptionsBuilder().setTemperature(0.9f).build());
+ assertNotNull(result);
+ assertNotNull(result.getResponse());
+ assertFalse(result.getResponse().isEmpty());
+ } catch (IOException | OllamaBaseException | InterruptedException e) {
+ fail(e);
+ }
+ }
+
+ @Test
+ @Order(3)
+ void testChat() {
+ testEndpointReachability();
+ try {
+ OllamaChatRequestBuilder builder = OllamaChatRequestBuilder.getInstance(config.getModel());
+ OllamaChatRequest requestModel = builder.withMessage(OllamaChatMessageRole.USER, "What is the capital of France?")
+ .withMessage(OllamaChatMessageRole.ASSISTANT, "Should be Paris!")
+ .withMessage(OllamaChatMessageRole.USER, "And what is the second larges city?")
+ .build();
+
+ OllamaChatResult chatResult = ollamaAPI.chat(requestModel);
+ assertNotNull(chatResult);
+ assertFalse(chatResult.getResponse().isBlank());
+ assertEquals(4, chatResult.getChatHistory().size());
+ } catch (IOException | OllamaBaseException | InterruptedException e) {
+ fail(e);
+ }
+ }
+
+ @Test
+ @Order(3)
+ void testChatWithSystemPrompt() {
+ testEndpointReachability();
+ try {
+ OllamaChatRequestBuilder builder = OllamaChatRequestBuilder.getInstance(config.getModel());
+ OllamaChatRequest requestModel = builder.withMessage(OllamaChatMessageRole.SYSTEM,
+ "You are a silent bot that only says 'NI'. Do not say anything else under any circumstances!")
+ .withMessage(OllamaChatMessageRole.USER,
+ "What is the capital of France? And what's France's connection with Mona Lisa?")
+ .build();
+
+ OllamaChatResult chatResult = ollamaAPI.chat(requestModel);
+ assertNotNull(chatResult);
+ assertFalse(chatResult.getResponse().isBlank());
+ assertTrue(chatResult.getResponse().startsWith("NI"));
+ assertEquals(3, chatResult.getChatHistory().size());
+ } catch (IOException | OllamaBaseException | InterruptedException e) {
+ fail(e);
+ }
+ }
+
+ @Test
+ @Order(3)
+ void testChatWithStream() {
+ testEndpointReachability();
+ try {
+ OllamaChatRequestBuilder builder = OllamaChatRequestBuilder.getInstance(config.getModel());
+ OllamaChatRequest requestModel = builder.withMessage(OllamaChatMessageRole.USER,
+ "What is the capital of France? And what's France's connection with Mona Lisa?")
+ .build();
+
+ StringBuffer sb = new StringBuffer("");
+
+ OllamaChatResult chatResult = ollamaAPI.chat(requestModel, (s) -> {
+ LOG.info(s);
+ String substring = s.substring(sb.toString().length(), s.length());
+ LOG.info(substring);
+ sb.append(substring);
+ });
+ assertNotNull(chatResult);
+ assertEquals(sb.toString().trim(), chatResult.getResponse().trim());
+ } catch (IOException | OllamaBaseException | InterruptedException e) {
+ fail(e);
+ }
+ }
+
+ @Test
+ @Order(3)
+ void testChatWithImageFromFileWithHistoryRecognition() {
+ testEndpointReachability();
+ try {
+ OllamaChatRequestBuilder builder =
+ OllamaChatRequestBuilder.getInstance(config.getImageModel());
+ OllamaChatRequest requestModel =
+ builder.withMessage(OllamaChatMessageRole.USER, "What's in the picture?",
+ List.of(getImageFileFromClasspath("dog-on-a-boat.jpg"))).build();
+
+ OllamaChatResult chatResult = ollamaAPI.chat(requestModel);
+ assertNotNull(chatResult);
+ assertNotNull(chatResult.getResponse());
+
+ builder.reset();
+
+ requestModel =
+ builder.withMessages(chatResult.getChatHistory())
+ .withMessage(OllamaChatMessageRole.USER, "What's the dogs breed?").build();
+
+ chatResult = ollamaAPI.chat(requestModel);
+ assertNotNull(chatResult);
+ assertNotNull(chatResult.getResponse());
+
+
+ } catch (IOException | OllamaBaseException | InterruptedException e) {
+ fail(e);
+ }
+ }
+
+ @Test
+ @Order(3)
+ void testChatWithImageFromURL() {
+ testEndpointReachability();
+ try {
+ OllamaChatRequestBuilder builder = OllamaChatRequestBuilder.getInstance(config.getImageModel());
+ OllamaChatRequest requestModel = builder.withMessage(OllamaChatMessageRole.USER, "What's in the picture?",
+ "https://t3.ftcdn.net/jpg/02/96/63/80/360_F_296638053_0gUVA4WVBKceGsIr7LNqRWSnkusi07dq.jpg")
+ .build();
+
+ OllamaChatResult chatResult = ollamaAPI.chat(requestModel);
+ assertNotNull(chatResult);
+ } catch (IOException | OllamaBaseException | InterruptedException e) {
+ fail(e);
+ }
+ }
+
+ @Test
+ @Order(3)
+ void testAskModelWithOptionsAndImageFiles() {
+ testEndpointReachability();
+ File imageFile = getImageFileFromClasspath("dog-on-a-boat.jpg");
+ try {
+ OllamaResult result =
+ ollamaAPI.generateWithImageFiles(
+ config.getImageModel(),
+ "What is in this image?",
+ List.of(imageFile),
+ new OptionsBuilder().build());
+ assertNotNull(result);
+ assertNotNull(result.getResponse());
+ assertFalse(result.getResponse().isEmpty());
+ } catch (IOException | OllamaBaseException | InterruptedException e) {
+ fail(e);
+ }
+ }
+
+ @Test
+ @Order(3)
+ void testAskModelWithOptionsAndImageFilesStreamed() {
+ testEndpointReachability();
+ File imageFile = getImageFileFromClasspath("dog-on-a-boat.jpg");
+ try {
+ StringBuffer sb = new StringBuffer("");
+
+ OllamaResult result = ollamaAPI.generateWithImageFiles(config.getImageModel(),
+ "What is in this image?", List.of(imageFile), new OptionsBuilder().build(), (s) -> {
+ LOG.info(s);
+ String substring = s.substring(sb.toString().length(), s.length());
+ LOG.info(substring);
+ sb.append(substring);
+ });
+ assertNotNull(result);
+ assertNotNull(result.getResponse());
+ assertFalse(result.getResponse().isEmpty());
+ assertEquals(sb.toString().trim(), result.getResponse().trim());
+ } catch (IOException | OllamaBaseException | InterruptedException e) {
+ fail(e);
+ }
+ }
+
+ @Test
+ @Order(3)
+ void testAskModelWithOptionsAndImageURLs() {
+ testEndpointReachability();
+ try {
+ OllamaResult result =
+ ollamaAPI.generateWithImageURLs(
+ config.getImageModel(),
+ "What is in this image?",
+ List.of(
+ "https://t3.ftcdn.net/jpg/02/96/63/80/360_F_296638053_0gUVA4WVBKceGsIr7LNqRWSnkusi07dq.jpg"),
+ new OptionsBuilder().build());
+ assertNotNull(result);
+ assertNotNull(result.getResponse());
+ assertFalse(result.getResponse().isEmpty());
+ } catch (IOException | OllamaBaseException | InterruptedException | URISyntaxException e) {
+ fail(e);
+ }
+ }
+
+ @Test
+ @Order(3)
+ public void testEmbedding() {
+ testEndpointReachability();
+ try {
+ OllamaEmbeddingsRequestModel request = OllamaEmbeddingsRequestBuilder
+ .getInstance(config.getModel(), "What is the capital of France?").build();
+
+ List embeddings = ollamaAPI.generateEmbeddings(request);
+
+ assertNotNull(embeddings);
+ assertFalse(embeddings.isEmpty());
+ } catch (IOException | OllamaBaseException | InterruptedException e) {
+ fail(e);
+ }
+ }
+}
+
+@Data
+class Config {
+ private String ollamaURL;
+ private String model;
+ private String imageModel;
+ private int requestTimeoutSeconds;
+
+ public Config() {
+ Properties properties = new Properties();
+ try (InputStream input =
+ getClass().getClassLoader().getResourceAsStream("test-config.properties")) {
+ if (input == null) {
+ throw new RuntimeException("Sorry, unable to find test-config.properties");
+ }
+ properties.load(input);
+ this.ollamaURL = properties.getProperty("ollama.url");
+ this.model = properties.getProperty("ollama.model");
+ this.imageModel = properties.getProperty("ollama.model.image");
+ this.requestTimeoutSeconds =
+ Integer.parseInt(properties.getProperty("ollama.request-timeout-seconds"));
+ } catch (IOException e) {
+ throw new RuntimeException("Error loading properties", e);
+ }
+ }
+}
diff --git a/src/test/java/io/github/ollama4j/unittests/TestMockedAPIs.java b/src/test/java/io/github/ollama4j/unittests/TestMockedAPIs.java
new file mode 100644
index 0000000..921ccf7
--- /dev/null
+++ b/src/test/java/io/github/ollama4j/unittests/TestMockedAPIs.java
@@ -0,0 +1,164 @@
+package io.github.ollama4j.unittests;
+
+import io.github.ollama4j.OllamaAPI;
+import io.github.ollama4j.exceptions.OllamaBaseException;
+import io.github.ollama4j.models.response.ModelDetail;
+import io.github.ollama4j.models.response.OllamaAsyncResultStreamer;
+import io.github.ollama4j.models.response.OllamaResult;
+import io.github.ollama4j.types.OllamaModelType;
+import io.github.ollama4j.utils.OptionsBuilder;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.Collections;
+
+import static org.mockito.Mockito.*;
+
+class TestMockedAPIs {
+ @Test
+ void testPullModel() {
+ OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class);
+ String model = OllamaModelType.LLAMA2;
+ try {
+ doNothing().when(ollamaAPI).pullModel(model);
+ ollamaAPI.pullModel(model);
+ verify(ollamaAPI, times(1)).pullModel(model);
+ } catch (IOException | OllamaBaseException | InterruptedException | URISyntaxException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Test
+ void testListModels() {
+ OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class);
+ try {
+ when(ollamaAPI.listModels()).thenReturn(new ArrayList<>());
+ ollamaAPI.listModels();
+ verify(ollamaAPI, times(1)).listModels();
+ } catch (IOException | OllamaBaseException | InterruptedException | URISyntaxException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Test
+ void testCreateModel() {
+ OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class);
+ String model = OllamaModelType.LLAMA2;
+ String modelFilePath = "FROM llama2\nSYSTEM You are mario from Super Mario Bros.";
+ try {
+ doNothing().when(ollamaAPI).createModelWithModelFileContents(model, modelFilePath);
+ ollamaAPI.createModelWithModelFileContents(model, modelFilePath);
+ verify(ollamaAPI, times(1)).createModelWithModelFileContents(model, modelFilePath);
+ } catch (IOException | OllamaBaseException | InterruptedException | URISyntaxException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Test
+ void testDeleteModel() {
+ OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class);
+ String model = OllamaModelType.LLAMA2;
+ try {
+ doNothing().when(ollamaAPI).deleteModel(model, true);
+ ollamaAPI.deleteModel(model, true);
+ verify(ollamaAPI, times(1)).deleteModel(model, true);
+ } catch (IOException | OllamaBaseException | InterruptedException | URISyntaxException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Test
+ void testGetModelDetails() {
+ OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class);
+ String model = OllamaModelType.LLAMA2;
+ try {
+ when(ollamaAPI.getModelDetails(model)).thenReturn(new ModelDetail());
+ ollamaAPI.getModelDetails(model);
+ verify(ollamaAPI, times(1)).getModelDetails(model);
+ } catch (IOException | OllamaBaseException | InterruptedException | URISyntaxException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Test
+ void testGenerateEmbeddings() {
+ OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class);
+ String model = OllamaModelType.LLAMA2;
+ String prompt = "some prompt text";
+ try {
+ when(ollamaAPI.generateEmbeddings(model, prompt)).thenReturn(new ArrayList<>());
+ ollamaAPI.generateEmbeddings(model, prompt);
+ verify(ollamaAPI, times(1)).generateEmbeddings(model, prompt);
+ } catch (IOException | OllamaBaseException | InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Test
+ void testAsk() {
+ OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class);
+ String model = OllamaModelType.LLAMA2;
+ String prompt = "some prompt text";
+ OptionsBuilder optionsBuilder = new OptionsBuilder();
+ try {
+ when(ollamaAPI.generate(model, prompt, false, optionsBuilder.build()))
+ .thenReturn(new OllamaResult("", 0, 200));
+ ollamaAPI.generate(model, prompt, false, optionsBuilder.build());
+ verify(ollamaAPI, times(1)).generate(model, prompt, false, optionsBuilder.build());
+ } catch (IOException | OllamaBaseException | InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Test
+ void testAskWithImageFiles() {
+ OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class);
+ String model = OllamaModelType.LLAMA2;
+ String prompt = "some prompt text";
+ try {
+ when(ollamaAPI.generateWithImageFiles(
+ model, prompt, Collections.emptyList(), new OptionsBuilder().build()))
+ .thenReturn(new OllamaResult("", 0, 200));
+ ollamaAPI.generateWithImageFiles(
+ model, prompt, Collections.emptyList(), new OptionsBuilder().build());
+ verify(ollamaAPI, times(1))
+ .generateWithImageFiles(
+ model, prompt, Collections.emptyList(), new OptionsBuilder().build());
+ } catch (IOException | OllamaBaseException | InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Test
+ void testAskWithImageURLs() {
+ OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class);
+ String model = OllamaModelType.LLAMA2;
+ String prompt = "some prompt text";
+ try {
+ when(ollamaAPI.generateWithImageURLs(
+ model, prompt, Collections.emptyList(), new OptionsBuilder().build()))
+ .thenReturn(new OllamaResult("", 0, 200));
+ ollamaAPI.generateWithImageURLs(
+ model, prompt, Collections.emptyList(), new OptionsBuilder().build());
+ verify(ollamaAPI, times(1))
+ .generateWithImageURLs(
+ model, prompt, Collections.emptyList(), new OptionsBuilder().build());
+ } catch (IOException | OllamaBaseException | InterruptedException | URISyntaxException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Test
+ void testAskAsync() {
+ OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class);
+ String model = OllamaModelType.LLAMA2;
+ String prompt = "some prompt text";
+ when(ollamaAPI.generateAsync(model, prompt, false))
+ .thenReturn(new OllamaAsyncResultStreamer(null, null, 3));
+ ollamaAPI.generateAsync(model, prompt, false);
+ verify(ollamaAPI, times(1)).generateAsync(model, prompt, false);
+ }
+}
diff --git a/src/test/java/io/github/amithkoujalgi/ollama4j/unittests/jackson/AbstractSerializationTest.java b/src/test/java/io/github/ollama4j/unittests/jackson/AbstractSerializationTest.java
similarity index 89%
rename from src/test/java/io/github/amithkoujalgi/ollama4j/unittests/jackson/AbstractSerializationTest.java
rename to src/test/java/io/github/ollama4j/unittests/jackson/AbstractSerializationTest.java
index d0ffc2c..6e03566 100644
--- a/src/test/java/io/github/amithkoujalgi/ollama4j/unittests/jackson/AbstractSerializationTest.java
+++ b/src/test/java/io/github/ollama4j/unittests/jackson/AbstractSerializationTest.java
@@ -1,10 +1,10 @@
-package io.github.amithkoujalgi.ollama4j.unittests.jackson;
+package io.github.ollama4j.unittests.jackson;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
-import io.github.amithkoujalgi.ollama4j.core.utils.Utils;
+import io.github.ollama4j.utils.Utils;
public abstract class AbstractSerializationTest {
diff --git a/src/test/java/io/github/amithkoujalgi/ollama4j/unittests/jackson/TestChatRequestSerialization.java b/src/test/java/io/github/ollama4j/unittests/jackson/TestChatRequestSerialization.java
similarity index 63%
rename from src/test/java/io/github/amithkoujalgi/ollama4j/unittests/jackson/TestChatRequestSerialization.java
rename to src/test/java/io/github/ollama4j/unittests/jackson/TestChatRequestSerialization.java
index 3ad049c..2391a94 100644
--- a/src/test/java/io/github/amithkoujalgi/ollama4j/unittests/jackson/TestChatRequestSerialization.java
+++ b/src/test/java/io/github/ollama4j/unittests/jackson/TestChatRequestSerialization.java
@@ -1,20 +1,20 @@
-package io.github.amithkoujalgi.ollama4j.unittests.jackson;
+package io.github.ollama4j.unittests.jackson;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.File;
import java.util.List;
+import io.github.ollama4j.models.chat.OllamaChatRequest;
import org.json.JSONObject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-import io.github.amithkoujalgi.ollama4j.core.models.chat.OllamaChatMessageRole;
-import io.github.amithkoujalgi.ollama4j.core.models.chat.OllamaChatRequestBuilder;
-import io.github.amithkoujalgi.ollama4j.core.models.chat.OllamaChatRequestModel;
-import io.github.amithkoujalgi.ollama4j.core.utils.OptionsBuilder;
+import io.github.ollama4j.models.chat.OllamaChatMessageRole;
+import io.github.ollama4j.models.chat.OllamaChatRequestBuilder;
+import io.github.ollama4j.utils.OptionsBuilder;
-public class TestChatRequestSerialization extends AbstractSerializationTest {
+public class TestChatRequestSerialization extends AbstractSerializationTest {
private OllamaChatRequestBuilder builder;
@@ -25,32 +25,32 @@ public class TestChatRequestSerialization extends AbstractSerializationTest {
diff --git a/src/test/java/io/github/amithkoujalgi/ollama4j/unittests/jackson/TestGenerateRequestSerialization.java b/src/test/java/io/github/ollama4j/unittests/jackson/TestGenerateRequestSerialization.java
similarity index 68%
rename from src/test/java/io/github/amithkoujalgi/ollama4j/unittests/jackson/TestGenerateRequestSerialization.java
rename to src/test/java/io/github/ollama4j/unittests/jackson/TestGenerateRequestSerialization.java
index 8e95288..4ca0672 100644
--- a/src/test/java/io/github/amithkoujalgi/ollama4j/unittests/jackson/TestGenerateRequestSerialization.java
+++ b/src/test/java/io/github/ollama4j/unittests/jackson/TestGenerateRequestSerialization.java
@@ -1,17 +1,17 @@
-package io.github.amithkoujalgi.ollama4j.unittests.jackson;
+package io.github.ollama4j.unittests.jackson;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import io.github.ollama4j.models.generate.OllamaGenerateRequest;
import org.json.JSONObject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-import io.github.amithkoujalgi.ollama4j.core.models.generate.OllamaGenerateRequestBuilder;
-import io.github.amithkoujalgi.ollama4j.core.models.generate.OllamaGenerateRequestModel;
-import io.github.amithkoujalgi.ollama4j.core.utils.OptionsBuilder;
+import io.github.ollama4j.models.generate.OllamaGenerateRequestBuilder;
+import io.github.ollama4j.utils.OptionsBuilder;
-public class TestGenerateRequestSerialization extends AbstractSerializationTest {
+public class TestGenerateRequestSerialization extends AbstractSerializationTest {
private OllamaGenerateRequestBuilder builder;
@@ -22,27 +22,27 @@ public class TestGenerateRequestSerialization extends AbstractSerializationTest<
@Test
public void testRequestOnlyMandatoryFields() {
- OllamaGenerateRequestModel req = builder.withPrompt("Some prompt").build();
+ OllamaGenerateRequest req = builder.withPrompt("Some prompt").build();
String jsonRequest = serialize(req);
- assertEqualsAfterUnmarshalling(deserialize(jsonRequest, OllamaGenerateRequestModel.class), req);
+ assertEqualsAfterUnmarshalling(deserialize(jsonRequest, OllamaGenerateRequest.class), req);
}
@Test
public void testRequestWithOptions() {
OptionsBuilder b = new OptionsBuilder();
- OllamaGenerateRequestModel req =
+ OllamaGenerateRequest req =
builder.withPrompt("Some prompt").withOptions(b.setMirostat(1).build()).build();
String jsonRequest = serialize(req);
- OllamaGenerateRequestModel deserializeRequest = deserialize(jsonRequest, OllamaGenerateRequestModel.class);
+ OllamaGenerateRequest deserializeRequest = deserialize(jsonRequest, OllamaGenerateRequest.class);
assertEqualsAfterUnmarshalling(deserializeRequest, req);
assertEquals(1, deserializeRequest.getOptions().get("mirostat"));
}
@Test
public void testWithJsonFormat() {
- OllamaGenerateRequestModel req =
+ OllamaGenerateRequest req =
builder.withPrompt("Some prompt").withGetJsonResponse().build();
String jsonRequest = serialize(req);
diff --git a/src/test/java/io/github/amithkoujalgi/ollama4j/unittests/jackson/TestModelRequestSerialization.java b/src/test/java/io/github/ollama4j/unittests/jackson/TestModelRequestSerialization.java
similarity index 93%
rename from src/test/java/io/github/amithkoujalgi/ollama4j/unittests/jackson/TestModelRequestSerialization.java
rename to src/test/java/io/github/ollama4j/unittests/jackson/TestModelRequestSerialization.java
index 712e507..5bc44f3 100644
--- a/src/test/java/io/github/amithkoujalgi/ollama4j/unittests/jackson/TestModelRequestSerialization.java
+++ b/src/test/java/io/github/ollama4j/unittests/jackson/TestModelRequestSerialization.java
@@ -1,6 +1,6 @@
-package io.github.amithkoujalgi.ollama4j.unittests.jackson;
+package io.github.ollama4j.unittests.jackson;
-import io.github.amithkoujalgi.ollama4j.core.models.Model;
+import io.github.ollama4j.models.response.Model;
import org.junit.jupiter.api.Test;
public class TestModelRequestSerialization extends AbstractSerializationTest {