主题
PostgresML 嵌入
Spring AI 支持 PostgresML 的文本嵌入模型。
嵌入是文本的数值表示。 它们把词语和句子表示为向量,即数字数组。 嵌入可用于查找相似的文本,方法是通过距离度量比较数值向量的相似度;它们也可作为其他机器学习模型的输入特征,因为大多数算法无法直接使用文本。
可以在 PostgresML 内使用许多预训练的 LLM 从文本生成嵌入。 你可以浏览所有模型 以在 Hugging Face 上找到最佳方案。
添加仓库与 BOM
Spring AI 的制品发布在 Maven Central 和 Spring Snapshot 仓库中。 参见 制品仓库 一节,把这些仓库添加到你的构建系统。
为便于依赖管理,Spring AI 提供了一个 BOM(物料清单),以确保整个项目中使用一致版本的 Spring AI。参见 依赖管理 一节,把 Spring AI BOM 添加到你的构建系统。
自动配置
NOTE
Spring AI 的自动配置、starter 模块的制品名称发生了重大变更。 更多信息请参阅 升级说明。
Spring AI 为 PostgresML 嵌入模型提供 Spring Boot 自动配置。 要启用它,请将以下依赖添加到你的项目的 Maven pom.xml 文件:
xml
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-postgresml-embedding</artifactId>
</dependency>1
2
3
4
2
3
4
或添加到你的 Gradle build.gradle 构建文件。
groovy
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-model-postgresml-embedding'
}1
2
3
2
3
TIP
参见 依赖管理 一节,把 Spring AI BOM 添加到你的构建文件。
使用 spring.ai.postgresml.embedding.* 属性来配置你的 PostgresMlEmbeddingModel。 links
嵌入属性
NOTE
嵌入自动配置的启用与禁用,现已通过以 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' 以启用该扩展 | false |
| spring.ai.postgresml.embedding.transformer | 用于嵌入的 Hugging Face 变换器模型。 | distilbert-base-uncased |
| spring.ai.postgresml.embedding.kwargs | 额外的变换器专用选项。 | empty map |
| spring.ai.postgresml.embedding.vector-type | 用于嵌入的 PostgresML 向量类型。支持两种:PG_ARRAY 和 PG_VECTOR。 | PG_ARRAY |
| spring.ai.postgresml.embedding.metadata-mode | 文档元数据聚合模式 | EMBED |
TIP
所有以 spring.ai.postgresml.embedding 为前缀的属性,都可通过在 EmbeddingRequest 调用中添加一个请求专用的 embedding-options 在运行时被覆盖。
运行时选项
使用 PostgresMlEmbeddingOptions.java 通过选项配置 PostgresMlEmbeddingModel,例如要使用的模型等。
在启动时,你可以把一个 PostgresMlEmbeddingOptions 传给 PostgresMlEmbeddingModel 构造器,以配置所有嵌入请求使用的默认选项。
在运行时,你可以使用 EmbeddingRequest 中的 PostgresMlEmbeddingOptions 来覆盖默认选项。
例如,为某个特定请求覆盖默认模型名:
java
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()));1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
示例控制器
这会创建一个可注入到你类中的 EmbeddingModel 实现。 下面是一个使用 EmbeddingModel 实现的简单 @Controller 类示例。
application.properties
spring.ai.postgresml.embedding.transformer=distilbert-base-uncased
spring.ai.postgresml.embedding.vector-type=PG_ARRAY
spring.ai.postgresml.embedding.metadata-mode=EMBED
spring.ai.postgresml.embedding.kwargs.device=cpu1
2
3
4
2
3
4
java
@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);
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
手动配置
与其使用 Spring Boot 自动配置,不如手动创建 PostgresMlEmbeddingModel。 为此,将 spring-ai-postgresml 依赖添加到你的项目的 Maven pom.xml 文件:
xml
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-postgresml</artifactId>
</dependency>1
2
3
4
2
3
4
或添加到你的 Gradle build.gradle 构建文件。
groovy
dependencies {
implementation 'org.springframework.ai:spring-ai-postgresml'
}1
2
3
2
3
TIP
参见 依赖管理 一节,把 Spring AI BOM 添加到你的构建文件。
接下来,创建一个 PostgresMlEmbeddingModel 实例,并用它计算两个输入文本之间的相似度:
java
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"));1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
NOTE
手动创建时,你必须在设置属性之后、使用客户端之前调用 afterPropertiesSet()。
把 PostgresMlEmbeddingModel 作为一个 @Bean 来创建更便捷(也更推荐)。 那样你就无需手动调用 afterPropertiesSet():
java
@Bean
public EmbeddingModel embeddingModel(JdbcTemplate jdbcTemplate) {
return new PostgresMlEmbeddingModel(jdbcTemplate,
PostgresMlEmbeddingOptions.builder()
....
.build());
}1
2
3
4
5
6
7
2
3
4
5
6
7