Skip to content

Commit 16ae04e

Browse files
committed
fix the issue of limit for vectordb
1 parent 31c2c05 commit 16ae04e

2 files changed

Lines changed: 134 additions & 62 deletions

File tree

examples/hello_epsilla.py

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from pyepsilla import vectordb
1111

1212
# Connect to Epsilla VectorDB
13-
client = vectordb.Client(protocol='http', host='127.0.0.1', port='8888')
13+
client = vectordb.Client(protocol="http", host="127.0.0.1", port="8888")
1414

1515
# You can also use Epsilla Cloud
1616
# client = vectordb.Client(protocol='https', host='demo.epsilla.com', port='443')
@@ -25,12 +25,12 @@
2525

2626
# Create a table with schema in current DB
2727
status_code, response = client.create_table(
28-
table_name="MyTable",
29-
table_fields=[
30-
{"name": "ID", "dataType": "INT", "primaryKey": True},
31-
{"name": "Doc", "dataType": "STRING"},
32-
{"name": "Embedding", "dataType": "VECTOR_FLOAT", "dimensions": 4}
33-
]
28+
table_name="MyTable",
29+
table_fields=[
30+
{"name": "ID", "dataType": "INT", "primaryKey": True},
31+
{"name": "Doc", "dataType": "STRING"},
32+
{"name": "Embedding", "dataType": "VECTOR_FLOAT", "dimensions": 4},
33+
],
3434
)
3535
print(response)
3636

@@ -40,43 +40,45 @@
4040

4141
# Insert new vector records into table
4242
status_code, response = client.insert(
43-
table_name="MyTable",
44-
records=[
45-
{"ID": 1, "Doc": "Berlin", "Embedding": [0.05, 0.61, 0.76, 0.74]},
46-
{"ID": 2, "Doc": "London", "Embedding": [0.19, 0.81, 0.75, 0.11]},
47-
{"ID": 3, "Doc": "Moscow", "Embedding": [0.36, 0.55, 0.47, 0.94]},
48-
{"ID": 4, "Doc": "San Francisco", "Embedding": [0.18, 0.01, 0.85, 0.80]},
49-
{"ID": 5, "Doc": "Shanghai", "Embedding": [0.24, 0.18, 0.22, 0.44]}
50-
]
43+
table_name="MyTable",
44+
records=[
45+
{"ID": 1, "Doc": "Berlin", "Embedding": [0.05, 0.61, 0.76, 0.74]},
46+
{"ID": 2, "Doc": "London", "Embedding": [0.19, 0.81, 0.75, 0.11]},
47+
{"ID": 3, "Doc": "Moscow", "Embedding": [0.36, 0.55, 0.47, 0.94]},
48+
{"ID": 4, "Doc": "San Francisco", "Embedding": [0.18, 0.01, 0.85, 0.80]},
49+
{"ID": 5, "Doc": "Shanghai", "Embedding": [0.24, 0.18, 0.22, 0.44]},
50+
],
5151
)
5252
print(response)
5353

5454
# Query Vectors with specific response field
5555
status_code, response = client.query(
56-
table_name="MyTable",
57-
query_field="Embedding",
58-
query_vector=[0.35, 0.55, 0.47, 0.94],
59-
response_fields = ["Doc"],
60-
limit=2
56+
table_name="MyTable",
57+
query_field="Embedding",
58+
query_vector=[0.35, 0.55, 0.47, 0.94],
59+
response_fields=["Doc"],
60+
limit=2,
6161
)
6262

6363
# Query Vectors without specific response field, then it will return all fields
6464
status_code, response = client.query(
65-
table_name="MyTable",
66-
query_field="Embedding",
67-
query_vector=[0.35, 0.55, 0.47, 0.94],
68-
limit=2
65+
table_name="MyTable",
66+
query_field="Embedding",
67+
query_vector=[0.35, 0.55, 0.47, 0.94],
68+
limit=2,
6969
)
7070
print(response)
7171

72+
# Get Vectors
73+
status_code, response = client.get(table_name="MyTable", limit=2)
74+
print(response)
7275

