99import com .sap .ai .sdk .foundationmodels .openai .generated .model .CreateChatCompletionRequestAllOfResponseFormat ;
1010import com .sap .ai .sdk .foundationmodels .openai .generated .model .CreateChatCompletionRequestAllOfStop ;
1111import java .math .BigDecimal ;
12+ import java .util .ArrayList ;
1213import java .util .List ;
1314import java .util .Map ;
1415import java .util .Objects ;
@@ -125,6 +126,15 @@ public class OpenAiChatCompletionRequest {
125126 /** List of tools that the model may invoke during the completion. */
126127 @ Nullable List <ChatCompletionTool > tools ;
127128
129+ /**
130+ * List of tools that are executable at runtime of the application.
131+ *
132+ * @since 1.7.0
133+ */
134+ @ Getter (value = AccessLevel .PACKAGE )
135+ @ Nullable
136+ List <OpenAiTool > toolsExecutable ;
137+
128138 /** Option to control which tool is invoked by the model. */
129139 @ With (AccessLevel .PRIVATE )
130140 @ Nullable
@@ -179,6 +189,7 @@ public OpenAiChatCompletionRequest(@Nonnull final List<OpenAiMessage> messages)
179189 null ,
180190 null ,
181191 null ,
192+ null ,
182193 null );
183194 }
184195
@@ -226,6 +237,7 @@ public OpenAiChatCompletionRequest withParallelToolCalls(
226237 this .streamOptions ,
227238 this .responseFormat ,
228239 this .tools ,
240+ this .toolsExecutable ,
229241 this .toolChoice );
230242 }
231243
@@ -258,6 +270,7 @@ public OpenAiChatCompletionRequest withLogprobs(@Nonnull final Boolean logprobs)
258270 this .streamOptions ,
259271 this .responseFormat ,
260272 this .tools ,
273+ this .toolsExecutable ,
261274 this .toolChoice );
262275 }
263276
@@ -282,36 +295,35 @@ public OpenAiChatCompletionRequest withToolChoice(@Nonnull final OpenAiToolChoic
282295 return this .withToolChoice (choice .toolChoice );
283296 }
284297
285- /**
286- * Sets the tools to be used in the request with convenience class {@code OpenAiTool}.
287- *
288- * @param tools the list of tools to be used
289- * @return a new OpenAiChatCompletionRequest instance with the specified tools
290- * @throws IllegalArgumentException if the tool type is not supported
291- * @since 1.7.0
292- */
293- @ Nonnull
294- @ Beta
295- public OpenAiChatCompletionRequest withToolsExecutable (@ Nonnull final List <OpenAiTool > tools ) {
296- return this .withTools (tools .stream ().map (OpenAiTool ::createChatCompletionTool ).toList ());
297- }
298-
299298 /**
300299 * Converts the request to a generated model class CreateChatCompletionRequest.
301300 *
302301 * @return the CreateChatCompletionRequest
303302 */
304303 CreateChatCompletionRequest createCreateChatCompletionRequest () {
305- final var request = new CreateChatCompletionRequest ();
306- this .messages .forEach (
307- message ->
308- request .addMessagesItem (OpenAiUtils .createChatCompletionRequestMessage (message )));
304+ final var toolsCombined = new ArrayList <ChatCompletionTool >();
305+ if (this .tools != null ) {
306+ toolsCombined .addAll (this .tools );
307+ }
308+ if (this .toolsExecutable != null ) {
309+ for (OpenAiTool tool : this .toolsExecutable ) {
310+ toolsCombined .add (tool .createChatCompletionTool ());
311+ }
312+ }
309313
310- request .stop (this .stop != null ? CreateChatCompletionRequestAllOfStop .create (this .stop ) : null );
314+ final var request = new CreateChatCompletionRequest ();
315+ for (OpenAiMessage message : this .messages ) {
316+ request .addMessagesItem (OpenAiUtils .createChatCompletionRequestMessage (message ));
317+ }
318+ if (this .stop != null ) {
319+ request .stop (CreateChatCompletionRequestAllOfStop .create (this .stop ));
320+ }
321+ if (!toolsCombined .isEmpty ()) {
322+ request .tools (toolsCombined );
323+ }
311324
312325 request .temperature (this .temperature );
313326 request .topP (this .topP );
314-
315327 request .stream (null );
316328 request .maxTokens (this .maxTokens );
317329 request .maxCompletionTokens (this .maxCompletionTokens );
@@ -326,7 +338,6 @@ CreateChatCompletionRequest createCreateChatCompletionRequest() {
326338 request .seed (this .seed );
327339 request .streamOptions (this .streamOptions );
328340 request .responseFormat (this .responseFormat );
329- request .tools (this .tools );
330341 request .toolChoice (this .toolChoice );
331342 request .functionCall (null );
332343 request .functions (null );
0 commit comments