@@ -295,46 +295,49 @@ private ChatResponse toChatResponse(ChatCompletionResponse chatCompletion, Usage
295
295
return new ChatResponse (List .of ());
296
296
}
297
297
298
- List <Generation > generations = chatCompletion .content ()
299
- .stream ()
300
- .filter (content -> content .type () != ContentBlock .Type .TOOL_USE )
301
- .map (content -> new Generation (new AssistantMessage (content .text (), Map .of ()),
302
- ChatGenerationMetadata .builder ().finishReason (chatCompletion .stopReason ()).build ()))
303
- .toList ();
304
-
305
- List <Generation > allGenerations = new ArrayList <>(generations );
298
+ List <Generation > generations = new ArrayList <>();
299
+ List <AssistantMessage .ToolCall > toolCalls = new ArrayList <>();
300
+ for (ContentBlock content : chatCompletion .content ()) {
301
+ switch (content .type ()) {
302
+ case TEXT , TEXT_DELTA :
303
+ generations .add (new Generation (new AssistantMessage (content .text (), Map .of ()),
304
+ ChatGenerationMetadata .builder ().finishReason (chatCompletion .stopReason ()).build ()));
305
+ break ;
306
+ case THINKING , THINKING_DELTA :
307
+ Map <String , Object > thinkingProperties = new HashMap <>();
308
+ thinkingProperties .put ("signature" , content .signature ());
309
+ generations .add (new Generation (new AssistantMessage (content .thinking (), thinkingProperties ),
310
+ ChatGenerationMetadata .builder ().finishReason (chatCompletion .stopReason ()).build ()));
311
+ break ;
312
+ case REDACTED_THINKING :
313
+ Map <String , Object > redactedProperties = new HashMap <>();
314
+ redactedProperties .put ("data" , content .data ());
315
+ generations .add (new Generation (new AssistantMessage (null , redactedProperties ),
316
+ ChatGenerationMetadata .builder ().finishReason (chatCompletion .stopReason ()).build ()));
317
+ break ;
318
+ case TOOL_USE :
319
+ var functionCallId = content .id ();
320
+ var functionName = content .name ();
321
+ var functionArguments = JsonParser .toJson (content .input ());
322
+ toolCalls .add (
323
+ new AssistantMessage .ToolCall (functionCallId , "function" , functionName , functionArguments ));
324
+ break ;
325
+ }
326
+ }
306
327
307
328
if (chatCompletion .stopReason () != null && generations .isEmpty ()) {
308
329
Generation generation = new Generation (new AssistantMessage (null , Map .of ()),
309
330
ChatGenerationMetadata .builder ().finishReason (chatCompletion .stopReason ()).build ());
310
- allGenerations .add (generation );
331
+ generations .add (generation );
311
332
}
312
333
313
- List <ContentBlock > toolToUseList = chatCompletion .content ()
314
- .stream ()
315
- .filter (c -> c .type () == ContentBlock .Type .TOOL_USE )
316
- .toList ();
317
-
318
- if (!CollectionUtils .isEmpty (toolToUseList )) {
319
- List <AssistantMessage .ToolCall > toolCalls = new ArrayList <>();
320
-
321
- for (ContentBlock toolToUse : toolToUseList ) {
322
-
323
- var functionCallId = toolToUse .id ();
324
- var functionName = toolToUse .name ();
325
- var functionArguments = JsonParser .toJson (toolToUse .input ());
326
-
327
- toolCalls
328
- .add (new AssistantMessage .ToolCall (functionCallId , "function" , functionName , functionArguments ));
329
- }
330
-
334
+ if (!CollectionUtils .isEmpty (toolCalls )) {
331
335
AssistantMessage assistantMessage = new AssistantMessage ("" , Map .of (), toolCalls );
332
336
Generation toolCallGeneration = new Generation (assistantMessage ,
333
337
ChatGenerationMetadata .builder ().finishReason (chatCompletion .stopReason ()).build ());
334
- allGenerations .add (toolCallGeneration );
338
+ generations .add (toolCallGeneration );
335
339
}
336
-
337
- return new ChatResponse (allGenerations , this .from (chatCompletion , usage ));
340
+ return new ChatResponse (generations , this .from (chatCompletion , usage ));
338
341
}
339
342
340
343
private ChatResponseMetadata from (AnthropicApi .ChatCompletionResponse result ) {
@@ -506,7 +509,7 @@ else if (message.getMessageType() == MessageType.TOOL) {
506
509
List <ToolDefinition > toolDefinitions = this .toolCallingManager .resolveToolDefinitions (requestOptions );
507
510
if (!CollectionUtils .isEmpty (toolDefinitions )) {
508
511
request = ModelOptionsUtils .merge (request , this .defaultOptions , ChatCompletionRequest .class );
509
- request = ChatCompletionRequest .from (request ).withTools (getFunctionTools (toolDefinitions )).build ();
512
+ request = ChatCompletionRequest .from (request ).tools (getFunctionTools (toolDefinitions )).build ();
510
513
}
511
514
512
515
return request ;
0 commit comments