7376
# status_code, response = client.delete(table_name="MyTable", ids=[3])
74-
status_code, response = client.delete(table_name="MyTable", primary_keys=[3, 4])
77+
status_code, response = client.delete(table_name="MyTable", primary_keys=[3, 4])
7578
# status_code, response = client.delete(table_name="MyTable", filter="Doc <> 'San Francisco'")
7679
print(response)
7780

7881

79-
8082
# Drop table
8183
# status_code, response = client.drop_table("MyTable")
8284
# print(response)

pyepsilla/vectordb/client.py

Lines changed: 106 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
#!/usr/bin/env python
22
# -*- coding:utf-8 -*-
3-
import json, requests, socket, datetime, time, sentry_sdk
4-
from typing import Union, Optional
5-
from requests.packages.urllib3.exceptions import InsecureRequestWarning
3+
import datetime
4+
import json
5+
import socket
6+
import time
7+
from typing import Optional, Union
8+
9+
import requests
10+
import sentry_sdk
11+
from requests.packages.urllib3.exceptions import InsecureRequestWarning
12+
613
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
714

8-
class Client():
9-
def __init__(self, protocol: str = 'http', host: str = 'localhost', port: str = '8888'):
15+
16+
class Client:
17+
def __init__(
18+
self, protocol: str = "http", host: str = "localhost", port: str = "8888"
19+
):
1020
self._protocol = protocol
1121
self._host = host
1222
self._port = port
1323
self._baseurl = "{}://{}:{}".format(self._protocol, self._host, self._port)
1424
self._db = None
1525
self._timeout = 10
16-
self._header = {'Content-type': 'application/json', "Connection": "close"}
26+
self._header = {"Content-type": "application/json", "Connection": "close"}
1727
self.check_networking()
1828

1929
def check_networking(self):
@@ -22,14 +32,24 @@ def check_networking(self):
2232
check_result = s.connect_ex((self._host, int(self._port)))
2333
s.close()
2434
if check_result == 0:
25-
print("[INFO] Connected to {}:{} successfully.".format(self._host, self._port))
35+
print(
36+
"[INFO] Connected to {}:{} successfully.".format(self._host, self._port)
37+
)
2638
else:
27-
raise Exception("[ERROR] Failed to connect to {}:{}".format(self._host, self._port))
39+
raise Exception(
40+
"[ERROR] Failed to connect to {}:{}".format(self._host, self._port)
41+
)
2842

2943
def welcome(self):
3044
req_url = "{}/".format(self._baseurl)
3145
req_data = None
32-
res = requests.get(url=req_url, data=json.dumps(req_data), headers=self._header, timeout=self._timeout, verify=False)
46+
res = requests.get(
47+
url=req_url,
48+
data=json.dumps(req_data),
49+
headers=self._header,
50+
timeout=self._timeout,
51+
verify=False,
52+
)
3353
status_code = res.status_code
3454
body = res.text
3555
res.close()
@@ -38,7 +58,9 @@ def welcome(self):
3858
def state(self):
3959
req_url = "{}/state".format(self._baseurl)
4060
req_data = None
41-
res = requests.get(url=req_url, data=json.dumps(req_data), headers=self._header, verify=False)
61+
res = requests.get(
62+
url=req_url, data=json.dumps(req_data), headers=self._header, verify=False
63+
)
4264
status_code = res.status_code
4365
body = res.json()
4466
res.close()
@@ -47,20 +69,27 @@ def state(self):
4769
def use_db(self, db_name: str):
4870
self._db = db_name
4971

50-
def load_db(self, db_name: str, db_path: str, vector_scale: int = None, wal_enabled: bool = False):
72+
def load_db(
73+
self,
74+
db_name: str,
75+
db_path: str,
76+
vector_scale: int = None,
77+
wal_enabled: bool = False,
78+
):
5179
req_url = "{}/api/load".format(self._baseurl)
5280
req_data = {"name": db_name, "path": db_path}
5381
if vector_scale is not None:
5482
req_data["vectorScale"] = vector_scale
5583
if wal_enabled is not None:
5684
req_data["walEnabled"] = wal_enabled
57-
res = requests.post(url=req_url, data=json.dumps(req_data), headers=self._header, verify=False)
85+
res = requests.post(
86+
url=req_url, data=json.dumps(req_data), headers=self._header, verify=False
87+
)
5888
status_code = res.status_code
5989
body = res.json()
6090
res.close()
6191
return status_code, body
6292

