committed by
Ilayaperumal Gopinathan
parent
3068c04dca
commit
0c9d0e49fa
@@ -791,7 +791,7 @@ public class BedrockProxyChatModel implements ChatModel {
|
||||
|
||||
private Builder() {
|
||||
try {
|
||||
region = DefaultAwsRegionProviderChain.builder().build().getRegion();
|
||||
this.region = DefaultAwsRegionProviderChain.builder().build().getRegion();
|
||||
}
|
||||
catch (SdkClientException e) {
|
||||
logger.warn("Failed to load region from DefaultAwsRegionProviderChain, using US_EAST_1", e);
|
||||
|
||||
@@ -37,9 +37,9 @@ class BedrockProxyChatModelTest {
|
||||
@Test
|
||||
void shouldIgnoreExceptionAndUseDefault() {
|
||||
try (MockedStatic<DefaultAwsRegionProviderChain> mocked = mockStatic(DefaultAwsRegionProviderChain.class)) {
|
||||
when(awsRegionProviderBuilder.build().getRegion())
|
||||
when(this.awsRegionProviderBuilder.build().getRegion())
|
||||
.thenThrow(SdkClientException.builder().message("failed load").build());
|
||||
mocked.when(DefaultAwsRegionProviderChain::builder).thenReturn(awsRegionProviderBuilder);
|
||||
mocked.when(DefaultAwsRegionProviderChain::builder).thenReturn(this.awsRegionProviderBuilder);
|
||||
BedrockProxyChatModel.builder().build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,7 +30,6 @@ import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Sinks;
|
||||
import reactor.core.publisher.Sinks.EmitFailureHandler;
|
||||
@@ -50,6 +49,7 @@ import software.amazon.awssdk.services.bedrockruntime.model.ResponseStream;
|
||||
|
||||
import org.springframework.ai.model.ModelOptionsUtils;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* Abstract class for the Bedrock API. It provides the basic functionality to invoke the chat completion model and
|
||||
@@ -322,6 +322,20 @@ public abstract class AbstractBedrockApi<I, O, SO> {
|
||||
return eventSink.asFlux();
|
||||
}
|
||||
|
||||
private Region getRegion(Region region) {
|
||||
if (ObjectUtils.isEmpty(region)) {
|
||||
try {
|
||||
return DefaultAwsRegionProviderChain.builder().build().getRegion();
|
||||
}
|
||||
catch (SdkClientException e) {
|
||||
throw new IllegalArgumentException("Region is empty and cannot be loaded from DefaultAwsRegionProviderChain: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
else {
|
||||
return region;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Encapsulates the metrics about the model invocation.
|
||||
* https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-claude.html
|
||||
@@ -341,16 +355,5 @@ public abstract class AbstractBedrockApi<I, O, SO> {
|
||||
@JsonProperty("invocationLatency") Long invocationLatency) {
|
||||
}
|
||||
|
||||
private Region getRegion(Region region) {
|
||||
if (ObjectUtils.isEmpty(region)) {
|
||||
try {
|
||||
return DefaultAwsRegionProviderChain.builder().build().getRegion();
|
||||
} catch (SdkClientException e) {
|
||||
throw new IllegalArgumentException("Region is empty and cannot be loaded from DefaultAwsRegionProviderChain: " + e.getMessage(), e);
|
||||
}
|
||||
} else {
|
||||
return region;
|
||||
}
|
||||
}
|
||||
}
|
||||
// @formatter:on
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.ai.bedrock.api;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
@@ -28,11 +30,11 @@ import software.amazon.awssdk.core.exception.SdkClientException;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.mockStatic;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class AbstractBedrockApiTest {
|
||||
@@ -49,10 +51,10 @@ class AbstractBedrockApiTest {
|
||||
@Test
|
||||
void shouldLoadRegionFromAwsDefaults() {
|
||||
try (MockedStatic<DefaultAwsRegionProviderChain> mocked = mockStatic(DefaultAwsRegionProviderChain.class)) {
|
||||
when(awsRegionProviderBuilder.build().getRegion()).thenReturn(Region.AF_SOUTH_1);
|
||||
mocked.when(DefaultAwsRegionProviderChain::builder).thenReturn(awsRegionProviderBuilder);
|
||||
when(this.awsRegionProviderBuilder.build().getRegion()).thenReturn(Region.AF_SOUTH_1);
|
||||
mocked.when(DefaultAwsRegionProviderChain::builder).thenReturn(this.awsRegionProviderBuilder);
|
||||
AbstractBedrockApi<Object, Object, Object> testBedrockApi = new TestBedrockApi("modelId",
|
||||
awsCredentialsProvider, null, objectMapper, Duration.ofMinutes(5));
|
||||
this.awsCredentialsProvider, null, this.objectMapper, Duration.ofMinutes(5));
|
||||
assertThat(testBedrockApi.getRegion()).isEqualTo(Region.AF_SOUTH_1);
|
||||
}
|
||||
}
|
||||
@@ -60,10 +62,10 @@ class AbstractBedrockApiTest {
|
||||
@Test
|
||||
void shouldThrowIllegalArgumentIfAwsDefaultsFailed() {
|
||||
try (MockedStatic<DefaultAwsRegionProviderChain> mocked = mockStatic(DefaultAwsRegionProviderChain.class)) {
|
||||
when(awsRegionProviderBuilder.build().getRegion())
|
||||
when(this.awsRegionProviderBuilder.build().getRegion())
|
||||
.thenThrow(SdkClientException.builder().message("failed load").build());
|
||||
mocked.when(DefaultAwsRegionProviderChain::builder).thenReturn(awsRegionProviderBuilder);
|
||||
assertThatThrownBy(() -> new TestBedrockApi("modelId", awsCredentialsProvider, null, objectMapper,
|
||||
mocked.when(DefaultAwsRegionProviderChain::builder).thenReturn(this.awsRegionProviderBuilder);
|
||||
assertThatThrownBy(() -> new TestBedrockApi("modelId", this.awsCredentialsProvider, null, this.objectMapper,
|
||||
Duration.ofMinutes(5)))
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessageContaining("failed load");
|
||||
|
||||
Reference in New Issue
Block a user