Skip to content

Commit

Permalink
Add Embeddings context manager, closes #832
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmezzetti committed Dec 6, 2024
1 parent e7aa46a commit e215ea1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/python/txtai/embeddings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ def __init__(self, config=None, models=None, **kwargs):
# 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.
Expand Down Expand Up @@ -530,6 +536,9 @@ def load(self, path=None, cloud=None, config=None, **kwargs):
cloud: cloud storage configuration
config: configuration overrides
kwargs: additional configuration as keyword args
Returns:
Embeddings
"""

# Load from cloud, if configured
Expand Down Expand Up @@ -589,6 +598,8 @@ def load(self, path=None, cloud=None, config=None, **kwargs):
# 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.
Expand Down
16 changes: 16 additions & 0 deletions test/python/testembeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,22 @@ def testColumns(self):
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
Expand Down

0 comments on commit e215ea1

Please sign in to comment.