Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Infer vector method using path, closes #100

davidmezzetti2583c83afb58
diff --git a/src/python/txtai/vectors/factory.py b/src/python/txtai/vectors/factory.py
index 413036e9..9569d357 100644
--- a/src/python/txtai/vectors/factory.py
+++ b/src/python/txtai/vectors/factory.py
@@ -2,6 +2,8 @@
Factory module
"""
+import os
+
from .transformers import TransformersVectors
from .words import WordVectors, WORDS
@@ -24,15 +26,38 @@ class VectorsFactory:
Vectors
"""
- # Derive vector type
- transformers = config.get("method") == "transformers"
+ # Determine vector method
+ method = VectorsFactory.method(config)
+ if method == "words":
+ if not WORDS:
+ # Raise error if trying to create Word Vectors without similarity extra
+ raise ImportError(
+ 'Word vector models are not available - install "similarity" extra to enable. Otherwise, specify '
+ + 'method="transformers" to use transformer backed models'
+ )
+
+ return WordVectors(config, scoring)
+
+ return TransformersVectors(config, scoring)
+
+ @staticmethod
+ def method(config):
+ """
+ Get or derive the vector method.
+
+ Args:
+ config: vector configuration
+
+ Returns:
+ vector method
+ """
+
+ # Determine vector type (words or transformers)
+ method = config.get("method")
+ path = config.get("path")
- if not transformers and not WORDS:
- # Raise error if trying to create Word Vectors without similarity extra
- raise ImportError(
- 'Word vector models are not available - install "similarity" extra to enable. Otherwise, specify '
- + 'method="transformers" to use transformer backed models'
- )
+ # Infer method from path, if blank
+ if not method and path:
+ method = "words" if os.path.isfile(path) else "transformers"
- # Create vector model instance
- return TransformersVectors(config, scoring) if transformers else WordVectors(config, scoring)
+ return method