PostgresML 嵌入

Spring AI 支持 PostgresML 文本嵌入模型。

嵌入是文本的数字表示。它们用于将单词和句子表示为向量,即一组数字。嵌入可以通过比较数字向量之间的相似性(使用距离度量)来查找相似的文本片段,或者它们可以用作其他机器学习模型的输入特征,因为大多数算法不能直接使用文本。

许多预训练的 LLM 可以在 PostgresML 中用于从文本生成嵌入。您可以在 Hugging Face 上浏览所有可用的模型,找到最佳解决方案。

添加存储库和 BOM

Spring AI 工件发布在 Maven Central 和 Spring Snapshot 存储库中。请参阅 工件存储库 部分,将这些存储库添加到您的构建系统。

为了帮助管理依赖项,Spring AI 提供了一个 BOM(物料清单),以确保在整个项目中使用的 Spring AI 版本一致。请参阅 依赖项管理 部分,将 Spring AI BOM 添加到您的构建系统。

自动配置

Spring AI 自动配置、启动模块的工件名称发生了重大变化。请参阅 升级说明 以获取更多信息。

Spring AI 为 Azure PostgresML 嵌入模型提供 Spring Boot 自动配置。要启用它,请将以下依赖项添加到您项目的 Maven pom.xml 文件中

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

或添加到您的 Gradle build.gradle 构建文件中。

dependencies {
    implementation 'org.springframework.ai:spring-ai-starter-model-postgresml-embedding'
}
请参阅 依赖项管理 部分,将 Spring AI BOM 添加到您的构建文件中。

使用 spring.ai.postgresml.embedding.options.* 属性配置您的 PostgresMlEmbeddingModel。链接

嵌入属性

嵌入自动配置的启用和禁用现在通过前缀为 spring.ai.model.embedding 的顶级属性进行配置。

要启用,spring.ai.model.embedding=postgresml(默认已启用)

要禁用,spring.ai.model.embedding=none(或任何不匹配 postgresml 的值)

此更改是为了允许配置多个模型。

前缀 spring.ai.postgresml.embedding 是属性前缀,用于配置 PostgresML 嵌入的 EmbeddingModel 实现。

财产

描述

默认值

spring.ai.postgresml.embedding.enabled (已删除且不再有效)

启用 PostgresML 嵌入模型。

true

spring.ai.model.embedding

启用 PostgresML 嵌入模型。

postgresml

spring.ai.postgresml.embedding.create-extension

执行 SQL 'CREATE EXTENSION IF NOT EXISTS pgml' 以启用扩展

spring.ai.postgresml.embedding.options.transformer

用于嵌入的 Hugging Face 转换器模型。

distilbert-base-uncased

spring.ai.postgresml.embedding.options.kwargs

额外的转换器特定选项。

空映射

spring.ai.postgresml.embedding.options.vectorType

用于嵌入的 PostgresML 向量类型。支持两种选项:PG_ARRAYPG_VECTOR

PG_ARRAY

spring.ai.postgresml.embedding.options.metadataMode

文档元数据聚合模式

EMBED

所有以 spring.ai.postgresml.embedding.options 为前缀的属性都可以在运行时通过在 EmbeddingRequest 调用中添加请求特定的运行时选项来覆盖。

运行时选项

使用 PostgresMlEmbeddingOptions.java 配置 PostgresMlEmbeddingModel 的选项,例如要使用的模型等。

在启动时,您可以将 PostgresMlEmbeddingOptions 传递给 PostgresMlEmbeddingModel 构造函数,以配置所有嵌入请求的默认选项。

在运行时,您可以在 EmbeddingRequest 中使用 PostgresMlEmbeddingOptions 覆盖默认选项。

例如,为特定请求覆盖默认模型名称

EmbeddingResponse embeddingResponse = embeddingModel.call(
    new EmbeddingRequest(List.of("Hello World", "World is big and salvation is near"),
            PostgresMlEmbeddingOptions.builder()
                .transformer("intfloat/e5-small")
                .vectorType(VectorType.PG_ARRAY)
                .kwargs(Map.of("device", "gpu"))
                .build()));

示例控制器

这将创建一个 EmbeddingModel 实现,您可以将其注入到您的类中。这是一个使用 EmbeddingModel 实现的简单 @Controller 类的示例。

spring.ai.postgresml.embedding.options.transformer=distilbert-base-uncased
spring.ai.postgresml.embedding.options.vectorType=PG_ARRAY
spring.ai.postgresml.embedding.options.metadataMode=EMBED
spring.ai.postgresml.embedding.options.kwargs.device=cpu
@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);
    }
}

手动配置

您可以通过手动创建 PostgresMlEmbeddingModel 来替代使用 Spring Boot 自动配置。为此,请将 spring-ai-postgresml 依赖项添加到您项目的 Maven pom.xml 文件中

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

或添加到您的 Gradle build.gradle 构建文件中。

dependencies {
    implementation 'org.springframework.ai:spring-ai-postgresml'
}
请参阅 依赖项管理 部分,将 Spring AI BOM 添加到您的构建文件中。

接下来,创建 PostgresMlEmbeddingModel 实例并使用它来计算两个输入文本之间的相似性

var jdbcTemplate = new JdbcTemplate(dataSource); // your posgresml data source

PostgresMlEmbeddingModel embeddingModel = new PostgresMlEmbeddingModel(this.jdbcTemplate,
        PostgresMlEmbeddingOptions.builder()
            .transformer("distilbert-base-uncased") // huggingface transformer model name.
            .vectorType(VectorType.PG_VECTOR) //vector type in PostgreSQL.
            .kwargs(Map.of("device", "cpu")) // optional arguments.
            .metadataMode(MetadataMode.EMBED) // Document metadata mode.
            .build());

embeddingModel.afterPropertiesSet(); // initialize the jdbc template and database.

EmbeddingResponse embeddingResponse = this.embeddingModel
	.embedForResponse(List.of("Hello World", "World is big and salvation is near"));
手动创建时,您必须在设置属性之后、使用客户端之前调用 afterPropertiesSet()。将 PostgresMlEmbeddingModel 创建为 @Bean 更方便(且更受推荐)。这样您就不必手动调用 afterPropertiesSet()
@Bean
public EmbeddingModel embeddingModel(JdbcTemplate jdbcTemplate) {
    return new PostgresMlEmbeddingModel(jdbcTemplate,
        PostgresMlEmbeddingOptions.builder()
             ....
            .build());
}
© . This site is unofficial and not affiliated with VMware.