PGvector
This section walks you through setting up the PGvector VectorStore
to store document embeddings and perform similarity searches.
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
First you need access to PostgreSQL instance with enabled vector
, hstore
and uuid-ossp
extensions.
You can run a PGvector database as a Spring Boot dev service via Docker Compose or Testcontainers. In alternative, the setup local Postgres/PGVector appendix shows how to set up a DB locally with a Docker container. |
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);
replace the 1536 with the actual embedding dimension if you are using a different dimension. PGvector supports at most 2000 dimensions for HNSW indexes.
|
Next, if required, an API key for the EmbeddingModel to generate the embeddings stored by the PgVectorStore
.
Auto-Configuration
Then add the PgVectorStore boot starter dependency to your project:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-pgvector-store-spring-boot-starter</artifactId>
</dependency>
or to your Gradle build.gradle
build file.
dependencies {
implementation 'org.springframework.ai:spring-ai-pgvector-store-spring-boot-starter'
}
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.
This is a breaking change! In earlier versions of Spring AI, this schema initialization happened by default. |
The Vector Store also requires an EmbeddingModel
instance to calculate embeddings for the documents.
You can pick one of the available EmbeddingModel Implementations.
For example, to use the OpenAI EmbeddingModel, add the following dependency to your project:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
or to your Gradle build.gradle
build file.
dependencies {
implementation 'org.springframework.ai:spring-ai-openai-spring-boot-starter'
}
Refer to the Dependency Management section to add the Spring AI BOM to your build file. Refer to the Repositories section to add Milestone and/or Snapshot Repositories to your build file. |
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
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. |
Check the list of configuration parameters to learn about the default values and configuration options. |
Now you can auto-wire the PgVectorStore
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.query("Spring").withTopK(5));
Configuration properties
You can use the following properties in your Spring Boot configuration to customize the PGVector vector store.
Property | Description | Default value |
---|---|---|
|
Nearest neighbor search index type. Options are |
HNSW |
|
Search distance type. Defaults to |
COSINE_DISTANCE |
|
Embeddings dimension. If not specified explicitly the PgVectorStore will retrieve the dimensions form the provided |
- |
|
Deletes the existing |
false |
|
Whether to initialize the required schema |
false |
|
Vector store schema name |
|
|
Vector store table name |
|
|
Enables schema and table name validation to ensure they are valid and existing objects. |
false |
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
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.defaults()
.withQuery("The World")
.withTopK(TOP_K)
.withSimilarityThreshold(SIMILARITY_THRESHOLD)
.withFilterExpression("author in ['john', 'jill'] && article_type == 'blog'"));
or programmatically using the Filter.Expression
DSL:
FilterExpressionBuilder b = new FilterExpressionBuilder();
vectorStore.similaritySearch(SearchRequest.defaults()
.withQuery("The World")
.withTopK(TOP_K)
.withSimilarityThreshold(SIMILARITY_THRESHOLD)
.withFilterExpression(b.and(
b.in("author","john", "jill"),
b.eq("article_type", "blog")).build()));
These filter expressions are converted into the equivalent PgVector filters. |
Manual Configuration
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>
Refer to the Dependency Management section to add the Spring AI BOM to your build file. |
To configure PgVector in your application, you can use the following setup:
@Bean
public VectorStore vectorStore(JdbcTemplate jdbcTemplate, EmbeddingModel embeddingModel) {
return new PgVectorStore(jdbcTemplate, embeddingModel);
}
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