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 Embeddings context manager, closes #832

davidmezzettie215ea13bbab
diff --git a/src/python/txtai/embeddings/base.py b/src/python/txtai/embeddings/base.py
index aad6e4cf..b3bcb521 100644
--- a/src/python/txtai/embeddings/base.py
+++ b/src/python/txtai/embeddings/base.py
@@ -85,6 +85,12 @@ class Embeddings:
# Set initial configuration
self.configure(config)
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *args):
+ self.close()
+
def score(self, documents):
"""
Builds a term weighting scoring index. Only used by word vectors models.
@@ -530,6 +536,9 @@ class Embeddings:
cloud: cloud storage configuration
config: configuration overrides
kwargs: additional configuration as keyword args
+
+ Returns:
+ Embeddings
"""
# Load from cloud, if configured
@@ -589,6 +598,8 @@ class Embeddings:
# Query model
self.query = self.loadquery()
+ return self
+
def save(self, path, cloud=None, **kwargs):
"""
Saves an index in a directory at path unless path ends with tar.gz, tar.bz2, tar.xz or zip.
diff --git a/test/python/testembeddings.py b/test/python/testembeddings.py
index b4bd70ee..97d92c65 100644
--- a/test/python/testembeddings.py
+++ b/test/python/testembeddings.py
@@ -81,6 +81,22 @@ class TestEmbeddings(unittest.TestCase):
uid = embeddings.search("lottery", 1)[0][0]
self.assertEqual(uid, 4)
+ def testContext(self):
+ """
+ Test embeddings context manager
+ """
+
+ # Generate temp file path
+ index = os.path.join(tempfile.gettempdir(), "embeddings.context")
+
+ with Embeddings() as embeddings:
+ embeddings.index(self.data)
+ embeddings.save(index)
+
+ with Embeddings().load(index) as embeddings:
+ uid = embeddings.search(self.data[4], 1)[0][0]
+ self.assertEqual(uid, 4)
+
def testDefaults(self):
"""
Test default configuration