1111import com .fasterxml .jackson .databind .ObjectMapper ;
1212import com .fasterxml .jackson .module .jsonSchema .JsonSchemaGenerator ;
1313import com .sap .ai .sdk .core .AiCoreService ;
14+ import com .sap .ai .sdk .foundationmodels .openai .OpenAiAssistantMessage ;
1415import com .sap .ai .sdk .foundationmodels .openai .OpenAiChatCompletionDelta ;
1516import com .sap .ai .sdk .foundationmodels .openai .OpenAiChatCompletionRequest ;
1617import com .sap .ai .sdk .foundationmodels .openai .OpenAiChatCompletionResponse ;
2021import com .sap .ai .sdk .foundationmodels .openai .OpenAiFunctionCall ;
2122import com .sap .ai .sdk .foundationmodels .openai .OpenAiImageItem ;
2223import com .sap .ai .sdk .foundationmodels .openai .OpenAiMessage ;
24+ import com .sap .ai .sdk .foundationmodels .openai .OpenAiToolCall ;
2325import com .sap .ai .sdk .foundationmodels .openai .generated .model .ChatCompletionTool ;
2426import com .sap .ai .sdk .foundationmodels .openai .generated .model .FunctionObject ;
2527import java .util .ArrayList ;
@@ -100,7 +102,8 @@ public OpenAiChatCompletionResponse chatCompletionImage(@Nonnull final String li
100102 public OpenAiChatCompletionResponse chatCompletionToolExecution (
101103 @ Nonnull final String location , @ Nonnull final String unit ) {
102104
103- var schemaMap = generateSchema (WeatherMethod .Request .class );
105+ // 1. Define the function
106+ Map <String , Object > schemaMap = generateSchema (WeatherMethod .Request .class );
104107 final var function =
105108 new FunctionObject ()
106109 .name ("weather" )
@@ -111,23 +114,28 @@ public OpenAiChatCompletionResponse chatCompletionToolExecution(
111114 final var messages = new ArrayList <OpenAiMessage >();
112115 messages .add (OpenAiMessage .user ("What's the weather in %s in %s?" .formatted (location , unit )));
113116
117+ // Assistant will call the function
114118 final var request = new OpenAiChatCompletionRequest (messages ).withTools (List .of (tool ));
115- final var response = OpenAiClient .forModel (GPT_4O_MINI ).chatCompletion (request );
119+ final OpenAiChatCompletionResponse response =
120+ OpenAiClient .forModel (GPT_4O_MINI ).chatCompletion (request );
116121
117- final var assistantMessage = response .getMessage ();
122+ // 2. Optionally, execute the function.
123+ final OpenAiAssistantMessage assistantMessage = response .getMessage ();
118124 messages .add (assistantMessage );
119125
120- var toolCall = assistantMessage .toolCalls ().get (0 );
126+ final OpenAiToolCall toolCall = assistantMessage .toolCalls ().get (0 );
121127 if (!(toolCall instanceof OpenAiFunctionCall functionCall )) {
122128 throw new IllegalArgumentException (
123129 "Expected a function call, but got: %s" .formatted (assistantMessage ));
124130 }
125131
126- var arguments = parseJson (functionCall .getArguments (), WeatherMethod .Request .class );
127- var weatherMethod = new WeatherMethod ().getCurrentWeather (arguments );
132+ WeatherMethod .Request arguments =
133+ parseJson (functionCall .getArguments (), WeatherMethod .Request .class );
134+ WeatherMethod .Response weatherMethod = WeatherMethod .getCurrentWeather (arguments );
128135
129136 messages .add (OpenAiMessage .tool (weatherMethod .toString (), functionCall .getId ()));
130137
138+ // Send back the results, and the model will incorporate them into its final response.
131139 return OpenAiClient .forModel (GPT_4O_MINI ).chatCompletion (request .withMessages (messages ));
132140 }
133141
0 commit comments