可干嵌入

提供基岩凝聚物嵌入模型。 将生成式AI功能整合到关键应用和工作流程中,提升业务成果。spring-doc.cadn.net.cn

AWS Bedrock Cohere 模型页面Amazon Bedrock 用户指南包含了如何使用 AWS 托管模型的详细信息。spring-doc.cadn.net.cn

前提条件

添加仓库和物料清单

Spring AI 产物发布于 Maven Central 和 Spring Snapshot 仓库中。 请参阅神器仓库部分,将这些仓库添加到你的构建系统中。spring-doc.cadn.net.cn

为帮助依赖管理,Spring AI 提供了物料清单(BOM),确保整个项目中使用一致版本的 Spring AI。请参考依赖管理部分,将春季AI物料清单添加到你的构建系统中。spring-doc.cadn.net.cn

自动配置

春季AI自动配置、起始模块的工件名称发生了重大变化。 更多信息请参阅升级说明spring-doc.cadn.net.cn

添加Spring-AI-入门-模型-基岩对你项目Maven的依赖pom.xml文件:spring-doc.cadn.net.cn

<dependency>
  <groupId>org.springframework.ai</groupId>
  <artifactId>spring-ai-starter-model-bedrock</artifactId>
</dependency>

或者去你的Gradlebuild.gradle构建文件。spring-doc.cadn.net.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-starter-model-bedrock'
}
请参考依赖管理部分,将Spring AI的物料清单添加到你的构建文件中。

启用 Cohere 嵌入支持

默认情况下,Cohere 嵌入模型被禁用。 要启用它,请设置spring.ai.model.embedding属性到基岩-凝聚力在你的应用配置中:spring-doc.cadn.net.cn

spring.ai.model.embedding=bedrock-cohere

或者,您可以使用 Spring 表达式语言(SpEL)来引用环境变量:spring-doc.cadn.net.cn

# In application.yml
spring:
  ai:
    model:
      embedding: ${AI_MODEL_EMBEDDING}
# In your environment or .env file
export AI_MODEL_EMBEDDING=bedrock-cohere

启动应用程序时,你也可以用 Java 系统属性设置此属性:spring-doc.cadn.net.cn

java -Dspring.ai.model.embedding=bedrock-cohere -jar your-application.jar

嵌入性质

前缀春.ai.bedrock.aws是配置与AWS Bedrock连接的属性前缀。spring-doc.cadn.net.cn

属性 描述 默认值

spring.ai.bedrock.aws.regionspring-doc.cadn.net.cn

AWS地区的使用。spring-doc.cadn.net.cn

美国东1号spring-doc.cadn.net.cn

spring.ai.bedrock.aws.访问密钥spring-doc.cadn.net.cn

AWS 访问密钥。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

Spring.ai.bedrock.aws.秘密密钥spring-doc.cadn.net.cn

AWS 秘密密钥。spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

嵌入自动配置的启用和禁用现在通过带有前缀的顶层属性配置spring.ai.model.embedding.spring-doc.cadn.net.cn

要启用,请使用spring.ai.model.embedding=bedrock-cohere(默认启用)spring-doc.cadn.net.cn

要禁用,请使用spring.ai.model.embedding=none(或任何与bedrock-cohere不匹配的值)spring-doc.cadn.net.cn

此改动旨在允许配置多个模型。spring-doc.cadn.net.cn

前缀spring.ai.bedrock.cohere.embedding(定义于BedrockCoheredingEmbdingProperties)是配置Cohere嵌入模型实现的属性前缀。spring-doc.cadn.net.cn

属性spring-doc.cadn.net.cn

描述spring-doc.cadn.net.cn

默认值spring-doc.cadn.net.cn

spring.ai.model.embeddingspring-doc.cadn.net.cn

启用或禁用对 Cohere 的支持spring-doc.cadn.net.cn

基岩-凝聚力spring-doc.cadn.net.cn

spring.ai.bedrock.cohere.embedding.enabled(已移除且不再有效)spring-doc.cadn.net.cn

启用或禁用对 Cohere 的支持spring-doc.cadn.net.cn

falsespring-doc.cadn.net.cn

spring.ai.bedrock.cohere.embedding.modelspring-doc.cadn.net.cn

型号ID。有关支持的模型,请参见 CohereEmbeddingModelspring-doc.cadn.net.cn

cohere.embed-multilingual-v3spring-doc.cadn.net.cn

spring.ai.bedrock.cohere.embedding.options.input-typespring-doc.cadn.net.cn

在前加特殊标记以区分每种类型。除了搜索和检索时混合类型外,不应混合不同类型。在这种情况下,将语料库嵌入search_document类型,嵌入查询类型为search_query类型。spring-doc.cadn.net.cn

SEARCH_DOCUMENTspring-doc.cadn.net.cn

spring.ai.bedrock.cohere.embedding.options.truncatespring-doc.cadn.net.cn

指定API如何处理超过最大Tokens长度的输入。如果你指定LEFT或RIGHT,模型会丢弃该输入,直到剩余输入正好是模型的最大输入标记长度。spring-doc.cadn.net.cn

没有spring-doc.cadn.net.cn

通过亚马逊Bedrock访问Cohere时,无法提供截断功能。这是Amazon Bedrock的问题。春季人工智能课程BedrockCohereembdingModel将截断为2048字符长度,这是模型支持的最大值。

可以看看 CohereEmbeddingModel 里的其他模型 ID。 支持的值有:cohere.embed-multilingual-v3cohere.embed-english-v3. 模型ID值也可以在AWS Bedrock文档中找到基础模型ID的资料。spring-doc.cadn.net.cn

所有以spring.ai.bedrock.cohere.embedding.optionss可以通过在运行时为 在嵌入请求叫。

运行时选项

BedrockCohereEmbeddingOptions.java提供模型配置,例如输入类型截断.spring-doc.cadn.net.cn

启动时,默认选项可以配置为BedrockCohereEmbeddingModel(api, options)构造者或spring.ai.bedrock.cohere.embedding.options.*性能。spring-doc.cadn.net.cn

运行时,你可以通过添加新的、针对请求的选项来覆盖默认选项嵌入请求叫。 例如,为了覆盖特定请求的默认输入类型:spring-doc.cadn.net.cn

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)依赖。spring-doc.cadn.net.cn

添加一个application.properties文件,在src/主/资源目录,用于启用和配置 Cohere 嵌入模型:spring-doc.cadn.net.cn

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使用聊天模式生成文本的课程。spring-doc.cadn.net.cn

@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 服务。spring-doc.cadn.net.cn

添加春艾基岩对你项目Maven的依赖pom.xml文件:spring-doc.cadn.net.cn

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-bedrock</artifactId>
</dependency>

或者去你的Gradlebuild.gradle构建文件。spring-doc.cadn.net.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-bedrock'
}
请参考依赖管理部分,将Spring AI的物料清单添加到你的构建文件中。

接下来,创建一个 BedrockCohereEmbeddingModel,并用于文本嵌入:spring-doc.cadn.net.cn

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 客户端。spring-doc.cadn.net.cn

下图展示了 CohereEmbeddingBedrockApi 接口和构建模块:spring-doc.cadn.net.cn

基岩凝聚物嵌入低级别API

CohereEmbeddingBedrockApi 支持cohere.embed-english-v3cohere.embed-multilingual-v3单次和批量嵌入计算模型。spring-doc.cadn.net.cn

这里有一个简单的示例,说明如何程序化使用该 API:spring-doc.cadn.net.cn

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);