Repository navigation
Expand file tree
/
Copy pathmodels.py
More file actions
224 lines (180 loc) 路 6.8 KB
/
Copy pathmodels.py
File metadata and controls
224 lines (180 loc) 路 6.8 KB
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
from dataclasses import dataclass
from enum import Enum
from typing import Any, List
from vortexdb.grpc import vector_db_pb2
# I found this to be a good idea, because
# 1. readability
# 2. will help in HTTP client
# 3. transport conversion at the very end, won't break if proto enum changes
class Similarity(Enum):
EUCLIDEAN = "euclidean"
MANHATTAN = "manhattan"
HAMMING = "hamming"
COSINE = "cosine"
def to_proto(self) -> int:
return {
Similarity.EUCLIDEAN: vector_db_pb2.Euclidean,
Similarity.MANHATTAN: vector_db_pb2.Manhattan,
Similarity.HAMMING: vector_db_pb2.Hamming,
Similarity.COSINE: vector_db_pb2.Cosine,
}[self]
class ContentType(Enum):
TEXT = "text"
IMAGE = "image"
def to_proto(self) -> int:
return {
ContentType.TEXT: vector_db_pb2.Text,
ContentType.IMAGE: vector_db_pb2.Image,
}[self]
@staticmethod
def from_proto(value: int) -> "ContentType":
return {
vector_db_pb2.Text: ContentType.TEXT,
vector_db_pb2.Image: ContentType.IMAGE,
}[value]
@dataclass(frozen=True)
class DenseVector:
values: List[float]
def __post_init__(self):
if isinstance(self.values, (list, tuple)):
normalized_values = list(self.values)
else:
normalized_values = self._array_like_to_list(self.values)
if not normalized_values:
raise ValueError("DenseVector cannot be empty")
if any(isinstance(value, (list, tuple)) for value in normalized_values):
raise ValueError("DenseVector expects a one-dimensional vector")
for v in normalized_values:
if not isinstance(v, (int, float)):
raise TypeError("DenseVector values must be numeric (int or float)")
object.__setattr__(self, "values", [float(v) for v in normalized_values])
@staticmethod
def _array_like_to_list(values: Any) -> list[Any]:
shape = getattr(values, "shape", None)
rank = getattr(values, "ndim", None)
if rank is None and shape is not None:
rank = getattr(shape, "rank", None)
if rank is None:
try:
rank = len(shape)
except (TypeError, ValueError):
pass
if rank is not None and rank != 1:
raise ValueError("DenseVector expects a one-dimensional vector")
to_list = getattr(values, "tolist", None)
if callable(to_list):
converted = to_list()
else:
to_numpy = getattr(values, "numpy", None)
if callable(to_numpy):
converted = to_numpy()
else:
to_array = getattr(values, "__array__", None)
if not callable(to_array):
raise TypeError("DenseVector could not convert the array or tensor")
converted = to_array()
converted_to_list = getattr(converted, "tolist", None)
if not callable(converted_to_list):
raise TypeError("DenseVector could not convert the array or tensor")
converted = converted_to_list()
if not isinstance(converted, list):
raise TypeError("DenseVector could not convert the array or tensor")
return converted
def to_proto(self) -> vector_db_pb2.DenseVector:
return vector_db_pb2.DenseVector(values=self.values)
def to_list(self) -> list[float]:
return list(self.values)
# Keep framework dependencies optional by importing only when conversion is requested.
def to_numpy(self) -> Any:
try:
import numpy
except ImportError as error:
raise ImportError(
"NumPy is required to convert DenseVector to an array"
) from error
return numpy.asarray(self.values, dtype=float)
def to_torch(self) -> Any:
try:
import torch
except ImportError as error:
raise ImportError(
"PyTorch is required to convert DenseVector to a tensor"
) from error
return torch.tensor(self.values)
def to_tensorflow(self) -> Any:
try:
import tensorflow
except ImportError as error:
raise ImportError(
"TensorFlow is required to convert DenseVector to a tensor"
) from error
return tensorflow.convert_to_tensor(self.values)
def to_jax(self) -> Any:
try:
import jax.numpy
except ImportError as error:
raise ImportError(
"JAX is required to convert DenseVector to an array"
) from error
return jax.numpy.asarray(self.values)
# & Helper Function for Batch of DenseVectors
def to_dense_vectors(arr):
return [DenseVector(x) for x in arr]
@dataclass(frozen=True)
class Payload:
content_type: ContentType
content: str
@staticmethod
def text(content: str) -> "Payload":
return Payload(ContentType.TEXT, content)
@staticmethod
def image(content: str) -> "Payload":
return Payload(ContentType.IMAGE, content)
def __post_init__(self):
if not isinstance(self.content_type, ContentType):
raise TypeError("content_type must be ContentType enum")
def to_proto(self) -> vector_db_pb2.Payload:
return vector_db_pb2.Payload(
content_type=self.content_type.to_proto(),
content=self.content,
)
@dataclass(frozen=True)
class Point:
id: str
vector: DenseVector
payload: Payload
@staticmethod
def from_proto(proto: vector_db_pb2.Point) -> "Point":
payload = proto.payload
if payload is None:
payload_obj = Payload.text("")
else:
payload_obj = Payload(
content_type=ContentType.from_proto(payload.content_type),
content=payload.content,
)
return Point(
id=proto.id.id.value,
vector=DenseVector(list(proto.vector.values)),
payload=payload_obj,
)
def pretty(self) -> str:
return (
f"\nPoint:\n id = {self.id},\n"
f" vector_dim = {len(self.vector.values)},\n"
f" vector = {self.vector},\n"
f" payload_type = {self.payload.content_type.name},\n"
f" payload = '{self.payload.content}'"
)
# I added this because using tuples will get messy if we increase fields in a search query
@dataclass(frozen=True)
class SearchQuery:
vector: DenseVector
similarity: Similarity
limit: int
def to_proto(self) -> vector_db_pb2.SearchRequest:
return vector_db_pb2.SearchRequest(
query_vector=self.vector.to_proto(),
similarity=self.similarity.to_proto(),
limit=self.limit,
)