-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathclient.py
More file actions
303 lines (268 loc) · 10.4 KB
/
client.py
File metadata and controls
303 lines (268 loc) · 10.4 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
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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from __future__ import annotations
import datetime
import json
import pprint
import socket
from typing import Optional, Union
import requests
import sentry_sdk
from pydantic import BaseModel, Field, constr
from ..abstract_class.vector_db import AbstractVectordb
from ..abstract_class.client import AbstractClient
from ..utils.rest_api import get_call, post_call, delete_call
from ..utils.search_engine import SearchEngine
requests.packages.urllib3.disable_warnings()
class Client(AbstractClient):
def __init__(self, project_id: str, api_key: str, headers: dict = None):
self._project_id = project_id
self._apikey = api_key
self._baseurl = "https://dispatch.epsilla.com/api/v3/project/{}".format(
self._project_id
)
self._timeout = 10
self._header = {
"Content-type": "application/json",
"Connection": "close",
"X-API-Key": api_key,
}
if headers is not None:
self._header.update(headers)
def validate(self):
_, data = get_call(
url=self._baseurl + "/vectordb/list",
data=None,
headers=self._header,
verify=False,
)
return data
def get_db_list(self):
db_list = []
req_url = "{}/vectordb/list".format(self._baseurl)
status_code, body = get_call(url=req_url, data=None, headers=self._header, verify=False)
if status_code == requests.ok and body["statusCode"] == requests.ok:
db_list = [db_id for db_id in body["result"]]
return db_list
def get_db_info(self, db_id: str):
req_url = "{}/vectordb/{}".format(self._baseurl, db_id)
return get_call(url=req_url, data=None, headers=self._header, verify=False)
def get_db_statistics(self, db_id: str):
req_url = "{}/vectordb/{}/statistics".format(self._baseurl, db_id)
req_data = None
return get_call(
url=req_url, data=json.dumps(req_data), headers=self._header, verify=False
)
def vectordb(self, db_id: str):
# validate project_id and api_key
resp = self.validate()
if resp["statusCode"] != 200:
if resp["statusCode"] == 404:
raise Exception("Invalid project_id")
if resp["statusCode"] == 401:
raise Exception("Invalid api_key")
# validate db_id
db_list = self.get_db_list()
if db_id not in db_list:
raise Exception("Invalid db_id")
# fetch db public endpoint
status_code, resp = self.get_db_info(db_id=db_id)
if resp["statusCode"] == requests.ok:
return Vectordb(
self._project_id, db_id, self._apikey, resp["result"]["public_endpoint"]
)
else:
print(resp)
del resp
raise Exception("Failed to get db info")
class Vectordb(Client, AbstractVectordb):
def __init__(
self,
project_id: str,
db_id: str,
api_key: str,
public_endpoint: str,
headers: dict = None,
):
self._project_id = project_id
self._db_id = db_id
self._api_key = api_key
self._public_endpoint = public_endpoint
self._baseurl = "https://{}/api/v3/project/{}/vectordb/{}".format(
self._public_endpoint, self._project_id, self._db_id
)
self._header = {"Content-type": "application/json", "X-API-Key": self._api_key}
if headers is not None:
self._header.update(headers)
# List table
def list_tables(self):
if self._db_id is None:
raise Exception("[ERROR] db_id is None!")
req_url = "{}/table/list".format(self._baseurl)
return get_call(url=req_url, headers=self._header, verify=False)
# Create table
def create_table(
self,
table_name: str,
table_fields: list[dict] = None,
indices: list[dict] = None,
):
if self._db_id is None:
raise Exception("[ERROR] db_id is None!")
if table_fields is None:
table_fields = []
req_url = "{}/table/create".format(self._baseurl)
req_data = {"name": table_name, "fields": table_fields}
if indices is not None:
req_data["indices"] = indices
return post_call(
url=req_url, data=json.dumps(req_data), headers=self._header, verify=False
)
# Drop table
def drop_table(self, table_name: str):
if self._db_id is None:
raise Exception("[ERROR] db_id is None!")
req_url = "{}/table/delete?table_name={}".format(self._baseurl, table_name)
req_data = {}
return delete_call(
url=req_url, data=json.dumps(req_data), headers=self._header, verify=False
)
# Insert data into table
def insert(self, table_name: str, records: list[dict]):
req_url = "{}/data/insert".format(self._baseurl)
req_data = {"table": table_name, "data": records}
return post_call(
url=req_url, data=json.dumps(req_data), headers=self._header, verify=False
)
def upsert(self, table_name: str, records: list[dict]):
req_url = "{}/data/insert".format(self._baseurl)
req_data = {"table": table_name, "data": records, "upsert": True}
return post_call(
url=req_url, data=json.dumps(req_data), headers=self._header, verify=False
)
# Query data from table
def query(
self,
table_name: str,
query_text: str = None,
query_index: str = None,
query_field: str = None,
query_vector: Union[list, dict] = None,
response_fields: Optional[list] = None,
limit: int = 2,
filter: Optional[str] = None,
with_distance: Optional[bool] = False,
facets: Optional[list[dict]] = None,
):
req_url = "{}/data/query".format(self._baseurl)
req_data = {"table": table_name, "limit": limit}
if response_fields is None:
response_fields = []
if query_text is not None:
req_data["query"] = query_text
if query_index is not None:
req_data["queryIndex"] = query_index
if query_field is not None:
req_data["queryField"] = query_field
if query_vector is not None:
req_data["queryVector"] = query_vector
if response_fields is not None:
req_data["response"] = response_fields
if filter is not None:
req_data["filter"] = filter
if with_distance is not False:
req_data["withDistance"] = with_distance
if facets is not None and len(facets) > 0:
aggregate_not_existing = 0
for facet in facets:
if "aggregate" not in facet:
aggregate_not_existing += 1
if aggregate_not_existing > 0:
raise Exception("[ERROR] key aggregate is a must in facets!")
else:
req_data["facets"] = facets
return post_call(
url=req_url, data=json.dumps(req_data), headers=self._header, verify=False
)
# Delete data from table
def delete(
self,
table_name: str,
primary_keys: Optional[list[Union[str, int]]] = None,
ids: Optional[list[Union[str, int]]] = None,
filter: Optional[str] = None,
):
"""Epsilla supports delete records by primary keys as default for now."""
if filter is None:
if primary_keys is None and ids is None:
raise Exception(
"[ERROR] Please provide at least one of primary keys(ids) and filter to delete record(s)."
)
if primary_keys is None and ids is not None:
primary_keys = ids
if primary_keys is not None and ids is not None:
try:
sentry_sdk.sdk("Duplicate Keys with both primary keys and ids", "info")
except Exception as e:
pass
print(
"[WARN] Both primary_keys and ids are prvoided, will use primary keys by default!"
)
req_url = "{}/data/delete".format(self._baseurl)
req_data = {"table": table_name}
if primary_keys is not None:
req_data["primaryKeys"] = primary_keys
if filter is not None:
req_data["filter"] = filter
return post_call(
url=req_url, data=json.dumps(req_data), headers=self._header, verify=False
)
# Get data from table
def get(
self,
table_name: str,
response_fields: Optional[list] = None,
primary_keys: Optional[list[Union[str, int]]] = None,
ids: Optional[list[Union[str, int]]] = None,
filter: Optional[str] = None,
skip: Optional[int] = None,
limit: Optional[int] = None,
facets: Optional[list[dict]] = None,
):
"""Epsilla supports get records by primary keys as default for now."""
if primary_keys is not None and ids is not None:
try:
sentry_sdk.sdk("Duplicate Keys with both primary_keys and ids", "info")
except Exception as e:
pass
print(
"[WARN]Both primary_keys and ids are prvoided, will use primary keys by default!"
)
if primary_keys is None and ids is not None:
primary_keys = ids
req_data = {"table": table_name}
if response_fields is not None:
req_data["response"] = response_fields
if primary_keys is not None:
req_data["primaryKeys"] = primary_keys
if filter is not None:
req_data["filter"] = filter
if skip is not None:
req_data["skip"] = skip
if limit is not None:
req_data["limit"] = limit
if facets is not None and len(facets) > 0:
aggregate_not_existing = 0
for facet in facets:
if "aggregate" not in facet:
aggregate_not_existing += 1
if aggregate_not_existing > 0:
raise Exception("[ERROR] key aggregate is a must in facets!")
else:
req_data["facets"] = facets
req_url = "{}/data/get".format(self._baseurl)
return post_call(
url=req_url, data=json.dumps(req_data), headers=self._header, verify=False
)
def as_search_engine(self):
return SearchEngine(self)