Cohere 嵌入
提供 Bedrock Cohere 嵌入模型。整合生成式 AI 功能到必要的應用程式和工作流程中,以改善業務成果。
在 AWS Bedrock Cohere 模型頁面 和 Amazon Bedrock 使用者指南 中包含關於如何使用 AWS 託管模型的詳細資訊。
必要條件
請參閱關於 Amazon Bedrock 的 Spring AI 文件 以設定 API 存取權。
自動配置
將 spring-ai-bedrock-ai-spring-boot-starter
依賴項新增至您專案的 Maven pom.xml
檔案
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bedrock-ai-spring-boot-starter</artifactId>
</dependency>
或新增至您的 Gradle build.gradle
建置檔案。
dependencies {
implementation 'org.springframework.ai:spring-ai-bedrock-ai-spring-boot-starter'
}
請參閱依賴項管理章節,將 Spring AI BOM 新增至您的建置檔案。 |
啟用 Cohere 嵌入支援
預設情況下,Cohere 模型已停用。若要啟用它,請將 spring.ai.bedrock.cohere.embedding.enabled
屬性設定為 true
。匯出環境變數是設定此組態屬性的一種方式
export SPRING_AI_BEDROCK_COHERE_EMBEDDING_ENABLED=true
嵌入屬性
字首 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 嵌入模型實作的屬性字首。
屬性 |
描述 |
預設值 |
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 |
請查看 CohereEmbeddingModel 以取得其他模型 ID。支援的值為: cohere.embed-multilingual-v3
和 cohere.embed-english-v3
。模型 ID 值也可以在 AWS Bedrock 文件中找到,用於基本模型 ID。
所有以 spring.ai.bedrock.cohere.embedding.options 為字首的屬性都可以在執行時覆寫,方法是將請求特定的執行時選項新增至 EmbeddingRequest 呼叫。 |
執行時選項
BedrockCohereEmbeddingOptions.java 提供模型配置,例如 input-type
或 truncate
。
在啟動時,可以使用 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-bedrock-ai-spring-boot-starter
新增至您的 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.bedrock.cohere.embedding.enabled=true
spring.ai.bedrock.cohere.embedding.options.input-type=search-document
將 regions 、access-key 和 secret-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 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);