|
1 | 1 | package com.sap.ai.sdk.orchestration.spring; |
2 | 2 |
|
| 3 | +import static com.sap.ai.sdk.orchestration.OrchestrationAiModel.Parameter.FREQUENCY_PENALTY; |
| 4 | +import static com.sap.ai.sdk.orchestration.OrchestrationAiModel.Parameter.MAX_TOKENS; |
| 5 | +import static com.sap.ai.sdk.orchestration.OrchestrationAiModel.Parameter.PRESENCE_PENALTY; |
| 6 | +import static com.sap.ai.sdk.orchestration.OrchestrationAiModel.Parameter.TEMPERATURE; |
| 7 | +import static com.sap.ai.sdk.orchestration.OrchestrationAiModel.Parameter.TOP_P; |
| 8 | + |
3 | 9 | import com.sap.ai.sdk.orchestration.OrchestrationModuleConfig; |
4 | 10 | import com.sap.ai.sdk.orchestration.model.LLMModuleConfig; |
5 | | - |
| 11 | +import java.util.ArrayList; |
6 | 12 | import java.util.LinkedHashMap; |
7 | 13 | import java.util.List; |
8 | | -import java.util.Map; |
9 | 14 | import java.util.Objects; |
10 | 15 | import javax.annotation.Nonnull; |
11 | 16 | import javax.annotation.Nullable; |
12 | 17 | import lombok.AccessLevel; |
13 | 18 | import lombok.Data; |
14 | 19 | import lombok.Getter; |
15 | 20 | import lombok.Setter; |
| 21 | +import lombok.val; |
16 | 22 | import org.springframework.ai.chat.prompt.ChatOptions; |
17 | 23 |
|
18 | 24 | /** Configuration to be used for orchestration requests. */ |
|
21 | 27 | @Setter(AccessLevel.NONE) |
22 | 28 | public class OrchestrationChatOptions implements ChatOptions { |
23 | 29 |
|
24 | | - @Getter(AccessLevel.PUBLIC) |
25 | | - @Nonnull |
26 | | - private Map<String, String> templateParameters = Map.of(); |
27 | | - |
28 | 30 | @Getter(AccessLevel.PUBLIC) |
29 | 31 | @Setter(AccessLevel.PUBLIC) |
30 | 32 | @Nonnull |
31 | 33 | OrchestrationModuleConfig config; |
32 | 34 |
|
33 | | - // region satisfy the ChatOptions interface, delegating to the LLM config |
34 | | - @Nullable |
| 35 | + /** |
| 36 | + * Returns the model to use for the chat. |
| 37 | + * |
| 38 | + * @return the model to use for the chat |
| 39 | + * @see com.sap.ai.sdk.orchestration.OrchestrationAiModel |
| 40 | + */ |
| 41 | + @Nonnull |
35 | 42 | @Override |
36 | 43 | public String getModel() { |
37 | 44 | return getLlmConfigNonNull().getModelName(); |
38 | 45 | } |
39 | 46 |
|
40 | | - @Nullable |
41 | | - String getModelVersion() { |
| 47 | + private void setModel(@Nonnull final String model) { |
| 48 | + getLlmConfigNonNull().setModelName(model); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Returns the model version to use for the chat. "latest" by default. |
| 53 | + * |
| 54 | + * @return the model version to use for the chat. |
| 55 | + */ |
| 56 | + @Nonnull |
| 57 | + public String getModelVersion() { |
42 | 58 | return getLlmConfigNonNull().getModelVersion(); |
43 | 59 | } |
44 | 60 |
|
| 61 | + private void setModelVersion(@Nonnull final String modelVersion) { |
| 62 | + getLlmConfigNonNull().setModelVersion(modelVersion); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Returns the frequency penalty to use for the chat. |
| 67 | + * |
| 68 | + * @return the frequency penalty to use for the chat |
| 69 | + */ |
45 | 70 | @Nullable |
46 | 71 | @Override |
47 | 72 | public Double getFrequencyPenalty() { |
48 | | - return getLlmConfigParam("frequencyPenalty", Double.class); |
| 73 | + return getLlmConfigParam(FREQUENCY_PENALTY.getName()); |
49 | 74 | } |
50 | 75 |
|
| 76 | + private void setFrequencyPenalty(@Nonnull final Double frequencyPenalty) { |
| 77 | + setLlmConfigParam(FREQUENCY_PENALTY.getName(), frequencyPenalty); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Returns the maximum number of tokens to use for the chat. |
| 82 | + * |
| 83 | + * @return the maximum number of tokens to use for the chat |
| 84 | + */ |
51 | 85 | @Nullable |
52 | 86 | @Override |
53 | 87 | public Integer getMaxTokens() { |
54 | | - return getLlmConfigParam("maxTokens", Integer.class); |
| 88 | + return getLlmConfigParam(MAX_TOKENS.getName()); |
| 89 | + } |
| 90 | + |
| 91 | + private void setMaxTokens(@Nonnull final Integer maxTokens) { |
| 92 | + setLlmConfigParam(MAX_TOKENS.getName(), maxTokens); |
55 | 93 | } |
56 | 94 |
|
| 95 | + /** |
| 96 | + * Returns the presence penalty to use for the chat. |
| 97 | + * |
| 98 | + * @return the presence penalty to use for the chat |
| 99 | + */ |
57 | 100 | @Nullable |
58 | 101 | @Override |
59 | 102 | public Double getPresencePenalty() { |
60 | | - return getLlmConfigParam("presencePenalty", Double.class); |
| 103 | + return getLlmConfigParam(PRESENCE_PENALTY.getName()); |
61 | 104 | } |
62 | 105 |
|
63 | | - @SuppressWarnings("unchecked") |
| 106 | + private void setPresencePenalty(@Nonnull final Double presencePenalty) { |
| 107 | + setLlmConfigParam(PRESENCE_PENALTY.getName(), presencePenalty); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Returns the stop sequences to use for the chat. |
| 112 | + * |
| 113 | + * @return the stop sequences to use for the chat |
| 114 | + */ |
64 | 115 | @Nullable |
65 | 116 | @Override |
66 | 117 | public List<String> getStopSequences() { |
67 | | - return getLlmConfigParam("stopSequences", List.class); |
| 118 | + return getLlmConfigParam("stop_sequences"); |
| 119 | + } |
| 120 | + |
| 121 | + private void setStopSequences(@Nonnull final List<String> stopSequences) { |
| 122 | + setLlmConfigParam("stop_sequences", stopSequences); |
68 | 123 | } |
69 | 124 |
|
| 125 | + /** |
| 126 | + * Returns the temperature to use for the chat. |
| 127 | + * |
| 128 | + * @return the temperature to use for the chat |
| 129 | + */ |
70 | 130 | @Nullable |
71 | 131 | @Override |
72 | 132 | public Double getTemperature() { |
73 | | - return getLlmConfigParam("temperature", Double.class); |
| 133 | + return getLlmConfigParam(TEMPERATURE.getName()); |
74 | 134 | } |
75 | 135 |
|
| 136 | + private void setTemperature(@Nonnull final Double temperature) { |
| 137 | + setLlmConfigParam(TEMPERATURE.getName(), temperature); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Returns the top K to use for the chat. |
| 142 | + * |
| 143 | + * @return the top K to use for the chat |
| 144 | + */ |
76 | 145 | @Nullable |
77 | 146 | @Override |
78 | 147 | public Integer getTopK() { |
79 | | - return getLlmConfigParam("topK", Integer.class); |
| 148 | + return getLlmConfigParam("top_k"); |
| 149 | + } |
| 150 | + |
| 151 | + private void setTopK(@Nonnull final Integer topK) { |
| 152 | + setLlmConfigParam("top_k", topK); |
80 | 153 | } |
81 | 154 |
|
| 155 | + /** |
| 156 | + * Returns the top P to use for the chat. |
| 157 | + * |
| 158 | + * @return the top P to use for the chat |
| 159 | + */ |
82 | 160 | @Nullable |
83 | 161 | @Override |
84 | 162 | public Double getTopP() { |
85 | | - return getLlmConfigParam("topP", Double.class); |
| 163 | + return getLlmConfigParam(TOP_P.getName()); |
86 | 164 | } |
87 | 165 |
|
| 166 | + private void setTopP(@Nonnull final Double topP) { |
| 167 | + setLlmConfigParam(TOP_P.getName(), topP); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Returns a copy of this {@link OrchestrationChatOptions}. |
| 172 | + * |
| 173 | + * @return a copy of this {@link OrchestrationChatOptions} |
| 174 | + */ |
| 175 | + @SuppressWarnings("unchecked") // The same suppress is in DefaultChatOptions |
| 176 | + @Nonnull |
88 | 177 | @Override |
89 | | - public OrchestrationChatOptions copy() { |
90 | | - var copy = new OrchestrationChatOptions(config); |
91 | | - copy.templateParameters.putAll(this.templateParameters); |
92 | | - return copy; |
| 178 | + public <T extends ChatOptions> T copy() { |
| 179 | + val copy = new OrchestrationChatOptions(config); |
| 180 | + copy.setModel(this.getModel()); |
| 181 | + copy.setModelVersion(this.getModelVersion()); |
| 182 | + if (getFrequencyPenalty() != null) { |
| 183 | + copy.setFrequencyPenalty(this.getFrequencyPenalty()); |
| 184 | + } |
| 185 | + if (getMaxTokens() != null) { |
| 186 | + copy.setMaxTokens(this.getMaxTokens()); |
| 187 | + } |
| 188 | + if (getPresencePenalty() != null) { |
| 189 | + copy.setPresencePenalty(this.getPresencePenalty()); |
| 190 | + } |
| 191 | + if (getStopSequences() != null) { |
| 192 | + copy.setStopSequences(new ArrayList<>(this.getStopSequences())); |
| 193 | + } |
| 194 | + if (getTemperature() != null) { |
| 195 | + copy.setTemperature(this.getTemperature()); |
| 196 | + } |
| 197 | + if (getTopK() != null) { |
| 198 | + copy.setTopK(this.getTopK()); |
| 199 | + } |
| 200 | + if (getTopP() != null) { |
| 201 | + copy.setTopP(this.getTopP()); |
| 202 | + } |
| 203 | + return (T) copy; |
93 | 204 | } |
94 | 205 |
|
95 | 206 | @SuppressWarnings("unchecked") |
96 | | - @Nonnull |
97 | | - private <T> T getLlmConfigParam(@Nonnull final String param, @Nonnull final Class<T> defaultValue) { |
98 | | - return ((LinkedHashMap<String, T>) getLlmConfigNonNull().getModelParams()).get(param); |
| 207 | + @Nullable |
| 208 | + private <T> T getLlmConfigParam(@Nonnull final String param) { |
| 209 | + if (getLlmConfigNonNull().getModelParams() instanceof LinkedHashMap) { |
| 210 | + return ((LinkedHashMap<String, T>) getLlmConfigNonNull().getModelParams()).get(param); |
| 211 | + } |
| 212 | + return null; |
| 213 | + } |
| 214 | + |
| 215 | + @SuppressWarnings("unchecked") |
| 216 | + private <T> void setLlmConfigParam(@Nonnull final String param, @Nonnull final T value) { |
| 217 | + ((LinkedHashMap<String, T>) getLlmConfigNonNull().getModelParams()).put(param, value); |
99 | 218 | } |
100 | 219 |
|
101 | 220 | @Nonnull |
|
0 commit comments