Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import reactor.core.publisher.Flux;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.regions.Region;

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

/**
* @author Christian Tzolov
* @author Wei Jiang
* @since 0.8.0
*/
// @formatter:off
Expand Down Expand Up @@ -92,6 +94,20 @@ public AnthropicChatBedrockApi(String modelId, AwsCredentialsProvider credential
super(modelId, credentialsProvider, region, objectMapper, timeout);
}

/**
* Create a new AnthropicChatBedrockApi instance using the provided credentials provider, region and object mapper.
*
* @param modelId The model id to use. See the {@link AnthropicChatModel} for the supported models.
* @param credentialsProvider The credentials provider to connect to AWS.
* @param region The AWS region to use.
* @param objectMapper The object mapper to use for JSON serialization and deserialization.
* @param timeout The timeout to use.
*/
public AnthropicChatBedrockApi(String modelId, AwsCredentialsProvider credentialsProvider, Region region,
ObjectMapper objectMapper, Duration timeout) {
super(modelId, credentialsProvider, region, objectMapper, timeout);
}

// https://github.com/build-on-aws/amazon-bedrock-java-examples/blob/main/example_code/bedrock-runtime/src/main/java/aws/community/examples/InvokeBedrockStreamingAsync.java

// https://docs.anthropic.com/claude/reference/complete_post
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.springframework.util.Assert;
import reactor.core.publisher.Flux;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.regions.Region;

import java.time.Duration;
import java.util.List;
Expand All @@ -39,6 +40,7 @@
*
* @author Ben Middleton
* @author Christian Tzolov
* @author Wei Jiang
* @since 1.0.0
*/
// @formatter:off
Expand Down Expand Up @@ -96,6 +98,20 @@ public Anthropic3ChatBedrockApi(String modelId, AwsCredentialsProvider credentia
super(modelId, credentialsProvider, region, objectMapper, timeout);
}

/**
* Create a new AnthropicChatBedrockApi instance using the provided credentials provider, region and object mapper.
*
* @param modelId The model id to use. See the {@link AnthropicChatModel} for the supported models.
* @param credentialsProvider The credentials provider to connect to AWS.
* @param region The AWS region to use.
* @param objectMapper The object mapper to use for JSON serialization and deserialization.
* @param timeout The timeout to use.
*/
public Anthropic3ChatBedrockApi(String modelId, AwsCredentialsProvider credentialsProvider, Region region,
ObjectMapper objectMapper, Duration timeout) {
super(modelId, credentialsProvider, region, objectMapper, timeout);
}

// https://github.com/build-on-aws/amazon-bedrock-java-examples/blob/main/example_code/bedrock-runtime/src/main/java/aws/community/examples/InvokeBedrockStreamingAsync.java

