From 6c07c4cf8f841ddd89e4eec5c6d25e66cfbd8163 Mon Sep 17 00:00:00 2001 From: Christian Tzolov Date: Fri, 12 Apr 2024 10:41:23 +0200 Subject: [PATCH] doc:improve pgvector layout --- .../ROOT/pages/api/vectordbs/pgvector.adoc | 102 +++++++++--------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/pgvector.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/pgvector.adoc index 6ab22fadd..d2111bc5e 100644 --- a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/pgvector.adoc +++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/pgvector.adoc @@ -34,7 +34,7 @@ TIP: replace the `1536` with the actual embedding dimension if you are using a d Next if required, an API key for the xref:api/embeddings.adoc#available-implementations[EmbeddingClient] to generate the embeddings stored by the `PgVectorStore`. -== Dependencies +== Auto-Configuration Then add the PgVectorStore boot starter dependency to your project: @@ -120,7 +120,56 @@ vectorStore.add(List.of(document)); List results = vectorStore.similaritySearch(SearchRequest.query("Spring").withTopK(5)); ---- -=== Manual Configuration +[[pgvector-properties]] +=== Configuration properties + +You can use the following properties in your Spring Boot configuration to customize the PGVector vector store. + +[cols="2,5,1"] +|=== +|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.dimension`| Embeddings dimension. If not specified explicitly the PgVectorStore will retrieve the dimensions form the provided `EmbeddingClient`. 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 + +|=== + +== Metadata filtering + +You can leverage the generic, portable link:https://docs.spring.io/spring-ai/reference/api/vectordbs.html#_metadata_filters[metadata filters] with the PgVector store. + +For example, you can use either the text expression language: + +[source,java] +---- +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: + +[source,java] +---- +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())); +---- + +NOTE: 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: @@ -156,55 +205,6 @@ public VectorStore vectorStore(JdbcTemplate jdbcTemplate, EmbeddingClient embedd } ---- -== Metadata filtering - -You can leverage the generic, portable link:https://docs.spring.io/spring-ai/reference/api/vectordbs.html#_metadata_filters[metadata filters] with the PgVector store. - -For example, you can use either the text expression language: - -[source,java] ----- -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: - -[source,java] ----- -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())); ----- - -NOTE: These filter expressions are converted into the equivalent PgVector filters. - -[[pgvector-properties]] -== PgVectorStore properties - -You can use the following properties in your Spring Boot configuration to customize the PGVector vector store. - -[cols="2,5,1"] -|=== -|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.dimension`| Embeddings dimension. If not specified explicitly the PgVectorStore will retrieve the dimensions form the provided `EmbeddingClient`. 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 -|=== - - == Run Postgres & PGVector DB locally ----