AWS Bedrock integration for LangChain4j enabling Java applications to interact with various LLM providers through a unified interface
Configure AWS credentials and permissions for Bedrock access.
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-bedrock</artifactId>
<version>1.11.0</version>
</dependency>The integration uses AWS SDK for Java v2, which discovers credentials through the default credential provider chain:
AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEYaws.accessKeyId, aws.secretAccessKey~/.aws/credentialsexport AWS_ACCESS_KEY_ID=your_access_key
export AWS_SECRET_ACCESS_KEY=your_secret_key
export AWS_DEFAULT_REGION=us-east-1Create ~/.aws/credentials:
[default]
aws_access_key_id = your_access_key
aws_secret_access_key = your_secret_keyCreate ~/.aws/config:
[default]
region = us-east-1import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeClient;
AwsBasicCredentials credentials = AwsBasicCredentials.create("accessKey", "secretKey");
BedrockRuntimeClient client = BedrockRuntimeClient.builder()
.region(Region.US_EAST_1)
.credentialsProvider(StaticCredentialsProvider.create(credentials))
.build();
BedrockChatModel model = BedrockChatModel.builder()
.client(client)
.modelId("anthropic.claude-3-5-sonnet-20241022-v2:0")
.build();{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"bedrock:InvokeModel",
"bedrock:InvokeModelWithResponseStream"
],
"Resource": "arn:aws:bedrock:*::foundation-model/*"
}
]
}{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"bedrock:InvokeModel",
"bedrock:InvokeModelWithResponseStream",
"bedrock:ApplyGuardrail"
],
"Resource": [
"arn:aws:bedrock:*::foundation-model/*",
"arn:aws:bedrock:*:*:guardrail/*"
]
}
]
}{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"bedrock:InvokeModel",
"bedrock:InvokeModelWithResponseStream"
],
"Resource": [
"arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-3-5-sonnet-20241022-v2:0",
"arn:aws:bedrock:us-east-1::foundation-model/amazon.nova-pro-v1:0"
]
}
]
}AWS Bedrock is available in select regions:
Common regions:
us-east-1 (US East, N. Virginia)us-west-2 (US West, Oregon)eu-central-1 (Europe, Frankfurt)ap-southeast-1 (Asia Pacific, Singapore)Setting region:
import software.amazon.awssdk.regions.Region;
BedrockChatModel model = BedrockChatModel.builder()
.region(Region.US_EAST_1)
.modelId("anthropic.claude-3-5-sonnet-20241022-v2:0")
.build();Some models require requesting access in the AWS Bedrock console:
import java.time.Duration;
BedrockChatModel model = BedrockChatModel.builder()
.modelId("anthropic.claude-3-5-sonnet-20241022-v2:0")
.timeout(Duration.ofSeconds(120))
.build();BedrockChatModel model = BedrockChatModel.builder()
.modelId("anthropic.claude-3-5-sonnet-20241022-v2:0")
.maxRetries(3)
.build();import software.amazon.awssdk.http.apache.ApacheHttpClient;
BedrockRuntimeClient client = BedrockRuntimeClient.builder()
.region(Region.US_EAST_1)
.httpClientBuilder(ApacheHttpClient.builder()
.maxConnections(100)
.connectionTimeout(Duration.ofSeconds(10))
)
.build();
BedrockChatModel model = BedrockChatModel.builder()
.client(client)
.modelId("anthropic.claude-3-5-sonnet-20241022-v2:0")
.build();Add to logback.xml:
<logger name="software.amazon.awssdk" level="DEBUG"/>BedrockChatModel model = BedrockChatModel.builder()
.modelId("anthropic.claude-3-5-sonnet-20241022-v2:0")
.logRequests(true) // WARNING: Logs entire request including sensitive data
.logResponses(true)
.build();Warning: Request logging will log the entire request body, including messages and potentially sensitive information. Use only in development environments.
AccessDeniedException:
ThrottlingException:
ResourceNotFoundException:
Related: