Cohere 嵌入
提供 Bedrock Cohere 嵌入模型。將生成式 AI 功能整合到關鍵應用程式和工作流程中,以改善業務成果。
AWS Bedrock Cohere 模型頁面和Amazon Bedrock 使用者指南包含有關如何使用 AWS 託管模型的詳細資訊。
先決條件
有關設定 API 訪問,請參閱Amazon Bedrock 上的 Spring AI 文件。
自動配置
|
Spring AI 自動配置、啟動模組的工件名稱發生了重大變化。請參閱 升級說明 以獲取更多資訊。 |
將 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 訪問金鑰。 |
- |
spring.ai.bedrock.aws.secret-key |
AWS 秘密金鑰。 |
- |
|
嵌入自動配置的啟用和停用現在透過字首為 要啟用,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 的支援 |
假 |
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-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()
.inputType(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
將 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 提供了一個輕量級 Java 客戶端,基於 AWS Bedrock Cohere Command 模型。
以下類圖說明了 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);