PGvector

本部分将引导您设置 PGvector VectorStore 以存储文档嵌入并执行相似性搜索。

This section walks you through setting up the PGvector VectorStore to store document embeddings and perform similarity searches.

PGvector 是 PostgreSQL 的开源扩展,支持存储和搜索机器学习生成的嵌入。它提供了不同的功能,让用户能够识别精确最近邻和近似最近邻。它设计用于与其他 PostgreSQL 特性(包括索引和查询)无缝协作。

PGvector is an open-source extension for PostgreSQL that enables storing and searching over machine learning-generated embeddings. It provides different capabilities that let users identify both exact and approximate nearest neighbors. It is designed to work seamlessly with other PostgreSQL features, including indexing and querying.

Prerequisites

首先,您需要访问启用了 vectorhstoreuuid-ossp 扩展的 PostgreSQL 实例。

First you need access to PostgreSQL instance with enabled vector, hstore and uuid-ossp extensions.

您可以通过 Docker ComposeTestcontainers 将 PGvector 数据库作为 Spring Boot 开发服务运行。另外, setup local Postgres/PGVector 附录展示了如何使用 Docker 容器在本地设置数据库。

You can run a PGvector database as a Spring Boot dev service via Docker Compose or Testcontainers. In alternative, the _run_postgres_pgvector_db_locally appendix shows how to set up a DB locally with a Docker container.

在启动时, PgVectorStore 将尝试安装所需的数据库扩展,并在不存在的情况下创建所需的 vector_store 表和索引。

On startup, the PgVectorStore will attempt to install the required database extensions and create the required vector_store table with an index if not existing.

您还可以按如下所示手动执行此操作:

Optionally, you can do this manually like so:

CREATE EXTENSION IF NOT EXISTS vector;
CREATE EXTENSION IF NOT EXISTS hstore;
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

CREATE TABLE IF NOT EXISTS vector_store (
	id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
	content text,
	metadata json,
	embedding vector(1536) // 1536 is the default embedding dimension
);

CREATE INDEX ON vector_store USING HNSW (embedding vector_cosine_ops);

如果您使用不同的维度,请将 1536 替换为实际的嵌入维度。PGvector 支持 HNSW 索引最多 2000 个维度。

replace the 1536 with the actual embedding dimension if you are using a different dimension. PGvector supports at most 2000 dimensions for HNSW indexes.

接下来,如果需要,可以使用 EmbeddingModel 的 API 密钥生成由 PgVectorStore 存储的嵌入。

Next, if required, an API key for the EmbeddingModel to generate the embeddings stored by the PgVectorStore.

Auto-Configuration

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

There has been a significant change in the Spring AI auto-configuration, starter modules' artifact names. Please refer to the upgrade notes for more information.

然后,将 PgVectorStore 引导启动程序依赖项添加到您的项目:

Then add the PgVectorStore boot starter dependency to your project:

<dependency>
	<groupId>org.springframework.ai</groupId>
	<artifactId>spring-ai-starter-vector-store-pgvector</artifactId>
</dependency>

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

or to your Gradle build.gradle build file.

dependencies {
    implementation 'org.springframework.ai:spring-ai-starter-vector-store-pgvector'
}

向量存储实现可以为您初始化所需的模式,但您必须通过在相应的构造函数中指定 initializeSchema 布尔值或通过在 application.properties 文件中设置 …​initialize-schema=true 来选择加入。

The vector store implementation can initialize the required schema for you, but you must opt-in by specifying the initializeSchema boolean in the appropriate constructor or by setting …​initialize-schema=true in the application.properties file.

这是一个重大更改!在早期版本的 Spring AI 中,此模式初始化是默认发生的。

This is a breaking change! In earlier versions of Spring AI, this schema initialization happened by default.

向量存储还需要一个 EmbeddingModel 实例来计算文档的嵌入。您可以选择其中一个可用的 EmbeddingModel Implementations

The Vector Store also requires an EmbeddingModel instance to calculate embeddings for the documents. You can pick one of the available EmbeddingModel Implementations.

例如,要使用 OpenAI EmbeddingModel ,请将以下依赖项添加到您的项目中:

For example, to use the OpenAI EmbeddingModel, add the following dependency to your project:

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

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

or to your Gradle build.gradle build file.

