💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows
Python agentsaiai-agentsembeddingsinformation-retrievallanguage-modellarge-language-modelsllmnlppythonragretrieval-augmented-generationsearchsearch-enginesemantic-searchsentence-embeddingstransformerstxtaivector-databasevector-search
Modify pgvector ann index to build after data load, closes #938
davidmezzetti1edf3ecc4c7d
diff --git a/src/python/txtai/ann/dense/pgvector.py b/src/python/txtai/ann/dense/pgvector.pyindex 5b78cc1b..577aecd6 100644--- a/src/python/txtai/ann/dense/pgvector.py+++ b/src/python/txtai/ann/dense/pgvector.py@@ -21,6 +21,7 @@ except ImportError:from ..base import ANN+# pylint: disable=R0904class PGVector(ANN):"""Builds an ANN index backed by a Postgres database.@@ -50,6 +51,9 @@ class PGVector(ANN):# Prepare embeddings and insert rowsself.database.execute(self.table.insert(), [{"indexid": x, "embedding": self.prepare(row)} for x, row in enumerate(embeddings)])+ # Create index+ self.createindex()+# Add id offset and index build metadataself.config["offset"] = embeddings.shape[0]self.metadata(self.settings())@@ -113,11 +117,23 @@ class PGVector(ANN):# Table nametable = self.setting("table", self.defaulttable())- # Get embedding column and index settings- column, index = self.column()-# Create vectors table- self.table = Table(table, MetaData(), Column("indexid", Integer, primary_key=True, autoincrement=False), Column("embedding", column))+ self.table = Table(table, MetaData(), Column("indexid", Integer, primary_key=True, autoincrement=False), Column("embedding", self.column()))++ # Drop and recreate table+ if recreate:+ self.table.drop(self.connection, checkfirst=True)++ # Create table and index+ self.table.create(self.connection, checkfirst=True)++ def createindex(self):+ """+ Creates a index with the current settings.+ """++ # Table name+ table = self.setting("table", self.defaulttable())# Create ANN index - inner product is equal to cosine similarity on normalized vectorsindex = Index(@@ -125,16 +141,11 @@ class PGVector(ANN):self.table.c["embedding"],postgresql_using="hnsw",postgresql_with=self.settings(),- postgresql_ops={"embedding": index},+ postgresql_ops={"embedding": self.operation()},)- # Drop and recreate table- if recreate:- self.table.drop(self.connection, checkfirst=True)- index.drop(self.connection, checkfirst=True)-- # Create table and index- self.table.create(self.connection, checkfirst=True)+ # Create or recreate index+ index.drop(self.connection, checkfirst=True)index.create(self.connection, checkfirst=True)def connect(self):@@ -213,22 +224,41 @@ class PGVector(ANN):def column(self):"""- Gets embedding column and index definitions for the current settings.+ Gets embedding column for the current settings.++ Returns:+ embedding column definition+ """++ if self.qbits:+ # If quantization is set, always return BIT vectors+ return BIT(self.config["dimensions"] * 8)++ if self.setting("precision") == "half":+ # 16-bit HALF precision vectors+ return HALFVEC(self.config["dimensions"])++ # Default is full 32-bit FULL precision vectors+ return VECTOR(self.config["dimensions"])++ def operation(self):+ """+ Gets the index operation for the current settings.Returns:- embedding column definition, index definition+ index operation"""if self.qbits:# If quantization is set, always return BIT vectors- return BIT(self.config["dimensions"] * 8), "bit_hamming_ops"+ return "bit_hamming_ops"if self.setting("precision") == "half":# 16-bit HALF precision vectors- return HALFVEC(self.config["dimensions"]), "halfvec_ip_ops"+ return "halfvec_ip_ops"# Default is full 32-bit FULL precision vectors- return VECTOR(self.config["dimensions"]), "vector_ip_ops"+ return "vector_ip_ops"def prepare(self, data):"""