generated from XpressAI/xai-component-library-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vecto_components.py
371 lines (264 loc) · 12.9 KB
/
vecto_components.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
from xai_components.base import InArg, OutArg, InCompArg, Component, xai_component, secret
from vecto.schema import LookupResult, VectoIngestData
from typing import Union, List
from vecto import Vecto
from pprint import pprint
from dotenv import load_dotenv
import os, io
load_dotenv()
@xai_component
class VectoClient(Component):
'''Component to initialize a Vecto client.
The client is set to the [ctx] so users do not need to pass the client around.
### inPorts:
- token (secret): vecto token. If not provided, will check `vecto_token` from env.
- vecto_base_url (str): vecto base url. Default: 'https://api.vecto.ai'
- vector_space_id (int): vector space id. If not provided, will check from env.
### outPorts:
- vecto_client: Vecto client object.
'''
token: InArg[secret]
vecto_base_url: InArg[str]
vector_space_id: InArg[int]
vecto_client: OutArg[any]
def execute(self, ctx) -> None:
token = self.token.value if self.token.value else os.environ['user_token']
vecto_base_url = ''
if self.vecto_base_url.value:
vecto_base_url = self.vecto_base_url.value
elif os.environ.get('vecto_base_url') is not None:
vecto_base_url = os.environ['vecto_base_url']
else:
vecto_base_url = 'https://api.vecto.ai'
vector_space_id = self.vector_space_id.value if self.vector_space_id.value else os.environ['vector_space_id']
vecto_client = Vecto(token, vector_space_id, vecto_base_url=vecto_base_url)
ctx.update({'vecto_client': vecto_client})
@xai_component
class VectoLookup(Component):
'''A component to search on Vecto, based on the lookup item.
### inPorts:
- vecto_client: VectoClient object. If not provided, will check whether VectoClient already exists in ctx.
- query (IO): A IO file-like object.
You can use open(path, 'rb') for IMAGE queries and io.StringIO(text) for TEXT queries.
- modality (str): The type of the file - "IMAGE" or "TEXT"
- top_k (int): The number of results to return. Default 5.
- ids (list): A list of vector ids to search on aka subset of vectors, defaults to None
- **kwargs: Other keyword arguments for clients other than `requests`
### outPorts:
- LookupResponse: named tuple that contains a list of LookupResult named tuples.
where LookResult is named tuple with `data`, `id`, and `similarity` keys.
'''
vecto_client: InArg[any]
query: InCompArg[any] #IO
modality: InCompArg[str]
top_k: InArg[int]
LookupResponse: OutArg[LookupResult]
def __init__(self):
super().__init__()
self.top_k.value = 5
def execute(self, ctx) -> None:
vecto_client = self.vecto_client.value if self.vecto_client.value else ctx['vecto_client']
f = ''
if self.modality.value == 'TEXT':
f = io.StringIO(self.query.value)
elif self.modality.value == 'IMAGE':
f = open(self.query.value, 'rb')
self.LookupResponse.value = vecto_client.lookup(f, self.modality.value, self.top_k.value)
# pprint(self.LookupResponse.value)
@xai_component
class VectoIngest(Component):
'''''
A component to ingest data into Vecto.
### InPorts:
- vecto_client: VectoClient object. If not provided, will check whether VectoClient already exists in ctx.
- ingest_data (VectoIngestData or list of VectoIngestData): you can also provide a dict, but ensure that it complies with VectoIngestData.
- modality (str): 'IMAGE' or 'TEXT'
### outPorts:
- IngestResponse: named tuple that contains the list of index of ingested objects.
'''
vecto_client: InArg[any]
ingest_data: InCompArg[any] #Union[VectoIngestData, List[VectoIngestData]]
modality: InCompArg[str]
ingestResponse : OutArg[any] #IngestResponse
def execute(self, ctx) -> None:
vecto_client = self.vecto_client.value if self.vecto_client.value else ctx['vecto_client']
data = self.ingest_data.value
if len(data) >= 100:
chunk_size = 100
chunks = [data[i:i + chunk_size] for i in range(0, len(data), chunk_size)]
i = 0
for chunk in chunks:
self.ingestResponse.value = vecto_client.ingest(chunk, self.modality.value)
print(f"ingest {(i / len(data)) * 100}% complete.")
else:
self.ingestResponse.value = vecto_client.ingest(self.ingest_data.value, self.modality.value)
# pprint(self.ingestResponse.value)
@xai_component
class VectoComputeAnalogy(Component):
'''A component to compute an analogy using Vecto.
It is also possible to do multiple analogies in one request body.
The computed analogy is not stored in Vecto.
### inPorts:
- query (IO): query in the form of an IO object query.
- analogy_start_end (VectoAnalogyStartEnd or list of VectoAnalogyStartEnd): start and end analogy to be computed.
- Use open(path, 'rb') for IMAGE or io.StringIO(text) for TEXT analogies.
- top_k (int): The number of results to return. Default 5.
- modality (str): The type of the file, 'IMAGE' or 'TEXT'
### outPorts:
- LookupResponse: named tuple that contains a list of LookupResult named tuples.
where LookResult is named tuple with `data`, `id`, and `similarity` keys.
'''
vecto_client: InArg[any]
query: InCompArg[any] #IO
analogy_start_end: InCompArg[any] #Union[VectoAnalogyStartEnd, List[VectoAnalogyStartEnd]]
top_k: InArg[int]
modality: InCompArg[str]
LookupResponse: OutArg[LookupResult]
def __init__(self):
super().__init__()
self.top_k.value = 5
def execute(self, ctx) -> None:
vecto_client = self.vecto_client.value if self.vecto_client.value else ctx['vecto_client']
self.LookupResponse.value = vecto_client.compute_analogy(self.query.value,
self.analogy_start_end.value,
self.top_k.value,
self.modality.value)
# pprint(self.LookupResponse.value)
@xai_component
class VectoUpdateVectorEmbeddings(Component):
'''A component to update current vector embeddings with new one.
### inPorts:
- vecto_client: VectoClient object. If not provided, will check whether VectoClient already exists in ctx.
- embedding_data (VectoEmbeddingData or list of VectoEmbeddingData): data that contains the embedding data to be updated.
- modality (str): The type of the file - "IMAGE" or "TEXT"
### outPorts:
- dict: Client response body
'''
vecto_client: InArg[any]
embedding_data: InCompArg[any] #Union[VectoEmbeddingData, List[VectoEmbeddingData]]
modality: InCompArg[str]
response : OutArg[any]
def execute(self, ctx) -> None:
vecto_client = self.vecto_client.value if self.vecto_client.value else ctx['vecto_client']
self.response.value = vecto_client.update_vector_embeddings(self.embedding_data.value, self.modality.value)
@xai_component
class VectoUpdateVectorAttribute(Component):
'''A component to update current vector attribute with new one.
### inPorts:
- vecto_client: VectoClient object. If not provided, will check whether VectoClient already exists in ctx.
- update_attribute (VectoAttribute or list of VectoAttribute) : attribute to be updated.
'''
vecto_client: InArg[any]
update_attribute: InCompArg[any] #Union[VectoAttribute, List[VectoAttribute]]
def execute(self, ctx) -> None:
vecto_client = self.vecto_client.value if self.vecto_client.value else ctx['vecto_client']
vecto_client.update_vector_attribute(self.update_attribute.value)
@xai_component
class VectoDeleteVectorEmbeddings(Component):
'''A component to delete vector embeddings that is stored in Vecto.
### inPorts:
- vecto_client: VectoClient object. If not provided, will check whether VectoClient already exists in ctx.
- vector_ids (list): A list of vector ids to be deleted
### outPorts:
- dict: Client response body
'''
vecto_client: InArg[any]
vector_ids: InCompArg[list]
response: OutArg[any]
def execute(self, ctx) -> None:
vecto_client = self.vecto_client.value if self.vecto_client.value else ctx['vecto_client']
self.response.value = vecto_client.delete_vector_embeddings(self.vector_ids.value)
@xai_component
class VectoDeleteVectorSpaceEntries(Component):
'''A component to delete the current vector space in Vecto.
All ingested entries will be deleted as well.
### outPort:
- response: Client response body
'''
vecto_client: InArg[any]
response: OutArg[any]
def execute(self, ctx) -> None:
vecto_client = self.vecto_client.value if self.vecto_client.value else ctx['vecto_client']
self.response.value = vecto_client.delete_vector_space_entries()
@xai_component
class VectoIngestImage(Component):
"""A component that accepts a str or list of image paths and their attribute, formats it
in a list of dicts to be accepted by the ingest function.
### inPorts:
- batch_path_list (str or list): Str or list of image paths.
- attribute_list (str or list): Str or list of image attribute.
### outPorts:
- IngestResponse: named tuple that contains the list of index of ingested objects.
"""
vecto_client: InArg[any]
batch_path_list: InCompArg[any] # Union[str, list]
attribute_list: InCompArg[str] # Union[str, list]
ingestResponse: OutArg[any]
def execute(self, ctx) -> None:
vecto_client = self.vecto_client.value if self.vecto_client.value else ctx['vecto_client']
self.ingestResponse.value = vecto_client.ingest_image(self.batch_path_list.value, self.attribute_list.value)
# pprint(self.ingestResponse.value)
@xai_component
class VectoIngestAllImages(Component):
"""A component that accepts a list of image paths and their attribute, then send them
to the ingest_image function in batches.
### inPorts:
- path_list (list): List of image paths.
- attribute_list (list): List of image attribute.
- batch_size (int): batch size of images to be sent at one request. Default 64.
### outPorts:
- IngestResponse: named tuple that contains the list of index of ingested objects.
"""
vecto_client: InArg[any]
path_list: InCompArg[list]
attribute_list: InCompArg[list]
batch_size: InArg[int]
ingestResponse: OutArg[any]
def __init__(self):
super().__init__()
self.batch_size.value = 64
def execute(self, ctx) -> None:
vecto_client = self.vecto_client.value if self.vecto_client.value else ctx['vecto_client']
self.ingestResponse.value = vecto_client.ingest_all_images(self.path_list.value, self.attribute_list.value, self.batch_size.value)
@xai_component
class VectoIngestText(Component):
"""A component that accepts a str or list of text and their attribute, formats it
in a list of dicts to be accepted by the ingest function.
### inPorts:
- batch_text_list (str or list): Str or list of text.
- attribute_list (str or list): Str or list of the text attribute.
### outPorts:
- IngestResponse: named tuple that contains the list of index of ingested objects.
"""
vecto_client: InArg[any]
batch_text_list: InCompArg[any] # Union[str, list]
attribute_list: InCompArg[str]
IngestResponse: OutArg[any]
def execute(self, ctx) -> None:
vecto_client = self.vecto_client.value if self.vecto_client.value else ctx['vecto_client']
self.IngestResponse.value = vecto_client.ingest_text(self.batch_text_list.value, self.attribute_list.value)
# pprint(self.IngestResponse.value)
@xai_component
class VectoIngestAllText(Component):
"""A component that accepts a list of text and their attribute, then send them
to the ingest_text function in batches.
### inPorts:
- batch_text_list (list): List of image paths.
- attribute_list (list): List of image attribute.
- batch_size (int): batch size of images to be sent at one request. Default 64.
- **kwargs: Other keyword arguments for clients other than `requests`
### outPorts:
- IngestResponse: named tuple that contains the list of index of ingested objects.
"""
vecto_client: InArg[any]
path_list: InCompArg[list]
attribute_list: InCompArg[list]
batch_size: InArg[int]
ingestResponse: OutArg[any]
def __init__(self):
super().__init__()
self.batch_size.value = 64
def execute(self, ctx) -> None:
vecto_client = self.vecto_client.value if self.vecto_client.value else ctx['vecto_client']
self.ingestResponse.value = vecto_client.ingest_all_text(self.path_list.value, self.attribute_list.value, self.batch_size.value)
# pprint(self.ingestResponse.value)