18
18
19
19
import org .springframework .ai .chat .prompt .ChatOptions ;
20
20
import org .springframework .ai .model .function .FunctionCallback ;
21
- import org .springframework .ai .tool .ToolCallback ;
22
21
import org .springframework .lang .Nullable ;
23
22
import org .springframework .util .Assert ;
24
- import org .springframework .util .CollectionUtils ;
25
- import org .springframework .util .StringUtils ;
26
23
27
24
import java .util .ArrayList ;
25
+ import java .util .Arrays ;
28
26
import java .util .HashMap ;
29
27
import java .util .HashSet ;
30
28
import java .util .List ;
39
37
*/
40
38
public class DefaultToolCallingChatOptions implements ToolCallingChatOptions {
41
39
42
- private List <ToolCallback > toolCallbacks = new ArrayList <>();
40
+ private List <FunctionCallback > toolCallbacks = new ArrayList <>();
43
41
44
42
private Set <String > tools = new HashSet <>();
45
43
46
44
private Map <String , Object > toolContext = new HashMap <>();
47
45
48
46
@ Nullable
49
- private Boolean toolCallReturnDirect ;
47
+ private Boolean internalToolExecutionEnabled ;
50
48
51
49
@ Nullable
52
50
private String model ;
@@ -73,23 +71,17 @@ public class DefaultToolCallingChatOptions implements ToolCallingChatOptions {
73
71
private Double topP ;
74
72
75
73
@ Override
76
- public List <ToolCallback > getToolCallbacks () {
74
+ public List <FunctionCallback > getToolCallbacks () {
77
75
return List .copyOf (this .toolCallbacks );
78
76
}
79
77
80
78
@ Override
81
- public void setToolCallbacks (List <ToolCallback > toolCallbacks ) {
79
+ public void setToolCallbacks (List <FunctionCallback > toolCallbacks ) {
82
80
Assert .notNull (toolCallbacks , "toolCallbacks cannot be null" );
83
81
Assert .noNullElements (toolCallbacks , "toolCallbacks cannot contain null elements" );
84
82
this .toolCallbacks = new ArrayList <>(toolCallbacks );
85
83
}
86
84
87
- @ Override
88
- public void setToolCallbacks (ToolCallback ... toolCallbacks ) {
89
- Assert .notNull (toolCallbacks , "toolCallbacks cannot be null" );
90
- setToolCallbacks (List .of (toolCallbacks ));
91
- }
92
-
93
85
@ Override
94
86
public Set <String > getTools () {
95
87
return Set .copyOf (this .tools );
@@ -103,12 +95,6 @@ public void setTools(Set<String> tools) {
103
95
this .tools = new HashSet <>(tools );
104
96
}
105
97
106
- @ Override
107
- public void setTools (String ... tools ) {
108
- Assert .notNull (tools , "tools cannot be null" );
109
- setTools (Set .of (tools ));
110
- }
111
-
112
98
@ Override
113
99
public Map <String , Object > getToolContext () {
114
100
return Map .copyOf (this .toolContext );
@@ -123,23 +109,23 @@ public void setToolContext(Map<String, Object> toolContext) {
123
109
124
110
@ Override
125
111
@ Nullable
126
- public Boolean getToolCallReturnDirect () {
127
- return this .toolCallReturnDirect ;
112
+ public Boolean isInternalToolExecutionEnabled () {
113
+ return this .internalToolExecutionEnabled ;
128
114
}
129
115
130
116
@ Override
131
- public void setToolCallReturnDirect (@ Nullable Boolean toolCallReturnDirect ) {
132
- this .toolCallReturnDirect = toolCallReturnDirect ;
117
+ public void setInternalToolExecutionEnabled (@ Nullable Boolean internalToolExecutionEnabled ) {
118
+ this .internalToolExecutionEnabled = internalToolExecutionEnabled ;
133
119
}
134
120
135
121
@ Override
136
122
public List <FunctionCallback > getFunctionCallbacks () {
137
- return getToolCallbacks (). stream (). map ( FunctionCallback . class :: cast ). toList () ;
123
+ return getToolCallbacks ();
138
124
}
139
125
140
126
@ Override
141
127
public void setFunctionCallbacks (List <FunctionCallback > functionCallbacks ) {
142
- throw new UnsupportedOperationException ( "Not supported. Call setToolCallbacks instead." );
128
+ setToolCallbacks ( functionCallbacks );
143
129
}
144
130
145
131
@ Override
@@ -155,12 +141,12 @@ public void setFunctions(Set<String> functions) {
155
141
@ Override
156
142
@ Nullable
157
143
public Boolean getProxyToolCalls () {
158
- return getToolCallReturnDirect () ;
144
+ return isInternalToolExecutionEnabled () != null ? ! isInternalToolExecutionEnabled () : null ;
159
145
}
160
146
161
147
@ Override
162
148
public void setProxyToolCalls (@ Nullable Boolean proxyToolCalls ) {
163
- setToolCallReturnDirect (proxyToolCalls != null && proxyToolCalls );
149
+ setInternalToolExecutionEnabled (proxyToolCalls == null || ! proxyToolCalls );
164
150
}
165
151
166
152
@ Override
@@ -250,7 +236,7 @@ public <T extends ChatOptions> T copy() {
250
236
options .setToolCallbacks (getToolCallbacks ());
251
237
options .setTools (getTools ());
252
238
options .setToolContext (getToolContext ());
253
- options .setToolCallReturnDirect ( getToolCallReturnDirect ());
239
+ options .setInternalToolExecutionEnabled ( isInternalToolExecutionEnabled ());
254
240
options .setModel (getModel ());
255
241
options .setFrequencyPenalty (getFrequencyPenalty ());
256
242
options .setMaxTokens (getMaxTokens ());
@@ -262,55 +248,6 @@ public <T extends ChatOptions> T copy() {
262
248
return (T ) options ;
263
249
}
264
250
265
- /**
266
- * Merge the given {@link ChatOptions} into this instance.
267
- */
268
- public ToolCallingChatOptions merge (ChatOptions options ) {
269
- ToolCallingChatOptions .Builder builder = ToolCallingChatOptions .builder ();
270
- builder .model (StringUtils .hasText (options .getModel ()) ? options .getModel () : this .getModel ());
271
- builder .frequencyPenalty (
272
- options .getFrequencyPenalty () != null ? options .getFrequencyPenalty () : this .getFrequencyPenalty ());
273
- builder .maxTokens (options .getMaxTokens () != null ? options .getMaxTokens () : this .getMaxTokens ());
274
- builder .presencePenalty (
275
- options .getPresencePenalty () != null ? options .getPresencePenalty () : this .getPresencePenalty ());
276
- builder .stopSequences (options .getStopSequences () != null ? new ArrayList <>(options .getStopSequences ())
277
- : this .getStopSequences ());
278
- builder .temperature (options .getTemperature () != null ? options .getTemperature () : this .getTemperature ());
279
- builder .topK (options .getTopK () != null ? options .getTopK () : this .getTopK ());
280
- builder .topP (options .getTopP () != null ? options .getTopP () : this .getTopP ());
281
-
282
- if (options instanceof ToolCallingChatOptions toolOptions ) {
283
- List <ToolCallback > toolCallbacks = new ArrayList <>(this .toolCallbacks );
284
- if (!CollectionUtils .isEmpty (toolOptions .getToolCallbacks ())) {
285
- toolCallbacks .addAll (toolOptions .getToolCallbacks ());
286
- }
287
- builder .toolCallbacks (toolCallbacks );
288
-
289
- Set <String > tools = new HashSet <>(this .tools );
290
- if (!CollectionUtils .isEmpty (toolOptions .getTools ())) {
291
- tools .addAll (toolOptions .getTools ());
292
- }
293
- builder .tools (tools );
294
-
295
- Map <String , Object > toolContext = new HashMap <>(this .toolContext );
296
- if (!CollectionUtils .isEmpty (toolOptions .getToolContext ())) {
297
- toolContext .putAll (toolOptions .getToolContext ());
298
- }
299
- builder .toolContext (toolContext );
300
-
301
- builder .toolCallReturnDirect (toolOptions .getToolCallReturnDirect () != null
302
- ? toolOptions .getToolCallReturnDirect () : this .getToolCallReturnDirect ());
303
- }
304
- else {
305
- builder .toolCallbacks (this .toolCallbacks );
306
- builder .tools (this .tools );
307
- builder .toolContext (this .toolContext );
308
- builder .toolCallReturnDirect (this .toolCallReturnDirect );
309
- }
310
-
311
- return builder .build ();
312
- }
313
-
314
251
public static Builder builder () {
315
252
return new Builder ();
316
253
}
@@ -323,14 +260,15 @@ public static class Builder implements ToolCallingChatOptions.Builder {
323
260
private final DefaultToolCallingChatOptions options = new DefaultToolCallingChatOptions ();
324
261
325
262
@ Override
326
- public ToolCallingChatOptions .Builder toolCallbacks (List <ToolCallback > toolCallbacks ) {
263
+ public ToolCallingChatOptions .Builder toolCallbacks (List <FunctionCallback > toolCallbacks ) {
327
264
this .options .setToolCallbacks (toolCallbacks );
328
265
return this ;
329
266
}
330
267
331
268
@ Override
332
- public ToolCallingChatOptions .Builder toolCallbacks (ToolCallback ... toolCallbacks ) {
333
- this .options .setToolCallbacks (toolCallbacks );
269
+ public ToolCallingChatOptions .Builder toolCallbacks (FunctionCallback ... toolCallbacks ) {
270
+ Assert .notNull (toolCallbacks , "toolCallbacks cannot be null" );
271
+ this .options .setToolCallbacks (Arrays .asList (toolCallbacks ));
334
272
return this ;
335
273
}
336
274
@@ -342,7 +280,8 @@ public ToolCallingChatOptions.Builder tools(Set<String> toolNames) {
342
280
343
281
@ Override
344
282
public ToolCallingChatOptions .Builder tools (String ... toolNames ) {
345
- this .options .setTools (toolNames );
283
+ Assert .notNull (toolNames , "toolNames cannot be null" );
284
+ this .options .setTools (Set .of (toolNames ));
346
285
return this ;
347
286
}
348
287
@@ -363,16 +302,16 @@ public ToolCallingChatOptions.Builder toolContext(String key, Object value) {
363
302
}
364
303
365
304
@ Override
366
- public ToolCallingChatOptions .Builder toolCallReturnDirect (@ Nullable Boolean toolCallReturnDirect ) {
367
- this .options .setToolCallReturnDirect (toolCallReturnDirect );
305
+ public ToolCallingChatOptions .Builder internalToolExecutionEnabled (
306
+ @ Nullable Boolean internalToolExecutionEnabled ) {
307
+ this .options .setInternalToolExecutionEnabled (internalToolExecutionEnabled );
368
308
return this ;
369
309
}
370
310
371
311
@ Override
372
312
@ Deprecated // Use toolCallbacks() instead
373
313
public ToolCallingChatOptions .Builder functionCallbacks (List <FunctionCallback > functionCallbacks ) {
374
- Assert .notNull (functionCallbacks , "functionCallbacks cannot be null" );
375
- return toolCallbacks (functionCallbacks .stream ().map (ToolCallback .class ::cast ).toList ());
314
+ return toolCallbacks (functionCallbacks );
376
315
}
377
316
378
317
@ Override
@@ -395,9 +334,9 @@ public ToolCallingChatOptions.Builder function(String function) {
395
334
}
396
335
397
336
@ Override
398
- @ Deprecated // Use toolCallReturnDirect () instead
337
+ @ Deprecated // Use internalToolExecutionEnabled () instead
399
338
public ToolCallingChatOptions .Builder proxyToolCalls (@ Nullable Boolean proxyToolCalls ) {
400
- return toolCallReturnDirect (proxyToolCalls != null && proxyToolCalls );
339
+ return internalToolExecutionEnabled (proxyToolCalls == null || ! proxyToolCalls );
401
340
}
402
341
403
342
@ Override
0 commit comments