/ src / python / txtai / vectors / dense / external.py
external.py
 1  """
 2  External module
 3  """
 4  
 5  import types
 6  
 7  import numpy as np
 8  
 9  from ...util import Resolver
10  
11  from ..base import Vectors
12  
13  
14  class External(Vectors):
15      """
16      Builds vectors using an external method. This can be a local function or an external API call.
17      """
18  
19      def __init__(self, config, scoring, models):
20          super().__init__(config, scoring, models)
21  
22          # Lookup and resolve transform function
23          self.transform = self.resolve(config.get("transform"))
24  
25      def loadmodel(self, path):
26          return None
27  
28      def encode(self, data, category=None):
29          # Call external transform function, if available and data not already an array
30          # Batching is handed by the external transform function
31          if self.transform and data and not isinstance(data[0], np.ndarray):
32              data = self.transform(data)
33  
34          # Cast to float32
35          return data.astype(np.float32) if isinstance(data, np.ndarray) else np.array(data, dtype=np.float32)
36  
37      def resolve(self, transform):
38          """
39          Resolves a transform function.
40  
41          Args:
42              transform: transform function
43  
44          Returns:
45              resolved transform function
46          """
47  
48          if transform:
49              # Resolve transform instance, if necessary
50              transform = Resolver()(transform) if transform and isinstance(transform, str) else transform
51  
52              # Get function or callable instance
53              transform = transform if isinstance(transform, types.FunctionType) else transform()
54  
55          return transform