💡 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 API add method to stream data, closes #56
davidmezzetti30446eab7594
diff --git a/src/python/txtai/api.py b/src/python/txtai/api.pyindex ccbc8e03..bd33c18c 100644--- a/src/python/txtai/api.py+++ b/src/python/txtai/api.py@@ -3,6 +3,8 @@ API module"""import os+import pickle+import tempfilefrom typing import List@@ -34,7 +36,7 @@ class API(object):"""# Initialize member variables- self.config, self.documents = config, None+ self.config, self.documents, self.batch = config, None, 0self.embeddings, self.extractor, self.labels, self.similar = None, None, None, None# Create/load embeddings index depending on writable flag@@ -132,12 +134,28 @@ class API(object):# Only add batch if index is marked writableif self.embeddings and self.config.get("writable"):- # Create current batch if necessary+ # Create documents file if not already openif not self.documents:- self.documents = []+ self.documents = tempfile.NamedTemporaryFile(mode="wb", suffix=".docs", delete=False)# Add batch- self.documents.extend([(document["id"], document["text"], None) for document in documents])+ pickle.dump([(document["id"], document["text"], None) for document in documents], self.documents)+ self.batch += 1++ def stream(self):+ """+ Generator that streams documents previously queued with add.+ """++ # Open stream file+ with open(self.documents.name, "rb") as queue:+ # Read each batch+ for _ in range(self.batch):+ documents = pickle.load(queue)++ # Yield each document+ for document in documents:+ yield documentdef index(self):"""@@ -148,18 +166,25 @@ class API(object):"""if self.embeddings and self.config.get("writable") and self.documents:+ # Close streaming file+ self.documents.close()+# Build scoring index if scoring method providedif self.config.get("scoring"):- embeddings.score(self.documents)+ embeddings.score(self.stream())# Build embeddings index- self.embeddings.index(self.documents)+ self.embeddings.index(self.stream())# Save indexself.embeddings.save(self.config["path"])- # Clear buffer+ # Cleanup stream file+ os.remove(self.documents.name)++ # Reset document parametersself.documents = None+ self.batch = 0def similarity(self, query, texts):"""