Skip to content

Commit 5d7d812

Browse files
authored
Merge pull request #37 from epsilla-cloud/dev
add create/drop table for cloud
2 parents bc67d36 + 9da668b commit 5d7d812

3 files changed

Lines changed: 88 additions & 43 deletions

File tree

examples/hello_epsilla_cloud.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,30 @@
66
# 2. create a table with schema in db
77
# 3. get the api key with project id, run this program
88

9+
import sys
910

1011
from pyepsilla import cloud
1112

1213
# Connect to Epsilla Cloud
1314
client = cloud.Client(
14-
project_id="32ef3a3f-fcb0-4c4b-98bb-fca01bca0d0a", api_key="epsilla"
15+
project_id="7a68814c-f839-4a67-9ec6-93c027c865e6",
16+
api_key="epsilla-cloud-api-key",
1517
)
1618

1719
# Connect to Vectordb
18-
db = client.vectordb(db_id="df7431d0-806b-4654-8b45-4bdb20038e26")
20+
db = client.vectordb(db_id="6accafb1-476d-43b0-aa64-12ebfbf7442d")
1921

2022

21-
# Create a table with schema on Epsilla Cloud Console
23+
# Create a table with schema
24+
status_code, response = db.create_table(
25+
table_name="MyTable",
26+
table_fields=[
27+
{"name": "ID", "dataType": "INT", "primaryKey": True},
28+
{"name": "Doc", "dataType": "STRING"},
29+
{"name": "Embedding", "dataType": "VECTOR_FLOAT", "dimensions": 4},
30+
],
31+
)
32+
print(status_code, response)
2233

2334

2435
# Insert new vector records into table
@@ -49,3 +60,7 @@
4960
status_code, response = db.delete(table_name="MyTable", primary_keys=[4, 5])
5061
status_code, response = db.delete(table_name="MyTable", filter="Doc <> 'San Francisco'")
5162
print(status_code, response)
63+
64+
# Drop table
65+
status_code, response = db.drop_table(table_name="MyTable")
66+
print(status_code, response)

pyepsilla/cloud/client.py

Lines changed: 69 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import requests
1212
import sentry_sdk
13+
from pydantic import BaseModel, Field, constr
1314

1415
requests.packages.urllib3.disable_warnings()
1516

