PostgresML 嵌入
Spring AI 支持 PostgresML 文本嵌入模型。
嵌入是文本的数字表示。它们用于将单词和句子表示为向量(一个数字数组)。通过使用距离度量比较数值向量的相似性,嵌入可用于查找相似的文本片段,或者它们可以用作其他机器学习模型的输入特征,因为大多数算法无法直接使用文本。
可以在 PostgresML 中使用许多预训练的 LLM 从文本生成嵌入。您可以浏览 Hugging Face 上所有可用的模型,找到最佳解决方案。
自动配置
Spring AI 的自动配置、Starter 模块的工件名称发生了重大变化。请参阅升级说明了解更多信息。 |
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.postgresml.embedding
是配置 PostgresML 嵌入的 EmbeddingModel
实现的属性前缀。
属性 |
描述 |
默认值 |
|
启用 PostgresML 嵌入模型。 |
|
|
启用 PostgresML 嵌入模型。 |
|
|
执行 SQL 'CREATE EXTENSION IF NOT EXISTS pgml' 以启用扩展 |
|
|
用于嵌入的 Hugging Face Transformer 模型。 |
|
|
额外的 Transformer 特有选项。 |
空映射 |
|
用于嵌入的 PostgresML 向量类型。支持两个选项: |
|
|
文档元数据聚合模式 |
|
所有带有 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()));
示例 Controller
这将创建一个 EmbeddingModel
实现,您可以将其注入到您的类中。这里是一个简单的 @Controller
类使用 EmbeddingModel
实现的示例。
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);
}
}
手动配置
除了使用 Spring Boot 自动配置外,您还可以手动创建 PostgresMlEmbeddingModel
。为此,请将 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());
}