此版本仍在开发中,尚未被视为稳定版。为了获取最新的快照版本,请使用Spring AI 1.1.3spring-doc.cadn.net.cn

Cohere 嵌入

提供 Bedrock Cohere 嵌入模型。 将生成式 AI 功能集成到关键应用程序和工作流中,以提升业务成果。spring-doc.cadn.net.cn

The AWS Bedrock Cohere 模型页面Amazon Bedrock 用户指南包含有关如何使用 AWS 托管模型的详细信息。spring-doc.cadn.net.cn

前提条件

请参阅Amazon Bedrock 上的 Spring AI 文档以设置 API 访问权限。spring-doc.cadn.net.cn

添加仓库和BOM

Spring AI 工件发布在 Maven Central 和 Spring Snapshot 仓库中。 请参阅 工件仓库 部分,以将这些仓库添加到您的构建系统。spring-doc.cadn.net.cn

为了帮助管理依赖,Spring AI 提供了一个 BOM(物料清单),以确保在整个项目中使用一致的 Spring AI 版本。请参阅 依赖管理 部分,将 Spring AI BOM 添加到您的构建系统。spring-doc.cadn.net.cn

自动配置

There has been a significant change in the Spring AI auto-configuration, starter modules' artifact names. Please refer to the 升级说明以获取更多信息。spring-doc.cadn.net.cn

spring-ai-starter-model-bedrock 依赖添加到您项目的 Maven pom.xml 文件中:spring-doc.cadn.net.cn

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

或者添加到您的Gradle 构建脚本文件中。spring-doc.cadn.net.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-starter-model-bedrock'
}
参考以下依赖管理部分,添加Spring AI BOM到你的构建文件中。

启用 Cohere 嵌入支持

默认情况下,Cohere嵌入模型是禁用的。 要启用它,请在应用程序配置中将spring.ai.model.embedding属性设置为bedrock-coherespring-doc.cadn.net.cn

spring.ai.model.embedding=bedrock-cohere

或者,您也可以使用 Spring 表达式语言 (SpEL) 来引用环境变量:spring-doc.cadn.net.cn

# In application.yml
spring:
  ai:
    model:
      embedding: ${AI_MODEL_EMBEDDING}
# In your environment or .env file
export AI_MODEL_EMBEDDING=bedrock-cohere

您也可以在启动应用程序时通过 Java 系统属性设置此属性:spring-doc.cadn.net.cn

java -Dspring.ai.model.embedding=bedrock-cohere -jar your-application.jar

嵌入属性

前缀 spring.ai.bedrock.aws 是配置到亚马逊 Bedrock 连接的属性前缀。spring-doc.cadn.net.cn

属性 描述 默认

spring.ai.bedrock.aws.regionspring-doc.cadn.net.cn

要使用的 AWS 区域。spring-doc.cadn.net.cn

us-east-1spring-doc.cadn.net.cn

spring.ai.bedrock.aws.access-keyspring-doc.cadn.net.cn

AWS访问密钥。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.bedrock.aws.secret-keyspring-doc.cadn.net.cn

AWS 密钥。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.bedrock.aws.profile.namespring-doc.cadn.net.cn

AWS配置文件名。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.bedrock.aws.profile.credentials-pathspring-doc.cadn.net.cn

AWS凭证文件路径。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.bedrock.aws.profile.configuration-pathspring-doc.cadn.net.cn

AWS 配置文件的路径。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

启用和禁用嵌入式自动配置现在通过带有前缀 spring.ai.model.embedding 的顶级属性进行配置。spring-doc.cadn.net.cn

要启用,spring.ai.model.embedding=bedrock-cohere(默认已启用)spring-doc.cadn.net.cn

要禁用,spring.ai.model.embedding=none(或任何与 bedrock-cohere 不匹配的值)spring-doc.cadn.net.cn

这种修改是为了允许配置多个模型。spring-doc.cadn.net.cn

前缀 spring.ai.bedrock.cohere.embedding(在 BedrockCohereEmbeddingProperties 中定义)是用于配置 Cohere 嵌入模型实现的属性前缀。spring-doc.cadn.net.cn

属性spring-doc.cadn.net.cn

描述spring-doc.cadn.net.cn

默认spring-doc.cadn.net.cn

spring.ai.model.embeddingspring-doc.cadn.net.cn

启用或禁用对 Cohere 的支持spring-doc.cadn.net.cn

bedrock-coherespring-doc.cadn.net.cn

spring.ai.bedrock.cohere.embedding.enabled(已移除且不再有效)spring-doc.cadn.net.cn

启用或禁用对 Cohere 的支持spring-doc.cadn.net.cn

falsespring-doc.cadn.net.cn

spring.ai.bedrock.cohere.embedding.modelspring-doc.cadn.net.cn

要使用的模型 ID。请参阅 CohereEmbeddingModel 以了解支持的模型。spring-doc.cadn.net.cn

