|  | 
| 23 | 23 | 
 | 
| 24 | 24 | import org.springframework.ai.chat.messages.AssistantMessage; | 
| 25 | 25 | import org.springframework.ai.chat.messages.UserMessage; | 
|  | 26 | +import org.springframework.ai.chat.metadata.ChatResponseMetadata; | 
|  | 27 | +import org.springframework.ai.chat.metadata.Usage; | 
| 26 | 28 | import reactor.core.publisher.Flux; | 
| 27 | 29 | 
 | 
| 28 | 30 | import org.springframework.ai.bedrock.anthropic3.api.Anthropic3ChatBedrockApi; | 
| @@ -83,11 +85,16 @@ public ChatResponse call(Prompt prompt) { | 
| 83 | 85 | 
 | 
| 84 | 86 | 		List<Generation> generations = response.content().stream().map(content -> { | 
| 85 | 87 | 			return new Generation(new AssistantMessage(content.text()), | 
| 86 |  | -					ChatGenerationMetadata.from(response.stopReason(), new Anthropic3ChatBedrockApi.AnthropicUsage( | 
| 87 |  | -							response.usage().inputTokens(), response.usage().outputTokens()))); | 
|  | 88 | +					ChatGenerationMetadata.from(response.stopReason(), null)); | 
| 88 | 89 | 		}).toList(); | 
| 89 | 90 | 
 | 
| 90 |  | -		return new ChatResponse(generations); | 
|  | 91 | +		ChatResponseMetadata metadata = ChatResponseMetadata.builder() | 
|  | 92 | +			.withId(response.id()) | 
|  | 93 | +			.withModel(response.model()) | 
|  | 94 | +			.withUsage(extractUsage(response)) | 
|  | 95 | +			.build(); | 
|  | 96 | + | 
|  | 97 | +		return new ChatResponse(generations, metadata); | 
| 91 | 98 | 	} | 
| 92 | 99 | 
 | 
| 93 | 100 | 	@Override | 
| @@ -117,6 +124,21 @@ public Flux<ChatResponse> stream(Prompt prompt) { | 
| 117 | 124 | 		}); | 
| 118 | 125 | 	} | 
| 119 | 126 | 
 | 
|  | 127 | +	private Usage extractUsage(AnthropicChatResponse response) { | 
|  | 128 | +		return new Usage() { | 
|  | 129 | + | 
|  | 130 | +			@Override | 
|  | 131 | +			public Long getPromptTokens() { | 
|  | 132 | +				return response.usage().inputTokens().longValue(); | 
|  | 133 | +			} | 
|  | 134 | + | 
|  | 135 | +			@Override | 
|  | 136 | +			public Long getGenerationTokens() { | 
|  | 137 | +				return response.usage().outputTokens().longValue(); | 
|  | 138 | +			} | 
|  | 139 | +		}; | 
|  | 140 | +	} | 
|  | 141 | + | 
| 120 | 142 | 	/** | 
| 121 | 143 | 	 * Accessible for testing. | 
| 122 | 144 | 	 */ | 
|  | 
0 commit comments