Skip to content

Commit 257b963

Browse files
sobychackomarkpollack
authored andcommitted
PgVectorStore package rename and builder support
- Move PgVectorStore and related classes to org.springframework.ai.pg.vectorstore package - Update builder pattern to use more idiomatic method names (e.g. withSchemaName -> schemaName) - Deprecate existing constructors and old Builder class in favor of new static builder() method - Update tests to reflect the new builder style usage - Update docs
1 parent 9e18652 commit 257b963

File tree

20 files changed

+286
-101
lines changed

20 files changed

+286
-101
lines changed

spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/pgvector.adoc

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ spring:
100100
index-type: HNSW
101101
distance-type: COSINE_DISTANCE
102102
dimensions: 1536
103+
batching-strategy: TOKEN_COUNT # Optional: Controls how documents are batched for embedding
104+
max-document-batch-size: 10000 # Optional: Maximum number of documents per batch
103105
----
104106

105107
TIP: If you run PGvector as a Spring Boot dev service via link:https://docs.spring.io/spring-boot/reference/features/dev-services.html#features.dev-services.docker-compose[Docker Compose]
@@ -108,7 +110,7 @@ you don't need to configure URL, username and password since they are autoconfig
108110

109111
TIP: Check the list of xref:#pgvector-properties[configuration parameters] to learn about the default values and configuration options.
110112

111-
Now you can auto-wire the `PgVectorStore` in your application and use it
113+
Now you can auto-wire the `VectorStore` in your application and use it
112114

113115
[source,java]
114116
----
@@ -137,14 +139,16 @@ You can use the following properties in your Spring Boot configuration to custom
137139
|===
138140
|Property| Description | Default value
139141

140-
|`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). Theres no training step like IVFFlat, so the index can be created without any data in the table.| HNSW
142+
|`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
141143
|`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
142144
|`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. | -
143145
|`spring.ai.vectorstore.pgvector.remove-existing-vector-store-table` | Deletes the existing `vector_store` table on start up. | false
144146
|`spring.ai.vectorstore.pgvector.initialize-schema` | Whether to initialize the required schema | false
145147
|`spring.ai.vectorstore.pgvector.schema-name` | Vector store schema name | `public`
146148
|`spring.ai.vectorstore.pgvector.table-name` | Vector store table name | `vector_store`
147149
|`spring.ai.vectorstore.pgvector.schema-validation` | Enables schema and table name validation to ensure they are valid and existing objects. | false
150+
|`spring.ai.vectorstore.pgvector.batching-strategy` | Strategy for batching documents when calculating embeddings. Options are `TOKEN_COUNT` or `FIXED_SIZE`. | TOKEN_COUNT
151+
|`spring.ai.vectorstore.pgvector.max-document-batch-size` | Maximum number of documents to process in a single batch. | 10000
148152

149153
|===
150154

@@ -182,7 +186,7 @@ vectorStore.similaritySearch(SearchRequest.defaults()
182186
b.eq("article_type", "blog")).build()));
183187
----
184188

185-
NOTE: These filter expressions are converted into the equivalent PgVector filters.
189+
NOTE: These filter expressions are converted into PostgreSQL JSON path expressions for efficient metadata filtering.
186190

187191
== Manual Configuration
188192

@@ -216,7 +220,17 @@ To configure PgVector in your application, you can use the following setup:
216220
----
217221
@Bean
218222
public VectorStore vectorStore(JdbcTemplate jdbcTemplate, EmbeddingModel embeddingModel) {
219-
return new PgVectorStore(jdbcTemplate, embeddingModel);
223+
return PgVectorStore.builder()
224+
.jdbcTemplate(jdbcTemplate)
225+
.embeddingModel(embeddingModel)
226+
.dimensions(1536) // Optional: defaults to model dimensions or 1536
227+
.distanceType(COSINE_DISTANCE) // Optional: defaults to COSINE_DISTANCE
228+
.indexType(HNSW) // Optional: defaults to HNSW
229+
.initializeSchema(true) // Optional: defaults to false
230+
.schemaName("public") // Optional: defaults to "public"
231+
.vectorTableName("vector_store") // Optional: defaults to "vector_store"
232+
.maxDocumentBatchSize(10000) // Optional: defaults to 10000
233+
.build();
220234
}
221235
----
222236