63-
6493
def unload_db(self, db_name: str):
6594
req_url = "{}/api/{}/unload".format(self._baseurl, db_name)
6695
res = requests.post(url=req_url, data=None, headers=self._header, verify=False)
@@ -76,7 +105,9 @@ def create_table(self, table_name: str, table_fields: list[str] = None):
76105
table_fields = []
77106
req_url = "{}/api/{}/schema/tables".format(self._baseurl, self._db)
78107
req_data = {"name": table_name, "fields": table_fields}
79-
res = requests.post(url=req_url, data=json.dumps(req_data), headers=self._header, verify=False)
108+
res = requests.post(
109+
url=req_url, data=json.dumps(req_data), headers=self._header, verify=False
110+
)
80111
status_code = res.status_code
81112
body = res.json()
82113
res.close()
@@ -92,56 +123,74 @@ def list_tables(self):
92123
res.close()
93124
return status_code, body
94125

95-
96126
def insert(self, table_name: str, records: list = None):
97127
if self._db is None:
98128
raise Exception("[ERROR] Please use_db() first!")
99129
if records is None:
100130
records = []
101131
req_url = "{}/api/{}/data/insert".format(self._baseurl, self._db)
102132
req_data = {"table": table_name, "data": records}
103-
res = requests.post(url=req_url, data=json.dumps(req_data), headers=self._header, verify=False)
133+
res = requests.post(
134+
url=req_url, data=json.dumps(req_data), headers=self._header, verify=False
135+
)
104136
status_code = res.status_code
105137
body = res.json()
106138
res.close()
107139
return status_code, body
108140

109-
def delete(self, table_name: str, primary_keys: list[Union[str,int]] = None, ids: list[Union[str,int]] = None, filter: Optional[str] = None):
141+
def delete(
142+
self,
143+
table_name: str,
144+
primary_keys: list[Union[str, int]] = None,
145+
ids: list[Union[str, int]] = None,
146+
filter: Optional[str] = None,
147+
):
110148
"""Epsilla supports delete records by primary keys as default for now."""
111149
if self._db is None:
112150
raise Exception("[ERROR] Please use_db() first!")
113151

114152
if filter == None:
115153
if primary_keys == None and ids == None:
116-
raise Exception("[ERROR] Please provide at least one of primary keys(ids) and filter to delete record(s).")
154+
raise Exception(
155+
"[ERROR] Please provide at least one of primary keys(ids) and filter to delete record(s)."
156+
)
117157
if primary_keys == None and ids != None:
118158
primary_keys = ids
119159
if primary_keys != None and ids != None:
120160
try:
121161
sentry_sdk.sdk("Duplicate Keys with both primary keys and ids", "info")
122162
except Exception as e:
123163
pass
124-
print("[WARN] Both primary_keys and ids are prvoided, will use primary keys by default!")
125-
164+
print(
165+
"[WARN] Both primary_keys and ids are prvoided, will use primary keys by default!"
166+
)
167+
126168
req_url = "{}/api/{}/data/delete".format(self._baseurl, self._db)
127169
req_data = {"table": table_name}
128170
if primary_keys != None:
129171
req_data["primaryKeys"] = primary_keys
130172
if filter != None:
131173
req_data["filter"] = filter
132-
res = requests.post(url=req_url, data=json.dumps(req_data), headers=self._header, verify=False)
174+
res = requests.post(
175+
url=req_url, data=json.dumps(req_data), headers=self._header, verify=False
176+
)
133177
status_code = res.status_code
134178
body = res.json()
135179
res.close()
136180
return status_code, body
137181

138-
139182
def rebuild(self, timeout: int = 7200):
140183
req_url = "{}/api/rebuild".format(self._baseurl)
141184
req_data = None
142185
print("[INFO] waiting until rebuild is finished ...")
143186
start_time = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
144-
res = requests.post(url=req_url, data=json.dumps(req_data), headers=self._header, timeout=timeout, verify=False)
187+
res = requests.post(
188+
url=req_url,
189+
data=json.dumps(req_data),
190+
headers=self._header,
191+
timeout=timeout,
192+
verify=False,
193+
)
145194
end_time = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
146195
print("[INFO] Start Time:{}\n End Time:{}".format(start_time, end_time))
147196
status_code = res.status_code
@@ -150,13 +199,14 @@ def rebuild(self, timeout: int = 7200):
150199
return status_code, body
151200

