massengine 1.2.0__tar.gz → 1.2.1__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.
- {massengine-1.2.0 → massengine-1.2.1}/PKG-INFO +4 -3
- {massengine-1.2.0 → massengine-1.2.1}/README.md +3 -2
- {massengine-1.2.0 → massengine-1.2.1}/massengine/client.py +70 -11
- {massengine-1.2.0 → massengine-1.2.1}/pyproject.toml +1 -1
- {massengine-1.2.0 → massengine-1.2.1}/tests/test_client.py +137 -4
- {massengine-1.2.0 → massengine-1.2.1}/.gitignore +0 -0
- {massengine-1.2.0 → massengine-1.2.1}/LICENSE +0 -0
- {massengine-1.2.0 → massengine-1.2.1}/massengine/__init__.py +0 -0
- {massengine-1.2.0 → massengine-1.2.1}/massengine/py.typed +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: massengine
|
|
3
|
-
Version: 1.2.
|
|
3
|
+
Version: 1.2.1
|
|
4
4
|
Summary: Official Python SDK for the MassEngine filtered vector and hybrid search database.
|
|
5
5
|
Project-URL: Homepage, https://massengine.com
|
|
6
6
|
Project-URL: Documentation, https://massengine.com/docs
|
|
@@ -56,7 +56,7 @@ client.ingest(
|
|
|
56
56
|
],
|
|
57
57
|
)
|
|
58
58
|
|
|
59
|
-
# 2. Dense vector search with rich metadata filter operators
|
|
59
|
+
# 2. Dense vector search with rich metadata filter operators ($eq/$ne/$in/$nin/$gt/$gte/$lt/$lte/$contains + $and/$or/$nor)
|
|
60
60
|
res = client.search(
|
|
61
61
|
"acme-prod",
|
|
62
62
|
vector=[0.12, -0.44, 0.88, 0.05],
|
|
@@ -66,6 +66,7 @@ res = client.search(
|
|
|
66
66
|
"department": {"$in": ["security", "legal"]},
|
|
67
67
|
"clearance": {"$gte": 2},
|
|
68
68
|
"tags": {"$contains": "soc2"},
|
|
69
|
+
"$nor": [{"status": "archived"}],
|
|
69
70
|
},
|
|
70
71
|
)
|
|
71
72
|
print(res["hits"])
|
|
@@ -97,7 +98,7 @@ print(usage)
|
|
|
97
98
|
Create isolated databases within a tenant and scope operations using `database=` (pass `tenant=""` when authenticating with a tenant-scoped `mk_...` key):
|
|
98
99
|
|
|
99
100
|
```python
|
|
100
|
-
client.create_database("", "analytics", data_type="int8")
|
|
101
|
+
client.create_database("", "analytics", data_type="int8", ttl_days=30, time_bucket_days=1, append_only=True)
|
|
101
102
|
|
|
102
103
|
client.ingest(
|
|
103
104
|
"",
|
|
@@ -32,7 +32,7 @@ client.ingest(
|
|
|
32
32
|
],
|
|
33
33
|
)
|
|
34
34
|
|
|
35
|
-
# 2. Dense vector search with rich metadata filter operators
|
|
35
|
+
# 2. Dense vector search with rich metadata filter operators ($eq/$ne/$in/$nin/$gt/$gte/$lt/$lte/$contains + $and/$or/$nor)
|
|
36
36
|
res = client.search(
|
|
37
37
|
"acme-prod",
|
|
38
38
|
vector=[0.12, -0.44, 0.88, 0.05],
|
|
@@ -42,6 +42,7 @@ res = client.search(
|
|
|
42
42
|
"department": {"$in": ["security", "legal"]},
|
|
43
43
|
"clearance": {"$gte": 2},
|
|
44
44
|
"tags": {"$contains": "soc2"},
|
|
45
|
+
"$nor": [{"status": "archived"}],
|
|
45
46
|
},
|
|
46
47
|
)
|
|
47
48
|
print(res["hits"])
|
|
@@ -73,7 +74,7 @@ print(usage)
|
|
|
73
74
|
Create isolated databases within a tenant and scope operations using `database=` (pass `tenant=""` when authenticating with a tenant-scoped `mk_...` key):
|
|
74
75
|
|
|
75
76
|
```python
|
|
76
|
-
client.create_database("", "analytics", data_type="int8")
|
|
77
|
+
client.create_database("", "analytics", data_type="int8", ttl_days=30, time_bucket_days=1, append_only=True)
|
|
77
78
|
|
|
78
79
|
client.ingest(
|
|
79
80
|
"",
|
|
@@ -11,7 +11,7 @@ import urllib.error
|
|
|
11
11
|
import urllib.parse
|
|
12
12
|
import urllib.request
|
|
13
13
|
|
|
14
|
-
SDK_VERSION = "1.2.
|
|
14
|
+
SDK_VERSION = "1.2.1"
|
|
15
15
|
USER_AGENT = f"massengine-python/{SDK_VERSION}"
|
|
16
16
|
DEFAULT_MAX_RESPONSE_BYTES = 64 * 1024 * 1024 # 64 MiB
|
|
17
17
|
MAX_ERROR_RESPONSE_BYTES = 65536 # 64 KiB
|
|
@@ -132,6 +132,12 @@ def _validate_hybrid_alpha(alpha: Any) -> float:
|
|
|
132
132
|
return val
|
|
133
133
|
|
|
134
134
|
|
|
135
|
+
def _validate_min_score(val: Any) -> float:
|
|
136
|
+
if isinstance(val, bool) or not isinstance(val, (int, float)) or not math.isfinite(float(val)):
|
|
137
|
+
raise ValueError("min_score must be a finite number")
|
|
138
|
+
return float(val)
|
|
139
|
+
|
|
140
|
+
|
|
135
141
|
def _extract_error_message(raw_text: str) -> str:
|
|
136
142
|
msg = raw_text.strip()
|
|
137
143
|
try:
|
|
@@ -143,27 +149,47 @@ def _extract_error_message(raw_text: str) -> str:
|
|
|
143
149
|
return msg
|
|
144
150
|
|
|
145
151
|
|
|
152
|
+
def _validate_doc_id_int(val: Any) -> int:
|
|
153
|
+
if isinstance(val, bool) or not isinstance(val, int) or val < 0 or val > 0xFFFFFFFFFFFFFFFF:
|
|
154
|
+
raise ValueError("doc_id must be a uint64 integer")
|
|
155
|
+
return val
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def _validate_effort(val: Any) -> int:
|
|
159
|
+
if isinstance(val, bool) or not isinstance(val, int) or val < 1 or val > 100:
|
|
160
|
+
raise ValueError("effort must be an integer between 1 and 100")
|
|
161
|
+
return val
|
|
162
|
+
|
|
163
|
+
|
|
146
164
|
@dataclass
|
|
147
165
|
class Doc:
|
|
148
166
|
"""Represents a document for ingestion or export."""
|
|
149
167
|
|
|
150
|
-
id: str
|
|
151
|
-
vector: Sequence[float]
|
|
168
|
+
id: str = ""
|
|
169
|
+
vector: Sequence[float] = ()
|
|
170
|
+
doc_id: Optional[int] = None
|
|
152
171
|
date: Optional[int] = None
|
|
153
172
|
category: Optional[int] = None
|
|
173
|
+
no_ttl: bool = False
|
|
154
174
|
metadata: Optional[Dict[str, Any]] = None
|
|
155
175
|
database: Optional[str] = None
|
|
156
176
|
sparse_indices: Optional[Sequence[int]] = None
|
|
157
177
|
sparse_values: Optional[Sequence[float]] = None
|
|
158
178
|
|
|
159
179
|
def to_dict(self) -> Dict[str, Any]:
|
|
160
|
-
|
|
180
|
+
if self.id == "" and self.doc_id is None:
|
|
181
|
+
raise ValueError("document requires a non-empty id or integer doc_id")
|
|
182
|
+
if self.id != "":
|
|
183
|
+
_escape_doc_id(self.id)
|
|
161
184
|
vec = _validate_vector(self.vector)
|
|
162
185
|
sparse = _validate_sparse(self.sparse_indices, self.sparse_values)
|
|
163
186
|
out: Dict[str, Any] = {
|
|
164
|
-
"id": self.id,
|
|
165
187
|
"vector": vec,
|
|
166
188
|
}
|
|
189
|
+
if self.id != "":
|
|
190
|
+
out["id"] = self.id
|
|
191
|
+
if self.doc_id is not None:
|
|
192
|
+
out["doc_id"] = _validate_doc_id_int(self.doc_id)
|
|
167
193
|
if sparse is not None:
|
|
168
194
|
out["sparse_indices"] = sparse[0]
|
|
169
195
|
out["sparse_values"] = sparse[1]
|
|
@@ -174,6 +200,8 @@ class Doc:
|
|
|
174
200
|
out["date"] = int(self.date)
|
|
175
201
|
if self.category is not None:
|
|
176
202
|
out["category"] = int(self.category)
|
|
203
|
+
if self.no_ttl:
|
|
204
|
+
out["no_ttl"] = True
|
|
177
205
|
if self.metadata is not None:
|
|
178
206
|
out["metadata"] = self.metadata
|
|
179
207
|
return out
|
|
@@ -194,6 +222,7 @@ class SearchRequest:
|
|
|
194
222
|
metadata_filter: Optional[Dict[str, Any]] = None
|
|
195
223
|
offset: int = 0
|
|
196
224
|
min_score: Optional[float] = None
|
|
225
|
+
effort: Optional[int] = None
|
|
197
226
|
rerank_pool: Optional[int] = None
|
|
198
227
|
nprobe: Optional[int] = None
|
|
199
228
|
skip_meta: bool = False
|
|
@@ -238,7 +267,9 @@ class SearchRequest:
|
|
|
238
267
|
if self.offset:
|
|
239
268
|
out["offset"] = int(self.offset)
|
|
240
269
|
if self.min_score is not None:
|
|
241
|
-
out["min_score"] =
|
|
270
|
+
out["min_score"] = _validate_min_score(self.min_score)
|
|
271
|
+
if self.effort is not None:
|
|
272
|
+
out["effort"] = _validate_effort(self.effort)
|
|
242
273
|
if self.rerank_pool:
|
|
243
274
|
out["rerank_pool"] = int(self.rerank_pool)
|
|
244
275
|
if self.nprobe:
|
|
@@ -287,14 +318,14 @@ class MassEngineClient:
|
|
|
287
318
|
|
|
288
319
|
def __init__(
|
|
289
320
|
self,
|
|
290
|
-
base_url: str = "http://localhost:
|
|
321
|
+
base_url: str = "http://localhost:8080",
|
|
291
322
|
api_key: Optional[str] = None,
|
|
292
323
|
timeout: float = 30.0,
|
|
293
324
|
max_response_bytes: int = DEFAULT_MAX_RESPONSE_BYTES,
|
|
294
325
|
) -> None:
|
|
295
326
|
self.base_url = self._validate_base_url(base_url)
|
|
296
327
|
self._api_key = self._validate_api_key(api_key)
|
|
297
|
-
if isinstance(timeout, bool) or not isinstance(timeout, (int, float)) or timeout <= 0:
|
|
328
|
+
if isinstance(timeout, bool) or not isinstance(timeout, (int, float)) or not math.isfinite(float(timeout)) or timeout <= 0:
|
|
298
329
|
raise ValueError("timeout must be a positive number")
|
|
299
330
|
if isinstance(max_response_bytes, bool) or not isinstance(max_response_bytes, int) or max_response_bytes <= 0:
|
|
300
331
|
raise ValueError("max_response_bytes must be a positive integer")
|
|
@@ -380,7 +411,7 @@ class MassEngineClient:
|
|
|
380
411
|
data: Optional[bytes] = None
|
|
381
412
|
content_type: Optional[str] = None
|
|
382
413
|
if body is not None:
|
|
383
|
-
data = json.dumps(body).encode("utf-8")
|
|
414
|
+
data = json.dumps(body, allow_nan=False).encode("utf-8")
|
|
384
415
|
content_type = "application/json"
|
|
385
416
|
|
|
386
417
|
req = urllib.request.Request(
|
|
@@ -447,7 +478,14 @@ class MassEngineClient:
|
|
|
447
478
|
return d.to_dict()
|
|
448
479
|
if not isinstance(d, dict):
|
|
449
480
|
raise ValueError("document must be a Doc or dict")
|
|
450
|
-
|
|
481
|
+
raw_id = d.get("id")
|
|
482
|
+
raw_doc_id = d.get("doc_id")
|
|
483
|
+
if (raw_id is None or raw_id == "") and raw_doc_id is None:
|
|
484
|
+
raise ValueError("document requires a non-empty id or integer doc_id")
|
|
485
|
+
if raw_id is not None and raw_id != "":
|
|
486
|
+
cls._esc_doc_id(raw_id) # type: ignore[arg-type]
|
|
487
|
+
if raw_doc_id is not None:
|
|
488
|
+
_validate_doc_id_int(raw_doc_id)
|
|
451
489
|
if "vector" in d:
|
|
452
490
|
_validate_vector(d.get("vector"))
|
|
453
491
|
if "sparse_indices" in d or "sparse_values" in d:
|
|
@@ -487,6 +525,7 @@ class MassEngineClient:
|
|
|
487
525
|
metadata_filter: Optional[Dict[str, Any]] = None,
|
|
488
526
|
offset: int = 0,
|
|
489
527
|
min_score: Optional[float] = None,
|
|
528
|
+
effort: Optional[int] = None,
|
|
490
529
|
rerank_pool: Optional[int] = None,
|
|
491
530
|
nprobe: Optional[int] = None,
|
|
492
531
|
skip_meta: bool = False,
|
|
@@ -510,6 +549,7 @@ class MassEngineClient:
|
|
|
510
549
|
metadata_filter=metadata_filter,
|
|
511
550
|
offset=offset,
|
|
512
551
|
min_score=min_score,
|
|
552
|
+
effort=effort,
|
|
513
553
|
rerank_pool=rerank_pool,
|
|
514
554
|
nprobe=nprobe,
|
|
515
555
|
skip_meta=skip_meta,
|
|
@@ -559,6 +599,10 @@ class MassEngineClient:
|
|
|
559
599
|
q_copy["sparse_values"] = sparse[1]
|
|
560
600
|
if q_copy.get("hybrid_alpha") is not None:
|
|
561
601
|
q_copy["hybrid_alpha"] = _validate_hybrid_alpha(q_copy["hybrid_alpha"])
|
|
602
|
+
if q_copy.get("min_score") is not None:
|
|
603
|
+
q_copy["min_score"] = _validate_min_score(q_copy["min_score"])
|
|
604
|
+
if q_copy.get("effort") is not None:
|
|
605
|
+
q_copy["effort"] = _validate_effort(q_copy["effort"])
|
|
562
606
|
if q_copy.get("tenant"):
|
|
563
607
|
self._esc_tenant(q_copy["tenant"])
|
|
564
608
|
if q_copy.get("database"):
|
|
@@ -639,12 +683,18 @@ class MassEngineClient:
|
|
|
639
683
|
docs: Iterable[Union[Doc, Dict[str, Any]]],
|
|
640
684
|
*,
|
|
641
685
|
database: str = "",
|
|
686
|
+
batch_size: int = 0,
|
|
642
687
|
) -> int:
|
|
643
688
|
"""Streams a collection of documents into `tenant` via NDJSON."""
|
|
644
689
|
esc_tenant = self._validate_optional_tenant(tenant)
|
|
690
|
+
if isinstance(batch_size, bool) or not isinstance(batch_size, int) or batch_size < 0 or batch_size > 4096:
|
|
691
|
+
raise ValueError("batch_size must be between 1 and 4096")
|
|
645
692
|
base_path = "/v1/import" if esc_tenant == "" else f"/v1/tenants/{esc_tenant}/import"
|
|
646
693
|
path = self._append_db_query(base_path, database)
|
|
647
|
-
|
|
694
|
+
if batch_size > 0:
|
|
695
|
+
sep = "&" if "?" in path else "?"
|
|
696
|
+
path = f"{path}{sep}batch_size={batch_size}"
|
|
697
|
+
lines = [json.dumps(self._serialize_doc(d), allow_nan=False) for d in docs]
|
|
648
698
|
payload = ("\n".join(lines) + "\n").encode("utf-8")
|
|
649
699
|
req = urllib.request.Request(
|
|
650
700
|
url=f"{self.base_url}{path}",
|
|
@@ -727,6 +777,9 @@ class MassEngineClient:
|
|
|
727
777
|
rf: int = 0,
|
|
728
778
|
data_type: str = "",
|
|
729
779
|
mode: str = "",
|
|
780
|
+
ttl_days: int = 0,
|
|
781
|
+
time_bucket_days: int = 0,
|
|
782
|
+
append_only: bool = False,
|
|
730
783
|
) -> Dict[str, Any]:
|
|
731
784
|
"""Creates or updates a named database under `tenant` (or the bound `mk_...` key's tenant)."""
|
|
732
785
|
esc_tenant = self._validate_optional_tenant(tenant)
|
|
@@ -741,6 +794,12 @@ class MassEngineClient:
|
|
|
741
794
|
body["data_type"] = data_type
|
|
742
795
|
if mode:
|
|
743
796
|
body["mode"] = mode
|
|
797
|
+
if ttl_days > 0:
|
|
798
|
+
body["ttl_days"] = int(ttl_days)
|
|
799
|
+
if time_bucket_days > 0:
|
|
800
|
+
body["time_bucket_days"] = int(time_bucket_days)
|
|
801
|
+
if append_only:
|
|
802
|
+
body["append_only"] = True
|
|
744
803
|
resp = self._request_json("POST", path, body)
|
|
745
804
|
if isinstance(resp, dict) and isinstance(resp.get("database"), dict):
|
|
746
805
|
db_obj = dict(resp["database"])
|
|
@@ -61,6 +61,26 @@ class _MockGatewayHandler(BaseHTTPRequestHandler):
|
|
|
61
61
|
self.end_headers()
|
|
62
62
|
elif p == "/v1/tenants/oversized":
|
|
63
63
|
self._send(200, {"tenant": "oversized", "pad": "x" * 2048})
|
|
64
|
+
elif p == "/v1/usage":
|
|
65
|
+
self._send(
|
|
66
|
+
200,
|
|
67
|
+
{
|
|
68
|
+
"tenants": [{"tenant": "acme", "plan": "pro", "live_docs": 2}],
|
|
69
|
+
"total_live_docs": 2,
|
|
70
|
+
"total_live_bytes": 4096,
|
|
71
|
+
"total_read_units": 4,
|
|
72
|
+
"total_write_units": 3,
|
|
73
|
+
},
|
|
74
|
+
)
|
|
75
|
+
elif p == "/v1/tenants":
|
|
76
|
+
self._send(200, {"tenants": [{"tenant": "acme", "doc_count": 2, "byte_count": 4096}]})
|
|
77
|
+
elif p.endswith("/quota"):
|
|
78
|
+
self._send(200, {"tenant": "acme", "quota": {"plan": "pro", "max_docs": 100, "max_bytes": 4096}})
|
|
79
|
+
elif p == "/v1/tenants/acme":
|
|
80
|
+
resp_obj = {"tenant": "acme", "doc_count": 2, "byte_count": 4096}
|
|
81
|
+
if qs.get("database"):
|
|
82
|
+
resp_obj["database"] = qs["database"][0]
|
|
83
|
+
self._send(200, resp_obj)
|
|
64
84
|
elif p.endswith("/usage"):
|
|
65
85
|
self._send(200, {"tenant": "acme", "plan": "pro", "live_docs": 2, "read_units": 4, "write_units": 3})
|
|
66
86
|
elif p == "/v1/export" or p.endswith("/export"):
|
|
@@ -142,8 +162,12 @@ class _MockGatewayHandler(BaseHTTPRequestHandler):
|
|
|
142
162
|
self.end_headers()
|
|
143
163
|
return
|
|
144
164
|
if p == "/v1/flush":
|
|
145
|
-
|
|
165
|
+
if _MockGatewayHandler.recorded_bodies and len(_MockGatewayHandler.recorded_bodies) > 0:
|
|
166
|
+
pass
|
|
167
|
+
time.sleep(0.08)
|
|
146
168
|
self._send(200, {"flushed": True})
|
|
169
|
+
elif p == "/v1/compact":
|
|
170
|
+
self._send(200, {"compacted": True})
|
|
147
171
|
elif p.endswith("/keys"):
|
|
148
172
|
self._send(200, {"tenant": "acme", "key": "mk_test_key_123"})
|
|
149
173
|
elif p == "/v1/databases" or p.endswith("/databases"):
|
|
@@ -254,10 +278,10 @@ class TestMassEngineClient(unittest.TestCase):
|
|
|
254
278
|
_MockGatewayHandler.get_db_405 = False
|
|
255
279
|
|
|
256
280
|
def test_sdk_lifecycle_and_user_agent(self) -> None:
|
|
257
|
-
self.assertEqual(massengine.__version__, "1.2.
|
|
281
|
+
self.assertEqual(massengine.__version__, "1.2.1")
|
|
258
282
|
admin = MassEngineClient(base_url=self.base_url, api_key=" admin-key ")
|
|
259
283
|
self.assertEqual(admin.health()["status"], "ok")
|
|
260
|
-
self.assertEqual(_MockGatewayHandler.last_user_agent, "massengine-python/1.2.
|
|
284
|
+
self.assertEqual(_MockGatewayHandler.last_user_agent, "massengine-python/1.2.1")
|
|
261
285
|
r_ok = admin.ready()
|
|
262
286
|
self.assertTrue(r_ok["ready"])
|
|
263
287
|
self.assertEqual(r_ok["unavailable_shards"], [])
|
|
@@ -523,12 +547,19 @@ class TestMassEngineClient(unittest.TestCase):
|
|
|
523
547
|
self.assertEqual(dbs[0]["database"], "analytics")
|
|
524
548
|
|
|
525
549
|
_MockGatewayHandler.get_db_405 = False
|
|
550
|
+
before_direct = len(_MockGatewayHandler.recorded_paths)
|
|
526
551
|
got_db = client.get_database(tenant, "analytics")
|
|
527
552
|
self.assertEqual(got_db["database"], "analytics")
|
|
553
|
+
self.assertEqual(len(_MockGatewayHandler.recorded_paths) - before_direct, 1)
|
|
528
554
|
|
|
529
555
|
_MockGatewayHandler.get_db_405 = True
|
|
556
|
+
before_fb = len(_MockGatewayHandler.recorded_paths)
|
|
530
557
|
got_fb = client.get_database(tenant, "analytics")
|
|
531
558
|
self.assertEqual(got_fb["database"], "analytics")
|
|
559
|
+
self.assertEqual(len(_MockGatewayHandler.recorded_paths) - before_fb, 2)
|
|
560
|
+
with self.assertRaises(MassEngineError) as miss_ctx:
|
|
561
|
+
client.get_database(tenant, "nonexistent")
|
|
562
|
+
self.assertEqual(miss_ctx.exception.status_code, 404)
|
|
532
563
|
|
|
533
564
|
deleted = client.delete_database(tenant, "analytics")
|
|
534
565
|
self.assertEqual(deleted, 2)
|
|
@@ -608,10 +639,112 @@ class TestMassEngineClient(unittest.TestCase):
|
|
|
608
639
|
tiny_export_client.export_tenant("acme")
|
|
609
640
|
self.assertIn("max_response_bytes", str(ctx2.exception))
|
|
610
641
|
|
|
611
|
-
fast_client = MassEngineClient(base_url=self.base_url, timeout=0.
|
|
642
|
+
fast_client = MassEngineClient(base_url=self.base_url, timeout=0.02)
|
|
612
643
|
with self.assertRaises((TimeoutError, socket.timeout, OSError)):
|
|
613
644
|
fast_client.flush()
|
|
614
645
|
|
|
646
|
+
def test_min_score_nan_timeout_and_strict_json_validation(self) -> None:
|
|
647
|
+
client = MassEngineClient(base_url=self.base_url, api_key="mk_valid")
|
|
648
|
+
_MockGatewayHandler.recorded_paths.clear()
|
|
649
|
+
_MockGatewayHandler.recorded_bodies.clear()
|
|
650
|
+
|
|
651
|
+
# Finite min_score (including 0.0 and negative) succeeds
|
|
652
|
+
self.assertEqual(SearchRequest(vector=[1.0, 0.0], min_score=0.0).to_dict()["min_score"], 0.0)
|
|
653
|
+
client.search("acme", vector=[1.0, 0.0], min_score=-0.25)
|
|
654
|
+
self.assertEqual(json.loads(_MockGatewayHandler.recorded_bodies[-1])["min_score"], -0.25)
|
|
655
|
+
client.batch_search([{"tenant": "acme", "vector": [1.0, 0.0], "min_score": 0.0}])
|
|
656
|
+
|
|
657
|
+
before = len(_MockGatewayHandler.recorded_paths)
|
|
658
|
+
for bad_ms in (float("nan"), float("inf"), float("-inf"), True, "0.5"):
|
|
659
|
+
with self.assertRaises(ValueError):
|
|
660
|
+
SearchRequest(vector=[1.0, 0.0], min_score=bad_ms).to_dict() # type: ignore[arg-type]
|
|
661
|
+
with self.assertRaises(ValueError):
|
|
662
|
+
client.search("acme", vector=[1.0, 0.0], min_score=bad_ms) # type: ignore[arg-type]
|
|
663
|
+
with self.assertRaises(ValueError):
|
|
664
|
+
client.batch_search([{"tenant": "acme", "vector": [1.0, 0.0], "min_score": bad_ms}])
|
|
665
|
+
|
|
666
|
+
for bad_to in (float("nan"), float("inf"), float("-inf"), -1.0, True):
|
|
667
|
+
with self.assertRaises(ValueError):
|
|
668
|
+
MassEngineClient(base_url=self.base_url, timeout=bad_to) # type: ignore[arg-type]
|
|
669
|
+
|
|
670
|
+
# Non-finite float in metadata must be rejected instead of producing invalid NaN JSON
|
|
671
|
+
with self.assertRaises(ValueError):
|
|
672
|
+
client.ingest("acme", [Doc(id="nan-meta", vector=[1.0, 0.0], metadata={"bad": float("nan")})])
|
|
673
|
+
with self.assertRaises(ValueError):
|
|
674
|
+
client.import_tenant("acme", [Doc(id="inf-meta", vector=[1.0, 0.0], metadata={"bad": float("inf")})])
|
|
675
|
+
self.assertEqual(len(_MockGatewayHandler.recorded_paths), before)
|
|
676
|
+
|
|
677
|
+
def test_admin_and_tenant_management_endpoints(self) -> None:
|
|
678
|
+
admin = MassEngineClient(base_url=self.base_url, api_key="meadm_test")
|
|
679
|
+
usage = admin.list_usage()
|
|
680
|
+
self.assertEqual(usage["total_live_docs"], 2)
|
|
681
|
+
|
|
682
|
+
tenants = admin.list_tenants()
|
|
683
|
+
self.assertEqual(len(tenants), 1)
|
|
684
|
+
self.assertEqual(tenants[0]["tenant"], "acme")
|
|
685
|
+
|
|
686
|
+
t_info = admin.get_tenant("acme", database="analytics")
|
|
687
|
+
self.assertEqual(t_info["database"], "analytics")
|
|
688
|
+
|
|
689
|
+
quota = admin.get_tenant_quota("acme")
|
|
690
|
+
self.assertEqual(quota["plan"], "pro")
|
|
691
|
+
|
|
692
|
+
rev = admin.revoke_tenant_key("acme", "mk_old")
|
|
693
|
+
self.assertTrue(rev["revoked"])
|
|
694
|
+
|
|
695
|
+
self.assertTrue(admin.flush()["flushed"])
|
|
696
|
+
self.assertTrue(admin.compact()["compacted"])
|
|
697
|
+
self.assertEqual(admin.delete_tenant("acme"), 2)
|
|
698
|
+
|
|
699
|
+
def test_api_parity_v121(self) -> None:
|
|
700
|
+
default_client = MassEngineClient()
|
|
701
|
+
self.assertEqual(default_client.base_url, "http://localhost:8080")
|
|
702
|
+
self.assertEqual(massengine.__version__, "1.2.1")
|
|
703
|
+
|
|
704
|
+
# Database creation with ttl_days, time_bucket_days, append_only
|
|
705
|
+
client = MassEngineClient(base_url=self.base_url, api_key="meadm_test")
|
|
706
|
+
_MockGatewayHandler.recorded_bodies.clear()
|
|
707
|
+
_MockGatewayHandler.recorded_paths.clear()
|
|
708
|
+
client.create_database(
|
|
709
|
+
"acme",
|
|
710
|
+
"events",
|
|
711
|
+
data_type="int8",
|
|
712
|
+
ttl_days=30,
|
|
713
|
+
time_bucket_days=1,
|
|
714
|
+
append_only=True,
|
|
715
|
+
)
|
|
716
|
+
db_body = json.loads(_MockGatewayHandler.recorded_bodies[-1])
|
|
717
|
+
self.assertEqual(db_body["ttl_days"], 30)
|
|
718
|
+
self.assertEqual(db_body["time_bucket_days"], 1)
|
|
719
|
+
self.assertTrue(db_body["append_only"])
|
|
720
|
+
|
|
721
|
+
# Doc with doc_id (without string id) and no_ttl + batch_size on import_tenant
|
|
722
|
+
imported = client.import_tenant(
|
|
723
|
+
"acme",
|
|
724
|
+
[
|
|
725
|
+
Doc(doc_id=42, vector=[1.0, 0.0], no_ttl=True),
|
|
726
|
+
{"id": "str-doc", "vector": [0.0, 1.0], "no_ttl": True},
|
|
727
|
+
],
|
|
728
|
+
database="events",
|
|
729
|
+
batch_size=500,
|
|
730
|
+
)
|
|
731
|
+
self.assertEqual(imported, 2)
|
|
732
|
+
self.assertIn("database=events", _MockGatewayHandler.recorded_paths[-1])
|
|
733
|
+
self.assertIn("batch_size=500", _MockGatewayHandler.recorded_paths[-1])
|
|
734
|
+
|
|
735
|
+
with self.assertRaises(ValueError):
|
|
736
|
+
Doc(vector=[1.0, 0.0]).to_dict()
|
|
737
|
+
with self.assertRaises(ValueError):
|
|
738
|
+
client.import_tenant("acme", [Doc(id="x", vector=[1.0, 0.0])], batch_size=5000)
|
|
739
|
+
|
|
740
|
+
# Effort validation in SearchRequest / search / batch_search
|
|
741
|
+
client.search("acme", vector=[1.0, 0.0], k=5, effort=50)
|
|
742
|
+
self.assertEqual(json.loads(_MockGatewayHandler.recorded_bodies[-1])["effort"], 50)
|
|
743
|
+
with self.assertRaises(ValueError):
|
|
744
|
+
client.search("acme", vector=[1.0, 0.0], k=5, effort=101)
|
|
745
|
+
with self.assertRaises(ValueError):
|
|
746
|
+
client.batch_search([{"tenant": "acme", "vector": [1.0, 0.0], "effort": 0}])
|
|
747
|
+
|
|
615
748
|
|
|
616
749
|
if __name__ == "__main__":
|
|
617
750
|
unittest.main()
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|