-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvelesdb_client.py
More file actions
255 lines (221 loc) · 8.36 KB
/
velesdb_client.py
File metadata and controls
255 lines (221 loc) · 8.36 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
"""
VelesDB HTTP Client for benchmarks.
Wraps the REST API so benchmarks use the same network path as competitors.
"""
import json
from typing import Any
import requests
class VelesDBClient:
"""Thin HTTP client for VelesDB REST API."""
def __init__(self, host: str = "localhost", port: int = 8080, timeout: int = 120):
self.base = f"http://{host}:{port}"
self.timeout = timeout
self.session = requests.Session()
self.session.headers["Content-Type"] = "application/json"
# ------------------------------------------------------------------
# Health
# ------------------------------------------------------------------
def health(self) -> dict:
r = self.session.get(f"{self.base}/health", timeout=self.timeout)
r.raise_for_status()
return r.json()
def ready(self) -> bool:
try:
r = self.session.get(f"{self.base}/ready", timeout=5)
return r.status_code == 200
except Exception:
return False
def wait_ready(self, retries: int = 30, interval: float = 1.0):
import time
for _ in range(retries):
if self.ready():
return True
time.sleep(interval)
raise RuntimeError("VelesDB server not ready")
# ------------------------------------------------------------------
# Collections
# ------------------------------------------------------------------
def create_collection(self, name: str, dimension: int, metric: str = "cosine") -> dict:
r = self.session.post(
f"{self.base}/collections",
json={"name": name, "dimension": dimension, "metric": metric},
timeout=self.timeout,
)
r.raise_for_status()
return r.json()
def delete_collection(self, name: str):
r = self.session.delete(f"{self.base}/collections/{name}", timeout=self.timeout)
# 404 is fine (already deleted)
if r.status_code not in (200, 204, 404):
r.raise_for_status()
def get_collection(self, name: str) -> dict:
r = self.session.get(f"{self.base}/collections/{name}", timeout=self.timeout)
r.raise_for_status()
return r.json()
def list_collections(self) -> list:
r = self.session.get(f"{self.base}/collections", timeout=self.timeout)
r.raise_for_status()
return r.json()
# ------------------------------------------------------------------
# Points
# ------------------------------------------------------------------
def upsert_points(self, collection: str, points: list[dict]):
"""Upsert a batch of points. Each point: {id, vector, payload?}."""
r = self.session.post(
f"{self.base}/collections/{collection}/points",
json={"points": points},
timeout=self.timeout,
)
r.raise_for_status()
return r.json()
# ------------------------------------------------------------------
# Search
# ------------------------------------------------------------------
def search(
self,
collection: str,
vector: list[float],
top_k: int = 10,
filter: dict | None = None,
mode: str | None = None,
) -> list[dict]:
body: dict[str, Any] = {"vector": vector, "top_k": top_k}
if filter:
body["filter"] = filter
if mode:
body["mode"] = mode
r = self.session.post(
f"{self.base}/collections/{collection}/search",
json=body,
timeout=self.timeout,
)
r.raise_for_status()
data = r.json()
return data.get("results", data)
# ------------------------------------------------------------------
# VelesQL
# ------------------------------------------------------------------
def execute_query(self, query: str, params: dict | None = None) -> list[dict]:
body: dict[str, Any] = {"query": query, "params": params or {}}
r = self.session.post(
f"{self.base}/query",
json=body,
timeout=self.timeout,
)
r.raise_for_status()
data = r.json()
return data.get("results", data)
# ------------------------------------------------------------------
# Graph
# ------------------------------------------------------------------
def create_graph_collection(self, name: str, dimension: int = 3) -> dict:
"""Create a collection that will hold graph data.
We create a minimal vector collection, then use graph endpoints."""
return self.create_collection(name, dimension=dimension, metric="cosine")
def add_edge(self, collection: str, edge: dict):
r = self.session.post(
f"{self.base}/collections/{collection}/graph/edges",
json=edge,
timeout=self.timeout,
)
r.raise_for_status()
return r.json()
def add_edges_batch(self, collection: str, edges: list[dict]):
"""Add edges one by one (server has no batch endpoint yet)."""
for e in edges:
self.add_edge(collection, e)
def store_node_payload(self, collection: str, node_id: int, payload: dict):
r = self.session.put(
f"{self.base}/collections/{collection}/graph/nodes/{node_id}/payload",
json={"payload": payload},
timeout=self.timeout,
)
r.raise_for_status()
def get_node_payload(self, collection: str, node_id: int) -> dict:
r = self.session.get(
f"{self.base}/collections/{collection}/graph/nodes/{node_id}/payload",
timeout=self.timeout,
)
r.raise_for_status()
return r.json()
def traverse_bfs(
self,
collection: str,
source: int,
max_depth: int = 3,
limit: int = 100,
rel_types: list[str] | None = None,
) -> list[dict]:
body: dict[str, Any] = {
"source": source,
"strategy": "bfs",
"max_depth": max_depth,
"limit": limit,
}
if rel_types:
body["rel_types"] = rel_types
r = self.session.post(
f"{self.base}/collections/{collection}/graph/traverse",
json=body,
timeout=self.timeout,
)
r.raise_for_status()
return r.json()
def traverse_dfs(
self,
collection: str,
source: int,
max_depth: int = 3,
limit: int = 100,
rel_types: list[str] | None = None,
) -> list[dict]:
body: dict[str, Any] = {
"source": source,
"strategy": "dfs",
"max_depth": max_depth,
"limit": limit,
}
if rel_types:
body["rel_types"] = rel_types
r = self.session.post(
f"{self.base}/collections/{collection}/graph/traverse",
json=body,
timeout=self.timeout,
)
r.raise_for_status()
return r.json()
def get_outgoing(self, collection: str, node_id: int) -> list[dict]:
r = self.session.get(
f"{self.base}/collections/{collection}/graph/nodes/{node_id}/edges",
params={"direction": "out"},
timeout=self.timeout,
)
r.raise_for_status()
return r.json()
def match_query(self, collection: str, query: str, vector: list | None = None) -> list[dict]:
body: dict[str, Any] = {"query": query}
if vector:
body["vector"] = vector
r = self.session.post(
f"{self.base}/collections/{collection}/match",
json=body,
timeout=self.timeout,
)
r.raise_for_status()
return r.json()
# ------------------------------------------------------------------
# Index
# ------------------------------------------------------------------
def create_index(self, collection: str, label: str):
r = self.session.post(
f"{self.base}/collections/{collection}/indexes",
json={"label": label, "property": "name"},
timeout=self.timeout,
)
# Ignore errors (some columns may not support indexing)
return r.status_code < 400
# ------------------------------------------------------------------
# Repr
# ------------------------------------------------------------------
def __repr__(self):
return f"VelesDBClient({self.base})"