spring-ai-docs/src/main/antora/modules/ROOT/pages/upgrade-notes.adoc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
[[upgrade-notes]]
22
= Upgrading Notes
33

4-
== Upgrading to 1.0.0.RC1
4+
5+
== Upgrading to 1.0.0.M5
6+
7+
* Vector Builders have been refactored for consistency.
8+
* Current VectorStore implementation constructors have been deprecated, use the builder pattern.
9+
* VectorStore implementation packages have been moved into unique package names, avoiding conflicts across artifact. For example `org.springframework.ai.vectorstore` to `org.springframework.ai.pgvector.vectorstore`.
10+
11+
== Upgrading to 1.0.0.RC3
512

613
* The type of the portable chat options (`frequencyPenalty`, `presencePenalty`, `temperature`, `topP`) has been changed from `Float` to `Double`.
714

spring-ai-integration-tests/src/test/java/org/springframework/ai/integration/tests/client/advisor/RetrievalAugmentationAdvisorIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
import org.springframework.ai.rag.retrieval.search.VectorStoreDocumentRetriever;
3939
import org.springframework.ai.reader.markdown.MarkdownDocumentReader;
4040
import org.springframework.ai.reader.markdown.config.MarkdownDocumentReaderConfig;
41-
import org.springframework.ai.vectorstore.PgVectorStore;
41+
import org.springframework.ai.pgvector.vectorstore.PgVectorStore;
4242
import org.springframework.beans.factory.annotation.Autowired;
4343
import org.springframework.beans.factory.annotation.Value;
4444
import org.springframework.boot.test.context.SpringBootTest;

spring-ai-integration-tests/src/test/java/org/springframework/ai/integration/tests/rag/retrieval/search/VectorStoreDocumentRetrieverIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import org.springframework.ai.rag.Query;
3030
import org.springframework.ai.rag.retrieval.search.DocumentRetriever;
3131
import org.springframework.ai.rag.retrieval.search.VectorStoreDocumentRetriever;
32-
import org.springframework.ai.vectorstore.PgVectorStore;
32+
import org.springframework.ai.pgvector.vectorstore.PgVectorStore;
3333
import org.springframework.ai.vectorstore.filter.Filter;
3434
import org.springframework.beans.factory.annotation.Autowired;
3535
import org.springframework.boot.test.context.SpringBootTest;

spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/vectorstore/pgvector/PgVectorStoreAutoConfiguration.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import org.springframework.ai.embedding.BatchingStrategy;
2424
import org.springframework.ai.embedding.EmbeddingModel;
2525
import org.springframework.ai.embedding.TokenCountBatchingStrategy;
26-
import org.springframework.ai.vectorstore.PgVectorStore;
26+
import org.springframework.ai.pgvector.vectorstore.PgVectorStore;
2727
import org.springframework.ai.vectorstore.observation.VectorStoreObservationConvention;
2828
import org.springframework.beans.factory.ObjectProvider;
2929
import org.springframework.boot.autoconfigure.AutoConfiguration;
@@ -62,18 +62,21 @@ public PgVectorStore vectorStore(JdbcTemplate jdbcTemplate, EmbeddingModel embed
6262

6363
var initializeSchema = properties.isInitializeSchema();
6464

65-
return new PgVectorStore.Builder(jdbcTemplate, embeddingModel).withSchemaName(properties.getSchemaName())
66-
.withVectorTableName(properties.getTableName())
67-
.withVectorTableValidationsEnabled(properties.isSchemaValidation())
68-
.withDimensions(properties.getDimensions())
69-
.withDistanceType(properties.getDistanceType())
70-
.withRemoveExistingVectorStoreTable(properties.isRemoveExistingVectorStoreTable())
71-
.withIndexType(properties.getIndexType())
72-
.withInitializeSchema(initializeSchema)
73-
.withObservationRegistry(observationRegistry.getIfUnique(() -> ObservationRegistry.NOOP))
74-
.withSearchObservationConvention(customObservationConvention.getIfAvailable(() -> null))
75-
.withBatchingStrategy(batchingStrategy)
76-
.withMaxDocumentBatchSize(properties.getMaxDocumentBatchSize())
65+
return PgVectorStore.builder()
66+
.jdbcTemplate(jdbcTemplate)
67+
.embeddingModel(embeddingModel)
68+
.schemaName(properties.getSchemaName())
69+
.vectorTableName(properties.getTableName())
70+
.vectorTableValidationsEnabled(properties.isSchemaValidation())
71+
.dimensions(properties.getDimensions())
72+
.distanceType(properties.getDistanceType())
73+
.removeExistingVectorStoreTable(properties.isRemoveExistingVectorStoreTable())
74+
.indexType(properties.getIndexType())
75+
.initializeSchema(initializeSchema)
76+
.observationRegistry(observationRegistry.getIfUnique(() -> ObservationRegistry.NOOP))
77+
.customObservationConvention(customObservationConvention.getIfAvailable(() -> null))
78+
.batchingStrategy(batchingStrategy)
79+
.maxDocumentBatchSize(properties.getMaxDocumentBatchSize())
7780
.build();
7881
}
7982

spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/vectorstore/pgvector/PgVectorStoreProperties.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
package org.springframework.ai.autoconfigure.vectorstore.pgvector;
1818

1919
import org.springframework.ai.autoconfigure.vectorstore.CommonVectorStoreProperties;
20-
import org.springframework.ai.vectorstore.PgVectorStore;
21-
import org.springframework.ai.vectorstore.PgVectorStore.PgDistanceType;
22-
import org.springframework.ai.vectorstore.PgVectorStore.PgIndexType;
20+
import org.springframework.ai.pgvector.vectorstore.PgVectorStore;
21+
import org.springframework.ai.pgvector.vectorstore.PgVectorStore.PgDistanceType;
22+
import org.springframework.ai.pgvector.vectorstore.PgVectorStore.PgIndexType;
2323
import org.springframework.boot.context.properties.ConfigurationProperties;
2424

2525
/**

spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/vectorstore/pgvector/PgVectorStoreAutoConfigurationIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import org.springframework.ai.embedding.EmbeddingModel;
3434
import org.springframework.ai.observation.conventions.VectorStoreProvider;
3535
import org.springframework.ai.transformers.TransformersEmbeddingModel;
36-
import org.springframework.ai.vectorstore.PgVectorStore;
36+
import org.springframework.ai.pgvector.vectorstore.PgVectorStore;
3737
import org.springframework.ai.vectorstore.SearchRequest;
3838
import org.springframework.ai.vectorstore.observation.VectorStoreObservationContext;
3939
import org.springframework.boot.autoconfigure.AutoConfigurations;

spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/vectorstore/pgvector/PgVectorStorePropertiesTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818

1919
import org.junit.jupiter.api.Test;
2020

21-
import org.springframework.ai.vectorstore.PgVectorStore;
22-
import org.springframework.ai.vectorstore.PgVectorStore.PgDistanceType;
23-
import org.springframework.ai.vectorstore.PgVectorStore.PgIndexType;
21+
import org.springframework.ai.pgvector.vectorstore.PgVectorStore;
22+
import org.springframework.ai.pgvector.vectorstore.PgVectorStore.PgDistanceType;
23+
import org.springframework.ai.pgvector.vectorstore.PgVectorStore.PgIndexType;
2424

2525
import static org.assertj.core.api.Assertions.assertThat;
2626

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.ai.vectorstore;
17+
package org.springframework.ai.pgvector.vectorstore;
1818

1919
import java.util.List;
2020

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.ai.vectorstore;
17+
package org.springframework.ai.pgvector.vectorstore;
1818

1919
import java.util.ArrayList;
2020
import java.util.List;

0 commit comments

Comments
 (0)