Cohere Embeddings
提供Bedrock Cohere Embedding模型。将生成式AI能力集成到核心应用程序和工作流程中,以提升业务成效。
AWS Bedrock Cohere 模型页面 和 Amazon Bedrock 用户指南 包含了关于如何使用 AWS 托管模型的详细信息。
先决条件
请参考Spring AI关于Amazon Bedrock的文档来设置API访问权限。
添加仓库与 BOM
Spring AI 的构件已发布在 Maven Central 和 Spring Snapshot 仓库中。请参阅构件仓库部分,将这些仓库添加到您的构建系统中。
为了便于依赖管理,Spring AI 提供了物料清单(BOM),以确保在整个项目中使用统一版本的 Spring AI。请参阅依赖管理部分,将 Spring AI BOM 添加到您的构建系统中。
自动配置
Spring AI 自动配置和 starter 模块的 artifact 名称发生了重大变更。更多信息请参考升级说明。
将 spring-ai-starter-model-bedrock 依赖项添加到项目的 Maven pom.xml 文件中:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-bedrock</artifactId>
</dependency>
或添加到你的 Gradle build.gradle 构建文件中。
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-model-bedrock'
}
:::提示
请参考依赖管理部分,将Spring AI BOM添加到您的构建文件中。
:::
启用 Cohere Embedding 支持
默认情况下,Cohere嵌入模型处于禁用状态。要启用它,请在您的应用程序配置中将 spring.ai.model.embedding 属性设置为 bedrock-cohere:
spring.ai.model.embedding=bedrock-cohere
或者,你也可以使用Spring表达式语言(SpEL)来引用环境变量:
# In application.yml
spring:
ai:
model:
embedding: ${AI_MODEL_EMBEDDING}
# In your environment or .env file
export AI_MODEL_EMBEDDING=bedrock-cohere
你也可以在启动应用程序时使用 Java 系统属性来设置此属性:
java -Dspring.ai.model.embedding=bedrock-cohere -jar your-application.jar
嵌入性质
spring.ai.bedrock.aws 是用于配置连接到 AWS Bedrock 的属性前缀。
| 属性 | 描述 | 默认值 |
|---|---|---|
| spring.ai.bedrock.aws.region | 使用的 AWS 区域。 | us-east-1 |
| spring.ai.bedrock.aws.access-key | AWS 访问密钥。 | - |
| spring.ai.bedrock.aws.secret-key | AWS 秘密密钥。 | - |
现在通过顶级属性 spring.ai.model.embedding 来配置嵌入自动配置的启用和禁用。
若要启用,spring.ai.model.embedding=bedrock-cohere(默认已启用)
若要禁用,spring.ai.model.embedding=none(或任何不匹配 bedrock-cohere 的值)
此项更改是为了支持配置多个模型。
前缀 spring.ai.bedrock.cohere.embedding(定义于 BedrockCohereEmbeddingProperties 中)是用于配置 Cohere 嵌入模型实现的属性前缀。
| 属性 | 描述 | 默认值 |
|---|---|---|
| spring.ai.model.embedding | 启用或禁用对 Cohere 的支持 | bedrock-cohere |
| spring.ai.bedrock.cohere.embedding.enabled (已移除且不再有效) | 启用或禁用对 Cohere 的支持 | false |
| spring.ai.bedrock.cohere.embedding.model | 要使用的模型 ID。有关支持的模型,请参阅 CohereEmbeddingModel。 | cohere.embed-multilingual-v3 |
| spring.ai.bedrock.cohere.embedding.options.input-type | 添加特殊标记以区分每种类型。不应混合使用不同类型,除非是混合用于搜索和检索的类型。在这种情况下,使用 search_document 类型嵌入语料库,并使用 search_query 类型嵌入查询。 | SEARCH_DOCUMENT |
| spring.ai.bedrock.cohere.embedding.options.truncate | 指定 API 如何处理超过最大 token 长度的输入。如果指定 LEFT 或 RIGHT,模型将丢弃输入,直到剩余输入恰好是模型的最大输入 token 长度。 | NONE |
通过 Amazon Bedrock 访问 Cohere 时,截断功能不可用。这是 Amazon Bedrock 的一个问题。Spring AI 类 BedrockCohereEmbeddingModel 会将输入截断至 2048 字符长度,这是模型支持的最大长度。
请参考 CohereEmbeddingModel 获取其他模型 ID。支持的值为:cohere.embed-multilingual-v3 和 cohere.embed-english-v3。模型 ID 值也可以在 AWS Bedrock 基础模型 ID 文档 中找到。
所有以 spring.ai.bedrock.cohere.embedding.options 为前缀的属性,都可以在运行时通过向 EmbeddingRequest 调用添加特定于请求的运行时选项来覆盖。
运行时选项
BedrockCohereEmbeddingOptions.java 提供了模型配置选项,例如 input-type 或 truncate。
在启动时,默认选项可通过BedrockCohereEmbeddingModel(api, options)构造函数或spring.ai.bedrock.cohere.embedding.options.*属性进行配置。
在运行时,你可以通过向 EmbeddingRequest 调用添加新的、特定于请求的选项来覆盖默认选项。例如,要覆盖特定请求的默认输入类型:
EmbeddingResponse embeddingResponse = embeddingModel.call(
new EmbeddingRequest(List.of("Hello World", "World is big and salvation is near"),
BedrockCohereEmbeddingOptions.builder()
.inputType(InputType.SEARCH_DOCUMENT)
.build()));
示例控制器
创建一个新的 Spring Boot 项目,并将 spring-ai-starter-model-bedrock 添加到你的 pom(或 gradle)依赖中。
在 src/main/resources 目录下添加 application.properties 文件,以启用并配置 Cohere Embedding 模型:
spring.ai.bedrock.aws.region=eu-central-1
spring.ai.bedrock.aws.access-key=${AWS_ACCESS_KEY_ID}
spring.ai.bedrock.aws.secret-key=${AWS_SECRET_ACCESS_KEY}
spring.ai.model.embedding=bedrock-cohere
spring.ai.bedrock.cohere.embedding.options.input-type=search-document
请将 regions、access-key 和 secret-key 替换为你的 AWS 凭据。
这将创建一个 BedrockCohereEmbeddingModel 实现,你可以将其注入到你的类中。下面是一个使用该聊天模型进行文本生成的简单 @Controller 类示例。
@RestController
public class EmbeddingController {
private final EmbeddingModel embeddingModel;
@Autowired
public EmbeddingController(EmbeddingModel embeddingModel) {
this.embeddingModel = embeddingModel;
}
@GetMapping("/ai/embedding")
public Map embed(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
EmbeddingResponse embeddingResponse = this.embeddingModel.embedForResponse(List.of(message));
return Map.of("embedding", embeddingResponse);
}
}
手动配置
BedrockCohereEmbeddingModel 实现了 EmbeddingModel 接口,并使用底层 CohereEmbeddingBedrockApi 客户端来连接 Bedrock Cohere 服务。
在您的项目 Maven pom.xml 文件中添加 spring-ai-bedrock 依赖:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bedrock</artifactId>
</dependency>
或添加到您的 Gradle build.gradle 构建文件中。
dependencies {
implementation 'org.springframework.ai:spring-ai-bedrock'
}
:::提示
请参考依赖管理部分,将Spring AI BOM添加到您的构建文件中。
:::
接下来,创建一个 BedrockCohereEmbeddingModel 并将其用于文本嵌入:
var cohereEmbeddingApi =new CohereEmbeddingBedrockApi(
CohereEmbeddingModel.COHERE_EMBED_MULTILINGUAL_V1.id(),
EnvironmentVariableCredentialsProvider.create(), Region.US_EAST_1.id(), new ObjectMapper());
var embeddingModel = new BedrockCohereEmbeddingModel(this.cohereEmbeddingApi);
EmbeddingResponse embeddingResponse = this.embeddingModel
.embedForResponse(List.of("Hello World", "World is big and salvation is near"));
低级 CohereEmbeddingBedrockApi 客户端
CohereEmbeddingBedrockApi 在 AWS Bedrock 的 Cohere Command 模型 之上提供了一个轻量级的 Java 客户端。
以下类图展示了CohereEmbeddingBedrockApi接口及其构建模块:

CohereEmbeddingBedrockApi支持cohere.embed-english-v3和cohere.embed-multilingual-v3模型,用于执行单条和批量的嵌入计算。
以下是一个关于如何以编程方式使用 API 的简单示例:
CohereEmbeddingBedrockApi api = new CohereEmbeddingBedrockApi(
CohereEmbeddingModel.COHERE_EMBED_MULTILINGUAL_V1.id(),
EnvironmentVariableCredentialsProvider.create(),
Region.US_EAST_1.id(), new ObjectMapper());
CohereEmbeddingRequest request = new CohereEmbeddingRequest(
List.of("I like to eat apples", "I like to eat oranges"),
CohereEmbeddingRequest.InputType.search_document,
CohereEmbeddingRequest.Truncate.NONE);
CohereEmbeddingResponse response = this.api.embedding(this.request);