Cohere 嵌入模型

本站(springdoc.cn)中的内容来源于 spring.io ,原始版权归属于 spring.io。由 springdoc.cn 进行翻译,整理。可供个人学习、研究,未经许可,不得进行任何转载、商用或与之相关的行为。 商标声明:Spring 是 Pivotal Software, Inc. 在美国以及其他国家的商标。

提供 Bedrock Cohere 嵌入模型。将生成式 AI 能力集成至核心应用程序与工作流,以提升业务成果。

AWS Bedrock Cohere 模型页面Amazon Bedrock 用户指南 包含有关如何使用 AWS 托管模型的详细信息。

先决条件

请参阅 Spring AI 关于 Amazon Bedrock 的文档 来设置 API 访问权限。

添加仓库和 BOM

Spring AI 构件已发布至 Maven 央仓库和 Spring 快照仓库。请参阅 “构件仓库” 章节将这些仓库添加到你的构建系统中。

为简化依赖管理,Spring AI 提供 BOM 以确保项目统一使用相同版本。请参考 “依赖管理” 章节将 Spring AI BOM 添加到你的构建系统中。

自动配置

Spring AI 自动配置及 Starter 模块的构件命名已发生重大变更,详情请参阅 升级说明 文档。

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 嵌入支持

默认情况下,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 access key。

-

spring.ai.bedrock.aws.secret-key

AWS secret key。

-

嵌入自动配置的启用与禁用现在通过顶级属性配置,前缀为 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 字符长度(该模型支持的最大长度)。

其他模型ID请参考 CohereEmbeddingModel。支持的值为:cohere.embed-multilingual-v3cohere.embed-english-v3。模型 ID 值也可在 AWS Bedrock 基础模型 ID 文档 中查询。

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

运行时选项

BedrockCohereEmbeddingOptions.java 提供模型配置参数,包括 input-typetruncate 等。

启动时,默认选项可通过 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()
        	.withInputType(InputType.SEARCH_DOCUMENT)
        .build()));

示例 Controller

创建 一个新的 Spring Boot 项目,并将 spring-ai-starter-model-bedrock 添加到你的 pom(或gradle)依赖中。

src/main/resources 目录下添加 application.properties 文件,用于启用和配置 Cohere 嵌入模型:

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 类示例,该类使用聊天模型进行文本生成。

@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-ai-bedrock 依赖添加到你项目的 Maven pom.xml 文件中:

<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 命令模型 的轻量级 Java 客户端。

以下类图展示了 CohereEmbeddingBedrockApi 接口及其核心组件:

bedrock cohere embedding low level api

CohereEmbeddingBedrockApi 支持 cohere.embed-english-v3cohere.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);