|
15 | 15 | */
|
16 | 16 | package org.springframework.ai.bedrock.anthropic3;
|
17 | 17 |
|
| 18 | +import com.fasterxml.jackson.annotation.JsonIgnore; |
18 | 19 | import com.fasterxml.jackson.annotation.JsonInclude;
|
19 | 20 | import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
20 | 21 | import com.fasterxml.jackson.annotation.JsonProperty;
|
21 | 22 | import org.springframework.ai.chat.prompt.ChatOptions;
|
| 23 | +import org.springframework.ai.model.function.FunctionCallback; |
| 24 | +import org.springframework.ai.model.function.FunctionCallingOptions; |
| 25 | +import org.springframework.boot.context.properties.NestedConfigurationProperty; |
| 26 | +import org.springframework.util.Assert; |
22 | 27 |
|
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.HashSet; |
23 | 30 | import java.util.List;
|
| 31 | +import java.util.Set; |
24 | 32 |
|
25 | 33 | /**
|
26 | 34 | * Java {@link ChatOptions} for the Bedrock Anthropic chat generative model chat options.
|
|
31 | 39 | * @since 1.0.0
|
32 | 40 | */
|
33 | 41 | @JsonInclude(Include.NON_NULL)
|
34 |
| -public class Anthropic3ChatOptions implements ChatOptions { |
| 42 | +public class Anthropic3ChatOptions implements ChatOptions, FunctionCallingOptions { |
35 | 43 |
|
36 | 44 | // @formatter:off
|
37 | 45 | /**
|
@@ -66,6 +74,31 @@ public class Anthropic3ChatOptions implements ChatOptions {
|
66 | 74 | */
|
67 | 75 | private @JsonProperty("stop_sequences") List<String> stopSequences;
|
68 | 76 |
|
| 77 | + /** |
| 78 | + * Tool Function Callbacks to register with the ChatModel. For Prompt |
| 79 | + * Options the functionCallbacks are automatically enabled for the duration of the |
| 80 | + * prompt execution. For Default Options the functionCallbacks are registered but |
| 81 | + * disabled by default. Use the enableFunctions to set the functions from the registry |
| 82 | + * to be used by the ChatModel chat completion requests. |
| 83 | + */ |
| 84 | + @NestedConfigurationProperty |
| 85 | + @JsonIgnore |
| 86 | + private List<FunctionCallback> functionCallbacks = new ArrayList<>(); |
| 87 | + |
| 88 | + /** |
| 89 | + * List of functions, identified by their names, to configure for function calling in |
| 90 | + * the chat completion requests. Functions with those names must exist in the |
| 91 | + * functionCallbacks registry. The {@link #functionCallbacks} from the PromptOptions |
| 92 | + * are automatically enabled for the duration of the prompt execution. |
| 93 | + * |
| 94 | + * Note that function enabled with the default options are enabled for all chat |
| 95 | + * completion requests. This could impact the token count and the billing. If the |
| 96 | + * functions is set in a prompt options, then the enabled functions are only active |
| 97 | + * for the duration of this prompt execution. |
| 98 | + */ |
| 99 | + @NestedConfigurationProperty |
| 100 | + @JsonIgnore |
| 101 | + private Set<String> functions = new HashSet<>(); |
69 | 102 | // @formatter:on
|
70 | 103 |
|
71 | 104 | public static Builder builder() {
|
@@ -101,6 +134,23 @@ public Builder withStopSequences(List<String> stopSequences) {
|
101 | 134 | return this;
|
102 | 135 | }
|
103 | 136 |
|
| 137 | + public Builder withFunctionCallbacks(List<FunctionCallback> functionCallbacks) { |
| 138 | + this.options.functionCallbacks = functionCallbacks; |
| 139 | + return this; |
| 140 | + } |
| 141 | + |
| 142 | + public Builder withFunctions(Set<String> functionNames) { |
| 143 | + Assert.notNull(functionNames, "Function names must not be null"); |
| 144 | + this.options.functions = functionNames; |
| 145 | + return this; |
| 146 | + } |
| 147 | + |
| 148 | + public Builder withFunction(String functionName) { |
| 149 | + Assert.hasText(functionName, "Function name must not be empty"); |
| 150 | + this.options.functions.add(functionName); |
| 151 | + return this; |
| 152 | + } |
| 153 | + |
104 | 154 | public Anthropic3ChatOptions build() {
|
105 | 155 | return this.options;
|
106 | 156 | }
|
@@ -150,12 +200,36 @@ public void setStopSequences(List<String> stopSequences) {
|
150 | 200 | this.stopSequences = stopSequences;
|
151 | 201 | }
|
152 | 202 |
|
| 203 | + @Override |
| 204 | + public List<FunctionCallback> getFunctionCallbacks() { |
| 205 | + return this.functionCallbacks; |
| 206 | + } |
| 207 | + |
| 208 | + @Override |
| 209 | + public void setFunctionCallbacks(List<FunctionCallback> functionCallbacks) { |
| 210 | + Assert.notNull(functionCallbacks, "FunctionCallbacks must not be null"); |
| 211 | + this.functionCallbacks = functionCallbacks; |
| 212 | + } |
| 213 | + |
| 214 | + @Override |
| 215 | + public Set<String> getFunctions() { |
| 216 | + return this.functions; |
| 217 | + } |
| 218 | + |
| 219 | + @Override |
| 220 | + public void setFunctions(Set<String> functions) { |
| 221 | + Assert.notNull(functions, "Function must not be null"); |
| 222 | + this.functions = functions; |
| 223 | + } |
| 224 | + |
153 | 225 | public static Anthropic3ChatOptions fromOptions(Anthropic3ChatOptions fromOptions) {
|
154 | 226 | return builder().withTemperature(fromOptions.getTemperature())
|
155 | 227 | .withMaxTokens(fromOptions.getMaxTokens())
|
156 | 228 | .withTopK(fromOptions.getTopK())
|
157 | 229 | .withTopP(fromOptions.getTopP())
|
158 | 230 | .withStopSequences(fromOptions.getStopSequences())
|
| 231 | + .withFunctionCallbacks(fromOptions.getFunctionCallbacks()) |
| 232 | + .withFunctions(fromOptions.getFunctions()) |
159 | 233 | .build();
|
160 | 234 | }
|
161 | 235 |
|
|
0 commit comments