From bce9be0e7463455eb5cf07db293cb9e5e9188832 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20D=C3=BCmont?= Date: Thu, 21 Nov 2024 16:25:47 +0100 Subject: [PATCH 1/9] Initial --- .../orchestration/OrchestrationAiModel.java | 57 +++++++++--- .../OrchestrationAiModelParameters.java | 86 +++++++++++++++++++ .../orchestration/OrchestrationUnitTest.java | 8 +- 3 files changed, 134 insertions(+), 17 deletions(-) create mode 100644 orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationAiModelParameters.java diff --git a/orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationAiModel.java b/orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationAiModel.java index 0342dd82c..7480188bb 100644 --- a/orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationAiModel.java +++ b/orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationAiModel.java @@ -3,17 +3,21 @@ import com.sap.ai.sdk.orchestration.client.model.LLMModuleConfig; import java.util.Map; import javax.annotation.Nonnull; +import lombok.AccessLevel; import lombok.AllArgsConstructor; -import lombok.Value; +import lombok.EqualsAndHashCode; +import lombok.Getter; import lombok.With; +import lombok.experimental.Tolerate; /** Large language models available in Orchestration. */ -@Value @With @AllArgsConstructor +@Getter(AccessLevel.PACKAGE) +@EqualsAndHashCode public class OrchestrationAiModel { /** The name of the model */ - String name; + private final String name; /** * Optional parameters on this model. @@ -26,10 +30,10 @@ public class OrchestrationAiModel { * "presence_penalty", 0) * } */ - Map params; + private final Map params; /** The version of the model, defaults to "latest". */ - String version; + private final String version; /** IBM Granite 13B chat completions model */ public static final OrchestrationAiModel IBM_GRANITE_13B_CHAT = @@ -76,23 +80,28 @@ public class OrchestrationAiModel { new OrchestrationAiModel("amazon--titan-text-express"); /** Azure OpenAI GPT-3.5 Turbo chat completions model */ - public static final OrchestrationAiModel GPT_35_TURBO = new OrchestrationAiModel("gpt-35-turbo"); + public static final Parameterized GPT_35_TURBO = + new Parameterized<>("gpt-35-turbo"); /** Azure OpenAI GPT-3.5 Turbo chat completions model */ - public static final OrchestrationAiModel GPT_35_TURBO_16K = - new OrchestrationAiModel("gpt-35-turbo-16k"); + public static final Parameterized GPT_35_TURBO_16K = + new Parameterized<>("gpt-35-turbo-16k"); /** Azure OpenAI GPT-4 chat completions model */ - public static final OrchestrationAiModel GPT_4 = new OrchestrationAiModel("gpt-4"); + public static final Parameterized GPT_4 = + new Parameterized<>("gpt-4"); /** Azure OpenAI GPT-4-32k chat completions model */ - public static final OrchestrationAiModel GPT_4_32K = new OrchestrationAiModel("gpt-4-32k"); + public static final Parameterized GPT_4_32K = + new Parameterized<>("gpt-4-32k"); /** Azure OpenAI GPT-4o chat completions model */ - public static final OrchestrationAiModel GPT_4O = new OrchestrationAiModel("gpt-4o"); + public static final Parameterized GPT_4O = + new Parameterized<>("gpt-4o"); /** Azure OpenAI GPT-4o-mini chat completions model */ - public static final OrchestrationAiModel GPT_4O_MINI = new OrchestrationAiModel("gpt-4o-mini"); + public static final Parameterized GPT_4O_MINI = + new Parameterized<>("gpt-4o-mini"); /** Google Cloud Platform Gemini 1.0 Pro model */ public static final OrchestrationAiModel GEMINI_1_0_PRO = @@ -114,4 +123,28 @@ public class OrchestrationAiModel { LLMModuleConfig createConfig() { return new LLMModuleConfig().modelName(name).modelParams(params).modelVersion(version); } + + /** + * Subclass to allow for parameterized models. + * + * @param The type of parameters for this model. + */ + public static final class Parameterized + extends OrchestrationAiModel { + private Parameterized(@Nonnull final String name) { + super(name); + } + + /** + * Set the typed parameters for this model. + * + * @param params The parameters for this model. + * @return The model with the parameters set. + */ + @Tolerate + @Nonnull + public OrchestrationAiModel withParams(@Nonnull final T params) { + return super.withParams(params.getParams()); + } + } } diff --git a/orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationAiModelParameters.java b/orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationAiModelParameters.java new file mode 100644 index 000000000..5483b6689 --- /dev/null +++ b/orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationAiModelParameters.java @@ -0,0 +1,86 @@ +package com.sap.ai.sdk.orchestration; + +import java.util.Map; +import javax.annotation.Nonnull; + +/** Helper interface to define typed parameters. */ +@FunctionalInterface +public interface OrchestrationAiModelParameters { + /** + * Get the parameters. + * + * @return the parameters. + */ + @Nonnull + Map getParams(); + + /** GPT model parameters. */ + interface GPT extends OrchestrationAiModelParameters { + /** + * Create a new builder. + * + * @return the builder. + */ + @Nonnull + static GPT.Builder0 params() { + return maxTokens -> + temperature -> + frequencyPenalty -> + presencePenalty -> + () -> + Map.of( + "max_tokens", maxTokens, + "temperature", temperature, + "frequency_penalty", frequencyPenalty, + "presence_penalty", presencePenalty); + } + + /** Builder for GPT model parameters. */ + interface Builder0 { + /** + * Set the max tokens. + * + * @param maxTokens the max tokens. + * @return the next builder. + */ + @Nonnull + GPT.Builder1 maxTokens(@Nonnull final Number maxTokens); + } + + /** Builder for GPT model parameters. */ + interface Builder1 { + /** + * Set the temperature. + * + * @param temperature the temperature. + * @return the next builder. + */ + @Nonnull + GPT.Builder2 temperature(@Nonnull final Number temperature); + } + + /** Builder for GPT model parameters. */ + interface Builder2 { + /** + * Set the frequency penalty. + * + * @param frequencyPenalty the frequency penalty. + * @return the next builder. + */ + @Nonnull + GPT.Builder3 frequencyPenalty(@Nonnull final Number frequencyPenalty); + } + + /** Builder for GPT model parameters. */ + interface Builder3 { + /** + * Set the presence penalty. + * + * @param presencePenalty the presence penalty. + * @return the final typed parameter object. + */ + @Nonnull + GPT presencePenalty(@Nonnull final Number presencePenalty); + } + } +} diff --git a/orchestration/src/test/java/com/sap/ai/sdk/orchestration/OrchestrationUnitTest.java b/orchestration/src/test/java/com/sap/ai/sdk/orchestration/OrchestrationUnitTest.java index ca37f456b..f3bcacb18 100644 --- a/orchestration/src/test/java/com/sap/ai/sdk/orchestration/OrchestrationUnitTest.java +++ b/orchestration/src/test/java/com/sap/ai/sdk/orchestration/OrchestrationUnitTest.java @@ -19,6 +19,7 @@ import static com.sap.ai.sdk.orchestration.AzureFilterThreshold.ALLOW_SAFE; import static com.sap.ai.sdk.orchestration.AzureFilterThreshold.ALLOW_SAFE_LOW_MEDIUM; import static com.sap.ai.sdk.orchestration.OrchestrationAiModel.GPT_35_TURBO_16K; +import static com.sap.ai.sdk.orchestration.OrchestrationAiModelParameters.GPT.params; import static org.apache.hc.core5.http.HttpStatus.SC_BAD_REQUEST; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -53,11 +54,8 @@ class OrchestrationUnitTest { static final OrchestrationAiModel CUSTOM_GPT_35 = GPT_35_TURBO_16K.withParams( - Map.of( - "max_tokens", 50, - "temperature", 0.1, - "frequency_penalty", 0, - "presence_penalty", 0)); + params().maxTokens(50).temperature(0.1).frequencyPenalty(0).presencePenalty(0)); + private final Function fileLoader = filename -> Objects.requireNonNull(getClass().getClassLoader().getResourceAsStream(filename)); From 3dfd32351ce3126d34c73ae21676c4e4534aa7f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20D=C3=BCmont?= Date: Thu, 21 Nov 2024 16:25:59 +0100 Subject: [PATCH 2/9] Docs --- docs/guides/ORCHESTRATION_CHAT_COMPLETION.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/docs/guides/ORCHESTRATION_CHAT_COMPLETION.md b/docs/guides/ORCHESTRATION_CHAT_COMPLETION.md index 01bc97477..8f8467102 100644 --- a/docs/guides/ORCHESTRATION_CHAT_COMPLETION.md +++ b/docs/guides/ORCHESTRATION_CHAT_COMPLETION.md @@ -201,10 +201,6 @@ Change your LLM configuration to add model parameters: OrchestrationAiModel customGPT4O = OrchestrationAiModel.GPT_4O .withParams( - Map.of( - "max_tokens", 50, - "temperature", 0.1, - "frequency_penalty", 0, - "presence_penalty", 0)) + params().maxTokens(50).temperature(0.1).frequencyPenalty(0).presencePenalty(0)) .withVersion("2024-05-13"); ``` From f56e6c5fb883ca612fb8a0e98141f86dd26052e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20D=C3=BCmont?= Date: Wed, 27 Nov 2024 15:52:54 +0100 Subject: [PATCH 3/9] Adjust solution --- .../orchestration/OrchestrationAiModel.java | 104 +++++++++++------- .../OrchestrationAiModelParameters.java | 86 --------------- .../OrchestrationModuleConfigTest.java | 39 +++++++ .../orchestration/OrchestrationUnitTest.java | 9 +- 4 files changed, 109 insertions(+), 129 deletions(-) delete mode 100644 orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationAiModelParameters.java diff --git a/orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationAiModel.java b/orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationAiModel.java index 0c771c03a..b7e86bcf8 100644 --- a/orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationAiModel.java +++ b/orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationAiModel.java @@ -1,23 +1,22 @@ package com.sap.ai.sdk.orchestration; import com.sap.ai.sdk.orchestration.client.model.LLMModuleConfig; +import java.util.LinkedHashMap; import java.util.Map; import javax.annotation.Nonnull; -import lombok.AccessLevel; +import javax.annotation.Nullable; import lombok.AllArgsConstructor; -import lombok.EqualsAndHashCode; -import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.Value; import lombok.With; -import lombok.experimental.Tolerate; /** Large language models available in Orchestration. */ +@Value @With @AllArgsConstructor -@Getter(AccessLevel.PACKAGE) -@EqualsAndHashCode public class OrchestrationAiModel { /** The name of the model */ - private final String name; + String name; /** * Optional parameters on this model. @@ -30,10 +29,10 @@ public class OrchestrationAiModel { * "presence_penalty", 0) * } */ - private final Map params; + Map params; /** The version of the model, defaults to "latest". */ - private final String version; + String version; /** IBM Granite 13B chat completions model */ public static final OrchestrationAiModel IBM_GRANITE_13B_CHAT = @@ -80,28 +79,23 @@ public class OrchestrationAiModel { new OrchestrationAiModel("amazon--titan-text-express"); /** Azure OpenAI GPT-3.5 Turbo chat completions model */ - public static final Parameterized GPT_35_TURBO = - new Parameterized<>("gpt-35-turbo"); + public static final OrchestrationAiModel GPT_35_TURBO = new OrchestrationAiModel("gpt-35-turbo"); /** Azure OpenAI GPT-3.5 Turbo chat completions model */ - public static final Parameterized GPT_35_TURBO_16K = - new Parameterized<>("gpt-35-turbo-16k"); + public static final OrchestrationAiModel GPT_35_TURBO_16K = + new OrchestrationAiModel("gpt-35-turbo-16k"); /** Azure OpenAI GPT-4 chat completions model */ - public static final Parameterized GPT_4 = - new Parameterized<>("gpt-4"); + public static final OrchestrationAiModel GPT_4 = new OrchestrationAiModel("gpt-4"); /** Azure OpenAI GPT-4-32k chat completions model */ - public static final Parameterized GPT_4_32K = - new Parameterized<>("gpt-4-32k"); + public static final OrchestrationAiModel GPT_4_32K = new OrchestrationAiModel("gpt-4-32k"); /** Azure OpenAI GPT-4o chat completions model */ - public static final Parameterized GPT_4O = - new Parameterized<>("gpt-4o"); + public static final OrchestrationAiModel GPT_4O = new OrchestrationAiModel("gpt-4o"); /** Azure OpenAI GPT-4o-mini chat completions model */ - public static final Parameterized GPT_4O_MINI = - new Parameterized<>("gpt-4o-mini"); + public static final OrchestrationAiModel GPT_4O_MINI = new OrchestrationAiModel("gpt-4o-mini"); /** Google Cloud Platform Gemini 1.0 Pro model */ public static final OrchestrationAiModel GEMINI_1_0_PRO = @@ -125,26 +119,56 @@ LLMModuleConfig createConfig() { } /** - * Subclass to allow for parameterized models. + * Additional parameter on this model. * - * @param The type of parameters for this model. + * @param key the parameter key. + * @param value the parameter value, nullable. + * @return A new model with the additional parameter. */ - public static final class Parameterized - extends OrchestrationAiModel { - private Parameterized(@Nonnull final String name) { - super(name); - } - - /** - * Set the typed parameters for this model. - * - * @param params The parameters for this model. - * @return The model with the parameters set. - */ - @Tolerate - @Nonnull - public OrchestrationAiModel withParams(@Nonnull final T params) { - return super.withParams(params.getParams()); - } + @Nonnull + public OrchestrationAiModel withParam(@Nonnull final String key, @Nullable final Object value) { + final var params = new LinkedHashMap<>(getParams()); + params.put(key, value); + return withParams(params); + } + + /** + * Additional parameter on this model. + * + * @param param the parameter key. + * @param value the parameter value, nullable. + * @return A new model with the additional parameter. + */ + @Nonnull + public OrchestrationAiModel withParam( + @Nonnull final Parameter param, @Nullable final Object value) { + return withParam(param.value, value); + } + + /** Parameter key for a model. */ + @RequiredArgsConstructor + public enum Parameter { + /** The maximum number of tokens to generate. */ + MAX_TOKENS("max_tokens"), + /** The sampling temperature. */ + TEMPERATURE("temperature"), + /** The frequency penalty. */ + FREQUENCY_PENALTY("frequency_penalty"), + /** The presence penalty. */ + PRESENCE_PENALTY("presence_penalty"), + /** The maximum number of tokens for completion */ + MAX_COMPLETION_TOKENS("max_completion_tokens"), + /** The probability mass to be considered . */ + TOP_P("top_p"), + /** The toggle to enable partial message delta. */ + STREAM("stream"), + /** The options for streaming response. */ + STREAM_OPTIONS("stream_options"), + /** The tokens where the API will stop generating further tokens. */ + STOP("stop"), + /** The number of chat completion choices to generate for each input message. */ + N("n"); + + private final String value; } } diff --git a/orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationAiModelParameters.java b/orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationAiModelParameters.java deleted file mode 100644 index 5483b6689..000000000 --- a/orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationAiModelParameters.java +++ /dev/null @@ -1,86 +0,0 @@ -package com.sap.ai.sdk.orchestration; - -import java.util.Map; -import javax.annotation.Nonnull; - -/** Helper interface to define typed parameters. */ -@FunctionalInterface -public interface OrchestrationAiModelParameters { - /** - * Get the parameters. - * - * @return the parameters. - */ - @Nonnull - Map getParams(); - - /** GPT model parameters. */ - interface GPT extends OrchestrationAiModelParameters { - /** - * Create a new builder. - * - * @return the builder. - */ - @Nonnull - static GPT.Builder0 params() { - return maxTokens -> - temperature -> - frequencyPenalty -> - presencePenalty -> - () -> - Map.of( - "max_tokens", maxTokens, - "temperature", temperature, - "frequency_penalty", frequencyPenalty, - "presence_penalty", presencePenalty); - } - - /** Builder for GPT model parameters. */ - interface Builder0 { - /** - * Set the max tokens. - * - * @param maxTokens the max tokens. - * @return the next builder. - */ - @Nonnull - GPT.Builder1 maxTokens(@Nonnull final Number maxTokens); - } - - /** Builder for GPT model parameters. */ - interface Builder1 { - /** - * Set the temperature. - * - * @param temperature the temperature. - * @return the next builder. - */ - @Nonnull - GPT.Builder2 temperature(@Nonnull final Number temperature); - } - - /** Builder for GPT model parameters. */ - interface Builder2 { - /** - * Set the frequency penalty. - * - * @param frequencyPenalty the frequency penalty. - * @return the next builder. - */ - @Nonnull - GPT.Builder3 frequencyPenalty(@Nonnull final Number frequencyPenalty); - } - - /** Builder for GPT model parameters. */ - interface Builder3 { - /** - * Set the presence penalty. - * - * @param presencePenalty the presence penalty. - * @return the final typed parameter object. - */ - @Nonnull - GPT presencePenalty(@Nonnull final Number presencePenalty); - } - } -} diff --git a/orchestration/src/test/java/com/sap/ai/sdk/orchestration/OrchestrationModuleConfigTest.java b/orchestration/src/test/java/com/sap/ai/sdk/orchestration/OrchestrationModuleConfigTest.java index 1d0c0527c..022cff5f6 100644 --- a/orchestration/src/test/java/com/sap/ai/sdk/orchestration/OrchestrationModuleConfigTest.java +++ b/orchestration/src/test/java/com/sap/ai/sdk/orchestration/OrchestrationModuleConfigTest.java @@ -2,6 +2,7 @@ import static com.sap.ai.sdk.orchestration.AzureFilterThreshold.ALLOW_SAFE_LOW_MEDIUM; import static com.sap.ai.sdk.orchestration.OrchestrationAiModel.GPT_4O; +import static com.sap.ai.sdk.orchestration.OrchestrationAiModel.Parameter.MAX_TOKENS; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -65,6 +66,44 @@ void testDpiMaskingConfig() { .hasSize(1); } + @Test + void testParams() { + // test withParams(Map) + { + var params = Map.of("foo", "bar", "fizz", "buzz"); + + var modelA = GPT_4O.withParams(params); + var modelB = modelA.withParams(params); + assertThat(modelA).isEqualTo(modelB); + + var modelC = modelA.withParams(Map.of("foo", "bar")); + assertThat(modelA).isNotEqualTo(modelC); + + var modelD = modelA.withParams(Map.of("foo", "bazz")); + assertThat(modelA).isNotEqualTo(modelD); + } + + // test withParam(String, Object) + { + var modelA = GPT_4O.withParam("foo", "bar"); + var modelB = modelA.withParam("foo", "bar"); + assertThat(modelA).isEqualTo(modelB); + + var modelC = modelA.withParam("foo", "bazz"); + assertThat(modelA).isNotEqualTo(modelC); + } + + // test withParam(Parameter, Object) + { + var modelA = GPT_4O.withParam(MAX_TOKENS, 10); + var modelB = modelA.withParam(MAX_TOKENS, 10); + assertThat(modelA).isEqualTo(modelB); + + var modelC = modelA.withParam(MAX_TOKENS, 20); + assertThat(modelA).isNotEqualTo(modelC); + } + } + @Test void testLLMConfig() { Map params = Map.of("foo", "bar"); diff --git a/orchestration/src/test/java/com/sap/ai/sdk/orchestration/OrchestrationUnitTest.java b/orchestration/src/test/java/com/sap/ai/sdk/orchestration/OrchestrationUnitTest.java index 1eb1089b1..b041b84fa 100644 --- a/orchestration/src/test/java/com/sap/ai/sdk/orchestration/OrchestrationUnitTest.java +++ b/orchestration/src/test/java/com/sap/ai/sdk/orchestration/OrchestrationUnitTest.java @@ -19,7 +19,7 @@ import static com.sap.ai.sdk.orchestration.AzureFilterThreshold.ALLOW_SAFE; import static com.sap.ai.sdk.orchestration.AzureFilterThreshold.ALLOW_SAFE_LOW_MEDIUM; import static com.sap.ai.sdk.orchestration.OrchestrationAiModel.GPT_35_TURBO_16K; -import static com.sap.ai.sdk.orchestration.OrchestrationAiModelParameters.GPT.params; +import static com.sap.ai.sdk.orchestration.OrchestrationAiModel.Parameter.*; import static org.apache.hc.core5.http.HttpStatus.SC_BAD_REQUEST; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -53,8 +53,11 @@ @WireMockTest class OrchestrationUnitTest { static final OrchestrationAiModel CUSTOM_GPT_35 = - GPT_35_TURBO_16K.withParams( - params().maxTokens(50).temperature(0.1).frequencyPenalty(0).presencePenalty(0)); + GPT_35_TURBO_16K + .withParam(MAX_TOKENS, 50) + .withParam(TEMPERATURE, 0.1) + .withParam(FREQUENCY_PENALTY, 0) + .withParam(PRESENCE_PENALTY, 0); private final Function fileLoader = filename -> Objects.requireNonNull(getClass().getClassLoader().getResourceAsStream(filename)); From a85d521cca5f8e9202d8ace36196d2020ed7b4ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20D=C3=BCmont?= <22489773+newtork@users.noreply.github.com> Date: Wed, 27 Nov 2024 15:54:13 +0100 Subject: [PATCH 4/9] Update ORCHESTRATION_CHAT_COMPLETION.md --- docs/guides/ORCHESTRATION_CHAT_COMPLETION.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/guides/ORCHESTRATION_CHAT_COMPLETION.md b/docs/guides/ORCHESTRATION_CHAT_COMPLETION.md index cc750f443..f37c2cb58 100644 --- a/docs/guides/ORCHESTRATION_CHAT_COMPLETION.md +++ b/docs/guides/ORCHESTRATION_CHAT_COMPLETION.md @@ -200,8 +200,10 @@ Change your LLM configuration to add model parameters: ```java OrchestrationAiModel customGPT4O = OrchestrationAiModel.GPT_4O - .withParams( - params().maxTokens(50).temperature(0.1).frequencyPenalty(0).presencePenalty(0)) + .withParam(MAX_TOKENS, 50) + .withParam(TEMPERATURE, 0.1) + .withParam(FREQUENCY_PENALTY, 0) + .withParam(PRESENCE_PENALTY, 0) .withVersion("2024-05-13"); ``` @@ -221,4 +223,4 @@ var prompt = new OrchestrationPrompt(Map.of("your-input-parameter", "your-param- new OrchestrationClient().executeRequestFromJsonModuleConfig(prompt, configJson); ``` -While this is not recommended for long term use, it can be useful for creating demos and PoCs. \ No newline at end of file +While this is not recommended for long term use, it can be useful for creating demos and PoCs. From f360c60e8d510ab1ac1d602eb8dd00ef3ce90c1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20D=C3=BCmont?= Date: Thu, 28 Nov 2024 11:02:09 +0100 Subject: [PATCH 5/9] Adjust solution to (value) type safety. --- .../orchestration/OrchestrationAiModel.java | 60 ++++++++++++------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationAiModel.java b/orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationAiModel.java index b7e86bcf8..1ce71d21c 100644 --- a/orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationAiModel.java +++ b/orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationAiModel.java @@ -2,11 +2,11 @@ import com.sap.ai.sdk.orchestration.client.model.LLMModuleConfig; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import javax.annotation.Nonnull; import javax.annotation.Nullable; import lombok.AllArgsConstructor; -import lombok.RequiredArgsConstructor; import lombok.Value; import lombok.With; @@ -137,38 +137,58 @@ public OrchestrationAiModel withParam(@Nonnull final String key, @Nullable final * * @param param the parameter key. * @param value the parameter value, nullable. + * @param the parameter value type. * @return A new model with the additional parameter. */ @Nonnull - public OrchestrationAiModel withParam( - @Nonnull final Parameter param, @Nullable final Object value) { - return withParam(param.value, value); + public OrchestrationAiModel withParam( + @Nonnull final Parameter param, @Nullable final ValueT value) { + return withParam(param.getName(), value); } - /** Parameter key for a model. */ - @RequiredArgsConstructor - public enum Parameter { + /** + * Parameter key for a model. + * + * @param the parameter value type. + */ + @FunctionalInterface + public interface Parameter { /** The maximum number of tokens to generate. */ - MAX_TOKENS("max_tokens"), + Parameter MAX_TOKENS = () -> "max_tokens"; + /** The sampling temperature. */ - TEMPERATURE("temperature"), + Parameter TEMPERATURE = () -> "temperature"; + /** The frequency penalty. */ - FREQUENCY_PENALTY("frequency_penalty"), + Parameter FREQUENCY_PENALTY = () -> "frequency_penalty"; + /** The presence penalty. */ - PRESENCE_PENALTY("presence_penalty"), + Parameter PRESENCE_PENALTY = () -> "presence_penalty"; + /** The maximum number of tokens for completion */ - MAX_COMPLETION_TOKENS("max_completion_tokens"), + Parameter MAX_COMPLETION_TOKENS = () -> "max_completion_tokens"; + /** The probability mass to be considered . */ - TOP_P("top_p"), + Parameter TOP_P = () -> "top_p"; + /** The toggle to enable partial message delta. */ - STREAM("stream"), - /** The options for streaming response. */ - STREAM_OPTIONS("stream_options"), + Parameter STREAM = () -> "stream"; + + /** The options for streaming response. Only used in combination with STREAM = true. */ + Parameter> STREAM_OPTIONS = () -> "stream_options"; + /** The tokens where the API will stop generating further tokens. */ - STOP("stop"), - /** The number of chat completion choices to generate for each input message. */ - N("n"); + Parameter> STOP = () -> "stop"; - private final String value; + /** The number of chat completion choices to generate for each input message. */ + Parameter N = () -> "n"; + + /** + * The name of the parameter. + * + * @return the name of the parameter. + */ + @Nonnull + String getName(); } } From 1a38f81646326c8cfe963bc671c0432efb35fccd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20D=C3=BCmont?= <22489773+newtork@users.noreply.github.com> Date: Thu, 28 Nov 2024 12:30:01 +0100 Subject: [PATCH 6/9] Remove unpopular parameters --- .../ai/sdk/orchestration/OrchestrationAiModel.java | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationAiModel.java b/orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationAiModel.java index 1ce71d21c..c373c8f00 100644 --- a/orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationAiModel.java +++ b/orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationAiModel.java @@ -165,21 +165,9 @@ public interface Parameter { /** The presence penalty. */ Parameter PRESENCE_PENALTY = () -> "presence_penalty"; - /** The maximum number of tokens for completion */ - Parameter MAX_COMPLETION_TOKENS = () -> "max_completion_tokens"; - /** The probability mass to be considered . */ Parameter TOP_P = () -> "top_p"; - /** The toggle to enable partial message delta. */ - Parameter STREAM = () -> "stream"; - - /** The options for streaming response. Only used in combination with STREAM = true. */ - Parameter> STREAM_OPTIONS = () -> "stream_options"; - - /** The tokens where the API will stop generating further tokens. */ - Parameter> STOP = () -> "stop"; - /** The number of chat completion choices to generate for each input message. */ Parameter N = () -> "n"; From 1d141dfa43cdc5534df91965eacfc7786d1944d7 Mon Sep 17 00:00:00 2001 From: SAP Cloud SDK Bot Date: Thu, 28 Nov 2024 11:30:30 +0000 Subject: [PATCH 7/9] Formatting --- .../java/com/sap/ai/sdk/orchestration/OrchestrationAiModel.java | 1 - 1 file changed, 1 deletion(-) diff --git a/orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationAiModel.java b/orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationAiModel.java index c373c8f00..f2dda0224 100644 --- a/orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationAiModel.java +++ b/orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationAiModel.java @@ -2,7 +2,6 @@ import com.sap.ai.sdk.orchestration.client.model.LLMModuleConfig; import java.util.LinkedHashMap; -import java.util.List; import java.util.Map; import javax.annotation.Nonnull; import javax.annotation.Nullable; From 5b0951879edb36bb5a6e75be334b47705128ebe6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20D=C3=BCmont?= Date: Thu, 28 Nov 2024 13:58:43 +0100 Subject: [PATCH 8/9] Add doc link to JavaDoc --- .../sap/ai/sdk/orchestration/OrchestrationAiModel.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationAiModel.java b/orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationAiModel.java index d3bc77129..d217a81f4 100644 --- a/orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationAiModel.java +++ b/orchestration/src/main/java/com/sap/ai/sdk/orchestration/OrchestrationAiModel.java @@ -27,6 +27,10 @@ public class OrchestrationAiModel { * "frequency_penalty", 0, * "presence_penalty", 0) * } + * + * @link SAP + * AI Core: Orchestration - Harmonized API */ Map params; @@ -123,6 +127,9 @@ LLMModuleConfig createConfig() { * @param key the parameter key. * @param value the parameter value, nullable. * @return A new model with the additional parameter. + * @link SAP + * AI Core: Orchestration - Harmonized API */ @Nonnull public OrchestrationAiModel withParam(@Nonnull final String key, @Nullable final Object value) { @@ -138,6 +145,9 @@ public OrchestrationAiModel withParam(@Nonnull final String key, @Nullable final * @param value the parameter value, nullable. * @param the parameter value type. * @return A new model with the additional parameter. + * @link SAP + * AI Core: Orchestration - Harmonized API */ @Nonnull public OrchestrationAiModel withParam( From df77e6d7ccd977f0ab8bc809061c37db7932b929 Mon Sep 17 00:00:00 2001 From: Matthias Kuhr Date: Fri, 29 Nov 2024 08:25:55 +0100 Subject: [PATCH 9/9] Update sample code --- .../sap/ai/sdk/app/controllers/OrchestrationController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/controllers/OrchestrationController.java b/sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/controllers/OrchestrationController.java index 59676db1e..ce35e4ab1 100644 --- a/sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/controllers/OrchestrationController.java +++ b/sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/controllers/OrchestrationController.java @@ -1,6 +1,7 @@ package com.sap.ai.sdk.app.controllers; import static com.sap.ai.sdk.orchestration.OrchestrationAiModel.GPT_35_TURBO; +import static com.sap.ai.sdk.orchestration.OrchestrationAiModel.Parameter.TEMPERATURE; import com.sap.ai.sdk.orchestration.AzureContentFilter; import com.sap.ai.sdk.orchestration.AzureFilterThreshold; @@ -26,8 +27,7 @@ class OrchestrationController { private final OrchestrationClient client = new OrchestrationClient(); OrchestrationModuleConfig config = - new OrchestrationModuleConfig() - .withLlmConfig(GPT_35_TURBO.withParams(Map.of("temperature", 0.0))); + new OrchestrationModuleConfig().withLlmConfig(GPT_35_TURBO.withParam(TEMPERATURE, 0.0)); /** * Chat request to OpenAI through the Orchestration service with a simple prompt.