Cohere Embeddings

提供 Bedrock Cohere 嵌入模型。将生成式 AI 能力集成到提升业务成果的关键应用和工作流程中。

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

前提条件(Prerequisites)

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

添加存储库和 BOM

Spring AI 工件发布在 Spring Milestone 和 Snapshot 存储库中。请参阅存储库部分将这些存储库添加到您的构建系统中。

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

自动配置(Auto-configuration)

Spring AI 为 Bedrock Cohere 嵌入模型提供 Spring Boot 自动配置。要启用它,请在项目的 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'
}

启用 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

嵌入属性(Embedding Properties)

前缀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.bedrock.cohere.embedding(定义于 ‘BedrockCohereEmbeddingProperties‘ 中)是用于配置 Cohere 嵌入模型实现的属性前缀。

查看 CohereEmbeddingModel 以获取其他模型 ID。 支持的模型包括:cohere.embed-multilingual-v3 和 cohere.embed-english-v3。 模型 ID 值也可在 AWS Bedrock 基础模型 ID 文档 中找到。

属性 描述 默认值
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 添加特殊token区分不同类型。除非混合搜索和检索类型,否则不应混合不同类型。此时应用search_document类型嵌入语料库,search_query类型嵌入查询 SEARCH_DOCUMENT
spring.ai.bedrock.cohere.embedding.options.truncate 指定API如何处理超过最大token长度的输入。LEFT或RIGHT会使模型丢弃输入直到剩余输入正好是模型的最大输入token长度 NONE

运行时选项(Runtime Options )

BedrockCohereEmbeddingOptions.java 提供模型配置,例如要使用的 input-type 或 runcate 等。

在启动时,可以使用 BedrockCohereEmbeddingModel (api,options) 构造函数或 spring.ai.bedrock.cohere.embedding.options.*属性来配置默认选项。

在运行时,您可以通过向 EmbeddingRequest 调用添加新的、特定于请求的选项来覆盖默认设置。例如,要为特定请求覆盖默认的 input-type :

EmbeddingResponse embeddingResponse = embeddingModel.call(
    new EmbeddingRequest(List.of("Hello World", "World is big and salvation is near"),
        BedrockCohereEmbeddingOptions.builder()
            .withInputType(InputType.SEARCH_DOCUMENT)
        .build()));

示例控制器(Sample Controller)

创建一个新的 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

这将创建一个 BedrockCohereEmbeddingModel 的实现,你可以将其注入到你的类中。下面是一个简单的 @Controller 类的示例,它使用聊天模型进行文本生成。

@RestController
public class EmbeddingController {

    private final BedrockTitanEmbeddingModel embeddingModel;

    @Autowired
    public EmbeddingController(BedrockTitanEmbeddingModel embeddingModel) {
        this.embeddingModel = embeddingModel;
    }

    @GetMapping("/ai/embedding")
    public Map<String, Object> embed(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        EmbeddingResponse embeddingResponse = this.embeddingModel.embedForResponse(List.of(message));
        return Map.of("embedding", embeddingResponse);
    }

}

手动配置(Manual Configuration)

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'
}

接下来,创建一个 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);
作者:Ddd4j  创建时间:2025-08-03 11:54
最后编辑:Ddd4j  更新时间:2026-04-23 14:31
上一篇:
下一篇: