From e33071ae38e07c87cec8483c4b1cbb349c3c5e53 Mon Sep 17 00:00:00 2001 From: Mitchell Lutzke Date: Sat, 26 Oct 2024 21:22:46 -0700 Subject: [PATCH] Add minp option and ability to set custom options --- .../github/ollama4j/utils/OptionsBuilder.java | 29 +++++++++++++++++++ .../jackson/TestChatRequestSerialization.java | 20 +++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/main/java/io/github/ollama4j/utils/OptionsBuilder.java b/src/main/java/io/github/ollama4j/utils/OptionsBuilder.java index c71818a..e34a292 100644 --- a/src/main/java/io/github/ollama4j/utils/OptionsBuilder.java +++ b/src/main/java/io/github/ollama4j/utils/OptionsBuilder.java @@ -207,6 +207,33 @@ public class OptionsBuilder { return this; } + /** + * Alternative to the top_p, and aims to ensure a balance of qualityand variety. The parameter p + * represents the minimum probability for a token to be considered, relative to the probability + * of the most likely token. For example, with p=0.05 and the most likely token having a + * probability of 0.9, logits with a value less than 0.045 are filtered out. (Default: 0.0) + */ + public OptionsBuilder setMinP(float value) { + options.getOptionsMap().put("min_p", value); + return this; + } + + /** + * Allows passing an option not formally supported by the library + * @param name The option name for the parameter. + * @param value The value for the "{name}" parameter. + * @return The updated OptionsBuilder. + */ + public OptionsBuilder setCustomOption(String name, Object value) throws IllegalArgumentException { + if (!(value instanceof Integer || value instanceof Float || value instanceof String)) { + throw new IllegalArgumentException("Invalid type for parameter. Allowed types are: Integer, Float, or String."); + } + options.getOptionsMap().put(name, value); + return this; + } + + + /** * Builds the options map. * @@ -215,4 +242,6 @@ public class OptionsBuilder { public Options build() { return options; } + + } diff --git a/src/test/java/io/github/ollama4j/unittests/jackson/TestChatRequestSerialization.java b/src/test/java/io/github/ollama4j/unittests/jackson/TestChatRequestSerialization.java index 2391a94..80cb063 100644 --- a/src/test/java/io/github/ollama4j/unittests/jackson/TestChatRequestSerialization.java +++ b/src/test/java/io/github/ollama4j/unittests/jackson/TestChatRequestSerialization.java @@ -3,10 +3,12 @@ package io.github.ollama4j.unittests.jackson; import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.File; +import java.security.InvalidParameterException; import java.util.List; import io.github.ollama4j.models.chat.OllamaChatRequest; import org.json.JSONObject; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -59,6 +61,10 @@ public class TestChatRequestSerialization extends AbstractSerializationTest { + OllamaChatRequest req = builder.withMessage(OllamaChatMessageRole.USER, "Some prompt") + .withOptions(b.setCustomOption("cust_obj", new Object()).build()) + .build(); + }); } @Test