2828import lombok .AccessLevel ;
2929import lombok .AllArgsConstructor ;
3030import lombok .Getter ;
31- import lombok .RequiredArgsConstructor ;
3231import lombok .Setter ;
3332import lombok .Value ;
3433import lombok .With ;
@@ -163,14 +162,34 @@ private static SchemaGenerator createSchemaGenerator() {
163162 *
164163 * @param tools the list of tools to execute
165164 * @param msg the assistant message containing a list of tool calls with arguments
166- * @return a result object that contains the list of tool messages with the results
165+ * @return The list of tool messages with the results.
167166 */
168167 @ Beta
169168 @ Nonnull
170- public static Execution execute (
169+ public static List < OpenAiToolMessage > execute (
171170 @ Nonnull final List <OpenAiTool > tools , @ Nonnull final OpenAiAssistantMessage msg ) {
172- final var result = new LinkedHashMap <OpenAiFunctionCall , Object >();
171+ final var toolResults = executeInternal (tools , msg );
172+ final var result = new ArrayList <OpenAiToolMessage >();
173+ for (final var entry : toolResults .entrySet ()) {
174+ final var functionCall = entry .getKey ().getId ();
175+ final var serializedValue = serializeObject (entry .getValue ());
176+ result .add (OpenAiMessage .tool (serializedValue , functionCall ));
177+ }
178+ return result ;
179+ }
173180
181+ /**
182+ * Executes the given tool calls with the provided tools and returns the results as a list of
183+ * {@link OpenAiToolMessage} containing execution results encoded as JSON string.
184+ *
185+ * @param tools the list of tools to execute
186+ * @param msg the assistant message containing a list of tool calls with arguments
187+ * @return a map that contains the function calls and their respective tool results.
188+ */
189+ @ Nonnull
190+ protected static Map <OpenAiFunctionCall , Object > executeInternal (
191+ @ Nonnull final List <OpenAiTool > tools , @ Nonnull final OpenAiAssistantMessage msg ) {
192+ final var result = new LinkedHashMap <OpenAiFunctionCall , Object >();
174193 final var toolMap = tools .stream ().collect (Collectors .toMap (OpenAiTool ::getName , identity ()));
175194 for (final OpenAiToolCall toolCall : msg .toolCalls ()) {
176195 if (toolCall instanceof OpenAiFunctionCall functionCall ) {
@@ -183,7 +202,7 @@ public static Execution execute(
183202 result .put (functionCall , toolResult );
184203 }
185204 }
186- return new Execution ( result ) ;
205+ return result ;
187206 }
188207
189208 @ Nonnull
@@ -202,31 +221,4 @@ private static String serializeObject(@Nonnull final Object obj) throws IllegalA
202221 throw new IllegalArgumentException ("Failed to serialize object to JSON" , e );
203222 }
204223 }
205-
206- /**
207- * Represents the result of executing a tool call, containing the results of the function calls.
208- */
209- @ RequiredArgsConstructor
210- @ Beta
211- public static class Execution {
212- @ Getter @ Beta @ Nonnull private final Map <OpenAiFunctionCall , Object > results ;
213-
214- /**
215- * Creates a new list of serialized OpenAI tool messages.
216- *
217- * @return the list of serialized OpenAI tool messages.
218- * @throws IllegalArgumentException if the tool results cannot be serialized to JSON
219- */
220- @ Beta
221- @ Nonnull
222- public List <OpenAiToolMessage > getMessages () {
223- final var result = new ArrayList <OpenAiToolMessage >();
224- for (final var entry : getResults ().entrySet ()) {
225- final var functionCall = entry .getKey ().getId ();
226- final var serializedValue = serializeObject (entry .getValue ());
227- result .add (OpenAiMessage .tool (serializedValue , functionCall ));
228- }
229- return result ;
230- }
231- }
232224}
0 commit comments