1
1
/*
2
- * Copyright 2023-2024 the original author or authors.
2
+ * Copyright 2023-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
17
17
package org .springframework .ai .anthropic ;
18
18
19
19
import java .util .ArrayList ;
20
+ import java .util .Arrays ;
21
+ import java .util .HashMap ;
20
22
import java .util .HashSet ;
21
23
import java .util .List ;
22
24
import java .util .Map ;
30
32
import org .springframework .ai .anthropic .api .AnthropicApi ;
31
33
import org .springframework .ai .anthropic .api .AnthropicApi .ChatCompletionRequest ;
32
34
import org .springframework .ai .model .function .FunctionCallback ;
33
- import org .springframework .ai .model .function .FunctionCallingOptions ;
35
+ import org .springframework .ai .model .tool .ToolCallingChatOptions ;
36
+ import org .springframework .ai .tool .ToolCallback ;
37
+ import org .springframework .lang .Nullable ;
34
38
import org .springframework .util .Assert ;
35
39
36
40
/**
42
46
* @since 1.0.0
43
47
*/
44
48
@ JsonInclude (Include .NON_NULL )
45
- public class AnthropicChatOptions implements FunctionCallingOptions {
49
+ public class AnthropicChatOptions implements ToolCallingChatOptions {
46
50
47
51
// @formatter:off
48
52
private @ JsonProperty ("model" ) String model ;
@@ -54,34 +58,27 @@ public class AnthropicChatOptions implements FunctionCallingOptions {
54
58
private @ JsonProperty ("top_k" ) Integer topK ;
55
59
56
60
/**
57
- * Tool Function Callbacks to register with the ChatModel. For Prompt
58
- * Options the functionCallbacks are automatically enabled for the duration of the
59
- * prompt execution. For Default Options the functionCallbacks are registered but
60
- * disabled by default. Use the enableFunctions to set the functions from the registry
61
- * to be used by the ChatModel chat completion requests.
61
+ * Collection of {@link ToolCallback}s to be used for tool calling in the chat
62
+ * completion requests.
62
63
*/
63
64
@ JsonIgnore
64
- private List <FunctionCallback > functionCallbacks = new ArrayList <>();
65
+ private List <FunctionCallback > toolCallbacks = new ArrayList <>();
65
66
66
67
/**
67
- * List of functions, identified by their names, to configure for function calling in
68
- * the chat completion requests. Functions with those names must exist in the
69
- * functionCallbacks registry. The {@link #functionCallbacks} from the PromptOptions
70
- * are automatically enabled for the duration of the prompt execution.
71
- *
72
- * Note that function enabled with the default options are enabled for all chat
73
- * completion requests. This could impact the token count and the billing. If the
74
- * functions is set in a prompt options, then the enabled functions are only active
75
- * for the duration of this prompt execution.
68
+ * Collection of tool names to be resolved at runtime and used for tool calling in the
69
+ * chat completion requests.
76
70
*/
77
71
@ JsonIgnore
78
- private Set <String > functions = new HashSet <>();
72
+ private Set <String > toolNames = new HashSet <>();
79
73
74
+ /**
75
+ * Whether to enable the tool execution lifecycle internally in ChatModel.
76
+ */
80
77
@ JsonIgnore
81
- private Boolean proxyToolCalls ;
78
+ private Boolean internalToolExecutionEnabled ;
82
79
83
80
@ JsonIgnore
84
- private Map <String , Object > toolContext ;
81
+ private Map <String , Object > toolContext = new HashMap <>() ;
85
82
86
83
// @formatter:on
87
84
@@ -97,9 +94,9 @@ public static AnthropicChatOptions fromOptions(AnthropicChatOptions fromOptions)
97
94
.temperature (fromOptions .getTemperature ())
98
95
.topP (fromOptions .getTopP ())
99
96
.topK (fromOptions .getTopK ())
100
- .functionCallbacks (fromOptions .getFunctionCallbacks ())
101
- .functions (fromOptions .getFunctions ())
102
- .proxyToolCalls (fromOptions .getProxyToolCalls ())
97
+ .toolCallbacks (fromOptions .getToolCallbacks ())
98
+ .toolNames (fromOptions .getToolNames ())
99
+ .internalToolExecutionEnabled (fromOptions .isInternalToolExecutionEnabled ())
103
100
.toolContext (fromOptions .getToolContext ())
104
101
.build ();
105
102
}
@@ -167,25 +164,73 @@ public void setTopK(Integer topK) {
167
164
}
168
165
169
166
@ Override
167
+ @ JsonIgnore
168
+ public List <FunctionCallback > getToolCallbacks () {
169
+ return this .toolCallbacks ;
170
+ }
171
+
172
+ @ Override
173
+ @ JsonIgnore
174
+ public void setToolCallbacks (List <FunctionCallback > toolCallbacks ) {
175
+ Assert .notNull (toolCallbacks , "toolCallbacks cannot be null" );
176
+ Assert .noNullElements (toolCallbacks , "toolCallbacks cannot contain null elements" );
177
+ this .toolCallbacks = toolCallbacks ;
178
+ }
179
+
180
+ @ Override
181
+ @ JsonIgnore
182
+ public Set <String > getToolNames () {
183
+ return this .toolNames ;
184
+ }
185
+
186
+ @ Override
187
+ @ JsonIgnore
188
+ public void setToolNames (Set <String > toolNames ) {
189
+ Assert .notNull (toolNames , "toolNames cannot be null" );
190
+ Assert .noNullElements (toolNames , "toolNames cannot contain null elements" );
191
+ toolNames .forEach (tool -> Assert .hasText (tool , "toolNames cannot contain empty elements" ));
192
+ this .toolNames = toolNames ;
193
+ }
194
+
195
+ @ Override
196
+ @ Nullable
197
+ @ JsonIgnore
198
+ public Boolean isInternalToolExecutionEnabled () {
199
+ return internalToolExecutionEnabled ;
200
+ }
201
+
202
+ @ Override
203
+ @ JsonIgnore
204
+ public void setInternalToolExecutionEnabled (@ Nullable Boolean internalToolExecutionEnabled ) {
205
+ this .internalToolExecutionEnabled = internalToolExecutionEnabled ;
206
+ }
207
+
208
+ @ Override
209
+ @ Deprecated
210
+ @ JsonIgnore
170
211
public List <FunctionCallback > getFunctionCallbacks () {
171
- return this .functionCallbacks ;
212
+ return this .getToolCallbacks () ;
172
213
}
173
214
174
215
@ Override
216
+ @ Deprecated
217
+ @ JsonIgnore
175
218
public void setFunctionCallbacks (List <FunctionCallback > functionCallbacks ) {
176
- Assert .notNull (functionCallbacks , "FunctionCallbacks must not be null" );
177
- this .functionCallbacks = functionCallbacks ;
219
+ this .setToolCallbacks (functionCallbacks );
178
220
}
179
221
180
222
@ Override
223
+ @ Deprecated
224
+ @ JsonIgnore
181
225
public Set <String > getFunctions () {
182
- return this .functions ;
226
+ return this .getToolNames () ;
183
227
}
184
228
185
229
@ Override
186
- public void setFunctions (Set <String > functions ) {
187
- Assert .notNull (functions , "Function must not be null" );
188
- this .functions = functions ;
230
+ @ Deprecated
231
+ @ JsonIgnore
232
+ public void setFunctions (Set <String > functionNames ) {
233
+ this .setToolNames (functionNames );
189
234
}
190
235
191
236
@ Override
@@ -201,20 +246,26 @@ public Double getPresencePenalty() {
201
246
}
202
247
203
248
@ Override
249
+ @ Deprecated
250
+ @ JsonIgnore
204
251
public Boolean getProxyToolCalls () {
205
- return this .proxyToolCalls ;
252
+ return this .internalToolExecutionEnabled != null ? ! this . internalToolExecutionEnabled : null ;
206
253
}
207
254
255
+ @ Deprecated
256
+ @ JsonIgnore
208
257
public void setProxyToolCalls (Boolean proxyToolCalls ) {
209
- this .proxyToolCalls = proxyToolCalls ;
258
+ this .internalToolExecutionEnabled = proxyToolCalls != null ? ! proxyToolCalls : null ;
210
259
}
211
260
212
261
@ Override
262
+ @ JsonIgnore
213
263
public Map <String , Object > getToolContext () {
214
264
return this .toolContext ;
215
265
}
216
266
217
267
@ Override
268
+ @ JsonIgnore
218
269
public void setToolContext (Map <String , Object > toolContext ) {
219
270
this .toolContext = toolContext ;
220
271
}
@@ -268,25 +319,54 @@ public Builder topK(Integer topK) {
268
319
return this ;
269
320
}
270
321
271
- public Builder functionCallbacks (List <FunctionCallback > functionCallbacks ) {
272
- this .options .functionCallbacks = functionCallbacks ;
322
+ public Builder toolCallbacks (List <FunctionCallback > toolCallbacks ) {
323
+ this .options .setToolCallbacks ( toolCallbacks ) ;
273
324
return this ;
274
325
}
275
326
276
- public Builder functions ( Set < String > functionNames ) {
277
- Assert .notNull (functionNames , "Function names must not be null" );
278
- this .options .functions = functionNames ;
327
+ public Builder toolCallbacks ( FunctionCallback ... toolCallbacks ) {
328
+ Assert .notNull (toolCallbacks , "toolCallbacks cannot be null" );
329
+ this .options .toolCallbacks . addAll ( Arrays . asList ( toolCallbacks )) ;
279
330
return this ;
280
331
}
281
332
282
- public Builder function (String functionName ) {
283
- Assert .hasText (functionName , "Function name must not be empty" );
284
- this .options .functions .add (functionName );
333
+ public Builder toolNames (Set <String > toolNames ) {
334
+ Assert .notNull (toolNames , "toolNames cannot be null" );
335
+ this .options .setToolNames (toolNames );
336
+ return this ;
337
+ }
338
+
339
+ public Builder toolNames (String ... toolNames ) {
340
+ Assert .notNull (toolNames , "toolNames cannot be null" );
341
+ this .options .toolNames .addAll (Set .of (toolNames ));
285
342
return this ;
286
343
}
287
344
345
+ public Builder internalToolExecutionEnabled (@ Nullable Boolean internalToolExecutionEnabled ) {
346
+ this .options .setInternalToolExecutionEnabled (internalToolExecutionEnabled );
347
+ return this ;
348
+ }
349
+
350
+ @ Deprecated
351
+ public Builder functionCallbacks (List <FunctionCallback > functionCallbacks ) {
352
+ return toolCallbacks (functionCallbacks );
353
+ }
354
+
355
+ @ Deprecated
356
+ public Builder functions (Set <String > functionNames ) {
357
+ return toolNames (functionNames );
358
+ }
359
+
360
+ @ Deprecated
361
+ public Builder function (String functionName ) {
362
+ return toolNames (functionName );
363
+ }
364
+
365
+ @ Deprecated
288
366
public Builder proxyToolCalls (Boolean proxyToolCalls ) {
289
- this .options .proxyToolCalls = proxyToolCalls ;
367
+ if (proxyToolCalls != null ) {
368
+ this .options .setInternalToolExecutionEnabled (!proxyToolCalls );
369
+ }
290
370
return this ;
291
371
}
292
372
0 commit comments