massengine 1.1.4__tar.gz → 1.2.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.
- {massengine-1.1.4 → massengine-1.2.0}/PKG-INFO +16 -6
- {massengine-1.1.4 → massengine-1.2.0}/README.md +13 -3
- {massengine-1.1.4 → massengine-1.2.0}/massengine/client.py +145 -17
- {massengine-1.1.4 → massengine-1.2.0}/pyproject.toml +3 -3
- {massengine-1.1.4 → massengine-1.2.0}/tests/test_client.py +80 -11
- {massengine-1.1.4 → massengine-1.2.0}/.gitignore +0 -0
- {massengine-1.1.4 → massengine-1.2.0}/LICENSE +0 -0
- {massengine-1.1.4 → massengine-1.2.0}/massengine/__init__.py +0 -0
- {massengine-1.1.4 → massengine-1.2.0}/massengine/py.typed +0 -0
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: massengine
|
|
3
|
-
Version: 1.
|
|
4
|
-
Summary: Official Python SDK for the MassEngine filtered vector search database.
|
|
3
|
+
Version: 1.2.0
|
|
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
|
|
7
7
|
Project-URL: Repository, https://github.com/massengine-dev/massengine-python
|
|
8
8
|
Author-email: MassEngine <dev@massengine.com>
|
|
9
9
|
License-Expression: Apache-2.0
|
|
10
10
|
License-File: LICENSE
|
|
11
|
-
Keywords: embeddings,massengine,rag,vector-database,vector-search
|
|
11
|
+
Keywords: bm25,embeddings,hybrid-search,massengine,rag,vector-database,vector-search
|
|
12
12
|
Classifier: Development Status :: 5 - Production/Stable
|
|
13
13
|
Classifier: Intended Audience :: Developers
|
|
14
14
|
Classifier: Programming Language :: Python :: 3
|
|
@@ -24,7 +24,7 @@ Description-Content-Type: text/markdown
|
|
|
24
24
|
|
|
25
25
|
# MassEngine Python SDK (`massengine`)
|
|
26
26
|
|
|
27
|
-
Official zero-dependency Python client for [MassEngine](https://massengine.com) — the sub-5ms filtered vector search database.
|
|
27
|
+
Official zero-dependency Python client for [MassEngine](https://massengine.com) — the sub-5ms filtered vector and hybrid search database.
|
|
28
28
|
|
|
29
29
|
## Installation
|
|
30
30
|
|
|
@@ -70,14 +70,24 @@ res = client.search(
|
|
|
70
70
|
)
|
|
71
71
|
print(res["hits"])
|
|
72
72
|
|
|
73
|
-
# 3.
|
|
73
|
+
# 3. Hybrid search (dense + BM25 lexical + sparse vectors with weighted RRF)
|
|
74
|
+
hybrid = client.search(
|
|
75
|
+
"acme-prod",
|
|
76
|
+
vector=[0.12, -0.44, 0.88, 0.05],
|
|
77
|
+
query_text="soc2 security policy",
|
|
78
|
+
hybrid_alpha=0.6,
|
|
79
|
+
k=10,
|
|
80
|
+
)
|
|
81
|
+
print(hybrid["hits"])
|
|
82
|
+
|
|
83
|
+
# 4. In-place metadata patching without re-embedding
|
|
74
84
|
client.patch_doc(
|
|
75
85
|
"acme-prod",
|
|
76
86
|
"docs/2026/policy-1",
|
|
77
87
|
metadata={"clearance": 4, "reviewed": True},
|
|
78
88
|
)
|
|
79
89
|
|
|
80
|
-
#
|
|
90
|
+
# 5. Inspect tenant quota & billable usage
|
|
81
91
|
usage = client.get_tenant_usage("acme-prod")
|
|
82
92
|
print(usage)
|
|
83
93
|
```
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# MassEngine Python SDK (`massengine`)
|
|
2
2
|
|
|
3
|
-
Official zero-dependency Python client for [MassEngine](https://massengine.com) — the sub-5ms filtered vector search database.
|
|
3
|
+
Official zero-dependency Python client for [MassEngine](https://massengine.com) — the sub-5ms filtered vector and hybrid search database.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -46,14 +46,24 @@ res = client.search(
|
|
|
46
46
|
)
|
|
47
47
|
print(res["hits"])
|
|
48
48
|
|
|
49
|
-
# 3.
|
|
49
|
+
# 3. Hybrid search (dense + BM25 lexical + sparse vectors with weighted RRF)
|
|
50
|
+
hybrid = client.search(
|
|
51
|
+
"acme-prod",
|
|
52
|
+
vector=[0.12, -0.44, 0.88, 0.05],
|
|
53
|
+
query_text="soc2 security policy",
|
|
54
|
+
hybrid_alpha=0.6,
|
|
55
|
+
k=10,
|
|
56
|
+
)
|
|
57
|
+
print(hybrid["hits"])
|
|
58
|
+
|
|
59
|
+
# 4. In-place metadata patching without re-embedding
|
|
50
60
|
client.patch_doc(
|
|
51
61
|
"acme-prod",
|
|
52
62
|
"docs/2026/policy-1",
|
|
53
63
|
metadata={"clearance": 4, "reviewed": True},
|
|
54
64
|
)
|
|
55
65
|
|
|
56
|
-
#
|
|
66
|
+
# 5. Inspect tenant quota & billable usage
|
|
57
67
|
usage = client.get_tenant_usage("acme-prod")
|
|
58
68
|
print(usage)
|
|
59
69
|
```
|
|
@@ -4,17 +4,19 @@ from __future__ import annotations
|
|
|
4
4
|
|
|
5
5
|
from dataclasses import dataclass
|
|
6
6
|
import json
|
|
7
|
+
import math
|
|
7
8
|
import ssl
|
|
8
|
-
from typing import Any, Dict, Iterable, List, Optional, Sequence, Union
|
|
9
|
+
from typing import Any, Dict, Iterable, List, Optional, Sequence, Tuple, Union
|
|
9
10
|
import urllib.error
|
|
10
11
|
import urllib.parse
|
|
11
12
|
import urllib.request
|
|
12
13
|
|
|
13
|
-
SDK_VERSION = "1.
|
|
14
|
+
SDK_VERSION = "1.2.0"
|
|
14
15
|
USER_AGENT = f"massengine-python/{SDK_VERSION}"
|
|
15
16
|
DEFAULT_MAX_RESPONSE_BYTES = 64 * 1024 * 1024 # 64 MiB
|
|
16
17
|
MAX_ERROR_RESPONSE_BYTES = 65536 # 64 KiB
|
|
17
18
|
_MAX_LINE_READ_CHUNK = 4 * 1024 * 1024
|
|
19
|
+
_MAX_SPARSE_TERMS = 65536
|
|
18
20
|
|
|
19
21
|
|
|
20
22
|
class MassEngineError(Exception):
|
|
@@ -64,18 +66,72 @@ def _escape_doc_id(doc_id: str) -> str:
|
|
|
64
66
|
return "/".join(_validate_segment(part) for part in parts)
|
|
65
67
|
|
|
66
68
|
|
|
67
|
-
def
|
|
69
|
+
def _validate_finite_float_seq(vector: Any, label: str = "vector") -> List[float]:
|
|
68
70
|
if vector is None or isinstance(vector, (str, bytes, bytearray)):
|
|
69
|
-
raise ValueError("
|
|
71
|
+
raise ValueError(f"{label} must be a sequence of finite numbers")
|
|
70
72
|
try:
|
|
71
|
-
|
|
73
|
+
raw = list(vector)
|
|
72
74
|
except TypeError:
|
|
73
|
-
raise ValueError("
|
|
75
|
+
raise ValueError(f"{label} must be a sequence of finite numbers") from None
|
|
76
|
+
out: List[float] = []
|
|
77
|
+
for v in raw:
|
|
78
|
+
if isinstance(v, bool) or not isinstance(v, (int, float)) or not math.isfinite(float(v)):
|
|
79
|
+
raise ValueError(f"{label} must contain only finite numbers")
|
|
80
|
+
out.append(float(v))
|
|
81
|
+
return out
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _validate_vector(vector: Any) -> List[float]:
|
|
85
|
+
vals = _validate_finite_float_seq(vector, "vector")
|
|
74
86
|
if len(vals) == 0:
|
|
75
87
|
raise ValueError("vector must be a non-empty sequence of numbers")
|
|
76
88
|
return vals
|
|
77
89
|
|
|
78
90
|
|
|
91
|
+
def _validate_optional_vector(vector: Any) -> List[float]:
|
|
92
|
+
if vector is None:
|
|
93
|
+
return []
|
|
94
|
+
return _validate_finite_float_seq(vector, "vector")
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def _validate_sparse(
|
|
98
|
+
sparse_indices: Any,
|
|
99
|
+
sparse_values: Any,
|
|
100
|
+
) -> Optional[Tuple[List[int], List[float]]]:
|
|
101
|
+
if sparse_indices is None and sparse_values is None:
|
|
102
|
+
return None
|
|
103
|
+
if sparse_indices is None or sparse_values is None:
|
|
104
|
+
raise ValueError("sparse_indices and sparse_values must both be provided with equal length")
|
|
105
|
+
if isinstance(sparse_indices, (str, bytes, bytearray)):
|
|
106
|
+
raise ValueError("sparse_indices must be a sequence of uint32 integers")
|
|
107
|
+
try:
|
|
108
|
+
raw_idx = list(sparse_indices)
|
|
109
|
+
except TypeError:
|
|
110
|
+
raise ValueError("sparse_indices must be a sequence of uint32 integers") from None
|
|
111
|
+
vals = _validate_finite_float_seq(sparse_values, "sparse_values")
|
|
112
|
+
if len(raw_idx) != len(vals):
|
|
113
|
+
raise ValueError("sparse_indices and sparse_values must have equal length")
|
|
114
|
+
if len(raw_idx) > _MAX_SPARSE_TERMS:
|
|
115
|
+
raise ValueError(f"sparse vector exceeds maximum of {_MAX_SPARSE_TERMS} terms")
|
|
116
|
+
if len(raw_idx) == 0:
|
|
117
|
+
return None
|
|
118
|
+
idx_out: List[int] = []
|
|
119
|
+
for idx in raw_idx:
|
|
120
|
+
if isinstance(idx, bool) or not isinstance(idx, int) or idx < 0 or idx > 0xFFFFFFFF:
|
|
121
|
+
raise ValueError("sparse_indices must contain integers in [0, 4294967295]")
|
|
122
|
+
idx_out.append(idx)
|
|
123
|
+
return idx_out, vals
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def _validate_hybrid_alpha(alpha: Any) -> float:
|
|
127
|
+
if isinstance(alpha, bool) or not isinstance(alpha, (int, float)) or not math.isfinite(float(alpha)):
|
|
128
|
+
raise ValueError("hybrid_alpha must be a finite number between 0.0 and 1.0")
|
|
129
|
+
val = float(alpha)
|
|
130
|
+
if val < 0.0 or val > 1.0:
|
|
131
|
+
raise ValueError("hybrid_alpha must be between 0.0 and 1.0")
|
|
132
|
+
return val
|
|
133
|
+
|
|
134
|
+
|
|
79
135
|
def _extract_error_message(raw_text: str) -> str:
|
|
80
136
|
msg = raw_text.strip()
|
|
81
137
|
try:
|
|
@@ -89,7 +145,7 @@ def _extract_error_message(raw_text: str) -> str:
|
|
|
89
145
|
|
|
90
146
|
@dataclass
|
|
91
147
|
class Doc:
|
|
92
|
-
"""Represents a
|
|
148
|
+
"""Represents a document for ingestion or export."""
|
|
93
149
|
|
|
94
150
|
id: str
|
|
95
151
|
vector: Sequence[float]
|
|
@@ -97,13 +153,20 @@ class Doc:
|
|
|
97
153
|
category: Optional[int] = None
|
|
98
154
|
metadata: Optional[Dict[str, Any]] = None
|
|
99
155
|
database: Optional[str] = None
|
|
156
|
+
sparse_indices: Optional[Sequence[int]] = None
|
|
157
|
+
sparse_values: Optional[Sequence[float]] = None
|
|
100
158
|
|
|
101
159
|
def to_dict(self) -> Dict[str, Any]:
|
|
102
160
|
_escape_doc_id(self.id)
|
|
161
|
+
vec = _validate_vector(self.vector)
|
|
162
|
+
sparse = _validate_sparse(self.sparse_indices, self.sparse_values)
|
|
103
163
|
out: Dict[str, Any] = {
|
|
104
164
|
"id": self.id,
|
|
105
|
-
"vector":
|
|
165
|
+
"vector": vec,
|
|
106
166
|
}
|
|
167
|
+
if sparse is not None:
|
|
168
|
+
out["sparse_indices"] = sparse[0]
|
|
169
|
+
out["sparse_values"] = sparse[1]
|
|
107
170
|
if self.database:
|
|
108
171
|
_validate_identifier(self.database, "database")
|
|
109
172
|
out["database"] = self.database
|
|
@@ -118,28 +181,52 @@ class Doc:
|
|
|
118
181
|
|
|
119
182
|
@dataclass
|
|
120
183
|
class SearchRequest:
|
|
121
|
-
"""Specifies a
|
|
184
|
+
"""Specifies a dense vector, BM25 lexical, sparse vector, or hybrid search query."""
|
|
122
185
|
|
|
123
186
|
tenant: str = ""
|
|
124
187
|
k: int = 10
|
|
125
|
-
vector: Sequence[float] =
|
|
188
|
+
vector: Optional[Sequence[float]] = None
|
|
189
|
+
query_text: Optional[str] = None
|
|
190
|
+
hybrid_alpha: Optional[float] = None
|
|
191
|
+
sparse_indices: Optional[Sequence[int]] = None
|
|
192
|
+
sparse_values: Optional[Sequence[float]] = None
|
|
126
193
|
database: str = ""
|
|
127
194
|
metadata_filter: Optional[Dict[str, Any]] = None
|
|
128
195
|
offset: int = 0
|
|
129
196
|
min_score: Optional[float] = None
|
|
130
197
|
rerank_pool: Optional[int] = None
|
|
131
198
|
nprobe: Optional[int] = None
|
|
199
|
+
skip_meta: bool = False
|
|
132
200
|
min_date: Optional[int] = None
|
|
133
201
|
max_date: Optional[int] = None
|
|
134
202
|
category: Optional[int] = None
|
|
135
203
|
categories: Optional[Sequence[int]] = None
|
|
204
|
+
min_doc_id: Optional[int] = None
|
|
205
|
+
max_doc_id: Optional[int] = None
|
|
136
206
|
|
|
137
207
|
def to_dict(self) -> Dict[str, Any]:
|
|
138
|
-
vec =
|
|
208
|
+
vec = _validate_optional_vector(self.vector)
|
|
209
|
+
sparse = _validate_sparse(self.sparse_indices, self.sparse_values)
|
|
210
|
+
q_text = ""
|
|
211
|
+
if self.query_text is not None:
|
|
212
|
+
if not isinstance(self.query_text, str):
|
|
213
|
+
raise ValueError("query_text must be a string")
|
|
214
|
+
q_text = self.query_text.strip()
|
|
215
|
+
if len(vec) == 0 and q_text == "" and sparse is None:
|
|
216
|
+
raise ValueError("search requires a non-empty vector, query_text, or sparse_indices")
|
|
217
|
+
|
|
139
218
|
out: Dict[str, Any] = {
|
|
140
|
-
"vector": vec,
|
|
141
219
|
"k": int(self.k),
|
|
142
220
|
}
|
|
221
|
+
if len(vec) > 0:
|
|
222
|
+
out["vector"] = vec
|
|
223
|
+
if q_text != "":
|
|
224
|
+
out["query_text"] = self.query_text
|
|
225
|
+
if self.hybrid_alpha is not None:
|
|
226
|
+
out["hybrid_alpha"] = _validate_hybrid_alpha(self.hybrid_alpha)
|
|
227
|
+
if sparse is not None:
|
|
228
|
+
out["sparse_indices"] = sparse[0]
|
|
229
|
+
out["sparse_values"] = sparse[1]
|
|
143
230
|
if self.tenant != "":
|
|
144
231
|
_validate_identifier(self.tenant, "tenant")
|
|
145
232
|
out["tenant"] = self.tenant
|
|
@@ -156,6 +243,8 @@ class SearchRequest:
|
|
|
156
243
|
out["rerank_pool"] = int(self.rerank_pool)
|
|
157
244
|
if self.nprobe:
|
|
158
245
|
out["nprobe"] = int(self.nprobe)
|
|
246
|
+
if self.skip_meta:
|
|
247
|
+
out["skip_meta"] = True
|
|
159
248
|
if self.min_date is not None:
|
|
160
249
|
out["min_date"] = int(self.min_date)
|
|
161
250
|
if self.max_date is not None:
|
|
@@ -164,6 +253,10 @@ class SearchRequest:
|
|
|
164
253
|
out["category"] = int(self.category)
|
|
165
254
|
if self.categories:
|
|
166
255
|
out["categories"] = [int(c) for c in self.categories]
|
|
256
|
+
if self.min_doc_id is not None:
|
|
257
|
+
out["min_doc_id"] = int(self.min_doc_id)
|
|
258
|
+
if self.max_doc_id is not None:
|
|
259
|
+
out["max_doc_id"] = int(self.max_doc_id)
|
|
167
260
|
return out
|
|
168
261
|
|
|
169
262
|
|
|
@@ -355,6 +448,10 @@ class MassEngineClient:
|
|
|
355
448
|
if not isinstance(d, dict):
|
|
356
449
|
raise ValueError("document must be a Doc or dict")
|
|
357
450
|
cls._esc_doc_id(d.get("id")) # type: ignore[arg-type]
|
|
451
|
+
if "vector" in d:
|
|
452
|
+
_validate_vector(d.get("vector"))
|
|
453
|
+
if "sparse_indices" in d or "sparse_values" in d:
|
|
454
|
+
_validate_sparse(d.get("sparse_indices"), d.get("sparse_values"))
|
|
358
455
|
if d.get("database"):
|
|
359
456
|
cls._esc_database(d["database"])
|
|
360
457
|
return d
|
|
@@ -380,7 +477,11 @@ class MassEngineClient:
|
|
|
380
477
|
self,
|
|
381
478
|
tenant: str = "",
|
|
382
479
|
*,
|
|
383
|
-
vector: Sequence[float],
|
|
480
|
+
vector: Optional[Sequence[float]] = None,
|
|
481
|
+
query_text: Optional[str] = None,
|
|
482
|
+
hybrid_alpha: Optional[float] = None,
|
|
483
|
+
sparse_indices: Optional[Sequence[int]] = None,
|
|
484
|
+
sparse_values: Optional[Sequence[float]] = None,
|
|
384
485
|
database: str = "",
|
|
385
486
|
k: int = 10,
|
|
386
487
|
metadata_filter: Optional[Dict[str, Any]] = None,
|
|
@@ -388,26 +489,36 @@ class MassEngineClient:
|
|
|
388
489
|
min_score: Optional[float] = None,
|
|
389
490
|
rerank_pool: Optional[int] = None,
|
|
390
491
|
nprobe: Optional[int] = None,
|
|
492
|
+
skip_meta: bool = False,
|
|
391
493
|
min_date: Optional[int] = None,
|
|
392
494
|
max_date: Optional[int] = None,
|
|
393
495
|
category: Optional[int] = None,
|
|
394
496
|
categories: Optional[Sequence[int]] = None,
|
|
497
|
+
min_doc_id: Optional[int] = None,
|
|
498
|
+
max_doc_id: Optional[int] = None,
|
|
395
499
|
) -> Dict[str, Any]:
|
|
396
|
-
"""Executes a dense vector
|
|
500
|
+
"""Executes a dense vector, BM25 lexical, sparse vector, or hybrid search query."""
|
|
397
501
|
sr = SearchRequest(
|
|
398
502
|
tenant=tenant,
|
|
399
503
|
k=k,
|
|
400
504
|
vector=vector,
|
|
505
|
+
query_text=query_text,
|
|
506
|
+
hybrid_alpha=hybrid_alpha,
|
|
507
|
+
sparse_indices=sparse_indices,
|
|
508
|
+
sparse_values=sparse_values,
|
|
401
509
|
database=database,
|
|
402
510
|
metadata_filter=metadata_filter,
|
|
403
511
|
offset=offset,
|
|
404
512
|
min_score=min_score,
|
|
405
513
|
rerank_pool=rerank_pool,
|
|
406
514
|
nprobe=nprobe,
|
|
515
|
+
skip_meta=skip_meta,
|
|
407
516
|
min_date=min_date,
|
|
408
517
|
max_date=max_date,
|
|
409
518
|
category=category,
|
|
410
519
|
categories=categories,
|
|
520
|
+
min_doc_id=min_doc_id,
|
|
521
|
+
max_doc_id=max_doc_id,
|
|
411
522
|
)
|
|
412
523
|
return self._request_json("POST", "/v1/search", sr.to_dict())
|
|
413
524
|
|
|
@@ -418,7 +529,7 @@ class MassEngineClient:
|
|
|
418
529
|
tenant: str = "",
|
|
419
530
|
database: str = "",
|
|
420
531
|
) -> Dict[str, Any]:
|
|
421
|
-
"""Executes multiple dense
|
|
532
|
+
"""Executes multiple dense, lexical, sparse, or hybrid search queries in a single round-trip."""
|
|
422
533
|
if not queries:
|
|
423
534
|
raise ValueError("queries must be non-empty")
|
|
424
535
|
esc_tenant = self._validate_optional_tenant(tenant)
|
|
@@ -428,9 +539,26 @@ class MassEngineClient:
|
|
|
428
539
|
if isinstance(q, SearchRequest):
|
|
429
540
|
serialized.append(q.to_dict())
|
|
430
541
|
elif isinstance(q, dict):
|
|
431
|
-
vec = _validate_vector(q.get("vector"))
|
|
432
542
|
q_copy = dict(q)
|
|
433
|
-
q_copy
|
|
543
|
+
vec = _validate_optional_vector(q_copy.get("vector"))
|
|
544
|
+
sparse = _validate_sparse(q_copy.get("sparse_indices"), q_copy.get("sparse_values"))
|
|
545
|
+
q_text_raw = q_copy.get("query_text")
|
|
546
|
+
q_text = ""
|
|
547
|
+
if q_text_raw is not None:
|
|
548
|
+
if not isinstance(q_text_raw, str):
|
|
549
|
+
raise ValueError("query_text must be a string")
|
|
550
|
+
q_text = q_text_raw.strip()
|
|
551
|
+
if len(vec) == 0 and q_text == "" and sparse is None:
|
|
552
|
+
raise ValueError("search requires a non-empty vector, query_text, or sparse_indices")
|
|
553
|
+
if len(vec) > 0:
|
|
554
|
+
q_copy["vector"] = vec
|
|
555
|
+
else:
|
|
556
|
+
q_copy.pop("vector", None)
|
|
557
|
+
if sparse is not None:
|
|
558
|
+
q_copy["sparse_indices"] = sparse[0]
|
|
559
|
+
q_copy["sparse_values"] = sparse[1]
|
|
560
|
+
if q_copy.get("hybrid_alpha") is not None:
|
|
561
|
+
q_copy["hybrid_alpha"] = _validate_hybrid_alpha(q_copy["hybrid_alpha"])
|
|
434
562
|
if q_copy.get("tenant"):
|
|
435
563
|
self._esc_tenant(q_copy["tenant"])
|
|
436
564
|
if q_copy.get("database"):
|
|
@@ -4,8 +4,8 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "massengine"
|
|
7
|
-
version = "1.
|
|
8
|
-
description = "Official Python SDK for the MassEngine filtered vector search database."
|
|
7
|
+
version = "1.2.0"
|
|
8
|
+
description = "Official Python SDK for the MassEngine filtered vector and hybrid search database."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = "Apache-2.0"
|
|
11
11
|
license-files = ["LICENSE"]
|
|
@@ -13,7 +13,7 @@ requires-python = ">=3.9"
|
|
|
13
13
|
authors = [
|
|
14
14
|
{ name = "MassEngine", email = "dev@massengine.com" }
|
|
15
15
|
]
|
|
16
|
-
keywords = ["vector-database", "vector-search", "rag", "embeddings", "massengine"]
|
|
16
|
+
keywords = ["vector-database", "vector-search", "hybrid-search", "bm25", "rag", "embeddings", "massengine"]
|
|
17
17
|
classifiers = [
|
|
18
18
|
"Development Status :: 5 - Production/Stable",
|
|
19
19
|
"Intended Audience :: Developers",
|
|
@@ -254,10 +254,10 @@ class TestMassEngineClient(unittest.TestCase):
|
|
|
254
254
|
_MockGatewayHandler.get_db_405 = False
|
|
255
255
|
|
|
256
256
|
def test_sdk_lifecycle_and_user_agent(self) -> None:
|
|
257
|
-
self.assertEqual(massengine.__version__, "1.
|
|
257
|
+
self.assertEqual(massengine.__version__, "1.2.0")
|
|
258
258
|
admin = MassEngineClient(base_url=self.base_url, api_key=" admin-key ")
|
|
259
259
|
self.assertEqual(admin.health()["status"], "ok")
|
|
260
|
-
self.assertEqual(_MockGatewayHandler.last_user_agent, "massengine-python/1.
|
|
260
|
+
self.assertEqual(_MockGatewayHandler.last_user_agent, "massengine-python/1.2.0")
|
|
261
261
|
r_ok = admin.ready()
|
|
262
262
|
self.assertTrue(r_ok["ready"])
|
|
263
263
|
self.assertEqual(r_ok["unavailable_shards"], [])
|
|
@@ -316,20 +316,89 @@ class TestMassEngineClient(unittest.TestCase):
|
|
|
316
316
|
revoked_client.get_doc("acme", "py-1")
|
|
317
317
|
self.assertEqual(ctx.exception.status_code, 401)
|
|
318
318
|
|
|
319
|
-
def
|
|
319
|
+
def test_hybrid_lexical_sparse_search_and_validation(self) -> None:
|
|
320
320
|
client = MassEngineClient(base_url=self.base_url, api_key="mk_valid")
|
|
321
|
-
|
|
322
|
-
SearchRequest(tenant="acme", vector=[1.0, 0.0], query_text="removed") # type: ignore[call-arg]
|
|
323
|
-
with self.assertRaises(TypeError):
|
|
324
|
-
SearchRequest(tenant="acme", vector=[1.0, 0.0], hybrid_alpha=0.5) # type: ignore[call-arg]
|
|
325
|
-
with self.assertRaises(TypeError):
|
|
326
|
-
client.search("acme", vector=[1.0, 0.0], query_text="removed") # type: ignore[call-arg]
|
|
327
|
-
with self.assertRaises(TypeError):
|
|
328
|
-
client.search("acme", vector=[1.0, 0.0], hybrid_alpha=0.5) # type: ignore[call-arg]
|
|
321
|
+
_MockGatewayHandler.recorded_bodies.clear()
|
|
329
322
|
|
|
323
|
+
# 1. Ingest with sparse_indices + sparse_values
|
|
324
|
+
ing = client.ingest(
|
|
325
|
+
"acme",
|
|
326
|
+
[
|
|
327
|
+
Doc(
|
|
328
|
+
id="py-hybrid-1",
|
|
329
|
+
vector=[1.0, 0.0],
|
|
330
|
+
sparse_indices=[10, 42],
|
|
331
|
+
sparse_values=[1.5, 0.75],
|
|
332
|
+
metadata={"title": "distributed consensus"},
|
|
333
|
+
)
|
|
334
|
+
],
|
|
335
|
+
)
|
|
336
|
+
self.assertEqual(ing["ingested"], 1)
|
|
337
|
+
ing_payload = json.loads(_MockGatewayHandler.recorded_bodies[-1])
|
|
338
|
+
self.assertEqual(ing_payload["docs"][0]["sparse_indices"], [10, 42])
|
|
339
|
+
self.assertEqual(ing_payload["docs"][0]["sparse_values"], [1.5, 0.75])
|
|
340
|
+
|
|
341
|
+
# 2. Pure BM25 lexical search (vector omitted)
|
|
342
|
+
sr_bm25 = client.search("acme", query_text="distributed consensus", k=5)
|
|
343
|
+
self.assertEqual(len(sr_bm25["hits"]), 1)
|
|
344
|
+
bm25_body = json.loads(_MockGatewayHandler.recorded_bodies[-1])
|
|
345
|
+
self.assertNotIn("vector", bm25_body)
|
|
346
|
+
self.assertEqual(bm25_body["query_text"], "distributed consensus")
|
|
347
|
+
|
|
348
|
+
# 3. Pure sparse vector search (vector omitted)
|
|
349
|
+
sr_sparse = client.search("acme", sparse_indices=[10, 42], sparse_values=[1.0, 0.5], k=5)
|
|
350
|
+
self.assertEqual(len(sr_sparse["hits"]), 1)
|
|
351
|
+
sparse_body = json.loads(_MockGatewayHandler.recorded_bodies[-1])
|
|
352
|
+
self.assertNotIn("vector", sparse_body)
|
|
353
|
+
self.assertEqual(sparse_body["sparse_indices"], [10, 42])
|
|
354
|
+
|
|
355
|
+
# 4. Hybrid dense + BM25 + sparse RRF with hybrid_alpha & skip_meta
|
|
356
|
+
sr_hybrid = client.search(
|
|
357
|
+
"acme",
|
|
358
|
+
vector=[1.0, 0.0],
|
|
359
|
+
query_text="consensus",
|
|
360
|
+
hybrid_alpha=0.65,
|
|
361
|
+
sparse_indices=[10],
|
|
362
|
+
sparse_values=[0.8],
|
|
363
|
+
skip_meta=True,
|
|
364
|
+
k=10,
|
|
365
|
+
)
|
|
366
|
+
self.assertEqual(len(sr_hybrid["hits"]), 1)
|
|
367
|
+
hybrid_body = json.loads(_MockGatewayHandler.recorded_bodies[-1])
|
|
368
|
+
self.assertEqual(hybrid_body["hybrid_alpha"], 0.65)
|
|
369
|
+
self.assertTrue(hybrid_body["skip_meta"])
|
|
370
|
+
|
|
371
|
+
# 5. Batch search with SearchRequest & dict hybrid/BM25/sparse queries
|
|
372
|
+
bs = client.batch_search(
|
|
373
|
+
[
|
|
374
|
+
SearchRequest(tenant="acme", query_text="consensus", k=3),
|
|
375
|
+
{"tenant": "acme", "sparse_indices": [42], "sparse_values": [1.25], "k": 3},
|
|
376
|
+
]
|
|
377
|
+
)
|
|
378
|
+
self.assertEqual(len(bs["results"]), 2)
|
|
379
|
+
|
|
380
|
+
# 6. Client-side validation rejects empty or malformed queries without contacting server
|
|
330
381
|
before_count = len(_MockGatewayHandler.recorded_paths)
|
|
382
|
+
with self.assertRaises(ValueError):
|
|
383
|
+
client.search("acme")
|
|
331
384
|
with self.assertRaises(ValueError):
|
|
332
385
|
client.search("acme", vector=[])
|
|
386
|
+
with self.assertRaises(ValueError):
|
|
387
|
+
client.search("acme", query_text=" ")
|
|
388
|
+
with self.assertRaises(ValueError):
|
|
389
|
+
client.search("acme", vector=[1.0, 0.0], hybrid_alpha=-0.1)
|
|
390
|
+
with self.assertRaises(ValueError):
|
|
391
|
+
client.search("acme", vector=[1.0, 0.0], hybrid_alpha=1.5)
|
|
392
|
+
with self.assertRaises(ValueError):
|
|
393
|
+
client.search("acme", vector=[1.0, 0.0], hybrid_alpha=float("nan"))
|
|
394
|
+
with self.assertRaises(ValueError):
|
|
395
|
+
client.search("acme", sparse_indices=[1, 2], sparse_values=[0.5])
|
|
396
|
+
with self.assertRaises(ValueError):
|
|
397
|
+
client.search("acme", sparse_indices=[-1], sparse_values=[0.5])
|
|
398
|
+
with self.assertRaises(ValueError):
|
|
399
|
+
client.search("acme", sparse_indices=[1], sparse_values=[float("inf")])
|
|
400
|
+
with self.assertRaises(ValueError):
|
|
401
|
+
client.ingest("acme", [Doc(id="bad", vector=[1.0, 0.0], sparse_indices=[1], sparse_values=[0.5, 0.6])])
|
|
333
402
|
with self.assertRaises(ValueError):
|
|
334
403
|
client.batch_search([])
|
|
335
404
|
with self.assertRaises(ValueError):
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|