17
17
package org .springframework .ai .azure .openai ;
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 com .fasterxml .jackson .annotation .JsonProperty ;
31
33
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
/**
44
48
* @author Ilayaperumal Gopinathan
45
49
*/
46
50
@ JsonInclude (Include .NON_NULL )
47
- public class AzureOpenAiChatOptions implements FunctionCallingOptions {
51
+ public class AzureOpenAiChatOptions implements ToolCallingChatOptions {
48
52
49
53
/**
50
54
* The maximum number of tokens to generate.
@@ -138,33 +142,6 @@ public class AzureOpenAiChatOptions implements FunctionCallingOptions {
138
142
@ JsonProperty ("response_format" )
139
143
private AzureOpenAiResponseFormat responseFormat ;
140
144
141
- /**
142
- * OpenAI Tool Function Callbacks to register with the ChatModel. For Prompt Options
143
- * the functionCallbacks are automatically enabled for the duration of the prompt
144
- * execution. For Default Options the functionCallbacks are registered but disabled by
145
- * default. Use the enableFunctions to set the functions from the registry to be used
146
- * by the ChatModel chat completion requests.
147
- */
148
- @ JsonIgnore
149
- private List <FunctionCallback > functionCallbacks = new ArrayList <>();
150
-
151
- /**
152
- * List of functions, identified by their names, to configure for function calling in
153
- * the chat completion requests. Functions with those names must exist in the
154
- * functionCallbacks registry. The {@link #functionCallbacks} from the PromptOptions
155
- * are automatically enabled for the duration of the prompt execution.
156
- *
157
- * Note that function enabled with the default options are enabled for all chat
158
- * completion requests. This could impact the token count and the billing. If the
159
- * functions is set in a prompt options, then the enabled functions are only active
160
- * for the duration of this prompt execution.
161
- */
162
- @ JsonIgnore
163
- private Set <String > functions = new HashSet <>();
164
-
165
- @ JsonIgnore
166
- private Boolean proxyToolCalls ;
167
-
168
145
/**
169
146
* Seed value for deterministic sampling such that the same seed and parameters return
170
147
* the same result.
@@ -199,7 +176,68 @@ public class AzureOpenAiChatOptions implements FunctionCallingOptions {
199
176
private ChatCompletionStreamOptions streamOptions ;
200
177
201
178
@ JsonIgnore
202
- private Map <String , Object > toolContext ;
179
+ private Map <String , Object > toolContext = new HashMap <>();
180
+
181
+ /**
182
+ * Collection of {@link ToolCallback}s to be used for tool calling in the chat
183
+ * completion requests.
184
+ */
185
+ @ JsonIgnore
186
+ private List <FunctionCallback > toolCallbacks = new ArrayList <>();
187
+
188
+ /**
189
+ * Collection of tool names to be resolved at runtime and used for tool calling in the
190
+ * chat completion requests.
191
+ */
192
+ @ JsonIgnore
193
+ private Set <String > toolNames = new HashSet <>();
194
+
195
+ /**
196
+ * Whether to enable the tool execution lifecycle internally in ChatModel.
197
+ */
198
+ @ JsonIgnore
199
+ private Boolean internalToolExecutionEnabled ;
200
+
201
+ @ Override
202
+ @ JsonIgnore
203
+ public List <FunctionCallback > getToolCallbacks () {
204
+ return this .toolCallbacks ;
205
+ }
206
+
207
+ @ Override
208
+ @ JsonIgnore
209
+ public void setToolCallbacks (List <FunctionCallback > toolCallbacks ) {
210
+ Assert .notNull (toolCallbacks , "toolCallbacks cannot be null" );
211
+ Assert .noNullElements (toolCallbacks , "toolCallbacks cannot contain null elements" );
212
+ this .toolCallbacks = toolCallbacks ;
213
+ }
214
+
215
+ @ Override
216
+ @ JsonIgnore
217
+ public Set <String > getToolNames () {
218
+ return this .toolNames ;
219
+ }
220
+
221
+ @ Override
222
+ @ JsonIgnore
223
+ public void setToolNames (Set <String > toolNames ) {
224
+ Assert .notNull (toolNames , "toolNames cannot be null" );
225
+ Assert .noNullElements (toolNames , "toolNames cannot contain null elements" );
226
+ this .toolNames = toolNames ;
227
+ }
228
+
229
+ @ Override
230
+ @ Nullable
231
+ @ JsonIgnore
232
+ public Boolean isInternalToolExecutionEnabled () {
233
+ return internalToolExecutionEnabled ;
234
+ }
235
+
236
+ @ Override
237
+ @ JsonIgnore
238
+ public void setInternalToolExecutionEnabled (@ Nullable Boolean internalToolExecutionEnabled ) {
239
+ this .internalToolExecutionEnabled = internalToolExecutionEnabled ;
240
+ }
203
241
204
242
public static Builder builder () {
205
243
return new Builder ();
@@ -224,7 +262,10 @@ public static AzureOpenAiChatOptions fromOptions(AzureOpenAiChatOptions fromOpti
224
262
.topLogprobs (fromOptions .getTopLogProbs ())
225
263
.enhancements (fromOptions .getEnhancements ())
226
264
.toolContext (fromOptions .getToolContext ())
265
+ .internalToolExecutionEnabled (fromOptions .isInternalToolExecutionEnabled ())
227
266
.streamOptions (fromOptions .getStreamOptions ())
267
+ .toolCallbacks (fromOptions .getToolCallbacks ())
268
+ .toolNames (fromOptions .getToolNames ())
228
269
.build ();
229
270
}
230
271
@@ -336,21 +377,28 @@ public void setTopP(Double topP) {
336
377
}
337
378
338
379
@ Override
380
+ @ Deprecated
381
+ @ JsonIgnore
339
382
public List <FunctionCallback > getFunctionCallbacks () {
340
- return this .functionCallbacks ;
383
+ return this .getToolCallbacks () ;
341
384
}
342
385
386
+ @ Override
387
+ @ Deprecated
388
+ @ JsonIgnore
343
389
public void setFunctionCallbacks (List <FunctionCallback > functionCallbacks ) {
344
- this .functionCallbacks = functionCallbacks ;
390
+ this .setToolCallbacks ( functionCallbacks ) ;
345
391
}
346
392
347
393
@ Override
394
+ @ Deprecated
395
+ @ JsonIgnore
348
396
public Set <String > getFunctions () {
349
- return this .functions ;
397
+ return this .getToolNames () ;
350
398
}
351
399
352
400
public void setFunctions (Set <String > functions ) {
353
- this .functions = functions ;
401
+ this .setToolNames ( functions ) ;
354
402
}
355
403
356
404
public AzureOpenAiResponseFormat getResponseFormat () {
@@ -400,12 +448,16 @@ public void setEnhancements(AzureChatEnhancementConfiguration enhancements) {
400
448
}
401
449
402
450
@ Override
451
+ @ Deprecated
452
+ @ JsonIgnore
403
453
public Boolean getProxyToolCalls () {
404
- return this .proxyToolCalls ;
454
+ return this .internalToolExecutionEnabled != null ? ! this . internalToolExecutionEnabled : null ;
405
455
}
406
456
457
+ @ Deprecated
458
+ @ JsonIgnore
407
459
public void setProxyToolCalls (Boolean proxyToolCalls ) {
408
- this .proxyToolCalls = proxyToolCalls ;
460
+ this .internalToolExecutionEnabled = proxyToolCalls != null ? ! proxyToolCalls : null ;
409
461
}
410
462
411
463
@ Override
@@ -493,30 +545,31 @@ public Builder user(String user) {
493
545
return this ;
494
546
}
495
547
548
+ @ Deprecated
496
549
public Builder functionCallbacks (List <FunctionCallback > functionCallbacks ) {
497
- this .options .functionCallbacks = functionCallbacks ;
498
- return this ;
550
+ return toolCallbacks (functionCallbacks );
499
551
}
500
552
553
+ @ Deprecated
501
554
public Builder functions (Set <String > functionNames ) {
502
- Assert .notNull (functionNames , "Function names must not be null" );
503
- this .options .functions = functionNames ;
504
- return this ;
555
+ return toolNames (functionNames );
505
556
}
506
557
558
+ @ Deprecated
507
559
public Builder function (String functionName ) {
508
- Assert .hasText (functionName , "Function name must not be empty" );
509
- this .options .functions .add (functionName );
510
- return this ;
560
+ return toolNames (functionName );
511
561
}
512
562
513
563
public Builder responseFormat (AzureOpenAiResponseFormat responseFormat ) {
514
564
this .options .responseFormat = responseFormat ;
515
565
return this ;
516
566
}
517
567
568
+ @ Deprecated
518
569
public Builder proxyToolCalls (Boolean proxyToolCalls ) {
519
- this .options .proxyToolCalls = proxyToolCalls ;
570
+ if (proxyToolCalls != null ) {
571
+ this .options .setInternalToolExecutionEnabled (!proxyToolCalls );
572
+ }
520
573
return this ;
521
574
}
522
575
@@ -555,6 +608,34 @@ public Builder streamOptions(ChatCompletionStreamOptions streamOptions) {
555
608
return this ;
556
609
}
557
610
611
+ public Builder toolCallbacks (List <FunctionCallback > toolCallbacks ) {
612
+ this .options .setToolCallbacks (toolCallbacks );
613
+ return this ;
614
+ }
615
+
616
+ public Builder toolCallbacks (FunctionCallback ... toolCallbacks ) {
617
+ Assert .notNull (toolCallbacks , "toolCallbacks cannot be null" );
618
+ this .options .toolCallbacks .addAll (Arrays .asList (toolCallbacks ));
619
+ return this ;
620
+ }
621
+
622
+ public Builder toolNames (Set <String > toolNames ) {
623
+ Assert .notNull (toolNames , "toolNames cannot be null" );
624
+ this .options .setToolNames (toolNames );
625
+ return this ;
626
+ }
627
+
628
+ public Builder toolNames (String ... toolNames ) {
629
+ Assert .notNull (toolNames , "toolNames cannot be null" );
630
+ this .options .toolNames .addAll (Set .of (toolNames ));
631
+ return this ;
632
+ }
633
+
634
+ public Builder internalToolExecutionEnabled (@ Nullable Boolean internalToolExecutionEnabled ) {
635
+ this .options .setInternalToolExecutionEnabled (internalToolExecutionEnabled );
636
+ return this ;
637
+ }
638
+
558
639
public AzureOpenAiChatOptions build () {
559
640
return this .options ;
560
641
}
0 commit comments