Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Add close methods to ANN and Graph, closes #711

davidmezzetti600152ca70e7
diff --git a/src/python/txtai/ann/base.py b/src/python/txtai/ann/base.py
index 5d66ae20..72767843 100644
--- a/src/python/txtai/ann/base.py
+++ b/src/python/txtai/ann/base.py
@@ -102,6 +102,13 @@ class ANN:
raise NotImplementedError
+ def close(self):
+ """
+ Closes this ANN.
+ """
+
+ self.backend = None
+
def setting(self, name, default=None):
"""
Looks up backend specific setting.
diff --git a/src/python/txtai/ann/pgvector.py b/src/python/txtai/ann/pgvector.py
index 661d04f8..d80a1754 100644
--- a/src/python/txtai/ann/pgvector.py
+++ b/src/python/txtai/ann/pgvector.py
@@ -40,10 +40,6 @@ class PGVector(ANN):
# Table instance
self.table = None
- def __del__(self):
- if self.database:
- self.database.close()
-
def load(self, path):
# Reset database to original checkpoint
self.database.rollback()
@@ -89,6 +85,13 @@ class PGVector(ANN):
def save(self, path):
self.database.commit()
+ def close(self):
+ # Parent logic
+ super().close()
+
+ # Close database connection
+ self.database.close()
+
def initialize(self, recreate=False):
"""
Initializes a new database session.
diff --git a/src/python/txtai/embeddings/base.py b/src/python/txtai/embeddings/base.py
index d1b02cf6..bc430a2d 100644
--- a/src/python/txtai/embeddings/base.py
+++ b/src/python/txtai/embeddings/base.py
@@ -87,13 +87,6 @@ class Embeddings:
# Set initial configuration
self.configure(config)
- def __del__(self):
- """
- Clean resources when embeddings is deleted.
- """
-
- self.close()
-
def score(self, documents):
"""
Builds a term weighting scoring index. Only used by word vectors models.
@@ -664,21 +657,31 @@ class Embeddings:
Closes this embeddings index and frees all resources.
"""
- self.ann, self.config, self.graph, self.archive = None, None, None, None
+ self.config, self.archive = None, None
self.reducer, self.query, self.model, self.models = None, None, None, None
self.ids = None
- # Close database connection if open
+ # Close ANN
+ if self.ann:
+ self.ann.close()
+ self.ann = None
+
+ # Close database
if self.database:
self.database.close()
self.database, self.functions = None, None
- # Close scoring instance if open
+ # Close scoring
if self.scoring:
self.scoring.close()
self.scoring = None
- # Close indexes if open
+ # Close graph
+ if self.graph:
+ self.graph.close()
+ self.graph = None
+
+ # Close indexes
if self.indexes:
self.indexes.close()
self.indexes = None
diff --git a/src/python/txtai/graph/base.py b/src/python/txtai/graph/base.py
index 3b3602b9..73939923 100644
--- a/src/python/txtai/graph/base.py
+++ b/src/python/txtai/graph/base.py
@@ -417,6 +417,13 @@ class Graph:
archive = ArchiveFactory.create(directory)
archive.save(path, "tar")
+ def close(self):
+ """
+ Closes this graph.
+ """
+
+ self.backend, self.categories, self.topics = None, None, None
+
def insert(self, documents, index=0):
"""
Insert graph nodes for each document.
diff --git a/src/python/txtai/graph/rdbms.py b/src/python/txtai/graph/rdbms.py
index 3aa64535..229b7ec9 100644
--- a/src/python/txtai/graph/rdbms.py
+++ b/src/python/txtai/graph/rdbms.py
@@ -67,6 +67,13 @@ class RDBMS(NetworkX):
def save(self, path):
self.database.commit()
+ def close(self):
+ # Parent logic
+ super().close()
+
+ # Close database connection
+ self.database.close()
+
def filter(self, nodes, graph=None):
return super().filter(nodes, graph if graph else NetworkX(self.config))