Cohere 嵌入

提供了 Bedrock Cohere 嵌入模型。將生成式 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 模組的構件名稱發生了重大變化。請參考升級說明獲取更多資訊。

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。匯出環境變數是設定此配置屬性的一種方法

export SPRING_AI_MODEL_EMBEDDING=bedrock-cohere

嵌入屬性

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 如何處理長於最大令牌長度的輸入。如果指定 LEFT 或 RIGHT,模型會丟棄輸入直到剩餘輸入的長度正好等於模型的最大輸入令牌長度。

NONE

透過 Amazon Bedrock 訪問 Cohere 時,截斷功能不可用。這是 Amazon Bedrock 的一個問題。Spring AI 類 BedrockCohereEmbeddingModel 將截斷到 2048 個字元長度,這是該模型支援的最大長度。

檢視CohereEmbeddingModel 瞭解其他模型 ID。支援的值有: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()));

示例控制器

建立一個新的 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 提供了一個輕量級的 Java 客戶端,基於 AWS Bedrock Cohere Command 模型

以下類圖說明了 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);