From cbb59ce616e526023ea5518d6894f9877219f3da Mon Sep 17 00:00:00 2001 From: Christian Tzolov Date: Sun, 3 Mar 2024 08:54:04 +0100 Subject: [PATCH] Improve readability of the PgVector Store code Related to #461 --- .../ROOT/pages/api/vectordbs/pgvector.adoc | 2 +- .../ai/vectorstore/PgVectorStore.java | 17 +++++++++++------ 2 files changed, 12 insertions(+), 7 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 190d57589..cf01482c9 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 @@ -120,7 +120,7 @@ vectorStore.add(List.of(document)); List results = vectorStore.similaritySearch(SearchRequest.query("Spring").withTopK(5)); ---- -== Manual Configuration +=== 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: diff --git a/vector-stores/spring-ai-pgvector-store/src/main/java/org/springframework/ai/vectorstore/PgVectorStore.java b/vector-stores/spring-ai-pgvector-store/src/main/java/org/springframework/ai/vectorstore/PgVectorStore.java index 82d499e88..37941a15a 100644 --- a/vector-stores/spring-ai-pgvector-store/src/main/java/org/springframework/ai/vectorstore/PgVectorStore.java +++ b/vector-stores/spring-ai-pgvector-store/src/main/java/org/springframework/ai/vectorstore/PgVectorStore.java @@ -342,15 +342,20 @@ public class PgVectorStore implements VectorStore, InitializingBean { this.jdbcTemplate.execute("DROP TABLE IF EXISTS " + VECTOR_TABLE_NAME); } - this.jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS " + VECTOR_TABLE_NAME + " ( " - + "id uuid DEFAULT uuid_generate_v4 () PRIMARY KEY, " + "content text, " + "metadata json, " - + "embedding vector(" + this.embeddingDimensions() + "))"); + this.jdbcTemplate.execute(String.format(""" + CREATE TABLE IF NOT EXISTS %s ( + id uuid DEFAULT uuid_generate_v4() PRIMARY KEY, + content text, + metadata json, + embedding vector(%d) + ) + """, VECTOR_TABLE_NAME, this.embeddingDimensions())); if (this.createIndexMethod != PgIndexType.NONE) { - this.jdbcTemplate.execute("CREATE INDEX ON " + VECTOR_TABLE_NAME + " USING " + this.createIndexMethod - + " (embedding " + this.getDistanceType().index + ")"); + this.jdbcTemplate.execute(String.format(""" + CREATE INDEX ON %s USING %s (embedding %s) + """, VECTOR_TABLE_NAME, this.createIndexMethod, this.getDistanceType().index)); } - } int embeddingDimensions() {