Skip to main content

Amazon Bedrock

Maven Dependency

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-bedrock</artifactId>
<version>1.18.0</version>
</dependency>

AWS credentials

In order to use Amazon Bedrock embeddings, you need to configure AWS credentials. One of the options is to set the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables. More information can be found here.

Cohere Models

  • BedrockCohereEmbeddingModel

Cohere Embedding Models

Support is provided for Bedrock Cohere embedding models, enabling the use of the following versions:

  • cohere.embed-english-v3
  • cohere.embed-multilingual-v3

These models are ideal for generating high-quality text embeddings for English and multilingual text processing tasks.

Implementation Example

Below is an example of how to configure and use a Bedrock embedding model:

BedrockCohereEmbeddingModel embeddingModel = BedrockCohereEmbeddingModel
.builder()
.region(Region.US_EAST_1)
.model("cohere.embed-multilingual-v3")
.inputType(BedrockCohereEmbeddingModel.InputType.SEARCH_QUERY)
.truncation(BedrockCohereEmbeddingModel.Truncate.NONE)
.build();

APIs

  • BedrockTitanEmbeddingModel
  • BedrockCohereEmbeddingModel

Titan Multimodal Embeddings

BedrockTitanEmbeddingModel supports Amazon Titan Multimodal Embeddings (amazon.titan-embed-image-v1): it embeds text and/or a single image into one (fused) embedding.

EmbeddingModel model = BedrockTitanEmbeddingModel.builder()
.model("amazon.titan-embed-image-v1")
.region(Region.US_EAST_1)
.build();

EmbeddingResponse response = model.embed(EmbeddingRequest.builder()
.input(TextContent.from("a photo of a cat"), ImageContent.from(base64Data, "image/png"))
.build());

Titan requires base64 image data (a URL is not supported). Listeners can be configured via .listeners(...). See Embedding Model for the request/response API.

Examples