-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathpython_example.py
More file actions
247 lines (206 loc) · 7.57 KB
/
Copy pathpython_example.py
File metadata and controls
247 lines (206 loc) · 7.57 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
#!/usr/bin/env python3
"""
VelesDB Python REST Example (Legacy)
DEPRECATED: This file demonstrates the VelesDB HTTP REST API using the requests
library. For new projects, use the native Python SDK instead:
pip install velesdb
import velesdb
db = velesdb.Database("./my_data")
col = db.get_or_create_collection("docs", dimension=768)
col.upsert([{"id": 1, "vector": [...], "payload": {"title": "Doc"}}])
results = col.search_request(velesdb.SearchOptions(vector=[...], top_k=10))
See examples/langchain/hybrid_search.py and examples/llamaindex/hybrid_search.py
for production-ready integration examples.
This REST client is still useful when connecting to a remote VelesDB server
from an environment where the native extension cannot be compiled.
"""
import logging
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
from typing import List, Dict, Any
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
# VelesDB server URL
VELESDB_URL = "http://localhost:8080"
# Request timeout in seconds
DEFAULT_TIMEOUT = 30
class VelesDBClient:
"""Simple VelesDB client for Python.
Args:
base_url: VelesDB server URL (default: http://localhost:8080)
timeout: Request timeout in seconds (default: 30)
retries: Number of retry attempts for failed requests (default: 3)
"""
def __init__(
self,
base_url: str = VELESDB_URL,
timeout: float = DEFAULT_TIMEOUT,
retries: int = 3
):
self.base_url = base_url.rstrip("/")
self.timeout = timeout
# Configure session with retry logic
self.session = requests.Session()
retry_strategy = Retry(
total=retries,
backoff_factor=0.5,
status_forcelist=[500, 502, 503, 504],
)
adapter = HTTPAdapter(max_retries=retry_strategy)
self.session.mount("http://", adapter)
self.session.mount("https://", adapter)
def health(self) -> Dict[str, Any]:
"""Check server health.
Returns:
Dict with 'status' and 'version' keys.
Raises:
requests.RequestException: If server is unreachable.
"""
response = self.session.get(f"{self.base_url}/health", timeout=self.timeout)
response.raise_for_status()
return response.json()
def create_collection(
self,
name: str,
dimension: int,
metric: str = "cosine"
) -> Dict[str, Any]:
"""Create a new collection.
Args:
name: Collection name (alphanumeric, underscores allowed).
dimension: Vector dimension (must match your embeddings).
metric: Distance metric ('cosine', 'euclidean', 'dot').
Returns:
Dict with creation confirmation.
"""
response = self.session.post(
f"{self.base_url}/collections",
json={"name": name, "dimension": dimension, "metric": metric},
timeout=self.timeout
)
response.raise_for_status()
return response.json()
def list_collections(self) -> List[str]:
"""List all collections.
Returns:
List of collection names.
"""
response = self.session.get(f"{self.base_url}/collections", timeout=self.timeout)
response.raise_for_status()
return response.json()["collections"]
def delete_collection(self, name: str) -> Dict[str, Any]:
"""Delete a collection.
Args:
name: Collection name to delete.
Returns:
Dict with deletion confirmation.
"""
response = self.session.delete(
f"{self.base_url}/collections/{name}",
timeout=self.timeout
)
response.raise_for_status()
return response.json()
def upsert(
self,
collection: str,
points: List[Dict[str, Any]]
) -> Dict[str, Any]:
"""Insert or update points (vectors with optional payload).
Args:
collection: Target collection name.
points: List of point dicts with 'id', 'vector', and optional 'payload'.
Returns:
Dict with 'count' of upserted points.
"""
response = self.session.post(
f"{self.base_url}/collections/{collection}/points",
json={"points": points},
timeout=self.timeout
)
response.raise_for_status()
return response.json()
def search(
self,
collection: str,
vector: List[float],
top_k: int = 10
) -> List[Dict[str, Any]]:
"""Search for similar vectors.
Args:
collection: Collection to search in.
vector: Query vector (must match collection dimension).
top_k: Number of results to return (default: 10).
Returns:
List of results with 'id', 'score', and 'payload'.
"""
response = self.session.post(
f"{self.base_url}/collections/{collection}/search",
json={"vector": vector, "top_k": top_k},
timeout=self.timeout
)
response.raise_for_status()
return response.json()["results"]
def main():
"""Example usage of VelesDB."""
client = VelesDBClient()
# Check health
logger.info("Checking server health...")
try:
health = client.health()
logger.info(f"Server status: {health['status']}, version: {health['version']}")
except requests.ConnectionError:
logger.error(f"Cannot connect to VelesDB at {VELESDB_URL}")
logger.error("Make sure VelesDB server is running: cargo run --release")
return
# Create a collection
logger.info("Creating collection 'documents'...")
try:
client.create_collection("documents", dimension=4, metric="cosine")
logger.info("Collection created!")
except requests.HTTPError as e:
if e.response.status_code == 400:
logger.info("Collection already exists, continuing...")
else:
raise
# Insert some vectors
logger.info("Inserting vectors...")
points = [
{
"id": 1,
"vector": [1.0, 0.0, 0.0, 0.0],
"payload": {"title": "Document A", "category": "tech"}
},
{
"id": 2,
"vector": [0.0, 1.0, 0.0, 0.0],
"payload": {"title": "Document B", "category": "science"}
},
{
"id": 3,
"vector": [0.0, 0.0, 1.0, 0.0],
"payload": {"title": "Document C", "category": "tech"}
},
{
"id": 4,
"vector": [0.9, 0.1, 0.0, 0.0],
"payload": {"title": "Document D", "category": "tech"}
},
]
result = client.upsert("documents", points)
logger.info(f"Inserted {result['count']} points")
# Search for similar vectors
logger.info("Searching for vectors similar to [1.0, 0.0, 0.0, 0.0]...")
query = [1.0, 0.0, 0.0, 0.0]
results = client.search("documents", query, top_k=3)
logger.info("Search results:")
for i, result in enumerate(results, 1):
logger.info(f" {i}. ID: {result['id']}, Score: {result['score']:.4f}")
if result.get('payload'):
logger.info(f" Title: {result['payload'].get('title')}")
# List collections
logger.info(f"Collections: {client.list_collections()}")
if __name__ == "__main__":
main()