Amazon Bedrock

Spring AI 支持通过 Amazon Bedrock 获得各种 AI 提供者的基础模型,从而简化了基于 AI 的应用程序开发。通过 Spring 自动配置和引导启动器,Spring AI 使得引导和配置 Bedrock 模型变得更加容易,从而使开发者能够快速轻松地集成 AI 功能。此外,本文还为在 Spring Boot 项目中连接和启用 Bedrock 模型提供了分步指南,包括设置 AWS 凭据和配置特定模型。

Amazon Bedrock 是一项托管服务,它提供来自各种 AI 提供商的基础模型,可通过统一的 API 获得。

Amazon Bedrock is a managed service that provides foundation models from various AI providers, available through a unified API.

Spring AI 支持通过实现 Spring 接口 ChatClientStreamingChatClientEmbeddingClient 在 Amazon Bedrock 中提供的 all the Chat and Embedding AI models

Spring AI supports all the Chat and Embedding AI models available through Amazon Bedrock by implementing the Spring interfaces ChatClient, StreamingChatClient, and EmbeddingClient.

此外,Spring AI 为所有客户端提供 Spring 自动配置和引导启动器,从而可以轻松地引导和配置 Bedrock 模型。

Additionally, Spring AI provides Spring Auto-Configurations and Boot Starters for all clients, making it easy to bootstrap and configure for the Bedrock models.

Getting Started

开始有几个步骤:

There are a few steps to get started

  • Add Boot Starter: Add the Spring Boot starter for Bedrock to your project.

  • Obtain AWS credentials: If you don’t have an AWS account and AWS CLI configured yet, this video guide can help you configure it: AWS CLI & SDK Setup in Less Than 4 Minutes!. You should be able to obtain your access and security keys.

  • Enable Bedrock models to use: Go to Amazon Bedrock and from the Model Access menu on the left, configure access to the models you are going to use.

Project Dependencies

然后将 Spring Boot Starter 依赖添加到项目的 Maven pom.xml 构建文件中:

Then add the Spring Boot Starter dependency to your project’s Maven pom.xml build file:

<dependency>
 <artifactId>spring-ai-bedrock-ai-spring-boot-starter</artifactId>
 <groupId>org.springframework.ai</groupId>
</dependency>

或添加到 Gradle build.gradle 构建文件中。

or to your Gradle build.gradle build file.

dependencies {
    implementation 'org.springframework.ai:spring-ai-bedrock-ai-spring-boot-starter'
}
  1. 参见 Dependency Management 部分,将 Spring AI BOM 添加到你的构建文件中。

Refer to the Dependency Management section to add the Spring AI BOM to your build file.

Connect to AWS Bedrock

使用 BedrockAwsConnectionProperties 配置 AWS 凭据和区域:

Use the BedrockAwsConnectionProperties to configure AWS credentials and region:

spring.ai.bedrock.aws.region=us-east-1

spring.ai.bedrock.aws.access-key=YOUR_ACCESS_KEY
spring.ai.bedrock.aws.secret-key=YOUR_SECRET_KEY

region 属性是强制的。

The region property is compulsory.

AWS 凭证按以下顺序解析:

AWS credentials are resolved in the following order:

  1. Spring-AI Bedrock spring.ai.bedrock.aws.access-key and spring.ai.bedrock.aws.secret-key properties.

  2. Java System Properties - aws.accessKeyId and aws.secretAccessKey.

  3. Environment Variables - AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.

  4. Web Identity Token credentials from system properties or environment variables.

  5. Credential profiles file at the default location (~/.aws/credentials) shared by all AWS SDKs and the AWS CLI.

  6. Credentials delivered through the Amazon EC2 container service if the AWS_CONTAINER_CREDENTIALS_RELATIVE_URI environment variable is set and the security manager has permission to access the variable.

  7. Instance profile credentials delivered through the Amazon EC2 metadata service or set the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables.

Enable selected Bedrock model

默认情况下,所有模型都处于禁用状态。您必须使用 spring.ai.bedrock.<model>.<chat|embedding>.enabled=true 属性显式启用选定的 Bedrock 模型。

By default, all models are disabled. You have to enable the chosen Bedrock models explicitly using the spring.ai.bedrock.<model>.<chat|embedding>.enabled=true property.

以下是支持的 <model><chat|embedding> 组合:

Here are the supported <model> and <chat|embedding> combinations:

Model Chat Chat Streaming Embedding

llama2

Yes

Yes

No

cohere

Yes

Yes

Yes

anthropic

Yes

Yes

No

jurassic2 (WIP)

Yes

No

No

titan

Yes

Yes

Yes (however, no batch support)

例如,要启用 Bedrock Llama2 聊天客户端,您需要设置 spring.ai.bedrock.llama2.chat.enabled=true

For example, to enable the Bedrock Llama2 Chat client, you need to set spring.ai.bedrock.llama2.chat.enabled=true.

接下来,您可以使用 spring.ai.bedrock.<model>.<chat|embedding>.* 属性按提供的方式配置每个模型。

Next, you can use the spring.ai.bedrock.<model>.<chat|embedding>.* properties to configure each model as provided.

有关更多信息,请参阅下面所列每个支持的模型的文档。

For more information, refer to the documentation below for each supported model.