Cohere Embeddings
提供 Bedrock Cohere 嵌入模型。将生成式 AI 功能集成到可改善业务成果的关键应用和工作流中。
Provides Bedrock Cohere Embedding model. Integrate generative AI capabilities into essential apps and workflows that improve business outcomes.
AWS Bedrock Cohere Model Page和 Amazon Bedrock User Guide包含有关如何使用 AWS 托管模型的详细信息。
The AWS Bedrock Cohere Model Page and Amazon Bedrock User Guide contains detailed information on how to use the AWS hosted model.
Prerequisites
请参阅 Spring AI documentation on Amazon Bedrock 以设置 API 访问。
Refer to the Spring AI documentation on Amazon Bedrock for setting up API access.
Add Repositories and BOM
Spring AI 工件发布在 Maven Central 和 Spring Snapshot 存储库中。请参阅“添加 Spring AI 仓库”部分,将这些仓库添加到您的构建系统。
Spring AI artifacts are published in Maven Central and Spring Snapshot repositories. Refer to the Artifact Repositories section to add these repositories to your build system.
为了帮助进行依赖项管理,Spring AI 提供了一个 BOM(物料清单)以确保在整个项目中使用一致版本的 Spring AI。有关将 Spring AI BOM 添加到你的构建系统的说明,请参阅 Dependency Management 部分。
To help with dependency management, Spring AI provides a BOM (bill of materials) to ensure that a consistent version of Spring AI is used throughout the entire project. Refer to the Dependency Management section to add the Spring AI BOM to your build system.
Auto-configuration
Spring AI 自动配置、启动器模块的工件名称发生了重大变化。请参阅 upgrade notes 以获取更多信息。 There has been a significant change in the Spring AI auto-configuration, starter modules' artifact names. Please refer to the upgrade notes for more information. |
将 spring-ai-starter-model-bedrock
依赖项添加到您项目的 Maven pom.xml
文件中:
Add the spring-ai-starter-model-bedrock
dependency to your project’s Maven pom.xml
file:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-bedrock</artifactId>
</dependency>
或添加到 Gradle build.gradle
构建文件中。
or to your Gradle build.gradle
build file.
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-model-bedrock'
}
|
Refer to the Dependency Management section to add the Spring AI BOM to your build file. |
Enable Cohere Embedding Support
默认情况下,Cohere 嵌入模型是禁用的。要启用它,请在您的应用程序配置中将 spring.ai.model.embedding
属性设置为 bedrock-cohere
:
By default, the Cohere embedding model is disabled.
To enable it, set the spring.ai.model.embedding
property to bedrock-cohere
in your application configuration:
spring.ai.model.embedding=bedrock-cohere
或者,您可以使用 Spring Expression Language (SpEL) 来引用环境变量:
Alternatively, you can use Spring Expression Language (SpEL) to reference an environment variable:
# In application.yml
spring:
ai:
model:
embedding: ${AI_MODEL_EMBEDDING}
# In your environment or .env file
export AI_MODEL_EMBEDDING=bedrock-cohere
您还可以在启动应用程序时使用 Java 系统属性设置此属性:
You can also set this property using Java system properties when starting your application:
java -Dspring.ai.model.embedding=bedrock-cohere -jar your-application.jar
Embedding Properties
spring.ai.bedrock.aws
前缀是配置与 AWS Bedrock 的连接的属性前缀。
The prefix spring.ai.bedrock.aws
is the property prefix to configure the connection to AWS Bedrock.
Property | Description | Default |
---|---|---|
spring.ai.bedrock.aws.region |
AWS region to use. |
us-east-1 |
spring.ai.bedrock.aws.access-key |
AWS access key. |
- |
spring.ai.bedrock.aws.secret-key |
AWS secret key. |
- |
嵌入自动配置的启用和禁用现在通过前缀为 Enabling and disabling of the embedding auto-configurations are now configured via top level properties with the prefix 要启用,请设置 spring.ai.model.embedding=bedrock-cohere(默认情况下已启用) To enable, spring.ai.model.embedding=bedrock-cohere (It is enabled by default) 要禁用,请设置 spring.ai.model.embedding=none(或任何与 bedrock-cohere 不匹配的值) To disable, spring.ai.model.embedding=none (or any value which doesn’t match bedrock-cohere) 此更改旨在允许配置多个模型。 This change is done to allow configuration of multiple models. |
前缀 spring.ai.bedrock.cohere.embedding
(在 BedrockCohereEmbeddingProperties
中定义)是配置 Cohere 嵌入模型实现的属性前缀。
The prefix spring.ai.bedrock.cohere.embedding
(defined in BedrockCohereEmbeddingProperties
) is the property prefix that configures the embedding model implementation for Cohere.
Property |
Description |
Default |
spring.ai.model.embedding |
Enable or disable support for Cohere |
bedrock-cohere |
spring.ai.bedrock.cohere.embedding.enabled (Removed and no longer valid) |
Enable or disable support for Cohere |
false |
spring.ai.bedrock.cohere.embedding.model |
The model id to use. See the CohereEmbeddingModel for the supported models. |
cohere.embed-multilingual-v3 |
spring.ai.bedrock.cohere.embedding.options.input-type |
Prepends special tokens to differentiate each type from one another. You should not mix different types together, except when mixing types for search and retrieval. In this case, embed your corpus with the search_document type and embedded queries with type search_query type. |
SEARCH_DOCUMENT |
spring.ai.bedrock.cohere.embedding.options.truncate |
Specifies how the API handles inputs longer than the maximum token length. If you specify LEFT or RIGHT, the model discards the input until the remaining input is exactly the maximum input token length for the model. |
NONE |
通过 Amazon Bedrock 访问 Cohere 时,截断功能不可用。这是 Amazon Bedrock 的一个问题。Spring AI 类 |
When accessing Cohere via Amazon Bedrock, the functionality of truncating is not available. This is an issue with Amazon Bedrock. The Spring AI class |
查看 CohereEmbeddingModel 获取其他模型 ID。支持的值为: cohere.embed-multilingual-v3
和 cohere.embed-english-v3
。模型 ID 值也可以在 AWS Bedrock documentation for base model IDs 中找到。
Look at the CohereEmbeddingModel for other model IDs.
Supported values are: cohere.embed-multilingual-v3
and cohere.embed-english-v3
.
Model ID values can also be found in the AWS Bedrock documentation for base model IDs.
所有前缀为 |
All properties prefixed with |
Runtime Options
BedrockCohereEmbeddingOptions.java 提供模型配置,例如 input-type
或 truncate
。
The BedrockCohereEmbeddingOptions.java provides model configurations, such as input-type
or truncate
.
在启动时,可以使用 BedrockCohereEmbeddingModel(api, options)
构造函数或 spring.ai.bedrock.cohere.embedding.options.*
属性配置默认选项。
On start-up, the default options can be configured with the BedrockCohereEmbeddingModel(api, options)
constructor or the spring.ai.bedrock.cohere.embedding.options.*
properties.
在运行时,您可以通过向 EmbeddingRequest
调用添加新的、特定于请求的选项来覆盖默认选项。例如,要覆盖特定请求的默认输入类型:
At runtime you can override the default options by adding new, request-specific, options to the EmbeddingRequest
call.
For example to override the default input type for a specific request:
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
` Create 一个新的Spring Boot项目,并将
spring-ai-starter-model-bedrock
`添加到你的pom(或gradle)依赖中。
Create a new Spring Boot project and add the spring-ai-starter-model-bedrock
to your pom (or gradle) dependencies.
在 src/main/resources
目录下添加一个 application.properties
文件,以启用和配置 Cohere 嵌入模型:
Add a application.properties
file, under the src/main/resources
directory, to enable and configure the Cohere Embedding model:
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
将 |
replace the |
这将创建一个您可以注入到类中的 BedrockCohereEmbeddingModel
实现。以下是一个使用聊天模型进行文本生成的简单 @Controller
类的示例。
This will create a BedrockCohereEmbeddingModel
implementation that you can inject into your class.
Here is an example of a simple @Controller
class that uses the chat model for text generations.
@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);
}
}
Manual Configuration
BedrockCohereEmbeddingModel 实现了 EmbeddingModel
并使用 Low-level CohereEmbeddingBedrockApi Client 连接到 Bedrock Cohere 服务。
The BedrockCohereEmbeddingModel implements the EmbeddingModel
and uses the Low-level CohereEmbeddingBedrockApi Client to connect to the Bedrock Cohere service.
将 spring-ai-bedrock
依赖项添加到项目的 Maven pom.xml
文件:
Add the spring-ai-bedrock
dependency to your project’s Maven pom.xml
file:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bedrock</artifactId>
</dependency>
或添加到 Gradle build.gradle
构建文件中。
or to your Gradle build.gradle
build file.
dependencies {
implementation 'org.springframework.ai:spring-ai-bedrock'
}
|
Refer to the Dependency Management section to add the Spring AI BOM to your build file. |
接下来,创建一个 BedrockCohereEmbeddingModel 并将其用于文本嵌入:
Next, create an BedrockCohereEmbeddingModel and use it for text embeddings:
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"));
Low-level CohereEmbeddingBedrockApi Client
CohereEmbeddingBedrockApi 提供轻量级 Java 客户端,在 AWS Bedrock Cohere Command models 上。
The CohereEmbeddingBedrockApi provides is lightweight Java client on top of AWS Bedrock Cohere Command models.
以下类图说明了 CohereEmbeddingBedrockApi 接口和构建块:
Following class diagram illustrates the CohereEmbeddingBedrockApi interface and building blocks:

CohereEmbeddingBedrockApi 支持 cohere.embed-english-v3
和 cohere.embed-multilingual-v3
模型用于单个和批处理嵌入计算。
The CohereEmbeddingBedrockApi supports the cohere.embed-english-v3
and cohere.embed-multilingual-v3
models for single and batch embedding computation.
下面是一个简单的片段,说明如何以编程方式使用 API:
Here is a simple snippet how to use the api programmatically:
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);