cohere.embed-multilingual-v3spring-doc.cadn.net.cn

spring.ai.bedrock.cohere.embedding.options.input-typespring-doc.cadn.net.cn

为区分不同类型,会预先添加特殊标记。您不应将不同类型混合使用,除非在搜索与检索时需要混合使用。在这种情况下,请使用 search_document 类型对语料库进行嵌入,并使用 search_query 类型对嵌入式查询进行嵌入。spring-doc.cadn.net.cn

SEARCH_DOCUMENTspring-doc.cadn.net.cn

spring.ai.bedrock.cohere.embedding.options.truncatespring-doc.cadn.net.cn

指定 API 如何处理超过最大标记长度的输入。如果您指定 LEFT 或 RIGHT,模型会丢弃输入,直到剩余输入恰好等于模型的最大输入标记长度。spring-doc.cadn.net.cn

NONEspring-doc.cadn.net.cn

在通过 Amazon Bedrock 访问 Cohere 时,截断功能不可用。这是 Amazon Bedrock 的一个问题。Spring AI 类 BedrockCohereEmbeddingModel 将截断为 2048 个字符的长度,这是模型支持的最大长度。

查看 CohereEmbeddingModel 以获取其他模型 ID。 支持的值为:cohere.embed-multilingual-v3cohere.embed-english-v3。 模型 ID 值也可以在 AWS Bedrock 基础模型 ID 文档中找到。spring-doc.cadn.net.cn

所有以spring.ai.bedrock.cohere.embedding.options为前缀的属性都可以通过向EmbeddingRequest调用中添加特定于请求的运行时选项在运行时覆盖。

运行时选项

The BedrockCohereEmbeddingOptions.java provides model configurations, such as input-type or truncate.spring-doc.cadn.net.cn

On start-up, the default options can be configured with the BedrockCohereEmbeddingModel(api, options) constructor or the spring.ai.bedrock.cohere.embedding.options.* properties.spring-doc.cadn.net.cn

在运行时,您可以通过向 EmbeddingRequest 调用添加新的、特定于请求的选项来覆盖默认选项。 例如,要覆盖特定请求的默认输入类型:spring-doc.cadn.net.cn

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-boot-starter-web添加到您的pom(或gradle)依赖中。spring-doc.cadn.net.cn

添加一个 application.properties 文件,位于 src/main/resources 目录下,以启用并配置 Cohere Embedding 模型:spring-doc.cadn.net.cn

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
regionsaccess-keysecret-key替换为您的AWS凭证。

这将创建一个BedrockCohereEmbeddingModel实现,您可以将其注入到您的类中。 以下是一个使用聊天模型进行文本生成的简单@Controller类的例子。spring-doc.cadn.net.cn

@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);
    }
}

手动配置

The BedrockCohereEmbeddingModel implements the EmbeddingModel and uses the Low-level CohereEmbeddingBedrockApi Client to connect to the Bedrock Cohere service.spring-doc.cadn.net.cn

spring-ai-bedrock 依赖添加到您项目的 Maven pom.xml 文件中:spring-doc.cadn.net.cn

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

或者添加到您的Gradle 构建脚本文件中。spring-doc.cadn.net.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-bedrock'
}
参考以下依赖管理部分,添加Spring AI BOM到你的构建文件中。

接下来,创建一个 BedrockCohereEmbeddingModel 并将其用于文本嵌入:spring-doc.cadn.net.cn

var cohereEmbeddingApi =new CohereEmbeddingBedrockApi(
		CohereEmbeddingModel.COHERE_EMBED_MULTILINGUAL_V1.id(),
		EnvironmentVariableCredentialsProvider.create(), Region.US_EAST_1.id(), new JsonMapper());


var embeddingModel = new BedrockCohereEmbeddingModel(this.cohereEmbeddingApi);

EmbeddingResponse embeddingResponse = this.embeddingModel
	.embedForResponse(List.of("Hello World", "World is big and salvation is near"));

低级 CohereEmbeddingBedrockApi 客户端

The CohereEmbeddingBedrockApi provides a lightweight Java client on top of AWS Bedrock Cohere Command models.spring-doc.cadn.net.cn

以下类图展示了 CohereEmbeddingBedrockApi 接口及其构建模块:spring-doc.cadn.net.cn

bedrock cohere embedding low level api

CohereEmbeddingBedrockApi 支持 cohere.embed-english-v3cohere.embed-multilingual-v3 模型,用于单次和批量嵌入计算。spring-doc.cadn.net.cn

以下是如何以编程方式使用API的简单代码片段:spring-doc.cadn.net.cn

CohereEmbeddingBedrockApi api = new CohereEmbeddingBedrockApi(
		CohereEmbeddingModel.COHERE_EMBED_MULTILINGUAL_V1.id(),
		EnvironmentVariableCredentialsProvider.create(),
		Region.US_EAST_1.id(), new JsonMapper());

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);