Cohere 嵌入

提供 Bedrock Cohere Embedding 模型。 将生成式 AI 功能集成到基本应用程序和工作流中,以改善业务成果。spring-doc.cadn.net.cn

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

先决条件

添加存储库和 BOM

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

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

自动配置

添加spring-ai-bedrock-ai-spring-boot-starter依赖项添加到项目的 Mavenpom.xml文件:spring-doc.cadn.net.cn

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

或发送到您的 Gradlebuild.gradlebuild 文件。spring-doc.cadn.net.cn

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

启用 Cohere 嵌入支持

默认情况下,Cohere 模型处于禁用状态。 要启用它,请将spring.ai.bedrock.cohere.embedding.enabledproperty 设置为true. 导出环境变量是设置此配置属性的一种方法:spring-doc.cadn.net.cn

export SPRING_AI_BEDROCK_COHERE_EMBEDDING_ENABLED=true

嵌入属性

前缀spring.ai.bedrock.aws是用于配置与 AWS Bedrock 的连接的属性前缀。spring-doc.cadn.net.cn

财产 描述 违约

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

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

us-east-1 (美国东部-1)spring-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.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.bedrock.cohere.embedding.enabledspring-doc.cadn.net.cn

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

spring-doc.cadn.net.cn

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

要使用的模型 ID。有关支持的模型,请参阅 CohereEmbeddingModelspring-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 类型嵌入语料库,并使用 type 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

没有spring-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 文档中找到基本模型 IDspring-doc.cadn.net.cn

所有前缀为spring.ai.bedrock.cohere.embedding.options可以通过将特定于请求的运行时选项添加到EmbeddingRequest叫。

运行时选项

BedrockCohereEmbeddingOptions.java 提供模型配置,例如input-typetruncate.spring-doc.cadn.net.cn

启动时,可以使用BedrockCohereEmbeddingModel(api, options)constructor 或spring.ai.bedrock.cohere.embedding.options.*性能。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()
        	.withInputType(InputType.SEARCH_DOCUMENT)
        .build()));

Samples控制器

创建一个新的 Spring Boot 项目并添加spring-ai-bedrock-ai-spring-boot-starter添加到您的 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.bedrock.cohere.embedding.enabled=true
spring.ai.bedrock.cohere.embedding.options.input-type=search-document
regions,access-keysecret-key替换为您的 AWS 凭证。

这将创建一个BedrockCohereEmbeddingModel实现,您可以将其注入到您的类中。 下面是一个简单的示例@Controller使用 Chat 模型生成文本的类。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);
    }
}

手动配置

BedrockCohereEmbeddingModel 实现了EmbeddingModel并使用低级 CohereEmbeddingBedrockApi 客户端连接到 Bedrock Cohere 服务。spring-doc.cadn.net.cn

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

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

或发送到您的 Gradlebuild.gradlebuild 文件。spring-doc.cadn.net.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-bedrock'
}
请参阅 Dependency Management 部分,将 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 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 客户端。spring-doc.cadn.net.cn

以下类图说明了 CohereEmbeddingBedrockApi 接口和构建块:spring-doc.cadn.net.cn

Bedrock Cohere 嵌入低级 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 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);