|
| 1 | +/* |
| 2 | + * Copyright 2024-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.ai.bedrock.converse; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import io.micrometer.observation.ObservationRegistry; |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 25 | +import org.mockito.Mock; |
| 26 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 27 | +import software.amazon.awssdk.core.document.internal.MapDocument; |
| 28 | +import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeAsyncClient; |
| 29 | +import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeClient; |
| 30 | +import software.amazon.awssdk.services.bedrockruntime.model.ContentBlock; |
| 31 | +import software.amazon.awssdk.services.bedrockruntime.model.ConversationRole; |
| 32 | +import software.amazon.awssdk.services.bedrockruntime.model.ConverseMetrics; |
| 33 | +import software.amazon.awssdk.services.bedrockruntime.model.ConverseOutput; |
| 34 | +import software.amazon.awssdk.services.bedrockruntime.model.ConverseRequest; |
| 35 | +import software.amazon.awssdk.services.bedrockruntime.model.ConverseResponse; |
| 36 | +import software.amazon.awssdk.services.bedrockruntime.model.Message; |
| 37 | +import software.amazon.awssdk.services.bedrockruntime.model.StopReason; |
| 38 | +import software.amazon.awssdk.services.bedrockruntime.model.TokenUsage; |
| 39 | +import software.amazon.awssdk.services.bedrockruntime.model.ToolUseBlock; |
| 40 | + |
| 41 | +import org.springframework.ai.chat.prompt.Prompt; |
| 42 | +import org.springframework.ai.model.function.FunctionCallback; |
| 43 | +import org.springframework.ai.model.function.FunctionCallingOptions; |
| 44 | +import org.springframework.ai.model.function.FunctionCallingOptionsBuilder.PortableFunctionCallingOptions; |
| 45 | + |
| 46 | +import static org.assertj.core.api.Assertions.assertThat; |
| 47 | +import static org.mockito.ArgumentMatchers.isA; |
| 48 | +import static org.mockito.BDDMockito.given; |
| 49 | + |
| 50 | +/** |
| 51 | + * @author Christian Tzolov |
| 52 | + */ |
| 53 | +@ExtendWith(MockitoExtension.class) |
| 54 | +public class BedrockConverseUsageAggregationTests { |
| 55 | + |
| 56 | + private @Mock BedrockRuntimeClient bedrockRuntimeClient; |
| 57 | + |
| 58 | + private @Mock BedrockRuntimeAsyncClient bedrockRuntimeAsyncClient; |
| 59 | + |
| 60 | + private BedrockProxyChatModel chatModel; |
| 61 | + |
| 62 | + @BeforeEach |
| 63 | + public void beforeEach() { |
| 64 | + chatModel = new BedrockProxyChatModel(this.bedrockRuntimeClient, this.bedrockRuntimeAsyncClient, |
| 65 | + FunctionCallingOptions.builder().build(), null, List.of(), ObservationRegistry.NOOP); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void call() { |
| 70 | + ConverseResponse converseResponse = ConverseResponse.builder() |
| 71 | + |
| 72 | + .output(ConverseOutput.builder() |
| 73 | + .message(Message.builder() |
| 74 | + .role(ConversationRole.ASSISTANT) |
| 75 | + .content(ContentBlock.fromText("Response Content Block")) |
| 76 | + .build()) |
| 77 | + .build()) |
| 78 | + .usage(TokenUsage.builder().inputTokens(16).outputTokens(14).totalTokens(30).build()) |
| 79 | + .build(); |
| 80 | + |
| 81 | + given(this.bedrockRuntimeClient.converse(isA(ConverseRequest.class))).willReturn(converseResponse); |
| 82 | + |
| 83 | + var result = this.chatModel.call(new Prompt("text")); |
| 84 | + |
| 85 | + assertThat(result).isNotNull(); |
| 86 | + assertThat(result.getResult().getOutput().getContent()).isSameAs("Response Content Block"); |
| 87 | + |
| 88 | + assertThat(result.getMetadata().getUsage().getPromptTokens()).isEqualTo(16); |
| 89 | + assertThat(result.getMetadata().getUsage().getGenerationTokens()).isEqualTo(14); |
| 90 | + assertThat(result.getMetadata().getUsage().getTotalTokens()).isEqualTo(30); |
| 91 | + } |
| 92 | + |
| 93 | + public record Request(String location, String unit) { |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void callWithToolUse() { |
| 98 | + |
| 99 | + ConverseResponse converseResponseToolUse = ConverseResponse.builder() |
| 100 | + .output(ConverseOutput.builder() |
| 101 | + .message(Message.builder() |
| 102 | + .role(ConversationRole.ASSISTANT) |
| 103 | + .content(ContentBlock.fromText( |
| 104 | + "Certainly! I'd be happy to check the current weather in Paris for you, with the temperature in Celsius. To get this information, I'll use the getCurrentWeather function. Let me fetch that for you right away."), |
| 105 | + ContentBlock.fromToolUse(ToolUseBlock.builder() |
| 106 | + .toolUseId("tooluse_2SZuiUDkRbeGysun8O2Wag") |
| 107 | + .name("getCurrentWeather") |
| 108 | + .input(MapDocument.mapBuilder() |
| 109 | + .putString("location", "Paris, France") |
| 110 | + .putString("unit", "C") |
| 111 | + .build()) |
| 112 | + .build())) |
| 113 | + |
| 114 | + .build()) |
| 115 | + .build()) |
| 116 | + .usage(TokenUsage.builder().inputTokens(445).outputTokens(119).totalTokens(564).build()) |
| 117 | + .stopReason(StopReason.TOOL_USE) |
| 118 | + .metrics(ConverseMetrics.builder().latencyMs(3435L).build()) |
| 119 | + .build(); |
| 120 | + |
| 121 | + ConverseResponse converseResponseFinal = ConverseResponse.builder() |
| 122 | + .output(ConverseOutput.builder() |
| 123 | + .message(Message.builder() |
| 124 | + .role(ConversationRole.ASSISTANT) |
| 125 | + .content(ContentBlock.fromText( |
| 126 | + """ |
| 127 | + Based on the information from the weather tool, the current temperature in Paris, France is 15.0°C (Celsius). |
| 128 | +
|
| 129 | + Please note that weather conditions can change throughout the day, so this temperature represents the current |
| 130 | + reading at the time of the request. If you need more detailed information about the weather in Paris, such as |
| 131 | + humidity, wind speed, or forecast for the coming days, please let me know, and I'll be happy to provide more |
| 132 | + details if that information is available through our weather service. |
| 133 | + """)) |
| 134 | + .build()) |
| 135 | + .build()) |
| 136 | + .usage(TokenUsage.builder().inputTokens(540).outputTokens(106).totalTokens(646).build()) |
| 137 | + .stopReason(StopReason.END_TURN) |
| 138 | + .metrics(ConverseMetrics.builder().latencyMs(3435L).build()) |
| 139 | + .build(); |
| 140 | + |
| 141 | + given(this.bedrockRuntimeClient.converse(isA(ConverseRequest.class))).willReturn(converseResponseToolUse) |
| 142 | + .willReturn(converseResponseFinal); |
| 143 | + |
| 144 | + FunctionCallback functionCallback = FunctionCallback.builder() |
| 145 | + .description("Gets the weather in location") |
| 146 | + .function("getCurrentWeather", (Request request) -> "15.0°C") |
| 147 | + .inputType(Request.class) |
| 148 | + .build(); |
| 149 | + |
| 150 | + var result = this.chatModel.call(new Prompt("What is the weather in Paris?", |
| 151 | + PortableFunctionCallingOptions.builder().withFunctionCallbacks(functionCallback).build())); |
| 152 | + |
| 153 | + assertThat(result).isNotNull(); |
| 154 | + assertThat(result.getResult().getOutput().getContent()) |
| 155 | + .isSameAs(converseResponseFinal.output().message().content().get(0).text()); |
| 156 | + |
| 157 | + assertThat(result.getMetadata().getUsage().getPromptTokens()).isEqualTo(445 + 540); |
| 158 | + assertThat(result.getMetadata().getUsage().getGenerationTokens()).isEqualTo(119 + 106); |
| 159 | + assertThat(result.getMetadata().getUsage().getTotalTokens()).isEqualTo(564 + 646); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + public void streamWithToolUse() { |
| 164 | + // TODO: Implement the test |
| 165 | + } |
| 166 | + |
| 167 | +} |
0 commit comments