11/*
2- * Copyright 2023-2024 the original author or authors.
2+ * Copyright 2023-2025 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1717package org .springframework .ai .anthropic ;
1818
1919import java .util .ArrayList ;
20+ import java .util .Arrays ;
21+ import java .util .HashMap ;
2022import java .util .HashSet ;
2123import java .util .List ;
2224import java .util .Map ;
3032import org .springframework .ai .anthropic .api .AnthropicApi ;
3133import org .springframework .ai .anthropic .api .AnthropicApi .ChatCompletionRequest ;
3234import 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 ;
3438import org .springframework .util .Assert ;
3539
3640/**
4246 * @since 1.0.0
4347 */
4448@ JsonInclude (Include .NON_NULL )
45- public class AnthropicChatOptions implements FunctionCallingOptions {
49+ public class AnthropicChatOptions implements ToolCallingChatOptions {
4650
4751 // @formatter:off
4852 private @ JsonProperty ("model" ) String model ;
@@ -54,34 +58,27 @@ public class AnthropicChatOptions implements FunctionCallingOptions {
5458 private @ JsonProperty ("top_k" ) Integer topK ;
5559
5660 /**
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.
6263 */
6364 @ JsonIgnore
64- private List <FunctionCallback > functionCallbacks = new ArrayList <>();
65+ private List <FunctionCallback > toolCallbacks = new ArrayList <>();
6566
6667 /**
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.
7670 */
7771 @ JsonIgnore
78- private Set <String > functions = new HashSet <>();
72+ private Set <String > toolNames = new HashSet <>();
7973
74+ /**
75+ * Whether to enable the tool execution lifecycle internally in ChatModel.
76+ */
8077 @ JsonIgnore
81- private Boolean proxyToolCalls ;
78+ private Boolean internalToolExecutionEnabled ;
8279
8380 @ JsonIgnore
84- private Map <String , Object > toolContext ;
81+ private Map <String , Object > toolContext = new HashMap <>() ;
8582
8683 // @formatter:on
8784
@@ -97,9 +94,9 @@ public static AnthropicChatOptions fromOptions(AnthropicChatOptions fromOptions)
9794 .temperature (fromOptions .getTemperature ())
9895 .topP (fromOptions .getTopP ())
9996 .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 ())
103100 .toolContext (fromOptions .getToolContext ())
104101 .build ();
105102 }
@@ -167,25 +164,73 @@ public void setTopK(Integer topK) {
167164 }
168165
169166 @ 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
170211 public List <FunctionCallback > getFunctionCallbacks () {
171- return this .functionCallbacks ;
212+ return this .getToolCallbacks () ;
172213 }
173214
174215 @ Override
216+ @ Deprecated
217+ @ JsonIgnore
175218 public void setFunctionCallbacks (List <FunctionCallback > functionCallbacks ) {
176- Assert .notNull (functionCallbacks , "FunctionCallbacks must not be null" );
177- this .functionCallbacks = functionCallbacks ;
219+ this .setToolCallbacks (functionCallbacks );
178220 }
179221
180222 @ Override
223+ @ Deprecated
224+ @ JsonIgnore
181225 public Set <String > getFunctions () {
182- return this .functions ;
226+ return this .getToolNames () ;
183227 }
184228
185229 @ 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 );
189234 }
190235
191236 @ Override
@@ -201,20 +246,26 @@ public Double getPresencePenalty() {
201246 }
202247
203248 @ Override
249+ @ Deprecated
250+ @ JsonIgnore
204251 public Boolean getProxyToolCalls () {
205- return this .proxyToolCalls ;
252+ return this .internalToolExecutionEnabled != null ? ! this . internalToolExecutionEnabled : null ;
206253 }
207254
255+ @ Deprecated
256+ @ JsonIgnore
208257 public void setProxyToolCalls (Boolean proxyToolCalls ) {
209- this .proxyToolCalls = proxyToolCalls ;
258+ this .internalToolExecutionEnabled = proxyToolCalls != null ? ! proxyToolCalls : null ;
210259 }
211260
212261 @ Override
262+ @ JsonIgnore
213263 public Map <String , Object > getToolContext () {
214264 return this .toolContext ;
215265 }
216266
217267 @ Override
268+ @ JsonIgnore
218269 public void setToolContext (Map <String , Object > toolContext ) {
219270 this .toolContext = toolContext ;
220271 }
@@ -268,25 +319,54 @@ public Builder topK(Integer topK) {
268319 return this ;
269320 }
270321
271- public Builder functionCallbacks (List <FunctionCallback > functionCallbacks ) {
272- this .options .functionCallbacks = functionCallbacks ;
322+ public Builder toolCallbacks (List <FunctionCallback > toolCallbacks ) {
323+ this .options .setToolCallbacks ( toolCallbacks ) ;
273324 return this ;
274325 }
275326
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 )) ;
279330 return this ;
280331 }
281332
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 ));
285342 return this ;
286343 }
287344
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
288366 public Builder proxyToolCalls (Boolean proxyToolCalls ) {
289- this .options .proxyToolCalls = proxyToolCalls ;
367+ if (proxyToolCalls != null ) {
368+ this .options .setInternalToolExecutionEnabled (!proxyToolCalls );
369+ }
290370 return this ;
291371 }
292372
0 commit comments