pavedb-sdk 0.1.1__py3-none-any.whl

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.
@@ -0,0 +1,327 @@
1
+ Metadata-Version: 2.4
2
+ Name: pavedb-sdk
3
+ Version: 0.1.1
4
+ Summary: Python SDK for the PaveDB /v1 API
5
+ Author-email: Flowlexi <pavedb@flowlexi.com>
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Homepage, https://gitlab.com/flowlexi/pavedb-sdk-python
8
+ Project-URL: Source, https://gitlab.com/flowlexi/pavedb-sdk-python
9
+ Project-URL: Mirror, https://github.com/rodrigopitanga/pavedb-sdk-python
10
+ Requires-Python: >=3.10
11
+ Description-Content-Type: text/markdown
12
+ License-File: LICENSE
13
+ Requires-Dist: httpx>=0.27
14
+ Provides-Extra: test
15
+ Requires-Dist: pytest>=8; extra == "test"
16
+ Dynamic: license-file
17
+
18
+ <!-- (C) 2026 Rodrigo Rodrigues da Silva <rodrigo@flowlexi.com> -->
19
+ <!-- SPDX-License-Identifier: Apache-2.0 -->
20
+
21
+ # PaveDB Python SDK
22
+
23
+ Python SDK package for the PaveDB `/v1` API.
24
+
25
+ Use `pavedb-sdk` when your code should talk to PaveDB from Python.
26
+
27
+ There are three runtime paths:
28
+
29
+ - Connect to a PaveDB server over HTTP.
30
+ - Install `pavedb` alongside the SDK and use the same `Client` /
31
+ `Collection` handle API with a local embedded engine.
32
+ - Use ephemeral local mode for temporary in-process stores during tests,
33
+ notebooks, and short-lived experiments.
34
+
35
+ To run your own server instance, use the PaveDB core repository:
36
+ [GitLab](https://gitlab.com/flowlexi/pavedb) /
37
+ [GitHub](https://github.com/rodrigopitanga/pavedb). The core repository
38
+ remains the source of truth for the OpenAPI contract.
39
+
40
+ SDK source lives on
41
+ [GitLab](https://gitlab.com/flowlexi/pavedb-sdk-python) and
42
+ [GitHub](https://github.com/rodrigopitanga/pavedb-sdk-python).
43
+
44
+ ## Install
45
+
46
+ ```bash
47
+ pip install pavedb-sdk
48
+ ```
49
+
50
+ SDK `0.1.x` targets the PaveDB `/v1` API. SDK package versions are
51
+ independent from PaveDB core release versions; use `pavesdk.__version__`
52
+ for the SDK release and `pavesdk.PAVEDB_API_PREFIX` for the wire API.
53
+
54
+ For local embedded/persisted mode:
55
+
56
+ ```bash
57
+ pip install pavedb-sdk pavedb
58
+ ```
59
+
60
+ ## Local Package Build
61
+
62
+ Build the local PyPI package artifacts with GNU Make:
63
+
64
+ ```bash
65
+ gmake package
66
+ ```
67
+
68
+ That creates the source distribution (`.tar.gz`) and wheel in `dist/`,
69
+ checks them with Twine, and copies them to `artifacts/`.
70
+
71
+ Upload targets are explicit and do not infer release channels from the version:
72
+
73
+ ```bash
74
+ gmake pypitest-push
75
+ gmake pypi-push
76
+ ```
77
+
78
+ Runnable HTTP examples are installed with the package:
79
+
80
+ ```bash
81
+ python -m pavesdk.examples.http_search
82
+ python -m pavesdk.examples.observability
83
+ ```
84
+
85
+ The SDK checkout also includes `demo/20k_leagues.txt`, which the examples use
86
+ via a hardcoded relative path.
87
+
88
+ ## HTTP Client
89
+
90
+ ```python
91
+ from pavesdk.client import connect
92
+
93
+ db = connect(
94
+ "http://localhost:8086",
95
+ api_key="super-sekret",
96
+ tenant="demo",
97
+ )
98
+ books = db.collection("books")
99
+
100
+ hits = books.search("captain nemo", k=3)
101
+ hits
102
+ ```
103
+
104
+ ```text
105
+ [
106
+ {
107
+ "id": "note-1:0000",
108
+ "score": 0.86,
109
+ "text": "Captain Nemo commands the Nautilus.",
110
+ "meta": {"docid": "note-1", "kind": "note"},
111
+ },
112
+ {
113
+ "id": "note-2:0000",
114
+ "score": 0.73,
115
+ "text": "The Nautilus dives beneath the ice.",
116
+ "meta": {"docid": "note-2", "kind": "note"},
117
+ },
118
+ ]
119
+ ```
120
+
121
+ ```python
122
+ for hit in hits:
123
+ print(hit["score"], hit["meta"]["docid"], hit["text"][:80])
124
+ ```
125
+
126
+ ```text
127
+ 0.86 note-1 Captain Nemo commands the Nautilus.
128
+ 0.73 note-2 The Nautilus dives beneath the ice.
129
+ ```
130
+
131
+ `connect("http://...")` and `connect("https://...")` create an `HttpClient`.
132
+ Bare paths are local targets and require `pavedb` to be installed.
133
+
134
+ ## Collections
135
+
136
+ The API is handle-based: pick a collection once, then call methods on it.
137
+
138
+ ```python
139
+ from pavesdk.client import connect
140
+
141
+ db = connect("http://localhost:8086", api_key="super-sekret")
142
+ books = db.create_collection("books", tenant="demo")
143
+
144
+ books.add(
145
+ "Captain Nemo commands the Nautilus.",
146
+ docid="note-1",
147
+ metadata={"kind": "note"},
148
+ )
149
+ books.add_many([
150
+ ("The Nautilus dives beneath the ice.", "note-2", None),
151
+ {
152
+ "text": "Nemo studies ocean currents.",
153
+ "docid": "note-3",
154
+ "metadata": {"kind": "note"},
155
+ },
156
+ ])
157
+
158
+ matches = books.search(
159
+ "submarine captain",
160
+ k=5,
161
+ filters={"kind": "note"},
162
+ )
163
+ matches
164
+ ```
165
+
166
+ ```text
167
+ [
168
+ {
169
+ "id": "note-1:0000",
170
+ "score": 0.81,
171
+ "text": "Captain Nemo commands the Nautilus.",
172
+ "meta": {"docid": "note-1", "kind": "note"},
173
+ },
174
+ {
175
+ "id": "note-3:0000",
176
+ "score": 0.69,
177
+ "text": "Nemo studies ocean currents.",
178
+ "meta": {"docid": "note-3", "kind": "note"},
179
+ },
180
+ ]
181
+ ```
182
+
183
+ ## Observability
184
+
185
+ Searches are logged by PaveDB. Use query inspection to see what ran, replay it
186
+ against current data, and inspect the source chunks behind a document.
187
+
188
+ ```python
189
+ from pavesdk.client import connect
190
+
191
+ db = connect("http://localhost:8086", api_key="super-sekret")
192
+ books = db.collection("books", tenant="demo")
193
+
194
+ books.search("captain nemo", k=3)
195
+
196
+ latest = books.queries(limit=1)[0]
197
+ latest
198
+ ```
199
+
200
+ ```text
201
+ {
202
+ "query_id": "0d4f5a1b-9e4b-41c7-8b3f-8f6b5de3e74a",
203
+ "tenant": "demo",
204
+ "collection": "books",
205
+ "query_text": "captain nemo",
206
+ "k": 3,
207
+ "filters": None,
208
+ "result_count": 2,
209
+ "latency_ms": 12.4,
210
+ "created_at": "2026-06-20T18:42:16.153201Z",
211
+ }
212
+ ```
213
+
214
+ ```python
215
+ query = books.get_query(latest["query_id"])
216
+ query
217
+ ```
218
+
219
+ ```text
220
+ {
221
+ "query_id": "0d4f5a1b-9e4b-41c7-8b3f-8f6b5de3e74a",
222
+ "tenant": "demo",
223
+ "collection": "books",
224
+ "query_text": "captain nemo",
225
+ "k": 3,
226
+ "filters": None,
227
+ "result_ids": ["note-1:0000", "note-2:0000"],
228
+ "result_count": 2,
229
+ "latency_ms": 12.4,
230
+ }
231
+ ```
232
+
233
+ ```python
234
+ replayed = books.replay(query["query_id"])
235
+ replayed
236
+ ```
237
+
238
+ ```text
239
+ [
240
+ {
241
+ "id": "note-1:0000",
242
+ "score": 0.86,
243
+ "text": "Captain Nemo commands the Nautilus.",
244
+ "meta": {"docid": "note-1", "kind": "note"},
245
+ },
246
+ {
247
+ "id": "note-2:0000",
248
+ "score": 0.73,
249
+ "text": "The Nautilus dives beneath the ice.",
250
+ "meta": {"docid": "note-2", "kind": "note"},
251
+ },
252
+ ]
253
+ ```
254
+
255
+ ```python
256
+ docid = replayed[0]["meta"]["docid"]
257
+ chunks = books.list_chunks(docid)
258
+ chunks
259
+ ```
260
+
261
+ ```text
262
+ [
263
+ {
264
+ "rid": "note-1:0000",
265
+ "docid": "note-1",
266
+ "chunk": 0,
267
+ "text": "Captain Nemo commands the Nautilus.",
268
+ "metadata": {"kind": "note"},
269
+ }
270
+ ]
271
+ ```
272
+
273
+ ```python
274
+ chunk = books.get_chunk(chunks[0]["rid"])
275
+ chunk
276
+ ```
277
+
278
+ ```text
279
+ {
280
+ "rid": "note-1:0000",
281
+ "docid": "note-1",
282
+ "chunk": 0,
283
+ "text": "Captain Nemo commands the Nautilus.",
284
+ "metadata": {"kind": "note"},
285
+ }
286
+ ```
287
+
288
+ ```python
289
+ content = books.get_chunk_content(chunk["rid"])
290
+ content
291
+ ```
292
+
293
+ ```text
294
+ {
295
+ "content": b"Captain Nemo commands the Nautilus.",
296
+ "content_type": "text/plain; charset=utf-8",
297
+ }
298
+ ```
299
+
300
+ ## Local Mode
301
+
302
+ With `pavedb` installed, the same API can use a local persisted store:
303
+
304
+ ```python
305
+ from pavesdk.client import connect
306
+
307
+ with connect("./data", tenant="demo") as db:
308
+ books = db.create_collection("books")
309
+ books.add("Captain Nemo commands the Nautilus.", docid="note-1")
310
+ print(books.search("captain", k=3))
311
+ ```
312
+
313
+ If `pavedb` is not installed, local targets raise `LocalClientUnavailable`.
314
+
315
+ ## Archives
316
+
317
+ ```python
318
+ from pathlib import Path
319
+ from pavesdk.client import connect
320
+
321
+ with connect("http://localhost:8086", api_key="super-sekret") as db:
322
+ archive_bytes = db.dump_archive()
323
+ Path("pavedb-data.zip").write_bytes(archive_bytes)
324
+
325
+ saved_path = db.dump_archive("pavedb-data.zip")
326
+ db.restore_archive(Path(saved_path).read_bytes())
327
+ ```
@@ -0,0 +1,18 @@
1
+ pavedb_sdk-0.1.1.dist-info/licenses/LICENSE,sha256=AWKw2jn2nZyYIm5-r3BeUYxO6dTn4-PFXulgptEoyqs,9240
2
+ pavesdk/__init__.py,sha256=aWyHKEdRnZBvtTZHeYrTI4P7LqYVqiRAFgrvk4qQN0A,769
3
+ pavesdk/batch.py,sha256=6sL3IiIunTba10wOKxlcr_udQJI4nSdQSH9DFXkLEec,1173
4
+ pavesdk/client.py,sha256=aqzLxjpzdKes6v24ZNctQJpwooIRBaIJcRcYJWOBnfw,14211
5
+ pavesdk/compat.py,sha256=H2WnNzC4ypL2DSLbnrcjCXf7RNmg36iw0MUWVfNvpp8,188
6
+ pavesdk/errors.py,sha256=7BM32VU_CJ6NIeY5HW4Pqy31sL7gQNQ8_MAzhoom_zA,1660
7
+ pavesdk/interface.py,sha256=Iq6beV8GkKcXf37abNt2mGIEN_CrvwtX_7V6BORIK30,9286
8
+ pavesdk/providers.py,sha256=MBsuDEuNZHaE0Bql2G9foJSwH3pwAFbE8Im0ie3PwQ8,182
9
+ pavesdk/py.typed,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
10
+ pavesdk/examples/README.md,sha256=rSLqJxp-yBTdzqayPamH3ojFIE-B7m9_6mo2W7pV6Qg,713
11
+ pavesdk/examples/__init__.py,sha256=yc_hRkEy1NDvGf8XDos_wBq4d7BWQ8r12w9DdiELDjw,161
12
+ pavesdk/examples/__main__.py,sha256=Nte_uoCVbz2DRy57TEeMeCJRkVv2Ra_vxjKa2XsEF_4,341
13
+ pavesdk/examples/http_search.py,sha256=MijNKA2uoY-eXOMdh88n8vXIewIeY9FIjT_W-HF54aU,1397
14
+ pavesdk/examples/observability.py,sha256=7n8NRCW75sAaPqvIsfD8dW7JifvMxZp7bHLrpK3ApW8,2172
15
+ pavedb_sdk-0.1.1.dist-info/METADATA,sha256=noD97uz2as9eMhyaEMwQA3UgqvrznNxlSeL6eyNRIAY,7415
16
+ pavedb_sdk-0.1.1.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
17
+ pavedb_sdk-0.1.1.dist-info/top_level.txt,sha256=228gWC7DZN0YyOxxIEwhv0phs7kGNW_WrYgLQKw_iHM,8
18
+ pavedb_sdk-0.1.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (82.0.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,176 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer, and
167
+ charge a fee for, acceptance of support, warranty, indemnity, or
168
+ other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
@@ -0,0 +1 @@
1
+ pavesdk
pavesdk/__init__.py ADDED
@@ -0,0 +1,36 @@
1
+ # (C) 2026 Rodrigo Rodrigues da Silva <rodrigo@flowlexi.com>
2
+ # SPDX-License-Identifier: Apache-2.0
3
+
4
+ """Python SDK package for PaveDB."""
5
+
6
+ from .batch import batch_item
7
+ from .client import HttpClient, connect
8
+ from .compat import PAVEDB_API_PREFIX, PAVEDB_API_VERSION
9
+ from .errors import (
10
+ Conflict,
11
+ InvalidRequest,
12
+ LocalClientUnavailable,
13
+ NotFoundError,
14
+ PaveError,
15
+ Unavailable,
16
+ )
17
+ from .interface import BaseClient, Collection
18
+
19
+ __all__ = [
20
+ "BaseClient",
21
+ "Collection",
22
+ "Conflict",
23
+ "HttpClient",
24
+ "InvalidRequest",
25
+ "LocalClientUnavailable",
26
+ "NotFoundError",
27
+ "PaveError",
28
+ "PAVEDB_API_PREFIX",
29
+ "PAVEDB_API_VERSION",
30
+ "Unavailable",
31
+ "__version__",
32
+ "batch_item",
33
+ "connect",
34
+ ]
35
+
36
+ __version__ = "0.1.1"
pavesdk/batch.py ADDED
@@ -0,0 +1,36 @@
1
+ # (C) 2026 Rodrigo Rodrigues da Silva <rodrigo@flowlexi.com>
2
+ # SPDX-License-Identifier: Apache-2.0
3
+
4
+ from __future__ import annotations
5
+
6
+ from collections.abc import Mapping
7
+ from typing import Any
8
+
9
+ from .errors import InvalidRequest
10
+
11
+ JsonMap = dict[str, Any]
12
+
13
+
14
+ def batch_item(document: object) -> JsonMap:
15
+ if isinstance(document, str):
16
+ return {"text": document}
17
+ if isinstance(document, Mapping):
18
+ return {
19
+ "text": document.get("text"),
20
+ "docid": document.get("docid"),
21
+ "metadata": document.get("metadata"),
22
+ }
23
+ if isinstance(document, (tuple, list)):
24
+ if not 1 <= len(document) <= 3:
25
+ raise InvalidRequest(
26
+ "invalid_batch_item",
27
+ "batch items must be text, (text, docid, metadata), or a dict",
28
+ )
29
+ text = document[0]
30
+ docid = document[1] if len(document) > 1 else None
31
+ metadata = document[2] if len(document) > 2 else None
32
+ return {"text": text, "docid": docid, "metadata": metadata}
33
+ raise InvalidRequest(
34
+ "invalid_batch_item",
35
+ "batch items must be text, (text, docid, metadata), or a dict",
36
+ )