Amazon Bedrock
遵循 Bedrock 的建议,Spring AI 转换为在 Spring AI 的所有聊天对话实现中使用 Amazon Bedrock 的 Converse API。 Bedrock Converse API 具有以下主要优点: Following the Bedrock recommendations, Spring AI transitioned to using Amazon Bedrock’s Converse API for all Chat conversation implementations in Spring AI. The Bedrock Converse API has the following key benefits:
Converse API 不支持嵌入操作,因此这些操作将保留在当前 API 中,并且现有 The Converse API does not support embedding operations, so these will remain in the current API and the embedding model functionality in the existing |
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 EmbeddingModel
接口来支持通过 Amazon Bedrock 提供的 the Embedding AI models 。
Spring AI supports the Embedding AI models available through Amazon Bedrock by implementing the Spring EmbeddingModel
interface.
此外,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
-
将 Bedrock 的 Spring Boot Starter 添加到您的项目中。
-
Add the Spring Boot starter for Bedrock to your project.
-
获取 AWS 凭据:如果您还没有 AWS 账户和 AWS CLI 配置,此视频指南可以帮助您配置: AWS CLI & SDK Setup in Less Than 4 Minutes! 。您应该能够获取您的访问密钥和安全密钥。
-
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.
-
启用要使用的模型:转到 Amazon Bedrock ,然后从左侧的 Model Access 菜单中,配置对您将要使用的模型的访问权限。
-
Enable the 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-starter-model-bedrock</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-starter-model-bedrock'
}
|
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
spring.ai.bedrock.aws.timeout=10m
region
属性是强制的。
The region
property is compulsory.
AWS 凭证按以下顺序解析:
AWS credentials are resolved in the following order:
-
Spring-AI Bedrock
spring.ai.bedrock.aws.access-key
和spring.ai.bedrock.aws.secret-key
属性。 -
Spring-AI Bedrock
spring.ai.bedrock.aws.access-key
andspring.ai.bedrock.aws.secret-key
properties. -
Java 系统属性 -
aws.accessKeyId
和aws.secretAccessKey
。 -
Java System Properties -
aws.accessKeyId
andaws.secretAccessKey
. -
环境变量 -
AWS_ACCESS_KEY_ID
和AWS_SECRET_ACCESS_KEY
。 -
Environment Variables -
AWS_ACCESS_KEY_ID
andAWS_SECRET_ACCESS_KEY
. -
来自系统属性或环境变量的 Web Identity Token 凭证。
-
Web Identity Token credentials from system properties or environment variables.
-
位于默认位置 (
~/.aws/credentials
) 的凭证配置文件,由所有 AWS SDK 和 AWS CLI 共享。 -
Credential profiles file at the default location (
~/.aws/credentials
) shared by all AWS SDKs and the AWS CLI. -
如果
AWS_CONTAINER_CREDENTIALS_RELATIVE_URI
环境变量已设置,并且安全管理器有权限访问该变量,则通过 Amazon EC2 容器服务传递的凭证。 -
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. -
通过 Amazon EC2 元数据服务传递的实例配置文件凭证或者设置
AWS_ACCESS_KEY_ID
和AWS_SECRET_ACCESS_KEY
环境变量。 -
Instance profile credentials delivered through the Amazon EC2 metadata service or set the
AWS_ACCESS_KEY_ID
andAWS_SECRET_ACCESS_KEY
environment variables.
AWS 区域按以下顺序解析:
AWS region is resolved in the following order:
-
Spring-AI Bedrock
spring.ai.bedrock.aws.region
property. -
Java 系统属性 -
aws.region
。 -
Java System Properties -
aws.region
. -
Environment Variables -
AWS_REGION
. -
位于默认位置 (
~/.aws/credentials
) 的凭证配置文件,由所有 AWS SDK 和 AWS CLI 共享。 -
Credential profiles file at the default location (
~/.aws/credentials
) shared by all AWS SDKs and the AWS CLI. -
通过 Amazon EC2 元数据服务提供的实例配置文件区域。
-
Instance profile region delivered through the Amazon EC2 metadata service.
除了标准的 Spring-AI Bedrock 凭证和区域属性配置外,Spring-AI 还支持自定义 AwsCredentialsProvider
和 AwsRegionProvider
bean。
In addition to the standard Spring-AI Bedrock credentials and region properties configuration, Spring-AI provides support for custom AwsCredentialsProvider
and AwsRegionProvider
beans.
例如,同时使用 Spring-AI 和 Spring Cloud for Amazon Web Services 。Spring-AI 与 Spring Cloud for Amazon Web Services 凭证配置兼容。 |
For example, using Spring-AI and Spring Cloud for Amazon Web Services at the same time. Spring-AI is compatible with Spring Cloud for Amazon Web Services credential configuration. |
Enable selected Bedrock model
默认情况下,所有模型都已禁用。您必须使用 |
By default, all models are disabled. You have to enable the chosen Bedrock models explicitly using the |
以下是支持的 <model>
:
Here are the supported `<model>`s:
Model |
cohere |
titan (no batch support yet) |
例如,要启用 Bedrock Cohere 嵌入模型,您需要设置 spring.ai.bedrock.cohere.embedding.enabled=true
。
For example, to enable the Bedrock Cohere embedding model, you need to set spring.ai.bedrock.cohere.embedding.enabled=true
.
接下来,您可以使用 spring.ai.bedrock.<model>.embedding.*
属性来配置每个模型。
Next, you can use the spring.ai.bedrock.<model>.embedding.*
properties to configure each model as provided.
有关更多信息,请参阅下面所列每个支持的模型的文档。
For more information, refer to the documentation below for each supported model.
-
Spring AI Bedrock Cohere Embeddings:
spring.ai.bedrock.cohere.embedding.enabled=true
-
Spring AI Bedrock Titan Embeddings:
spring.ai.bedrock.titan.embedding.enabled=true