PostgresML嵌入

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

嵌入是文本的数字表示。它们用于将单词和句子表示为向量,即数字数组。嵌入可用于通过比较数字向量的相似性来

自动配置

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

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

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

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

使用spring.ai.postgresml.embedding.options.*属性来配置您的PostgresMlEmbeddingClient。links

嵌入属性

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

属性

描述

默认值

spring.ai.postgresml.embedding.enabled

启用 PostgresML 嵌入客户端。

true

spring.ai.postgresml.embedding.options.transformer

用于嵌入的 Huggingface transformer 模型。

distilbert-base-uncased

spring.ai.postgresml.embedding.options.kwargs

额外的 transformer 特定选项。

空映射

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 调用添加一个特定于请求的 EmbeddingOptions 来在运行时进行覆盖。

嵌入选项

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

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

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

例如,要覆盖特定请求的默认模型名称:

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

示例控制器(自动配置)

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

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 EmbeddingClient embeddingClient;

    @Autowired
    public EmbeddingController(EmbeddingClient embeddingClient) {
        this.embeddingClient = embeddingClient;
    }

    @GetMapping("/ai/embedding")
    public Map embed(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        EmbeddingResponse embeddingResponse = this.embeddingClient.embedForResponse(List.of(message));
        return Map.of("embedding", embeddingResponse);
    }
}

手动配置

而不是使用Spring Boot的自动配置,您可以手动创建PostgresMlEmbeddingClient。为此,请将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添加到您的构建文件中。

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

var jdbcTemplate = new JdbcTemplate(dataSource); // 您的posgresml数据源

PostgresMlEmbeddingClient embeddingClient = new PostgresMlEmbeddingClient(this.jdbcTemplate,
        PostgresMlEmbeddingOptions.builder()
            .withTransformer("distilbert-base-uncased") // huggingface转换器模型名称。
            .withVectorType(VectorType.PG_VECTOR) // PostgreSQL中的向量类型。
            .withKwargs(Map.of("device", "cpu")) // 可选参数。
            .withMetadataMode(MetadataMode.EMBED) // 文档元数据模式。
            .build());

embeddingClient.afterPropertiesSet(); // 初始化jdbc模板和数据库。

EmbeddingResponse embeddingResponse = embeddingClient
	.embedForResponse(List.of("Hello World", "World is big and salvation is near"));
手动创建时,必须在设置属性之后并在使用客户端之前调用afterPropertiesSet()。更方便(也更可取)的做法是将PostgresMlEmbeddingClient创建为@Bean。然后您就不必手动调用afterPropertiesSet()
@Bean
public EmbeddingClient embeddingClient(JdbcTemplate jdbcTemplate) {
    return new PostgresMlEmbeddingClient(jdbcTemplate,
        PostgresMlEmbeddingOptions.builder()
             ....
            .build());
}