主题
Cohere 嵌入
提供 Bedrock Cohere 嵌入模型。 把生成式 AI 能力集成到改善业务结果的关键应用与工作流中。
AWS Bedrock Cohere 模型页 与 Amazon Bedrock 用户指南 含有关于如何使用 AWS 托管模型的详细信息。
前置条件
关于设置 API 访问,参见 Spring AI 关于 Amazon Bedrock 的文档。
添加仓库与 BOM
Spring AI 的制品发布在 Maven Central 和 Spring Snapshot 仓库中。 参见 制品仓库 一节,把这些仓库添加到你的构建系统。
为便于依赖管理,Spring AI 提供了一个 BOM(物料清单),以确保整个项目中使用一致版本的 Spring AI。参见 依赖管理 一节,把 Spring AI BOM 添加到你的构建系统。
自动配置
NOTE
Spring AI 的自动配置、starter 模块的制品名称发生了重大变更。 更多信息请参阅 升级说明。
把 spring-ai-starter-model-bedrock 依赖添加到你的项目的 Maven pom.xml 文件:
xml
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-bedrock</artifactId>
</dependency>1
2
3
4
2
3
4
或添加到你的 Gradle build.gradle 构建文件。
gradle
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-model-bedrock'
}1
2
3
2
3
TIP
参见 依赖管理 一节,把 Spring AI BOM 添加到你的构建文件。
启用 Cohere 嵌入支持
默认情况下 Cohere 嵌入模型是禁用的。 要启用它,在你的应用配置中把 spring.ai.model.embedding 属性设为 bedrock-cohere:
properties
spring.ai.model.embedding=bedrock-cohere1
或者,你可以使用 Spring 表达式语言(SpEL)引用一个环境变量:
yaml
# In application.yml
spring:
ai:
model:
embedding: ${AI_MODEL_EMBEDDING}1
2
3
4
5
2
3
4
5
bash
# In your environment or .env file
export AI_MODEL_EMBEDDING=bedrock-cohere1
2
2
你也可以在启动应用时用 Java 系统属性设置此属性:
shell
java -Dspring.ai.model.embedding=bedrock-cohere -jar your-application.jar1
嵌入属性
前缀 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.aws.profile.name | AWS 配置档名。 | - |
| spring.ai.bedrock.aws.profile.credentials-path | AWS 凭据文件路径。 | - |
| spring.ai.bedrock.aws.profile.configuration-path | AWS 配置文件路径。 | - |
NOTE
嵌入自动配置的启用与禁用,现通过以 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 (Removed and no longer valid) | 启用或禁用 Cohere 支持 | false |
| spring.ai.bedrock.cohere.embedding.model | 要使用的模型 id。受支持的模型参见 CohereEmbeddingModel。 | cohere.embed-multilingual-v3 |
| spring.ai.bedrock.cohere.embedding.input-type | 前置特殊令牌以把每种类型彼此区分。你不应把不同类型混在一起,除了为搜索与检索混用类型时。这种情况下,用 searchdocument 类型嵌入你的语料,用 type searchquery 类型嵌入查询。 | SEARCH_DOCUMENT |
| spring.ai.bedrock.cohere.embedding.truncate | 指定 API 如何处理超过最大令牌长度的输入。若你指定 LEFT 或 RIGHT,模型会丢弃相应部分。 | NONE |
NOTE
通过 Amazon Bedrock 访问 Cohere 时,截断功能不可用。这是 Amazon Bedrock 的一个问题。Spring AI 的 BedrockCohereEmbeddingModel 类会截断到 2048 字符长度,即模型支持的最大值。
其他模型 ID 参见 CohereEmbeddingModel。受支持的值有:cohere.embed-multilingual-v3 和 cohere.embed-english-v3。模型 ID 值也可在 AWS Bedrock 基础模型 ID 文档中找到。
TIP
所有以 spring.ai.bedrock.cohere.embedding 为前缀的属性,都可通过在 EmbeddingRequest 调用中添加请求特定的 embedding-options 在运行时被覆盖。
运行时选项
BedrockCohereEmbeddingOptions.java 提供诸如 input-type 或 truncate 的模型配置。
在启动时,可通过 BedrockCohereEmbeddingModel(api, options) 构造器或 spring.ai.bedrock.cohere.embedding.* 属性配置默认选项。
在运行时,你可以通过向 EmbeddingRequest 调用添加新的、请求特定的选项来覆盖默认选项。 例如,要为某个特定请求覆盖默认输入类型:
java
EmbeddingResponse embeddingResponse = embeddingModel.call(
new EmbeddingRequest(List.of("Hello World", "World is big and salvation is near"),
BedrockCohereEmbeddingOptions.builder()
.inputType(InputType.SEARCH_DOCUMENT)
.build()));1
2
3
4
5
2
3
4
5
示例控制器
创建 一个新的 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.input-type=search-document1
2
3
4
5
6
2
3
4
5
6
TIP
用你的 AWS 凭据替换 regions、access-key 和 secret-key。
这会创建一个可注入到你类中的 BedrockCohereEmbeddingModel 实现。 下面是一个简单 @Controller 类的示例,它使用聊天模型进行文本生成。
java
@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);
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
手动配置
BedrockCohereEmbeddingModel 实现了 EmbeddingModel,并使用底层 api 连接到 Bedrock Cohere 服务。
把 spring-ai-bedrock 依赖添加到你的项目的 Maven pom.xml 文件:
xml
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bedrock</artifactId>
</dependency>1
2
3
4
2
3
4
或添加到你的 Gradle build.gradle 构建文件。
gradle
dependencies {
implementation 'org.springframework.ai:spring-ai-bedrock'
}1
2
3
2
3
TIP
参见 依赖管理 一节,把 Spring AI BOM 添加到你的构建文件。
接下来,创建一个 BedrockCohereEmbeddingModel 并将其用于文本嵌入:
java
var cohereEmbeddingApi =new CohereEmbeddingBedrockApi(
CohereEmbeddingModel.COHERE_EMBED_MULTILINGUAL_V1.id(),
EnvironmentVariableCredentialsProvider.create(), Region.US_EAST_1.id(), new JsonMapper());
var embeddingModel = new BedrockCohereEmbeddingModel(this.cohereEmbeddingApi);
EmbeddingResponse embeddingResponse = this.embeddingModel
.embedForResponse(List.of("Hello World", "World is big and salvation is near"));1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
底层 CohereEmbeddingBedrockApi 客户端
CohereEmbeddingBedrockApi 在 AWS Bedrock Cohere Command 模型之上提供一个轻量级 Java 客户端。
下面的类图展示了 CohereEmbeddingBedrockApi 接口与构建块:
CohereEmbeddingBedrockApi 支持 cohere.embed-english-v3 和 cohere.embed-multilingual-v3 模型进行单个与批量嵌入计算。
下面是一个演示如何以编程方式使用该 API 的简单片段:
java
CohereEmbeddingBedrockApi api = new CohereEmbeddingBedrockApi(
CohereEmbeddingModel.COHERE_EMBED_MULTILINGUAL_V1.id(),
EnvironmentVariableCredentialsProvider.create(),
Region.US_EAST_1.id(), new JsonMapper());
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);1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11