vortexdb 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,317 @@
1
+ # Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
2
+ """Client and server classes corresponding to protobuf-defined services."""
3
+ import grpc
4
+ import warnings
5
+
6
+ from google.protobuf import empty_pb2 as google_dot_protobuf_dot_empty__pb2
7
+ from . import vector_db_pb2 as vector__db__pb2
8
+
9
+ GRPC_GENERATED_VERSION = '1.81.1'
10
+ GRPC_VERSION = grpc.__version__
11
+ _version_not_supported = False
12
+
13
+ try:
14
+ from grpc._utilities import first_version_is_lower
15
+ _version_not_supported = first_version_is_lower(GRPC_VERSION, GRPC_GENERATED_VERSION)
16
+ except ImportError:
17
+ _version_not_supported = True
18
+
19
+ if _version_not_supported:
20
+ raise RuntimeError(
21
+ f'The grpc package installed is at version {GRPC_VERSION},'
22
+ + ' but the generated code in vector_db_pb2_grpc.py depends on'
23
+ + f' grpcio>={GRPC_GENERATED_VERSION}.'
24
+ + f' Please upgrade your grpc module to grpcio>={GRPC_GENERATED_VERSION}'
25
+ + f' or downgrade your generated code using grpcio-tools<={GRPC_VERSION}.'
26
+ )
27
+
28
+
29
+ class VectorDBStub:
30
+ """Missing associated documentation comment in .proto file."""
31
+
32
+ def __init__(self, channel):
33
+ """Constructor.
34
+
35
+ Args:
36
+ channel: A grpc.Channel.
37
+ """
38
+ self.InsertVector = channel.unary_unary(
39
+ '/vectordb.VectorDB/InsertVector',
40
+ request_serializer=vector__db__pb2.InsertVectorRequest.SerializeToString,
41
+ response_deserializer=vector__db__pb2.PointID.FromString,
42
+ _registered_method=True)
43
+ self.DeletePoint = channel.unary_unary(
44
+ '/vectordb.VectorDB/DeletePoint',
45
+ request_serializer=vector__db__pb2.PointID.SerializeToString,
46
+ response_deserializer=google_dot_protobuf_dot_empty__pb2.Empty.FromString,
47
+ _registered_method=True)
48
+ self.GetPoint = channel.unary_unary(
49
+ '/vectordb.VectorDB/GetPoint',
50
+ request_serializer=vector__db__pb2.PointID.SerializeToString,
51
+ response_deserializer=vector__db__pb2.Point.FromString,
52
+ _registered_method=True)
53
+ self.SearchPoints = channel.unary_unary(
54
+ '/vectordb.VectorDB/SearchPoints',
55
+ request_serializer=vector__db__pb2.SearchRequest.SerializeToString,
56
+ response_deserializer=vector__db__pb2.SearchResponse.FromString,
57
+ _registered_method=True)
58
+ self.InsertVectorsBatch = channel.unary_unary(
59
+ '/vectordb.VectorDB/InsertVectorsBatch',
60
+ request_serializer=vector__db__pb2.InsertVectorsBatchRequest.SerializeToString,
61
+ response_deserializer=vector__db__pb2.InsertVectorsBatchResponse.FromString,
62
+ _registered_method=True)
63
+ self.SearchPointsBatch = channel.unary_unary(
64
+ '/vectordb.VectorDB/SearchPointsBatch',
65
+ request_serializer=vector__db__pb2.SearchPointsBatchRequest.SerializeToString,
66
+ response_deserializer=vector__db__pb2.SearchPointsBatchResponse.FromString,
67
+ _registered_method=True)
68
+
69
+
70
+ class VectorDBServicer:
71
+ """Missing associated documentation comment in .proto file."""
72
+
73
+ def InsertVector(self, request, context):
74
+ """Insert a vector with a payload and return the assigned PointID
75
+ """
76
+ context.set_code(grpc.StatusCode.UNIMPLEMENTED)
77
+ context.set_details('Method not implemented!')
78
+ raise NotImplementedError('Method not implemented!')
79
+
80
+ def DeletePoint(self, request, context):
81
+ """Delete a vector by its PointID
82
+ """
83
+ context.set_code(grpc.StatusCode.UNIMPLEMENTED)
84
+ context.set_details('Method not implemented!')
85
+ raise NotImplementedError('Method not implemented!')
86
+
87
+ def GetPoint(self, request, context):
88
+ """Get a vector and its payload by PointID
89
+ """
90
+ context.set_code(grpc.StatusCode.UNIMPLEMENTED)
91
+ context.set_details('Method not implemented!')
92
+ raise NotImplementedError('Method not implemented!')
93
+
94
+ def SearchPoints(self, request, context):
95
+ """Search for the k nearest vectors to a target vector given a distance function
96
+ """
97
+ context.set_code(grpc.StatusCode.UNIMPLEMENTED)
98
+ context.set_details('Method not implemented!')
99
+ raise NotImplementedError('Method not implemented!')
100
+
101
+ def InsertVectorsBatch(self, request, context):
102
+ """Missing associated documentation comment in .proto file."""
103
+ context.set_code(grpc.StatusCode.UNIMPLEMENTED)
104
+ context.set_details('Method not implemented!')
105
+ raise NotImplementedError('Method not implemented!')
106
+
107
+ def SearchPointsBatch(self, request, context):
108
+ """Missing associated documentation comment in .proto file."""
109
+ context.set_code(grpc.StatusCode.UNIMPLEMENTED)
110
+ context.set_details('Method not implemented!')
111
+ raise NotImplementedError('Method not implemented!')
112
+
113
+
114
+ def add_VectorDBServicer_to_server(servicer, server):
115
+ rpc_method_handlers = {
116
+ 'InsertVector': grpc.unary_unary_rpc_method_handler(
117
+ servicer.InsertVector,
118
+ request_deserializer=vector__db__pb2.InsertVectorRequest.FromString,
119
+ response_serializer=vector__db__pb2.PointID.SerializeToString,
120
+ ),
121
+ 'DeletePoint': grpc.unary_unary_rpc_method_handler(
122
+ servicer.DeletePoint,
123
+ request_deserializer=vector__db__pb2.PointID.FromString,
124
+ response_serializer=google_dot_protobuf_dot_empty__pb2.Empty.SerializeToString,
125
+ ),
126
+ 'GetPoint': grpc.unary_unary_rpc_method_handler(
127
+ servicer.GetPoint,
128
+ request_deserializer=vector__db__pb2.PointID.FromString,
129
+ response_serializer=vector__db__pb2.Point.SerializeToString,
130
+ ),
131
+ 'SearchPoints': grpc.unary_unary_rpc_method_handler(
132
+ servicer.SearchPoints,
133
+ request_deserializer=vector__db__pb2.SearchRequest.FromString,
134
+ response_serializer=vector__db__pb2.SearchResponse.SerializeToString,
135
+ ),
136
+ 'InsertVectorsBatch': grpc.unary_unary_rpc_method_handler(
137
+ servicer.InsertVectorsBatch,
138
+ request_deserializer=vector__db__pb2.InsertVectorsBatchRequest.FromString,
139
+ response_serializer=vector__db__pb2.InsertVectorsBatchResponse.SerializeToString,
140
+ ),
141
+ 'SearchPointsBatch': grpc.unary_unary_rpc_method_handler(
142
+ servicer.SearchPointsBatch,
143
+ request_deserializer=vector__db__pb2.SearchPointsBatchRequest.FromString,
144
+ response_serializer=vector__db__pb2.SearchPointsBatchResponse.SerializeToString,
145
+ ),
146
+ }
147
+ generic_handler = grpc.method_handlers_generic_handler(
148
+ 'vectordb.VectorDB', rpc_method_handlers)
149
+ server.add_generic_rpc_handlers((generic_handler,))
150
+ server.add_registered_method_handlers('vectordb.VectorDB', rpc_method_handlers)
151
+
152
+
153
+ # This class is part of an EXPERIMENTAL API.
154
+ class VectorDB:
155
+ """Missing associated documentation comment in .proto file."""
156
+
157
+ @staticmethod
158
+ def InsertVector(request,
159
+ target,
160
+ options=(),
161
+ channel_credentials=None,
162
+ call_credentials=None,
163
+ insecure=False,
164
+ compression=None,
165
+ wait_for_ready=None,
166
+ timeout=None,
167
+ metadata=None):
168
+ return grpc.experimental.unary_unary(
169
+ request,
170
+ target,
171
+ '/vectordb.VectorDB/InsertVector',
172
+ vector__db__pb2.InsertVectorRequest.SerializeToString,
173
+ vector__db__pb2.PointID.FromString,
174
+ options,
175
+ channel_credentials,
176
+ insecure,
177
+ call_credentials,
178
+ compression,
179
+ wait_for_ready,
180
+ timeout,
181
+ metadata,
182
+ _registered_method=True)
183
+
184
+ @staticmethod
185
+ def DeletePoint(request,
186
+ target,
187
+ options=(),
188
+ channel_credentials=None,
189
+ call_credentials=None,
190
+ insecure=False,
191
+ compression=None,
192
+ wait_for_ready=None,
193
+ timeout=None,
194
+ metadata=None):
195
+ return grpc.experimental.unary_unary(
196
+ request,
197
+ target,
198
+ '/vectordb.VectorDB/DeletePoint',
199
+ vector__db__pb2.PointID.SerializeToString,
200
+ google_dot_protobuf_dot_empty__pb2.Empty.FromString,
201
+ options,
202
+ channel_credentials,
203
+ insecure,
204
+ call_credentials,
205
+ compression,
206
+ wait_for_ready,
207
+ timeout,
208
+ metadata,
209
+ _registered_method=True)
210
+
211
+ @staticmethod
212
+ def GetPoint(request,
213
+ target,
214
+ options=(),
215
+ channel_credentials=None,
216
+ call_credentials=None,
217
+ insecure=False,
218
+ compression=None,
219
+ wait_for_ready=None,
220
+ timeout=None,
221
+ metadata=None):
222
+ return grpc.experimental.unary_unary(
223
+ request,
224
+ target,
225
+ '/vectordb.VectorDB/GetPoint',
226
+ vector__db__pb2.PointID.SerializeToString,
227
+ vector__db__pb2.Point.FromString,
228
+ options,
229
+ channel_credentials,
230
+ insecure,
231
+ call_credentials,
232
+ compression,
233
+ wait_for_ready,
234
+ timeout,
235
+ metadata,
236
+ _registered_method=True)
237
+
238
+ @staticmethod
239
+ def SearchPoints(request,
240
+ target,
241
+ options=(),
242
+ channel_credentials=None,
243
+ call_credentials=None,
244
+ insecure=False,
245
+ compression=None,
246
+ wait_for_ready=None,
247
+ timeout=None,
248
+ metadata=None):
249
+ return grpc.experimental.unary_unary(
250
+ request,
251
+ target,
252
+ '/vectordb.VectorDB/SearchPoints',
253
+ vector__db__pb2.SearchRequest.SerializeToString,
254
+ vector__db__pb2.SearchResponse.FromString,
255
+ options,
256
+ channel_credentials,
257
+ insecure,
258
+ call_credentials,
259
+ compression,
260
+ wait_for_ready,
261
+ timeout,
262
+ metadata,
263
+ _registered_method=True)
264
+
265
+ @staticmethod
266
+ def InsertVectorsBatch(request,
267
+ target,
268
+ options=(),
269
+ channel_credentials=None,
270
+ call_credentials=None,
271
+ insecure=False,
272
+ compression=None,
273
+ wait_for_ready=None,
274
+ timeout=None,
275
+ metadata=None):
276
+ return grpc.experimental.unary_unary(
277
+ request,
278
+ target,
279
+ '/vectordb.VectorDB/InsertVectorsBatch',
280
+ vector__db__pb2.InsertVectorsBatchRequest.SerializeToString,
281
+ vector__db__pb2.InsertVectorsBatchResponse.FromString,
282
+ options,
283
+ channel_credentials,
284
+ insecure,
285
+ call_credentials,
286
+ compression,
287
+ wait_for_ready,
288
+ timeout,
289
+ metadata,
290
+ _registered_method=True)
291
+
292
+ @staticmethod
293
+ def SearchPointsBatch(request,
294
+ target,
295
+ options=(),
296
+ channel_credentials=None,
297
+ call_credentials=None,
298
+ insecure=False,
299
+ compression=None,
300
+ wait_for_ready=None,
301
+ timeout=None,
302
+ metadata=None):
303
+ return grpc.experimental.unary_unary(
304
+ request,
305
+ target,
306
+ '/vectordb.VectorDB/SearchPointsBatch',
307
+ vector__db__pb2.SearchPointsBatchRequest.SerializeToString,
308
+ vector__db__pb2.SearchPointsBatchResponse.FromString,
309
+ options,
310
+ channel_credentials,
311
+ insecure,
312
+ call_credentials,
313
+ compression,
314
+ wait_for_ready,
315
+ timeout,
316
+ metadata,
317
+ _registered_method=True)
vortexdb/models.py ADDED
@@ -0,0 +1,131 @@
1
+ from dataclasses import dataclass
2
+ from enum import Enum
3
+ from typing import List
4
+ from vortexdb.grpc import vector_db_pb2
5
+
6
+
7
+ # I found this to be a good idea, because
8
+ # 1. readability
9
+ # 2. will help in HTTP client
10
+ # 3. transport conversion at the very end, won't break if proto enum changes
11
+
12
+ class Similarity(Enum):
13
+ EUCLIDEAN = "euclidean"
14
+ MANHATTAN = "manhattan"
15
+ HAMMING = "hamming"
16
+ COSINE = "cosine"
17
+
18
+ def to_proto(self) -> int:
19
+ return {
20
+ Similarity.EUCLIDEAN: vector_db_pb2.Euclidean,
21
+ Similarity.MANHATTAN: vector_db_pb2.Manhattan,
22
+ Similarity.HAMMING: vector_db_pb2.Hamming,
23
+ Similarity.COSINE: vector_db_pb2.Cosine,
24
+ }[self]
25
+
26
+
27
+ class ContentType(Enum):
28
+ TEXT = "text"
29
+ IMAGE = "image"
30
+
31
+ def to_proto(self) -> int:
32
+ return {
33
+ ContentType.TEXT: vector_db_pb2.Text,
34
+ ContentType.IMAGE: vector_db_pb2.Image,
35
+ }[self]
36
+
37
+ @staticmethod
38
+ def from_proto(value: int) -> "ContentType":
39
+ return {
40
+ vector_db_pb2.Text: ContentType.TEXT,
41
+ vector_db_pb2.Image: ContentType.IMAGE,
42
+ }[value]
43
+
44
+
45
+ # TODO Extend support to other data types than lists or tuples (numpy arrays probably)
46
+ # TODO Further compatibility to allow conversions directly to numpy arrays (similar to .to_list())
47
+ @dataclass(frozen=True)
48
+ class DenseVector:
49
+ values: List[float]
50
+
51
+ def __post_init__(self):
52
+ if not isinstance(self.values, (list, tuple)):
53
+ raise TypeError("DenseVector expects a list or tuple of floats")
54
+
55
+ if not self.values:
56
+ raise ValueError("DenseVector cannot be empty")
57
+
58
+ for v in self.values:
59
+ if not isinstance(v, (int, float)):
60
+ raise TypeError(
61
+ "DenseVector values must be numeric (int or float)"
62
+ )
63
+
64
+ # force float normalization
65
+ object.__setattr__(self, "values", [float(v) for v in self.values])
66
+
67
+ def to_proto(self) -> vector_db_pb2.DenseVector:
68
+ return vector_db_pb2.DenseVector(values=self.values)
69
+
70
+ def to_list(self) -> list[float]:
71
+ return list(self.values)
72
+
73
+
74
+
75
+
76
+ @dataclass(frozen=True)
77
+ class Payload:
78
+ content_type: ContentType
79
+ content: str
80
+
81
+ @staticmethod
82
+ def text(content: str) -> "Payload":
83
+ return Payload(ContentType.TEXT, content)
84
+
85
+ @staticmethod
86
+ def image(content: str) -> "Payload":
87
+ return Payload(ContentType.IMAGE, content)
88
+
89
+ def __post_init__(self):
90
+ if not isinstance(self.content_type, ContentType):
91
+ raise TypeError("content_type must be ContentType enum")
92
+
93
+
94
+ def to_proto(self) -> vector_db_pb2.Payload:
95
+ return vector_db_pb2.Payload(
96
+ content_type=self.content_type.to_proto(),
97
+ content=self.content,
98
+ )
99
+
100
+
101
+ @dataclass(frozen=True)
102
+ class Point:
103
+ id: str
104
+ vector: DenseVector
105
+ payload: Payload
106
+
107
+ @staticmethod
108
+ def from_proto(proto: vector_db_pb2.Point) -> "Point":
109
+ payload = proto.payload
110
+ if payload is None:
111
+ payload_obj = Payload.text("")
112
+ else:
113
+ payload_obj = Payload(
114
+ content_type=ContentType.from_proto(payload.content_type),
115
+ content=payload.content,
116
+ )
117
+
118
+ return Point(
119
+ id=proto.id.id.value,
120
+ vector=DenseVector(list(proto.vector.values)),
121
+ payload=payload_obj,
122
+ )
123
+
124
+ def pretty(self) -> str:
125
+ return (
126
+ f"\nPoint:\n id = {self.id},\n"
127
+ f" vector_dim = {len(self.vector.values)},\n"
128
+ f" vector = {self.vector},\n"
129
+ f" payload_type = {self.payload.content_type.name},\n"
130
+ f" payload = '{self.payload.content}'"
131
+ )
vortexdb/protoutils.py ADDED
@@ -0,0 +1,64 @@
1
+ from vortexdb.grpc import vector_db_pb2
2
+ from vortexdb.models import DenseVector, Payload, Similarity
3
+
4
+
5
+ def build_insert_request(
6
+ *,
7
+ vector: DenseVector,
8
+ payload: Payload,
9
+ ) -> vector_db_pb2.InsertVectorRequest:
10
+ return vector_db_pb2.InsertVectorRequest(
11
+ vector=vector.to_proto(),
12
+ payload=payload.to_proto(),
13
+ )
14
+
15
+
16
+ def build_batch_insert_request(
17
+ *,
18
+ points: list[tuple[DenseVector, Payload]],
19
+ ) -> vector_db_pb2.InsertVectorsBatchRequest:
20
+ return vector_db_pb2.InsertVectorsBatchRequest(
21
+ vectors=[
22
+ build_insert_request(vector=vector, payload=payload)
23
+ for vector, payload in points
24
+ ]
25
+ )
26
+
27
+
28
+ def build_point_id_request(point_id: str) -> vector_db_pb2.PointID:
29
+ return vector_db_pb2.PointID(
30
+ id=vector_db_pb2.UUID(value=point_id)
31
+ )
32
+
33
+
34
+ def build_search_request(
35
+ *,
36
+ vector: DenseVector,
37
+ similarity: Similarity,
38
+ limit: int,
39
+ ef: int | None = None,
40
+ ) -> vector_db_pb2.SearchRequest:
41
+ return vector_db_pb2.SearchRequest(
42
+ query_vector=vector.to_proto(),
43
+ similarity=similarity.to_proto(),
44
+ limit=limit,
45
+ ef=ef or 0,
46
+ )
47
+
48
+
49
+ def build_batch_search_request(
50
+ *,
51
+ queries: list[tuple[DenseVector, Similarity, int]],
52
+ ef: int | None = None,
53
+ ) -> vector_db_pb2.SearchPointsBatchRequest:
54
+ return vector_db_pb2.SearchPointsBatchRequest(
55
+ queries=[
56
+ build_search_request(
57
+ vector=vector,
58
+ similarity=similarity,
59
+ limit=limit,
60
+ ef=ef,
61
+ )
62
+ for vector, similarity, limit in queries
63
+ ]
64
+ )