@@ -18,7 +19,7 @@ class Client(object):
1819
def __init__(self, project_id: str, api_key: str):
1920
self._project_id = project_id
2021
self._apikey = api_key
21-
self._baseurl = "https://dispatch.epsilla.com/api/v2/project/{}".format(
22+
self._baseurl = "https://dispatch.epsilla.com/api/v3/project/{}".format(
2223
self._project_id
2324
)
2425
self._timeout = 10
@@ -30,7 +31,10 @@ def __init__(self, project_id: str, api_key: str):
3031

3132
def validate(self):
3233
res = requests.get(
33-
url=self._baseurl, data=None, headers=self._header, verify=False
34+
url=self._baseurl + "/vectordb/list",
35+
data=None,
36+
headers=self._header,
37+
verify=False,
3438
)
3539
data = res.json()
3640
res.close()
@@ -56,19 +60,20 @@ def get_db_info(self, db_id: str):
5660
return status_code, body
5761

5862
def vectordb(self, db_id: str):
59-
## validate project_id and api_key
63+
# validate project_id and api_key
6064
res = self.validate()
6165
if res["statusCode"] != 200:
6266
if res["statusCode"] == 404:
6367
raise Exception("Invalid project_id")
6468
if res["statusCode"] == 401:
6569
raise Exception("Invalid api_key")
6670

67-
## validate db_id
68-
if not db_id in self.get_db_list():
71+
# validate db_id
72+
db_list = self.get_db_list()
73+
if db_id not in db_list:
6974
raise Exception("Invalid db_id")
7075

71-
## fetch db public endpoint
76+
# fetch db public endpoint
7277
status_code, resp = self.get_db_info(db_id=db_id)
7378
if resp["statusCode"] == 200:
7479
return Vectordb(
@@ -78,40 +83,65 @@ def vectordb(self, db_id: str):
7883
print(resp)
7984
raise Exception("Failed to get db info")
8085

81-
## TODO
82-
def create_db(
83-
self, db_id: str, db_type: str, db_name: str, db_description: str = ""
84-
):
85-
pass
86-
87-
def delete_db(self, db_id: str):
88-
pass
89-
9086

9187
class Vectordb(Client):
9288
def __init__(self, project_id: str, db_id: str, api_key: str, public_endpoint: str):
9389
self._project_id = project_id
9490
self._db_id = db_id
9591
self._api_key = api_key
9692
self._public_endpoint = public_endpoint
97-
self._baseurl = "https://{}/api/v2/project/{}/vectordb/{}".format(
93+
self._baseurl = "https://{}/api/v3/project/{}/vectordb/{}".format(
9894
self._public_endpoint, self._project_id, self._db_id
9995
)
10096
self._header = {"Content-type": "application/json", "X-API-Key": self._api_key}
10197

102-
## TODO
103-
## create table
104-
def create_table(self, table_name: str = "MyTable", table_fields: list[str] = None):
105-
pass
98+
# List table
99+
def list_tables(self):
100+
if self._db_id is None:
101+
raise Exception("[ERROR] db_id is None!")
102+
req_url = "{}/table/list".format(self._baseurl)
103+
res = requests.get(url=req_url, headers=self._header, verify=False)
104+
status_code = res.status_code
105+
body = res.json()
106+
res.close()
107+
return status_code, body
106108

107-
## drop table
109+
# Create table
110+
def create_table(self, table_name: str, table_fields: list[str] = None):
111+
if self._db_id is None:
112+
raise Exception("[ERROR] db_id is None!")
113+
if table_fields is None:
114+
table_fields = []
115+
req_url = "{}/table/create".format(self._baseurl)
116+
req_data = {"table_name": table_name, "fields": table_fields}
117+
res = requests.post(
118+
url=req_url, data=json.dumps(req_data), headers=self._header, verify=False
119+
)
120+
status_code = res.status_code
121+
body = res.json()
122+
res.close()
123+
return status_code, body
124+
125+
# Drop table
108126
def drop_table(self, table_name: str):
109-
pass
127+
if self._db_id is None:
128+
raise Exception("[ERROR] db_id is None!")
129+
req_url = "{}/table/delete?table_name={}".format(self._baseurl, table_name)
130+
req_data = {}
131+
res = requests.delete(
132+
url=req_url, data=json.dumps(req_data), headers=self._header, verify=False
133+
)
134+
status_code = res.status_code
135+
body = res.json()
136+
res.close()
137+
return status_code, body
110138

111-
## insert data into table
139+
# Insert data into table
112140
def insert(self, table_name: str, records: list[dict]):
113141
req_url = "{}/data/insert".format(self._baseurl)
114142
req_data = {"table": table_name, "data": records}
143+
print("req_url: ", req_url)
144+
print("req_data: ", req_data)
115145
res = requests.post(
116146
url=req_url, data=json.dumps(req_data), headers=self._header, verify=False
117147
)
@@ -120,7 +150,7 @@ def insert(self, table_name: str, records: list[dict]):
120150
res.close()
121151
return status_code, body
122152

123-
## query data from table
153+
# Query data from table
124154
def query(
125155
self,
126156
table_name: str,
@@ -154,7 +184,7 @@ def query(
154184
res.close()
155185
return status_code, body
156186

157-
## delete data from table
187+
# Delete data from table
158188
def delete(
159189
self,
160190
table_name: str,
@@ -163,14 +193,14 @@ def delete(
163193
filter: Optional[str] = None,
164194
):
165195
"""Epsilla supports delete records by primary keys as default for now."""
166-
if filter == None:
167-
if primary_keys == None and ids == None:
196+
if filter is None:
197+
if primary_keys is None and ids is None:
168198
raise Exception(
169199
"[ERROR] Please provide at least one of primary keys(ids) and filter to delete record(s)."
170200
)
171-
if primary_keys == None and ids != None:
201+
if primary_keys is None and ids is not None:
172202
primary_keys = ids
173-
if primary_keys != None and ids != None:
203+
if primary_keys is not None and ids is not None:
174204
try:
175205
sentry_sdk.sdk("Duplicate Keys with both primary keys and ids", "info")
176206
except Exception as e:
@@ -181,9 +211,9 @@ def delete(
181211

182212
req_url = "{}/data/delete".format(self._baseurl)
183213
req_data = {"table": table_name}
184-
if primary_keys != None:
214+
if primary_keys is not None:
185215
req_data["primaryKeys"] = primary_keys
186-
if filter != None:
216+
if filter is not None:
187217
req_data["filter"] = filter
188218

189219
res = requests.post(
@@ -194,7 +224,7 @@ def delete(
194224
res.close()
195225
return status_code, body
196226

197-
## get data from table
227+
# Get data from table
198228
def get(
199229
self,
200230
table_name: str,
@@ -206,27 +236,27 @@ def get(
206236
limit: Optional[int] = None,
207237
):
208238
"""Epsilla supports get records by primary keys as default for now."""
209-
if primary_keys != None and ids != None:
239+
if primary_keys is not None and ids is not None:
210240
try:
211241
sentry_sdk.sdk("Duplicate Keys with both primary_keys and ids", "info")
212242
except Exception as e:
213243
pass
214244
print(
215245
"[WARN]Both primary_keys and ids are prvoided, will use primary keys by default!"
216246
)
217-
if primary_keys == None and ids != None:
247+
if primary_keys is None and ids is not None:
218248
primary_keys = ids
219249

220250
req_data = {"table": table_name}
221-
if response_fields != None:
251+
if response_fields is not None:
222252
req_data["response"] = response_fields
223-
if primary_keys != None:
253+
if primary_keys is not None:
224254
req_data["primaryKeys"] = primary_keys
225-
if filter != None:
255+
if filter is not None:
226256
req_data["filter"] = filter
227-
if skip != None:
257+
if skip is not None:
228258
req_data["skip"] = skip
229-
if limit != None:
259+
if limit is not None:
230260
req_data["limit"] = limit
231261

232262
req_url = "{}/data/get".format(self._baseurl)

pyepsilla/vectordb/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.2.3"
1+
__version__ = "0.2.4"

0 commit comments

Comments
 (0)