Cohere 嵌入
提供 Bedrock Cohere 嵌入模型。将生成式 AI 功能集成到基本应用和工作流程中,以改善业务成果。
AWS Bedrock Cohere 模型页面和 Amazon Bedrock 用户指南包含有关如何使用 AWS 托管模型的详细信息。
前提条件
请参阅 Amazon Bedrock 上的 Spring AI 文档以设置 API 访问。
添加存储库和 BOM
Spring AI 工件发布在 Maven Central 和 Spring Snapshot 存储库中。请参阅 Artifact Repositories 部分,将这些存储库添加到您的构建系统中。
为了帮助进行依赖管理,Spring AI 提供了 BOM(物料清单),以确保在整个项目中使用一致的 Spring AI 版本。请参阅依赖项管理部分,将 Spring AI BOM 添加到您的构建系统中。
自动配置
Spring AI 自动配置、入门模块的工件名称发生了重大变化。有关更多信息,请参阅升级说明。 |
添加spring-ai-starter-model-bedrock
对项目 Maven 的依赖pom.xml
文件:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-bedrock</artifactId>
</dependency>
或 Gradlebuild.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.基岩.aws.region |
要使用的 AWS 区域。 |
美国东部 1 |
spring.ai.bedrock.aws.access-key |
AWS 访问密钥。 |
- |
spring.ai.基岩.aws.密钥 |
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 的支持 |
基岩连合 |
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,则模型会丢弃输入,直到剩余的输入恰好是模型的最大输入标记长度。 |
没有 |
通过 Amazon Bedrock 访问 Cohere 时,截断功能不可用。这是 Amazon Bedrock 的一个问题。Spring AI 类BedrockCohereEmbeddingModel 将截断为 2048 个字符长度,这是模型支持的最大值。 |
查看 CohereEmbeddingModel 以获取其他模型 ID。
支持的值包括:cohere.embed-multilingual-v3
和cohere.embed-english-v3
.
模型 ID 值也可以在 AWS 基岩文档中找到基本模型 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()));
Samples控制器
创建一个新的 Spring Boot 项目,并将spring-ai-starter-model-bedrock
到你的 pom(或 gradle)依赖项。
添加一个application.properties
文件,在src/main/resources
目录,以启用和配置 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>
或 Gradlebuild.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 命令模型的轻量级 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);