可干嵌入
提供基岩凝聚物嵌入模型。 将生成式AI功能整合到关键应用和工作流程中,提升业务成果。
AWS Bedrock Cohere 模型页面和 Amazon Bedrock 用户指南包含了如何使用 AWS 托管模型的详细信息。
前提条件
请参阅亚马逊Bedrock上的Spring AI文档来设置API访问权限。
自动配置
|
春季AI自动配置、起始模块的工件名称发生了重大变化。 更多信息请参阅升级说明。 |
添加Spring-AI-入门-模型-基岩对你项目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的物料清单添加到你的构建文件中。 |
启用 Cohere 嵌入支持
默认情况下,Cohere 嵌入模型被禁用。
要启用它,请设置spring.ai.model.embedding属性到基岩-凝聚力在你的应用配置中:
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
嵌入性质
前缀春.ai.bedrock.aws是配置与AWS Bedrock连接的属性前缀。
| 属性 | 描述 | 默认值 |
|---|---|---|
spring.ai.bedrock.aws.region |
AWS地区的使用。 |
美国东1号 |
spring.ai.bedrock.aws.访问密钥 |
AWS 访问密钥。 |
- |
Spring.ai.bedrock.aws.秘密密钥 |
AWS 秘密密钥。 |
- |
|
嵌入自动配置的启用和禁用现在通过带有前缀的顶层属性配置 要启用,请使用spring.ai.model.embedding=bedrock-cohere(默认启用) 要禁用,请使用spring.ai.model.embedding=none(或任何与bedrock-cohere不匹配的值) 此改动旨在允许配置多个模型。 |
前缀spring.ai.bedrock.cohere.embedding(定义于BedrockCoheredingEmbdingProperties)是配置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如何处理超过最大Tokens长度的输入。如果你指定LEFT或RIGHT,模型会丢弃该输入,直到剩余输入正好是模型的最大输入标记长度。 |
没有 |
通过亚马逊Bedrock访问Cohere时,无法提供截断功能。这是Amazon Bedrock的问题。春季人工智能课程BedrockCohereembdingModel将截断为2048字符长度,这是模型支持的最大值。 |
可以看看 CohereEmbeddingModel 里的其他模型 ID。
支持的值有:cohere.embed-multilingual-v3和cohere.embed-english-v3.
模型ID值也可以在AWS Bedrock文档中找到基础模型ID的资料。
运行时选项
BedrockCohereEmbeddingOptions.java提供模型配置,例如输入类型或截断.
启动时,默认选项可以配置为BedrockCohereEmbeddingModel(api, options)构造者或spring.ai.bedrock.cohere.embedding.options.*性能。
运行时,你可以通过添加新的、针对请求的选项来覆盖默认选项嵌入请求叫。
例如,为了覆盖特定请求的默认输入类型:
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-入门-模型-基岩对你的POM(或Gradle)依赖。
添加一个application.properties文件,在src/主/资源目录,用于启用和配置 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
替换地区,访问密钥和秘密密钥用你的AWS凭证。 |
这将产生BedrockCohereembdingModel你可以把这些实现注入到你的类里。
这里有一个简单的例子@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 实现了嵌入模型并使用低层 CohereEmbeddingBedrockApi 客户端连接 Bedrock Cohere 服务。
添加春艾基岩对你项目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的物料清单添加到你的构建文件中。 |
接下来,创建一个 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);