// https://docs.anthropic.com/claude/reference/complete_post
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
* @see <a href="https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters.html">Model Parameters</a>
* @author Christian Tzolov
* @author Wei Jiang
* @since 0.8.0
*/
public abstract class AbstractBedrockApi<I, O, SO> {
Expand All @@ -69,7 +70,7 @@ public abstract class AbstractBedrockApi<I, O, SO> {

private final String modelId;
private final ObjectMapper objectMapper;
private final String region;
private final Region region;
private final BedrockRuntimeClient client;
private final BedrockRuntimeAsyncClient clientStreaming;

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

/**
/**
* Create a new AbstractBedrockApi instance using the provided credentials provider, region and object mapper.
*
* @param modelId The model id to use.
Expand All @@ -105,6 +106,7 @@ public AbstractBedrockApi(String modelId, AwsCredentialsProvider credentialsProv
ObjectMapper objectMapper) {
this(modelId, credentialsProvider, region, objectMapper, Duration.ofMinutes(5));
}

/**
* Create a new AbstractBedrockApi instance using the provided credentials provider, region and object mapper.
*
Expand All @@ -118,10 +120,26 @@ public AbstractBedrockApi(String modelId, AwsCredentialsProvider credentialsProv
*/
public AbstractBedrockApi(String modelId, AwsCredentialsProvider credentialsProvider, String region,
ObjectMapper objectMapper, Duration timeout) {
this(modelId, credentialsProvider, Region.of(region), objectMapper, timeout);
}

/**
* Create a new AbstractBedrockApi instance using the provided credentials provider, region and object mapper.
*
* @param modelId The model id to use.
* @param credentialsProvider The credentials provider to connect to AWS.
* @param region The AWS region to use.
* @param objectMapper The object mapper to use for JSON serialization and deserialization.
* @param timeout Configure the amount of time to allow the client to complete the execution of an API call.
* This timeout covers the entire client execution except for marshalling. This includes request handler execution,
* all HTTP requests including retries, unmarshalling, etc. This value should always be positive, if present.
*/
public AbstractBedrockApi(String modelId, AwsCredentialsProvider credentialsProvider, Region region,
ObjectMapper objectMapper, Duration timeout) {

Assert.hasText(modelId, "Model id must not be empty");
Assert.notNull(credentialsProvider, "Credentials provider must not be null");
Assert.hasText(region, "Region must not be empty");
Assert.notNull(region, "Region must not be empty");
Assert.notNull(objectMapper, "Object mapper must not be null");
Assert.notNull(timeout, "Timeout must not be null");

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


this.client = BedrockRuntimeClient.builder()
.region(Region.of(this.region))
.region(this.region)
.credentialsProvider(credentialsProvider)
.overrideConfiguration(c -> c.apiCallTimeout(timeout))
.build();

this.clientStreaming = BedrockRuntimeAsyncClient.builder()
.region(Region.of(this.region))
.region(this.region)
.credentialsProvider(credentialsProvider)
.overrideConfiguration(c -> c.apiCallTimeout(timeout))
.build();
Expand All @@ -153,7 +171,7 @@ public String getModelId() {
/**
* @return The AWS region.
*/
public String getRegion() {
public Region getRegion() {
return this.region;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import reactor.core.publisher.Flux;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.regions.Region;

import org.springframework.ai.bedrock.api.AbstractBedrockApi;
import org.springframework.ai.bedrock.cohere.api.CohereChatBedrockApi.CohereChatRequest;
Expand All @@ -36,6 +37,7 @@
* https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-cohere.html
*
* @author Christian Tzolov
* @author Wei Jiang
* @since 0.8.0
*/
public class CohereChatBedrockApi extends
Expand Down Expand Up @@ -91,6 +93,20 @@ public CohereChatBedrockApi(String modelId, AwsCredentialsProvider credentialsPr
super(modelId, credentialsProvider, region, objectMapper, timeout);
}

/**
* Create a new CohereChatBedrockApi instance using the provided credentials provider, region and object mapper.
*
* @param modelId The model id to use. See the {@link CohereChatModel} for the supported models.
* @param credentialsProvider The credentials provider to connect to AWS.
* @param region The AWS region to use.
* @param objectMapper The object mapper to use for JSON serialization and deserialization.
* @param timeout The timeout to use.
*/
public CohereChatBedrockApi(String modelId, AwsCredentialsProvider credentialsProvider, Region region,
ObjectMapper objectMapper, Duration timeout) {
super(modelId, credentialsProvider, region, objectMapper, timeout);
}

/**
* CohereChatRequest encapsulates the request parameters for the Cohere command model.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.regions.Region;

import org.springframework.ai.bedrock.api.AbstractBedrockApi;
import org.springframework.ai.bedrock.cohere.api.CohereEmbeddingBedrockApi.CohereEmbeddingRequest;
Expand All @@ -34,6 +35,7 @@
* https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-cohere.html#model-parameters-embed
*
* @author Christian Tzolov
* @author Wei Jiang
* @since 0.8.0
*/
public class CohereEmbeddingBedrockApi extends
Expand Down Expand Up @@ -91,6 +93,21 @@ public CohereEmbeddingBedrockApi(String modelId, AwsCredentialsProvider credenti
super(modelId, credentialsProvider, region, objectMapper, timeout);
}

/**
* Create a new CohereEmbeddingBedrockApi instance using the provided credentials provider, region and object
* mapper.
*
* @param modelId The model id to use. See the {@link CohereEmbeddingModel} for the supported models.
* @param credentialsProvider The credentials provider to connect to AWS.
* @param region The AWS region to use.
* @param objectMapper The object mapper to use for JSON serialization and deserialization.
* @param timeout The timeout to use.
*/
public CohereEmbeddingBedrockApi(String modelId, AwsCredentialsProvider credentialsProvider, Region region,
ObjectMapper objectMapper, Duration timeout) {
super(modelId, credentialsProvider, region, objectMapper, timeout);
}

/**
* The Cohere Embed model request.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@
import org.springframework.ai.bedrock.jurassic2.api.Ai21Jurassic2ChatBedrockApi.Ai21Jurassic2ChatResponse;

import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.regions.Region;

/**
* Java client for the Bedrock Jurassic2 chat model.
* https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-jurassic2.html
*
* @author Christian Tzolov
* @author Wei Jiang
* @since 0.8.0
*/
public class Ai21Jurassic2ChatBedrockApi extends
Expand Down Expand Up @@ -92,6 +94,20 @@ public Ai21Jurassic2ChatBedrockApi(String modelId, AwsCredentialsProvider creden
super(modelId, credentialsProvider, region, objectMapper, timeout);
}

/**
* Create a new Ai21Jurassic2ChatBedrockApi instance.
*
* @param modelId The model id to use. See the {@link Ai21Jurassic2ChatBedrockApi.Ai21Jurassic2ChatModel} for the supported models.
* @param credentialsProvider The credentials provider to connect to AWS.
* @param region The AWS region to use.
* @param objectMapper The object mapper to use for JSON serialization and deserialization.
* @param timeout The timeout to use.
*/
public Ai21Jurassic2ChatBedrockApi(String modelId, AwsCredentialsProvider credentialsProvider, Region region,
ObjectMapper objectMapper, Duration timeout) {
super(modelId, credentialsProvider, region, objectMapper, timeout);
}

/**
* AI21 Jurassic2 chat request parameters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import reactor.core.publisher.Flux;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.regions.Region;

import org.springframework.ai.bedrock.api.AbstractBedrockApi;
import org.springframework.ai.bedrock.llama2.api.Llama2ChatBedrockApi.Llama2ChatRequest;
Expand All @@ -34,6 +35,7 @@
* https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-meta.html
*
* @author Christian Tzolov
* @author Wei Jiang
* @since 0.8.0
*/
public class Llama2ChatBedrockApi extends
Expand Down Expand Up @@ -89,6 +91,20 @@ public Llama2ChatBedrockApi(String modelId, AwsCredentialsProvider credentialsPr
super(modelId, credentialsProvider, region, objectMapper, timeout);
}

/**
* Create a new Llama2ChatBedrockApi instance using the provided credentials provider, region and object mapper.
*
* @param modelId The model id to use. See the {@link Llama2ChatModel} for the supported models.
* @param credentialsProvider The credentials provider to connect to AWS.
* @param region The AWS region to use.
* @param objectMapper The object mapper to use for JSON serialization and deserialization.
* @param timeout The timeout to use.
*/
public Llama2ChatBedrockApi(String modelId, AwsCredentialsProvider credentialsProvider, Region region,
ObjectMapper objectMapper, Duration timeout) {
super(modelId, credentialsProvider, region, objectMapper, timeout);
}

/**
* Llama2ChatRequest encapsulates the request parameters for the Meta Llama2 chat model.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import reactor.core.publisher.Flux;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.regions.Region;

import org.springframework.ai.bedrock.api.AbstractBedrockApi;
import org.springframework.ai.bedrock.titan.api.TitanChatBedrockApi.TitanChatRequest;
Expand All @@ -38,6 +39,7 @@
* https://docs.aws.amazon.com/bedrock/latest/userguide/titan-text-models.html
*
* @author Christian Tzolov
* @author Wei Jiang
* @since 0.8.0
*/
// @formatter:off
Expand Down Expand Up @@ -92,6 +94,20 @@ public TitanChatBedrockApi(String modelId, AwsCredentialsProvider credentialsPro
super(modelId, credentialsProvider, region, objectMapper, timeout);
}

/**
* Create a new TitanChatBedrockApi instance using the provided credentials provider, region and object mapper.
*
* @param modelId The model id to use. See the {@link TitanChatModel} for the supported models.
* @param credentialsProvider The credentials provider to connect to AWS.
* @param region The AWS region to use.
* @param objectMapper The object mapper to use for JSON serialization and deserialization.
* @param timeout The timeout to use.
*/
public TitanChatBedrockApi(String modelId, AwsCredentialsProvider credentialsProvider, Region region,
ObjectMapper objectMapper, Duration timeout) {
super(modelId, credentialsProvider, region, objectMapper, timeout);
}

/**
* TitanChatRequest encapsulates the request parameters for the Titan chat model.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.regions.Region;

import org.springframework.ai.bedrock.api.AbstractBedrockApi;
import org.springframework.ai.bedrock.titan.api.TitanEmbeddingBedrockApi.TitanEmbeddingRequest;
Expand All @@ -34,6 +35,7 @@
* https://docs.aws.amazon.com/bedrock/latest/userguide/titan-multiemb-models.html
*
* @author Christian Tzolov
* @author Wei Jiang
* @since 0.8.0
*/
// @formatter:off
Expand Down Expand Up @@ -65,6 +67,20 @@ public TitanEmbeddingBedrockApi(String modelId, AwsCredentialsProvider credentia
super(modelId, credentialsProvider, region, objectMapper, timeout);
}

/**
* Create a new TitanEmbeddingBedrockApi instance.
*
* @param modelId The model id to use. See the {@link TitanEmbeddingModel} for the supported models.
* @param credentialsProvider The credentials provider to connect to AWS.
* @param region The AWS region to use.
* @param objectMapper The object mapper to use for JSON serialization and deserialization.
* @param timeout The timeout to use.
*/
public TitanEmbeddingBedrockApi(String modelId, AwsCredentialsProvider credentialsProvider, Region region,
ObjectMapper objectMapper, Duration timeout) {
super(modelId, credentialsProvider, region, objectMapper, timeout);
}

/**
* Titan Embedding request parameters.
*
Expand Down
Loading