dependencies {
    implementation 'org.springframework.ai:spring-ai-starter-model-openai'
}

请参阅 Dependency Management 部分,将 Spring AI BOM 添加到您的构建文件中。请参阅 Artifact Repositories 部分,将 Maven Central 和/或快照存储库添加到您的构建文件中。

Refer to the Dependency Management section to add the Spring AI BOM to your build file. Refer to the Artifact Repositories section to add Maven Central and/or Snapshot Repositories to your build file.

要连接和配置 PgVectorStore ,您需要提供实例的访问详细信息。可以通过 Spring Boot 的 application.yml 提供一个简单的配置。

To connect to and configure the PgVectorStore, you need to provide access details for your instance. A simple configuration can be provided via Spring Boot’s application.yml.

spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/postgres
    username: postgres
    password: postgres
  ai:
	vectorstore:
	  pgvector:
		index-type: HNSW
		distance-type: COSINE_DISTANCE
		dimensions: 1536
		max-document-batch-size: 10000 # Optional: Maximum number of documents per batch

如果您通过 Docker ComposeTestcontainers 将 PGvector 作为 Spring Boot 开发服务运行,则无需配置 URL、用户名和密码,因为它们由 Spring Boot 自动配置。

If you run PGvector as a Spring Boot dev service via Docker Compose or Testcontainers, you don’t need to configure URL, username and password since they are autoconfigured by Spring Boot.

查看 configuration parameters 的列表以了解默认值和配置选项。

Check the list of pgvector-properties to learn about the default values and configuration options.

现在您可以在应用程序中自动装配 VectorStore 并使用它。

Now you can auto-wire the VectorStore in your application and use it

@Autowired VectorStore vectorStore;

// ...

List<Document> documents = List.of(
    new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("meta1", "meta1")),
    new Document("The World is Big and Salvation Lurks Around the Corner"),
    new Document("You walk forward facing the past and you turn back toward the future.", Map.of("meta2", "meta2")));

// Add the documents to PGVector
vectorStore.add(documents);

// Retrieve documents similar to a query
List<Document> results = this.vectorStore.similaritySearch(SearchRequest.builder().query("Spring").topK(5).build());

Configuration properties

您可以在 Spring Boot 配置中使用以下属性来自定义 PGVector 向量存储。

You can use the following properties in your Spring Boot configuration to customize the PGVector vector store.

Property Description Default value

spring.ai.vectorstore.pgvector.index-type

Nearest neighbor search index type. Options are NONE - exact nearest neighbor search, IVFFlat - index divides vectors into lists, and then searches a subset of those lists that are closest to the query vector. It has faster build times and uses less memory than HNSW, but has lower query performance (in terms of speed-recall tradeoff). HNSW - creates a multilayer graph. It has slower build times and uses more memory than IVFFlat, but has better query performance (in terms of speed-recall tradeoff). There’s no training step like IVFFlat, so the index can be created without any data in the table.

HNSW

spring.ai.vectorstore.pgvector.distance-type

Search distance type. Defaults to COSINE_DISTANCE. But if vectors are normalized to length 1, you can use EUCLIDEAN_DISTANCE or NEGATIVE_INNER_PRODUCT for best performance.

COSINE_DISTANCE

spring.ai.vectorstore.pgvector.dimensions

Embeddings dimension. If not specified explicitly the PgVectorStore will retrieve the dimensions form the provided EmbeddingModel. Dimensions are set to the embedding column the on table creation. If you change the dimensions your would have to re-create the vector_store table as well.

-

spring.ai.vectorstore.pgvector.remove-existing-vector-store-table

Deletes the existing vector_store table on start up.

false

spring.ai.vectorstore.pgvector.initialize-schema

Whether to initialize the required schema

false

spring.ai.vectorstore.pgvector.schema-name

Vector store schema name

public

spring.ai.vectorstore.pgvector.table-name

Vector store table name

vector_store

spring.ai.vectorstore.pgvector.schema-validation

Enables schema and table name validation to ensure they are valid and existing objects.

false

spring.ai.vectorstore.pgvector.max-document-batch-size

Maximum number of documents to process in a single batch.

10000

如果您配置了自定义模式和/或表名,请考虑通过设置 spring.ai.vectorstore.pgvector.schema-validation=true 来启用模式验证。这可确保名称的正确性并降低 SQL 注入攻击的风险。

