| 
18 | 18 | 
 
  | 
19 | 19 | import java.util.ArrayList;  | 
20 | 20 | import java.util.Base64;  | 
 | 21 | +import java.util.HashMap;  | 
21 | 22 | import java.util.List;  | 
22 | 23 | import java.util.Map;  | 
23 | 24 | import java.util.Set;  | 
@@ -379,46 +380,49 @@ private ChatResponse toChatResponse(ChatCompletionResponse chatCompletion, Usage  | 
379 | 380 | 			return new ChatResponse(List.of());  | 
380 | 381 | 		}  | 
381 | 382 | 
 
  | 
382 |  | -		List<Generation> generations = chatCompletion.content()  | 
383 |  | -			.stream()  | 
384 |  | -			.filter(content -> content.type() != ContentBlock.Type.TOOL_USE)  | 
385 |  | -			.map(content -> new Generation(new AssistantMessage(content.text(), Map.of()),  | 
386 |  | -					ChatGenerationMetadata.builder().finishReason(chatCompletion.stopReason()).build()))  | 
387 |  | -			.toList();  | 
388 |  | - | 
389 |  | -		List<Generation> allGenerations = new ArrayList<>(generations);  | 
 | 383 | +		List<Generation> generations = new ArrayList<>();  | 
 | 384 | +		List<AssistantMessage.ToolCall> toolCalls = new ArrayList<>();  | 
 | 385 | +		for (ContentBlock content : chatCompletion.content()) {  | 
 | 386 | +			switch (content.type()) {  | 
 | 387 | +				case TEXT:  | 
 | 388 | +					generations.add(new Generation(new AssistantMessage(content.text(), Map.of()),  | 
 | 389 | +							ChatGenerationMetadata.builder().finishReason(chatCompletion.stopReason()).build()));  | 
 | 390 | +					break;  | 
 | 391 | +				case THINKING:  | 
 | 392 | +					Map<String, Object> thinkingProperties = new HashMap<>();  | 
 | 393 | +					thinkingProperties.put("signature", content.signature());  | 
 | 394 | +					generations.add(new Generation(new AssistantMessage(content.thinking(), thinkingProperties),  | 
 | 395 | +							ChatGenerationMetadata.builder().finishReason(chatCompletion.stopReason()).build()));  | 
 | 396 | +					break;  | 
 | 397 | +				case REDACTED_THINKING:  | 
 | 398 | +					Map<String, Object> redactedProperties = new HashMap<>();  | 
 | 399 | +					redactedProperties.put("data", content.data());  | 
 | 400 | +					generations.add(new Generation(new AssistantMessage(null, redactedProperties),  | 
 | 401 | +							ChatGenerationMetadata.builder().finishReason(chatCompletion.stopReason()).build()));  | 
 | 402 | +					break;  | 
 | 403 | +				case TOOL_USE:  | 
 | 404 | +					var functionCallId = content.id();  | 
 | 405 | +					var functionName = content.name();  | 
 | 406 | +					var functionArguments = JsonParser.toJson(content.input());  | 
 | 407 | +					toolCalls.add(  | 
 | 408 | +							new AssistantMessage.ToolCall(functionCallId, "function", functionName, functionArguments));  | 
 | 409 | +					break;  | 
 | 410 | +			}  | 
 | 411 | +		}  | 
390 | 412 | 
 
  | 
391 | 413 | 		if (chatCompletion.stopReason() != null && generations.isEmpty()) {  | 
392 | 414 | 			Generation generation = new Generation(new AssistantMessage(null, Map.of()),  | 
393 | 415 | 					ChatGenerationMetadata.builder().finishReason(chatCompletion.stopReason()).build());  | 
394 |  | -			allGenerations.add(generation);  | 
 | 416 | +			generations.add(generation);  | 
395 | 417 | 		}  | 
396 | 418 | 
 
  | 
397 |  | -		List<ContentBlock> toolToUseList = chatCompletion.content()  | 
398 |  | -			.stream()  | 
399 |  | -			.filter(c -> c.type() == ContentBlock.Type.TOOL_USE)  | 
400 |  | -			.toList();  | 
401 |  | - | 
402 |  | -		if (!CollectionUtils.isEmpty(toolToUseList)) {  | 
403 |  | -			List<AssistantMessage.ToolCall> toolCalls = new ArrayList<>();  | 
404 |  | - | 
405 |  | -			for (ContentBlock toolToUse : toolToUseList) {  | 
406 |  | - | 
407 |  | -				var functionCallId = toolToUse.id();  | 
408 |  | -				var functionName = toolToUse.name();  | 
409 |  | -				var functionArguments = JsonParser.toJson(toolToUse.input());  | 
410 |  | - | 
411 |  | -				toolCalls  | 
412 |  | -					.add(new AssistantMessage.ToolCall(functionCallId, "function", functionName, functionArguments));  | 
413 |  | -			}  | 
414 |  | - | 
 | 419 | +		if (!CollectionUtils.isEmpty(toolCalls)) {  | 
415 | 420 | 			AssistantMessage assistantMessage = new AssistantMessage("", Map.of(), toolCalls);  | 
416 | 421 | 			Generation toolCallGeneration = new Generation(assistantMessage,  | 
417 | 422 | 					ChatGenerationMetadata.builder().finishReason(chatCompletion.stopReason()).build());  | 
418 |  | -			allGenerations.add(toolCallGeneration);  | 
 | 423 | +			generations.add(toolCallGeneration);  | 
419 | 424 | 		}  | 
420 |  | - | 
421 |  | -		return new ChatResponse(allGenerations, this.from(chatCompletion, usage));  | 
 | 425 | +		return new ChatResponse(generations, this.from(chatCompletion, usage));  | 
422 | 426 | 	}  | 
423 | 427 | 
 
  | 
424 | 428 | 	private ChatResponseMetadata from(AnthropicApi.ChatCompletionResponse result) {  | 
@@ -575,7 +579,7 @@ else if (message.getMessageType() == MessageType.TOOL) {  | 
575 | 579 | 		List<ToolDefinition> toolDefinitions = this.toolCallingManager.resolveToolDefinitions(requestOptions);  | 
576 | 580 | 		if (!CollectionUtils.isEmpty(toolDefinitions)) {  | 
577 | 581 | 			request = ModelOptionsUtils.merge(request, this.defaultOptions, ChatCompletionRequest.class);  | 
578 |  | -			request = ChatCompletionRequest.from(request).withTools(getFunctionTools(toolDefinitions)).build();  | 
 | 582 | +			request = ChatCompletionRequest.from(request).tools(getFunctionTools(toolDefinitions)).build();  | 
579 | 583 | 		}  | 
580 | 584 | 
 
  | 
581 | 585 | 		return request;  | 
 | 
0 commit comments