Skip to content

Commit 563f9b4

Browse files
author
wmz7year
committed
Bedrock client autoconfiguration support external AwsCredentialsProvider and AwsRegionProvider
1 parent 65e6880 commit 563f9b4

File tree

20 files changed

+404
-24
lines changed

20 files changed

+404
-24
lines changed

models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/anthropic/api/AnthropicChatBedrockApi.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.fasterxml.jackson.databind.ObjectMapper;
2525
import reactor.core.publisher.Flux;
2626
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
27+
import software.amazon.awssdk.regions.Region;
2728

2829
import org.springframework.ai.bedrock.anthropic.api.AnthropicChatBedrockApi.AnthropicChatRequest;
2930
import org.springframework.ai.bedrock.anthropic.api.AnthropicChatBedrockApi.AnthropicChatResponse;
@@ -32,6 +33,7 @@
3233

3334
/**
3435
* @author Christian Tzolov
36+
* @author Wei Jiang
3537
* @since 0.8.0
3638
*/
3739
// @formatter:off
@@ -92,6 +94,20 @@ public AnthropicChatBedrockApi(String modelId, AwsCredentialsProvider credential
9294
super(modelId, credentialsProvider, region, objectMapper, timeout);
9395
}
9496

97+
/**
98+
* Create a new AnthropicChatBedrockApi instance using the provided credentials provider, region and object mapper.
99+
*
100+
* @param modelId The model id to use. See the {@link AnthropicChatModel} for the supported models.
101+
* @param credentialsProvider The credentials provider to connect to AWS.
102+
* @param region The AWS region to use.
103+
* @param objectMapper The object mapper to use for JSON serialization and deserialization.
104+
* @param timeout The timeout to use.
105+
*/
106+
public AnthropicChatBedrockApi(String modelId, AwsCredentialsProvider credentialsProvider, Region region,
107+
ObjectMapper objectMapper, Duration timeout) {
108+
super(modelId, credentialsProvider, region, objectMapper, timeout);
109+
}
110+
95111
// https://github.com/build-on-aws/amazon-bedrock-java-examples/blob/main/example_code/bedrock-runtime/src/main/java/aws/community/examples/InvokeBedrockStreamingAsync.java
96112

97113
// https://docs.anthropic.com/claude/reference/complete_post

models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/anthropic3/api/Anthropic3ChatBedrockApi.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.util.Assert;
2727
import reactor.core.publisher.Flux;
2828
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
29+
import software.amazon.awssdk.regions.Region;
2930

3031
import java.time.Duration;
3132
import java.util.List;
@@ -39,6 +40,7 @@
3940
*
4041
* @author Ben Middleton
4142
* @author Christian Tzolov
43+
* @author Wei Jiang
4244
* @since 1.0.0
4345
*/
4446
// @formatter:off
@@ -96,6 +98,20 @@ public Anthropic3ChatBedrockApi(String modelId, AwsCredentialsProvider credentia
9698
super(modelId, credentialsProvider, region, objectMapper, timeout);
9799
}
98100

101+
/**
102+
* Create a new AnthropicChatBedrockApi instance using the provided credentials provider, region and object mapper.
103+
*
104+
* @param modelId The model id to use. See the {@link AnthropicChatModel} for the supported models.
105+
* @param credentialsProvider The credentials provider to connect to AWS.
106+
* @param region The AWS region to use.
107+
* @param objectMapper The object mapper to use for JSON serialization and deserialization.
108+
* @param timeout The timeout to use.
109+
*/
110+
public Anthropic3ChatBedrockApi(String modelId, AwsCredentialsProvider credentialsProvider, Region region,
111+
ObjectMapper objectMapper, Duration timeout) {
112+
super(modelId, credentialsProvider, region, objectMapper, timeout);
113+
}
114+
99115
// https://github.com/build-on-aws/amazon-bedrock-java-examples/blob/main/example_code/bedrock-runtime/src/main/java/aws/community/examples/InvokeBedrockStreamingAsync.java
100116

101117
// https://docs.anthropic.com/claude/reference/complete_post

