6
6
import numpy
7
7
import numpy .typing
8
8
9
- from superduper .backends .base .backends import BaseBackend
9
+ from superduper .backends .base .backends import BaseBackend , Bookkeeping
10
10
11
11
if t .TYPE_CHECKING :
12
12
from superduper .base .datalayer import Datalayer
13
- from superduper .components .vector_index import VectorIndex
13
+ from superduper .components .vector_index import VectorIndex , VectorItem
14
14
15
15
16
- class VectorSearchBackend (BaseBackend ):
16
+ class VectorSearchBackend (Bookkeeping , BaseBackend ):
17
17
"""Base vector-search backend."""
18
18
19
19
def __init__ (self ):
20
- self ._cache = {}
20
+ Bookkeeping .__init__ (self )
21
+ BaseBackend .__init__ (self )
21
22
22
- @abstractmethod
23
- def __getitem__ (self , identifier ):
24
- pass
25
-
26
- def add (self , identifier , vectors ):
23
+ def add (self , uuid : str , vectors : t .List ['VectorItem' ]):
27
24
"""Add vectors to a vector-index.
28
25
29
26
:param identifier: Identifier of index.
30
27
:param vectors: Vectors.
31
28
"""
32
- self .get ( identifier ).add (vectors )
29
+ self .get_tool ( uuid ).add (vectors )
33
30
34
- def delete (self , identifier , ids ):
31
+ def delete (self , uuid , ids ):
35
32
"""Delete ids from index.
36
33
37
34
:param identifier: Identifier of index.
38
35
:param ids: Ids to delete.
39
36
"""
40
- self .get ( identifier ).delete (ids )
37
+ self .get_tool ( uuid ).delete (ids )
41
38
42
39
@property
43
40
def db (self ) -> 'Datalayer' :
@@ -52,13 +49,48 @@ def db(self, value: 'Datalayer'):
52
49
"""
53
50
self ._db = value
54
51
52
+ @abstractmethod
53
+ def find_nearest_from_array (
54
+ self ,
55
+ h : numpy .typing .ArrayLike ,
56
+ vector_index : str ,
57
+ n : int = 100 ,
58
+ within_ids : t .Sequence [str ] = (),
59
+ ) -> t .Tuple [t .List [str ], t .List [float ]]:
60
+ """
61
+ Find the nearest vectors to the given vector.
62
+
63
+ :param h: vector
64
+ :param n: number of nearest vectors to return
65
+ :param within_ids: list of ids to search within
66
+ """
67
+
68
+ @abstractmethod
69
+ def find_nearest_from_id (
70
+ self ,
71
+ id : str ,
72
+ vector_index : str ,
73
+ n : int = 100 ,
74
+ within_ids : t .Sequence [str ] = (),
75
+ ) -> t .Tuple [t .List [str ], t .List [float ]]:
76
+ """
77
+ Find the nearest vectors to the given vector.
78
+
79
+ :param id: id of the vector to search with
80
+ :param n: number of nearest vectors to return
81
+ :param within_ids: list of ids to search within
82
+ """
83
+
55
84
56
85
class VectorSearcherInterface (ABC ):
57
86
"""Interface for vector searchers.
58
87
59
88
# noqa
60
89
"""
61
90
91
+ def __init__ (self , identifier : str ):
92
+ self .identifier = identifier
93
+
62
94
@abstractmethod
63
95
def add (self , items : t .Sequence ['VectorItem' ]) -> None :
64
96
"""
@@ -75,31 +107,31 @@ def delete(self, ids: t.Sequence[str]) -> None:
75
107
"""
76
108
77
109
@abstractmethod
78
- def find_nearest_from_id (
110
+ def find_nearest_from_array (
79
111
self ,
80
- _id ,
112
+ h : numpy . typing . ArrayLike ,
81
113
n : int = 100 ,
82
114
within_ids : t .Sequence [str ] = (),
83
115
) -> t .Tuple [t .List [str ], t .List [float ]]:
84
116
"""
85
- Find the nearest vectors to the vector with the given id .
117
+ Find the nearest vectors to the given vector .
86
118
87
- :param _id: id of the vector
119
+ :param h: vector
88
120
:param n: number of nearest vectors to return
89
121
:param within_ids: list of ids to search within
90
122
"""
91
123
92
124
@abstractmethod
93
- def find_nearest_from_array (
125
+ def find_nearest_from_id (
94
126
self ,
95
- h : numpy . typing . ArrayLike ,
127
+ id : str ,
96
128
n : int = 100 ,
97
129
within_ids : t .Sequence [str ] = (),
98
130
) -> t .Tuple [t .List [str ], t .List [float ]]:
99
131
"""
100
132
Find the nearest vectors to the given vector.
101
133
102
- :param h: vector
134
+ :param id: id of the vector to search with
103
135
:param n: number of nearest vectors to return
104
136
:param within_ids: list of ids to search within
105
137
"""
@@ -111,6 +143,13 @@ def post_create(self):
111
143
to perform a task after all vectors have been added
112
144
"""
113
145
146
+ def post_create (self ):
147
+ """Post create method.
148
+
149
+ This method is used for searchers which requires
150
+ to perform a task after all vectors have been added
151
+ """
152
+
114
153
115
154
class BaseVectorSearcher (VectorSearcherInterface ):
116
155
"""Base class for vector searchers.
@@ -122,10 +161,18 @@ class BaseVectorSearcher(VectorSearcherInterface):
122
161
123
162
native_service : t .ClassVar [bool ] = True
124
163
164
+ @property
165
+ def db (self ) -> 'Datalayer' :
166
+ return self ._db
167
+
168
+ @db .setter
169
+ def db (self , value : 'Datalayer' ):
170
+ self ._db = value
171
+
125
172
@abstractmethod
126
173
def __init__ (
127
174
self ,
128
- uuid : str ,
175
+ identifier : str ,
129
176
dimensions : int ,
130
177
measure : str ,
131
178
):
@@ -137,7 +184,7 @@ def from_component(cls, index: 'VectorIndex'):
137
184
138
185
:param vi: ``VectorIndex`` instance
139
186
"""
140
- return cls (uuid = index .uuid , dimensions = index .dimensions , measure = index .measure )
187
+ return cls (identifier = index .uuid , dimensions = index .dimensions , measure = index .measure )
141
188
142
189
@abstractmethod
143
190
def initialize (self , db ):
0 commit comments