/ src / python / txtai / ann / sparse / factory.py
factory.py
 1  """
 2  Factory module
 3  """
 4  
 5  from ...util import Resolver
 6  
 7  from .ivfsparse import IVFSparse
 8  from .pgsparse import PGSparse
 9  
10  
11  class SparseANNFactory:
12      """
13      Methods to create Sparse ANN indexes.
14      """
15  
16      @staticmethod
17      def create(config):
18          """
19          Create an Sparse ANN.
20  
21          Args:
22              config: index configuration parameters
23  
24          Returns:
25              Sparse ANN
26          """
27  
28          # ANN instance
29          ann = None
30          backend = config.get("backend", "ivfsparse")
31  
32          # Create ANN instance
33          if backend == "ivfsparse":
34              ann = IVFSparse(config)
35          elif backend == "pgsparse":
36              ann = PGSparse(config)
37          else:
38              ann = SparseANNFactory.resolve(backend, config)
39  
40          # Store config back
41          config["backend"] = backend
42  
43          return ann
44  
45      @staticmethod
46      def resolve(backend, config):
47          """
48          Attempt to resolve a custom backend.
49  
50          Args:
51              backend: backend class
52              config: index configuration parameters
53  
54          Returns:
55              ANN
56          """
57  
58          try:
59              return Resolver()(backend)(config)
60          except Exception as e:
61              raise ImportError(f"Unable to resolve sparse ann backend: '{backend}'") from e