models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/api/AbstractBedrockApi.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
* @see <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters.html">Model Parameters</a>
6262
6363
* @author Christian Tzolov
64+
* @author Wei Jiang
6465
* @since 0.8.0
6566
*/
6667
public abstract class AbstractBedrockApi<I, O, SO> {
@@ -69,7 +70,7 @@ public abstract class AbstractBedrockApi<I, O, SO> {
6970

7071
private final String modelId;
7172
private final ObjectMapper objectMapper;
72-
private final String region;
73+
private final Region region;
7374
private final BedrockRuntimeClient client;
7475
private final BedrockRuntimeAsyncClient clientStreaming;
7576

@@ -93,7 +94,7 @@ public AbstractBedrockApi(String modelId, String region, Duration timeout) {
9394
this(modelId, ProfileCredentialsProvider.builder().build(), region, ModelOptionsUtils.OBJECT_MAPPER, timeout);
9495
}
9596

96-
/**
97+
/**
9798
* Create a new AbstractBedrockApi instance using the provided credentials provider, region and object mapper.
9899
*
99100
* @param modelId The model id to use.
@@ -105,6 +106,7 @@ public AbstractBedrockApi(String modelId, AwsCredentialsProvider credentialsProv
105106
ObjectMapper objectMapper) {
106107
this(modelId, credentialsProvider, region, objectMapper, Duration.ofMinutes(5));
107108
}
109+
108110
/**
109111
* Create a new AbstractBedrockApi instance using the provided credentials provider, region and object mapper.
110112
*
@@ -118,10 +120,26 @@ public AbstractBedrockApi(String modelId, AwsCredentialsProvider credentialsProv
118120
*/
119121
public AbstractBedrockApi(String modelId, AwsCredentialsProvider credentialsProvider, String region,
120122
ObjectMapper objectMapper, Duration timeout) {
123+
this(modelId, credentialsProvider, Region.of(region), objectMapper, timeout);
124+
}
125+
126+
/**
127+
* Create a new AbstractBedrockApi instance using the provided credentials provider, region and object mapper.
128+
*
129+
* @param modelId The model id to use.
130+
* @param credentialsProvider The credentials provider to connect to AWS.
131+
* @param region The AWS region to use.
132+
* @param objectMapper The object mapper to use for JSON serialization and deserialization.
133+
* @param timeout Configure the amount of time to allow the client to complete the execution of an API call.
134+
* This timeout covers the entire client execution except for marshalling. This includes request handler execution,
135+
* all HTTP requests including retries, unmarshalling, etc. This value should always be positive, if present.
136+
*/
137+
public AbstractBedrockApi(String modelId, AwsCredentialsProvider credentialsProvider, Region region,
138+
ObjectMapper objectMapper, Duration timeout) {
121139

122140
Assert.hasText(modelId, "Model id must not be empty");
123141
Assert.notNull(credentialsProvider, "Credentials provider must not be null");
124-
Assert.hasText(region, "Region must not be empty");
142+
Assert.notNull(region, "Region must not be empty");
125143
Assert.notNull(objectMapper, "Object mapper must not be null");
126144
Assert.notNull(timeout, "Timeout must not be null");
127145

@@ -131,13 +149,13 @@ public AbstractBedrockApi(String modelId, AwsCredentialsProvider credentialsProv
131149

132150

133151
this.client = BedrockRuntimeClient.builder()
134-
.region(Region.of(this.region))
152+
.region(this.region)
135153
.credentialsProvider(credentialsProvider)
136154
.overrideConfiguration(c -> c.apiCallTimeout(timeout))
137155
.build();
138156

139157
this.clientStreaming = BedrockRuntimeAsyncClient.builder()
140-
.region(Region.of(this.region))
158+
.region(this.region)
141159
.credentialsProvider(credentialsProvider)
142160
.overrideConfiguration(c -> c.apiCallTimeout(timeout))
143161
.build();
@@ -153,7 +171,7 @@ public String getModelId() {
153171
/**
154172
* @return The AWS region.
155173
*/
156-
public String getRegion() {
174+
public Region getRegion() {
157175
return this.region;
158176
}
159177

models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/cohere/api/CohereChatBedrockApi.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.fasterxml.jackson.databind.ObjectMapper;
2626
import reactor.core.publisher.Flux;
2727
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
28+
import software.amazon.awssdk.regions.Region;
2829

2930
import org.springframework.ai.bedrock.api.AbstractBedrockApi;
3031
import org.springframework.ai.bedrock.cohere.api.CohereChatBedrockApi.CohereChatRequest;
@@ -36,6 +37,7 @@
3637
* https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-cohere.html
3738
*
3839
* @author Christian Tzolov
40+
* @author Wei Jiang
3941
* @since 0.8.0
4042
*/
4143
public class CohereChatBedrockApi extends
@@ -91,6 +93,20 @@ public CohereChatBedrockApi(String modelId, AwsCredentialsProvider credentialsPr
9193
super(modelId, credentialsProvider, region, objectMapper, timeout);
9294
}
9395

96+
/**
97+
* Create a new CohereChatBedrockApi instance using the provided credentials provider, region and object mapper.
98+
*
99+
* @param modelId The model id to use. See the {@link CohereChatModel} for the supported models.
100+
* @param credentialsProvider The credentials provider to connect to AWS.
101+
* @param region The AWS region to use.
102+
* @param objectMapper The object mapper to use for JSON serialization and deserialization.
103+
* @param timeout The timeout to use.
104+
*/
105+
public CohereChatBedrockApi(String modelId, AwsCredentialsProvider credentialsProvider, Region region,
106+
ObjectMapper objectMapper, Duration timeout) {
107+
super(modelId, credentialsProvider, region, objectMapper, timeout);
108+
}
109+
94110
/**
95111
* CohereChatRequest encapsulates the request parameters for the Cohere command model.
96112
*

models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/cohere/api/CohereEmbeddingBedrockApi.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.fasterxml.jackson.annotation.JsonProperty;
2525
import com.fasterxml.jackson.databind.ObjectMapper;
2626
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
27+
import software.amazon.awssdk.regions.Region;
2728

2829
import org.springframework.ai.bedrock.api.AbstractBedrockApi;
2930
import org.springframework.ai.bedrock.cohere.api.CohereEmbeddingBedrockApi.CohereEmbeddingRequest;
@@ -34,6 +35,7 @@
3435
* https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-cohere.html#model-parameters-embed
3536
*
3637
* @author Christian Tzolov
38+
* @author Wei Jiang
3739
* @since 0.8.0
3840
*/
3941
public class CohereEmbeddingBedrockApi extends
@@ -91,6 +93,21 @@ public CohereEmbeddingBedrockApi(String modelId, AwsCredentialsProvider credenti
9193
super(modelId, credentialsProvider, region, objectMapper, timeout);
9294
}
9395

96+
/**
97+
* Create a new CohereEmbeddingBedrockApi instance using the provided credentials provider, region and object
98+
* mapper.
99+
*
100+
* @param modelId The model id to use. See the {@link CohereEmbeddingModel} for the supported models.
101+
* @param credentialsProvider The credentials provider to connect to AWS.
102+
* @param region The AWS region to use.
103+
* @param objectMapper The object mapper to use for JSON serialization and deserialization.
104+
* @param timeout The timeout to use.
105+
*/
106+
public CohereEmbeddingBedrockApi(String modelId, AwsCredentialsProvider credentialsProvider, Region region,
107+
ObjectMapper objectMapper, Duration timeout) {
108+
super(modelId, credentialsProvider, region, objectMapper, timeout);
109+
}
110+
94111
/**
95112
* The Cohere Embed model request.
96113
*

models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/jurassic2/api/Ai21Jurassic2ChatBedrockApi.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@
2929
import org.springframework.ai.bedrock.jurassic2.api.Ai21Jurassic2ChatBedrockApi.Ai21Jurassic2ChatResponse;
3030

3131
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
32+
import software.amazon.awssdk.regions.Region;
3233

3334
/**
3435
* Java client for the Bedrock Jurassic2 chat model.
3536
* https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-jurassic2.html
3637
*
3738
* @author Christian Tzolov
39+
* @author Wei Jiang
3840
* @since 0.8.0
3941
*/
4042
public class Ai21Jurassic2ChatBedrockApi extends
@@ -92,6 +94,20 @@ public Ai21Jurassic2ChatBedrockApi(String modelId, AwsCredentialsProvider creden
9294
super(modelId, credentialsProvider, region, objectMapper, timeout);
9395
}
9496

97+
/**
98+
* Create a new Ai21Jurassic2ChatBedrockApi instance.
99+
*
100+
* @param modelId The model id to use. See the {@link Ai21Jurassic2ChatBedrockApi.Ai21Jurassic2ChatModel} for the supported models.
101+
* @param credentialsProvider The credentials provider to connect to AWS.
102+
* @param region The AWS region to use.
103+
* @param objectMapper The object mapper to use for JSON serialization and deserialization.
104+
* @param timeout The timeout to use.
105+
*/
106+
public Ai21Jurassic2ChatBedrockApi(String modelId, AwsCredentialsProvider credentialsProvider, Region region,
107+
ObjectMapper objectMapper, Duration timeout) {
108+
super(modelId, credentialsProvider, region, objectMapper, timeout);
109+
}
110+
95111
/**
96112
* AI21 Jurassic2 chat request parameters.
97113
*

models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/llama2/api/Llama2ChatBedrockApi.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.fasterxml.jackson.databind.ObjectMapper;
2222
import reactor.core.publisher.Flux;
2323
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
24+
import software.amazon.awssdk.regions.Region;
2425

2526
import org.springframework.ai.bedrock.api.AbstractBedrockApi;
2627
import org.springframework.ai.bedrock.llama2.api.Llama2ChatBedrockApi.Llama2ChatRequest;
@@ -34,6 +35,7 @@
3435
* https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-meta.html
3536
*
3637
* @author Christian Tzolov
38+
* @author Wei Jiang
3739
* @since 0.8.0
3840
*/
3941
public class Llama2ChatBedrockApi extends
@@ -89,6 +91,20 @@ public Llama2ChatBedrockApi(String modelId, AwsCredentialsProvider credentialsPr
8991
super(modelId, credentialsProvider, region, objectMapper, timeout);
9092
}
9193

94+
/**
95+
* Create a new Llama2ChatBedrockApi instance using the provided credentials provider, region and object mapper.
96+
*
97+
* @param modelId The model id to use. See the {@link Llama2ChatModel} for the supported models.
98+
* @param credentialsProvider The credentials provider to connect to AWS.
99+
* @param region The AWS region to use.
100+
* @param objectMapper The object mapper to use for JSON serialization and deserialization.
101+
* @param timeout The timeout to use.
102+
*/
103+
public Llama2ChatBedrockApi(String modelId, AwsCredentialsProvider credentialsProvider, Region region,
104+
ObjectMapper objectMapper, Duration timeout) {
105+
super(modelId, credentialsProvider, region, objectMapper, timeout);
106+
}
107+
92108
/**
93109
* Llama2ChatRequest encapsulates the request parameters for the Meta Llama2 chat model.
94110
*

models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/titan/api/TitanChatBedrockApi.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.fasterxml.jackson.databind.ObjectMapper;
2525
import reactor.core.publisher.Flux;
2626
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
27+
import software.amazon.awssdk.regions.Region;
2728

2829
import org.springframework.ai.bedrock.api.AbstractBedrockApi;
2930
import org.springframework.ai.bedrock.titan.api.TitanChatBedrockApi.TitanChatRequest;
@@ -38,6 +39,7 @@
3839
* https://docs.aws.amazon.com/bedrock/latest/userguide/titan-text-models.html
3940
*
4041
* @author Christian Tzolov
42+
* @author Wei Jiang
4143
* @since 0.8.0
4244
*/
4345
// @formatter:off
@@ -92,6 +94,20 @@ public TitanChatBedrockApi(String modelId, AwsCredentialsProvider credentialsPro
9294
super(modelId, credentialsProvider, region, objectMapper, timeout);
9395
}
9496

97+
/**
98+
* Create a new TitanChatBedrockApi instance using the provided credentials provider, region and object mapper.
99+
*
100+
* @param modelId The model id to use. See the {@link TitanChatModel} for the supported models.
101+
* @param credentialsProvider The credentials provider to connect to AWS.
102+
* @param region The AWS region to use.
103+
* @param objectMapper The object mapper to use for JSON serialization and deserialization.
104+
* @param timeout The timeout to use.
105+
*/
106+
public TitanChatBedrockApi(String modelId, AwsCredentialsProvider credentialsProvider, Region region,
107+
ObjectMapper objectMapper, Duration timeout) {
108+
super(modelId, credentialsProvider, region, objectMapper, timeout);
109+
}
110+
95111
/**
96112
* TitanChatRequest encapsulates the request parameters for the Titan chat model.
97113
*

models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/titan/api/TitanEmbeddingBedrockApi.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.fasterxml.jackson.annotation.JsonProperty;
2424
import com.fasterxml.jackson.databind.ObjectMapper;
2525
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
26+
import software.amazon.awssdk.regions.Region;
2627

2728
import org.springframework.ai.bedrock.api.AbstractBedrockApi;
2829
import org.springframework.ai.bedrock.titan.api.TitanEmbeddingBedrockApi.TitanEmbeddingRequest;
@@ -34,6 +35,7 @@
3435
* https://docs.aws.amazon.com/bedrock/latest/userguide/titan-multiemb-models.html
3536
*
3637
* @author Christian Tzolov
38+
* @author Wei Jiang
3739
* @since 0.8.0
3840
*/
3941
// @formatter:off
@@ -65,6 +67,20 @@ public TitanEmbeddingBedrockApi(String modelId, AwsCredentialsProvider credentia
6567
super(modelId, credentialsProvider, region, objectMapper, timeout);
6668
}
6769

70+
/**
71+
* Create a new TitanEmbeddingBedrockApi instance.
72+
*
73+
* @param modelId The model id to use. See the {@link TitanEmbeddingModel} for the supported models.
74+
* @param credentialsProvider The credentials provider to connect to AWS.
75+
* @param region The AWS region to use.
76+
* @param objectMapper The object mapper to use for JSON serialization and deserialization.
77+
* @param timeout The timeout to use.
78+
*/
79+
public TitanEmbeddingBedrockApi(String modelId, AwsCredentialsProvider credentialsProvider, Region region,
80+
ObjectMapper objectMapper, Duration timeout) {
81+
super(modelId, credentialsProvider, region, objectMapper, timeout);
82+
}
83+
6884
/**
6985
* Titan Embedding request parameters.
7086
*

0 commit comments

Comments
 (0)