Improve readability of the PgVector Store code

Related to #461
This commit is contained in:
Christian Tzolov
2024-03-03 08:54:04 +01:00
parent 811d048965
commit cbb59ce616
2 changed files with 12 additions and 7 deletions

View File

@@ -120,7 +120,7 @@ vectorStore.add(List.of(document));
List<Document> 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:

View File

@@ -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() {