152201
def query(
153-
self, table_name: str,
202+
self,
203+
table_name: str,
154204
query_field: str = "",
155-
query_vector: Union[list,dict] = None,
205+
query_vector: Union[list, dict] = None,
156206
response_fields: list = None,
157207
limit: int = 1,
158208
filter: str = "",
159-
with_distance: bool = False
209+
with_distance: bool = False,
160210
):
161211
if self._db is None:
162212
raise Exception("[ERROR] Please use_db() first!")
@@ -172,23 +222,36 @@ def query(
172222
"response": response_fields,
173223
"limit": limit,
174224
"filter": filter,
175-
"withDistance": with_distance
225+
"withDistance": with_distance,
176226
}
177-
res = requests.post(url=req_url, data=json.dumps(req_data), headers=self._header, verify=False)
227+
res = requests.post(
228+
url=req_url, data=json.dumps(req_data), headers=self._header, verify=False
229+
)
178230
status_code = res.status_code
179231
body = res.json()
180232
res.close()
181233
return status_code, body
182234

183-
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):
235+
def get(
236+
self,
237+
table_name: str,
238+
response_fields: Optional[list] = None,
239+
primary_keys: Optional[list[Union[str, int]]] = None,
240+
ids: Optional[list[Union[str, int]]] = None,
241+
filter: Optional[str] = None,
242+
skip: Optional[int] = None,
243+
limit: Optional[int] = None,
244+
):
184245
if self._db is None:
185246
raise Exception("[ERROR] Please use_db() first!")
186247
if primary_keys != None and ids != None:
187248
try:
188249
sentry_sdk.sdk("Duplicate Keys with both primary keys and ids", "info")
189250
except Exception as e:
190251
pass
191-
print("[WARN] Both primary_keys and ids are prvoided, will use primary keys by default!")
252+
print(
253+
"[WARN] Both primary_keys and ids are prvoided, will use primary keys by default!"
254+
)
192255
if primary_keys == None and ids != None:
193256
primary_keys = ids
194257

@@ -207,30 +270,37 @@ def get(self, table_name: str, response_fields: Optional[list] = None, primary_k
207270
req_data["skip"] = filter
208271

209272
if limit != None:
210-
req_data["limit"] = filter
273+
req_data["limit"] = limit
211274

212275
req_url = "{}/api/{}/data/get".format(self._baseurl, self._db)
213-
res = requests.post(url=req_url, data=json.dumps(req_data), headers=self._header, verify=False)
276+
res = requests.post(
277+
url=req_url, data=json.dumps(req_data), headers=self._header, verify=False
278+
)
214279
status_code = res.status_code
215280
body = res.json()
216281
res.close()
217282
return status_code, body
218283

219-
220284
def drop_table(self, table_name: str = None):
221285
if self._db is None:
222286
raise Exception("[ERROR] Please use_db() first!")
223-
req_url = "{}/api/{}/schema/tables/{}".format(self._baseurl, self._db, table_name)
287+
req_url = "{}/api/{}/schema/tables/{}".format(
288+
self._baseurl, self._db, table_name
289+
)
224290
req_data = None
225-
res = requests.delete(url=req_url, data=json.dumps(req_data), headers=self._header, verify=False)
291+
res = requests.delete(
292+
url=req_url, data=json.dumps(req_data), headers=self._header, verify=False
293+
)
226294
status_code = res.status_code
227295
body = res.json()
228296
res.close()
229297
return status_code, body
230298

231299
def drop_db(self, db_name: str):
232300
req_url = "{}/api/{}/drop".format(self._baseurl, db_name)
233-
res = requests.delete(url=req_url, data=None, headers=self._header, verify=False)
301+
res = requests.delete(
302+
url=req_url, data=None, headers=self._header, verify=False
303+
)
234304
status_code = res.status_code
235305
body = res.json()
236306
res.close()

0 commit comments

Comments
 (0)