If you configure a custom schema and/or table name, consider enabling schema validation by setting spring.ai.vectorstore.pgvector.schema-validation=true. This ensures the correctness of the names and reduces the risk of SQL injection attacks.

Metadata filtering

您可以利用 PgVector 存储中通用的可移植过滤器 metadata filters

You can leverage the generic, portable metadata filters with the PgVector store.

例如,你可以使用文本表达式语言:

For example, you can use either the text expression language:

vectorStore.similaritySearch(
    SearchRequest.builder()
    .query("The World")
    .topK(TOP_K)
    .similarityThreshold(SIMILARITY_THRESHOLD)
    .filterExpression("author in ['john', 'jill'] && article_type == 'blog'").build());

或使用 Filter.Expression DSL 以编程方式:

or programmatically using the Filter.Expression DSL:

FilterExpressionBuilder b = new FilterExpressionBuilder();

vectorStore.similaritySearch(SearchRequest.builder()
    .query("The World")
    .topK(TOP_K)
    .similarityThreshold(SIMILARITY_THRESHOLD)
    .filterExpression(b.and(
        b.in("author","john", "jill"),
        b.eq("article_type", "blog")).build()).build());

这些过滤器表达式被转换为 PostgreSQL JSON 路径表达式,以实现高效的元数据过滤。

These filter expressions are converted into PostgreSQL JSON path expressions for efficient metadata filtering.

Manual Configuration

您可以手动配置 PgVectorStore,而不是使用 Spring Boot 自动配置。为此,您需要向您的项目添加 PostgreSQL 连接和 JdbcTemplate 自动配置依赖项:

Instead of using the Spring Boot auto-configuration, you can manually configure the PgVectorStore. For this you need to add the PostgreSQL connection and JdbcTemplate auto-configuration dependencies to your project:

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

<dependency>
	<groupId>org.postgresql</groupId>
	<artifactId>postgresql</artifactId>
	<scope>runtime</scope>
</dependency>

<dependency>
	<groupId>org.springframework.ai</groupId>
	<artifactId>spring-ai-pgvector-store</artifactId>
</dependency>
  1. 参见 Dependency Management 部分,将 Spring AI BOM 添加到你的构建文件中。

Refer to the Dependency Management section to add the Spring AI BOM to your build file.

要在应用程序中配置 PgVector,可以使用以下设置:

To configure PgVector in your application, you can use the following setup:

@Bean
public VectorStore vectorStore(JdbcTemplate jdbcTemplate, EmbeddingModel embeddingModel) {
    return PgVectorStore.builder(jdbcTemplate, embeddingModel)
        .dimensions(1536)                    // Optional: defaults to model dimensions or 1536
        .distanceType(COSINE_DISTANCE)       // Optional: defaults to COSINE_DISTANCE
        .indexType(HNSW)                     // Optional: defaults to HNSW
        .initializeSchema(true)              // Optional: defaults to false
        .schemaName("public")                // Optional: defaults to "public"
        .vectorTableName("vector_store")     // Optional: defaults to "vector_store"
        .maxDocumentBatchSize(10000)         // Optional: defaults to 10000
        .build();
}

Run Postgres & PGVector DB locally

docker run -it --rm --name postgres -p 5432:5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres pgvector/pgvector

您可以像这样连接到此服务器:

You can connect to this server like this:

psql -U postgres -h localhost -p 5432

Accessing the Native Client

PGVector Store 实现通过 getNativeClient() 方法提供对底层原生 JDBC 客户端 ( JdbcTemplate ) 的访问:

The PGVector Store implementation provides access to the underlying native JDBC client (JdbcTemplate) through the getNativeClient() method:

PgVectorStore vectorStore = context.getBean(PgVectorStore.class);
Optional<JdbcTemplate> nativeClient = vectorStore.getNativeClient();

if (nativeClient.isPresent()) {
    JdbcTemplate jdbc = nativeClient.get();
    // Use the native client for PostgreSQL-specific operations
}

原生客户端允许您访问 PostgreSQL 特定的功能和操作,这些功能和操作可能未通过 VectorStore 接口公开。

The native client gives you access to PostgreSQL-specific features and operations that might not be exposed through the VectorStore interface.