kektordb-client 0.1.0__tar.gz
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.
- kektordb_client-0.1.0/PKG-INFO +24 -0
- kektordb_client-0.1.0/README.md +0 -0
- kektordb_client-0.1.0/kektordb_client/__init__.py +3 -0
- kektordb_client-0.1.0/kektordb_client/client.py +209 -0
- kektordb_client-0.1.0/kektordb_client.egg-info/PKG-INFO +24 -0
- kektordb_client-0.1.0/kektordb_client.egg-info/SOURCES.txt +9 -0
- kektordb_client-0.1.0/kektordb_client.egg-info/dependency_links.txt +1 -0
- kektordb_client-0.1.0/kektordb_client.egg-info/requires.txt +2 -0
- kektordb_client-0.1.0/kektordb_client.egg-info/top_level.txt +1 -0
- kektordb_client-0.1.0/setup.cfg +4 -0
- kektordb_client-0.1.0/setup.py +29 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: kektordb_client
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: An official Python client for KektorDB, a high-performance in-memory vector database.
|
|
5
|
+
Home-page: https://github.com/sanonone/kektordb
|
|
6
|
+
Author: Sanonone
|
|
7
|
+
Author-email: fedeld023@gmail.com
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Topic :: Database
|
|
12
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
13
|
+
Requires-Python: >=3.7
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
Requires-Dist: requests
|
|
16
|
+
Requires-Dist: numpy
|
|
17
|
+
Dynamic: author
|
|
18
|
+
Dynamic: author-email
|
|
19
|
+
Dynamic: classifier
|
|
20
|
+
Dynamic: description-content-type
|
|
21
|
+
Dynamic: home-page
|
|
22
|
+
Dynamic: requires-dist
|
|
23
|
+
Dynamic: requires-python
|
|
24
|
+
Dynamic: summary
|
|
File without changes
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import requests
|
|
2
|
+
from typing import List, Dict, Any, Union
|
|
3
|
+
|
|
4
|
+
# --- Custom Exceptions ---
|
|
5
|
+
|
|
6
|
+
class KektorDBError(Exception):
|
|
7
|
+
"""Base exception class for KektorDB client errors."""
|
|
8
|
+
pass
|
|
9
|
+
|
|
10
|
+
class APIError(KektorDBError):
|
|
11
|
+
"""Raised for status codes >= 400, indicating an API-level error."""
|
|
12
|
+
pass
|
|
13
|
+
|
|
14
|
+
class ConnectionError(KektorDBError):
|
|
15
|
+
"""Raised for network-related errors (e.g., connection refused)."""
|
|
16
|
+
pass
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class KektorDBClient:
|
|
20
|
+
"""
|
|
21
|
+
The official Python client for interacting with a KektorDB server via its REST API.
|
|
22
|
+
"""
|
|
23
|
+
def __init__(self, host: str = "localhost", port: int = 9091, timeout: int = 30):
|
|
24
|
+
"""
|
|
25
|
+
Initializes the KektorDB client.
|
|
26
|
+
|
|
27
|
+
Args:
|
|
28
|
+
host: The hostname or IP address of the KektorDB server.
|
|
29
|
+
port: The port of the KektorDB REST API server.
|
|
30
|
+
timeout: The request timeout in seconds.
|
|
31
|
+
"""
|
|
32
|
+
self.base_url = f"http://{host}:{port}"
|
|
33
|
+
self.timeout = timeout
|
|
34
|
+
self._session = requests.Session()
|
|
35
|
+
|
|
36
|
+
def _request(self, method: str, endpoint: str, **kwargs) -> Dict[str, Any]:
|
|
37
|
+
"""Internal helper method for making HTTP requests."""
|
|
38
|
+
try:
|
|
39
|
+
response = self._session.request(
|
|
40
|
+
method,
|
|
41
|
+
f"{self.base_url}{endpoint}",
|
|
42
|
+
timeout=self.timeout,
|
|
43
|
+
**kwargs
|
|
44
|
+
)
|
|
45
|
+
response.raise_for_status()
|
|
46
|
+
if response.status_code == 204: # No Content
|
|
47
|
+
return {}
|
|
48
|
+
return response.json()
|
|
49
|
+
except requests.exceptions.HTTPError as e:
|
|
50
|
+
try:
|
|
51
|
+
msg = e.response.json().get("error", str(e))
|
|
52
|
+
except:
|
|
53
|
+
msg = str(e)
|
|
54
|
+
raise APIError(f"KektorDB API Error: {msg}") from e
|
|
55
|
+
except requests.exceptions.RequestException as e:
|
|
56
|
+
raise ConnectionError(f"KektorDB Connection Error: {e}") from e
|
|
57
|
+
|
|
58
|
+
# --- Key-Value Store Methods ---
|
|
59
|
+
|
|
60
|
+
def set(self, key: str, value: Union[str, bytes]) -> None:
|
|
61
|
+
"""
|
|
62
|
+
Sets a value for a given key in the KV store.
|
|
63
|
+
|
|
64
|
+
Args:
|
|
65
|
+
key: The key to set.
|
|
66
|
+
value: The value to store (str or bytes).
|
|
67
|
+
|
|
68
|
+
Raises:
|
|
69
|
+
APIError: If the server returns an error.
|
|
70
|
+
ConnectionError: If a network error occurs.
|
|
71
|
+
"""
|
|
72
|
+
if isinstance(value, bytes):
|
|
73
|
+
value = value.decode('utf-8')
|
|
74
|
+
payload = {"value": value}
|
|
75
|
+
self._request("POST", f"/kv/{key}", json=payload)
|
|
76
|
+
|
|
77
|
+
def get(self, key: str) -> str:
|
|
78
|
+
"""
|
|
79
|
+
Retrieves a value for a given key from the KV store.
|
|
80
|
+
|
|
81
|
+
Args:
|
|
82
|
+
key: The key to retrieve.
|
|
83
|
+
|
|
84
|
+
Returns:
|
|
85
|
+
The value as a string.
|
|
86
|
+
|
|
87
|
+
Raises:
|
|
88
|
+
APIError: If the key is not found (404) or another error occurs.
|
|
89
|
+
ConnectionError: If a network error occurs.
|
|
90
|
+
"""
|
|
91
|
+
data = self._request("GET", f"/kv/{key}")
|
|
92
|
+
return data.get("value")
|
|
93
|
+
|
|
94
|
+
def delete(self, key: str) -> None:
|
|
95
|
+
"""
|
|
96
|
+
Deletes a key from the KV store.
|
|
97
|
+
|
|
98
|
+
Args:
|
|
99
|
+
key: The key to delete.
|
|
100
|
+
|
|
101
|
+
Raises:
|
|
102
|
+
APIError: If the server returns an error.
|
|
103
|
+
ConnectionError: If a network error occurs.
|
|
104
|
+
"""
|
|
105
|
+
self._request("DELETE", f"/kv/{key}")
|
|
106
|
+
|
|
107
|
+
# --- Vector Index Methods ---
|
|
108
|
+
|
|
109
|
+
def vcreate(self, index_name: str, metric: str = "euclidean", precision: str = "float32", m: int = 0, ef_construction: int = 0) -> None:
|
|
110
|
+
"""
|
|
111
|
+
Creates a new vector index.
|
|
112
|
+
|
|
113
|
+
Args:
|
|
114
|
+
index_name: The name for the new index.
|
|
115
|
+
metric: The distance metric to use ('euclidean' or 'cosine').
|
|
116
|
+
precision: The data precision for vectors ('float32', 'float16', 'int8').
|
|
117
|
+
m: HNSW M parameter (max connections). Server default if 0.
|
|
118
|
+
ef_construction: HNSW efConstruction parameter. Server default if 0.
|
|
119
|
+
|
|
120
|
+
Raises:
|
|
121
|
+
APIError: If the index already exists or parameters are invalid.
|
|
122
|
+
ConnectionError: If a network error occurs.
|
|
123
|
+
"""
|
|
124
|
+
payload = {"index_name": index_name, "metric": metric, "precision": precision}
|
|
125
|
+
if m > 0: payload["m"] = m
|
|
126
|
+
if ef_construction > 0: payload["ef_construction"] = ef_construction
|
|
127
|
+
self._request("POST", "/vector/create", json=payload)
|
|
128
|
+
|
|
129
|
+
def vadd(self, index_name: str, item_id: str, vector: List[float], metadata: Dict[str, Any] = None) -> None:
|
|
130
|
+
"""
|
|
131
|
+
Adds a vector to an index.
|
|
132
|
+
|
|
133
|
+
Args:
|
|
134
|
+
index_name: The name of the index.
|
|
135
|
+
item_id: A unique ID for the vector.
|
|
136
|
+
vector: The vector embedding as a list of floats.
|
|
137
|
+
metadata: An optional dictionary of metadata.
|
|
138
|
+
|
|
139
|
+
Raises:
|
|
140
|
+
APIError: If the index does not exist or the vector is invalid.
|
|
141
|
+
ConnectionError: If a network error occurs.
|
|
142
|
+
"""
|
|
143
|
+
payload = {"index_name": index_name, "id": item_id, "vector": vector}
|
|
144
|
+
if metadata: payload["metadata"] = metadata
|
|
145
|
+
self._request("POST", "/vector/add", json=payload)
|
|
146
|
+
|
|
147
|
+
def vdelete(self, index_name: str, item_id: str) -> None:
|
|
148
|
+
"""
|
|
149
|
+
Deletes a vector from an index (soft delete).
|
|
150
|
+
|
|
151
|
+
Args:
|
|
152
|
+
index_name: The name of the index.
|
|
153
|
+
item_id: The ID of the vector to delete.
|
|
154
|
+
|
|
155
|
+
Raises:
|
|
156
|
+
APIError: If the index does not exist.
|
|
157
|
+
ConnectionError: If a network error occurs.
|
|
158
|
+
"""
|
|
159
|
+
payload = {"index_name": index_name, "id": item_id}
|
|
160
|
+
self._request("POST", "/vector/delete", json=payload)
|
|
161
|
+
|
|
162
|
+
def vsearch(self, index_name: str, query_vector: List[float], k: int, filter_str: str = "") -> List[str]:
|
|
163
|
+
"""
|
|
164
|
+
Performs a nearest neighbor search in an index.
|
|
165
|
+
|
|
166
|
+
Args:
|
|
167
|
+
index_name: The name of the index to search in.
|
|
168
|
+
query_vector: The query vector.
|
|
169
|
+
k: The number of nearest neighbors to return.
|
|
170
|
+
filter_str: An optional filter string (e.g., "tag=cat AND price<50").
|
|
171
|
+
|
|
172
|
+
Returns:
|
|
173
|
+
A list of item IDs of the nearest neighbors.
|
|
174
|
+
|
|
175
|
+
Raises:
|
|
176
|
+
APIError: If the index does not exist or the query is invalid.
|
|
177
|
+
ConnectionError: If a network error occurs.
|
|
178
|
+
"""
|
|
179
|
+
payload = {"index_name": index_name, "k": k, "query_vector": query_vector}
|
|
180
|
+
if filter_str: payload["filter"] = filter_str
|
|
181
|
+
data = self._request("POST", "/vector/search", json=payload)
|
|
182
|
+
return data.get("results", [])
|
|
183
|
+
|
|
184
|
+
def vcompress(self, index_name: str, precision: str) -> None:
|
|
185
|
+
"""
|
|
186
|
+
Compresses an existing float32 index to a lower precision format.
|
|
187
|
+
|
|
188
|
+
Args:
|
|
189
|
+
index_name: The name of the index to compress.
|
|
190
|
+
precision: The target precision ('float16' or 'int8').
|
|
191
|
+
|
|
192
|
+
Raises:
|
|
193
|
+
APIError: If the index does not exist or compression is not possible.
|
|
194
|
+
ConnectionError: If a network error occurs.
|
|
195
|
+
"""
|
|
196
|
+
payload = {"index_name": index_name, "precision": precision}
|
|
197
|
+
self._request("POST", "/vector/compress", json=payload)
|
|
198
|
+
|
|
199
|
+
# --- System Methods ---
|
|
200
|
+
|
|
201
|
+
def aof_rewrite(self) -> None:
|
|
202
|
+
"""
|
|
203
|
+
Requests the server to perform an AOF compaction.
|
|
204
|
+
|
|
205
|
+
Raises:
|
|
206
|
+
APIError: If the server fails to rewrite the AOF.
|
|
207
|
+
ConnectionError: If a network error occurs.
|
|
208
|
+
"""
|
|
209
|
+
self._request("POST", "/system/aof-rewrite")
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: kektordb_client
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: An official Python client for KektorDB, a high-performance in-memory vector database.
|
|
5
|
+
Home-page: https://github.com/sanonone/kektordb
|
|
6
|
+
Author: Sanonone
|
|
7
|
+
Author-email: fedeld023@gmail.com
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Topic :: Database
|
|
12
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
13
|
+
Requires-Python: >=3.7
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
Requires-Dist: requests
|
|
16
|
+
Requires-Dist: numpy
|
|
17
|
+
Dynamic: author
|
|
18
|
+
Dynamic: author-email
|
|
19
|
+
Dynamic: classifier
|
|
20
|
+
Dynamic: description-content-type
|
|
21
|
+
Dynamic: home-page
|
|
22
|
+
Dynamic: requires-dist
|
|
23
|
+
Dynamic: requires-python
|
|
24
|
+
Dynamic: summary
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
setup.py
|
|
3
|
+
kektordb_client/__init__.py
|
|
4
|
+
kektordb_client/client.py
|
|
5
|
+
kektordb_client.egg-info/PKG-INFO
|
|
6
|
+
kektordb_client.egg-info/SOURCES.txt
|
|
7
|
+
kektordb_client.egg-info/dependency_links.txt
|
|
8
|
+
kektordb_client.egg-info/requires.txt
|
|
9
|
+
kektordb_client.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
kektordb_client
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
from setuptools import setup, find_packages
|
|
2
|
+
|
|
3
|
+
# Legge il README per la descrizione lunga
|
|
4
|
+
with open("README.md", "r", encoding="utf-8") as fh:
|
|
5
|
+
long_description = fh.read()
|
|
6
|
+
|
|
7
|
+
setup(
|
|
8
|
+
name="kektordb_client",
|
|
9
|
+
version="0.1.0",
|
|
10
|
+
author="Sanonone",
|
|
11
|
+
author_email="fedeld023@gmail.com",
|
|
12
|
+
description="An official Python client for KektorDB, a high-performance in-memory vector database.",
|
|
13
|
+
long_description=long_description,
|
|
14
|
+
long_description_content_type="text/markdown",
|
|
15
|
+
url="https://github.com/sanonone/kektordb",
|
|
16
|
+
packages=find_packages(),
|
|
17
|
+
classifiers=[
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"License :: OSI Approved :: Apache Software License",
|
|
20
|
+
"Operating System :: OS Independent",
|
|
21
|
+
"Topic :: Database",
|
|
22
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
23
|
+
],
|
|
24
|
+
python_requires='>=3.7',
|
|
25
|
+
install_requires=[
|
|
26
|
+
"requests",
|
|
27
|
+
"numpy",
|
|
28
|
+
],
|
|
29
|
+
)
|