index4ai 0.1.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.
- index4ai-0.1.0/LICENSE +177 -0
- index4ai-0.1.0/PKG-INFO +172 -0
- index4ai-0.1.0/README.md +158 -0
- index4ai-0.1.0/index4ai/__init__.py +38 -0
- index4ai-0.1.0/index4ai/_transport.py +106 -0
- index4ai-0.1.0/index4ai/client.py +1076 -0
- index4ai-0.1.0/index4ai/exceptions.py +82 -0
- index4ai-0.1.0/index4ai/models.py +236 -0
- index4ai-0.1.0/index4ai.egg-info/PKG-INFO +172 -0
- index4ai-0.1.0/index4ai.egg-info/SOURCES.txt +16 -0
- index4ai-0.1.0/index4ai.egg-info/dependency_links.txt +1 -0
- index4ai-0.1.0/index4ai.egg-info/requires.txt +1 -0
- index4ai-0.1.0/index4ai.egg-info/top_level.txt +1 -0
- index4ai-0.1.0/pyproject.toml +25 -0
- index4ai-0.1.0/setup.cfg +4 -0
- index4ai-0.1.0/tests/test_api_v1.py +484 -0
- index4ai-0.1.0/tests/test_attributes.py +250 -0
- index4ai-0.1.0/tests/test_client.py +701 -0
index4ai-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
|
|
2
|
+
Mozilla Public License - Version 2.0
|
|
3
|
+
====================================
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
1. Definitions
|
|
7
|
+
--------------
|
|
8
|
+
1.1. “Contributor”
|
|
9
|
+
means each individual or legal entity that creates, contributes to the creation of, or owns Covered Software.
|
|
10
|
+
|
|
11
|
+
1.2. “Contributor Version”
|
|
12
|
+
means the combination of the Contributions of others (if any) used by a Contributor and that particular Contributor’s Contribution.
|
|
13
|
+
|
|
14
|
+
1.3. “Contribution”
|
|
15
|
+
means Covered Software of a particular Contributor.
|
|
16
|
+
|
|
17
|
+
1.4. “Covered Software”
|
|
18
|
+
means Source Code Form to which the initial Contributor has attached the notice in Exhibit A, the Executable Form of such Source Code Form, and Modifications of such Source Code Form, in each case including portions thereof.
|
|
19
|
+
|
|
20
|
+
1.5. “Incompatible With Secondary Licenses”
|
|
21
|
+
means
|
|
22
|
+
that the initial Contributor has attached the notice described in Exhibit B to the Covered Software; or
|
|
23
|
+
that the Covered Software was made available under the terms of version 1.1 or earlier of the License, but not also under the terms of a Secondary License.
|
|
24
|
+
|
|
25
|
+
1.6. “Executable Form”
|
|
26
|
+
means any form of the work other than Source Code Form.
|
|
27
|
+
|
|
28
|
+
1.7. “Larger Work”
|
|
29
|
+
means a work that combines Covered Software with other material, in a separate file or files, that is not Covered Software.
|
|
30
|
+
|
|
31
|
+
1.8. “License”
|
|
32
|
+
means this document.
|
|
33
|
+
|
|
34
|
+
1.9. “Licensable”
|
|
35
|
+
means having the right to grant, to the maximum extent possible, whether at the time of the initial grant or subsequently, any and all of the rights conveyed by this License.
|
|
36
|
+
|
|
37
|
+
1.10. “Modifications”
|
|
38
|
+
means any of the following:
|
|
39
|
+
any file in Source Code Form that results from an addition to, deletion from, or modification of the contents of Covered Software; or
|
|
40
|
+
any new file in Source Code Form that contains any Covered Software.
|
|
41
|
+
|
|
42
|
+
1.11. “Patent Claims” of a Contributor
|
|
43
|
+
means any patent claim(s), including without limitation, method, process, and apparatus claims, in any patent Licensable by such Contributor that would be infringed, but for the grant of the License, by the making, using, selling, offering for sale, having made, import, or transfer of either its Contributions or its Contributor Version.
|
|
44
|
+
|
|
45
|
+
1.12. “Secondary License”
|
|
46
|
+
means either the GNU General Public License, Version 2.0, the GNU Lesser General Public License, Version 2.1, the GNU Affero General Public License, Version 3.0, or any later versions of those licenses.
|
|
47
|
+
|
|
48
|
+
1.13. “Source Code Form”
|
|
49
|
+
means the form of the work preferred for making modifications.
|
|
50
|
+
|
|
51
|
+
1.14. “You” (or “Your”)
|
|
52
|
+
means an individual or a legal entity exercising rights under this License. For legal entities, “You” includes any entity that controls, is controlled by, or is under common control with You. For purposes of this definition, “control” means (a) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (b) ownership of more than fifty percent (50%) of the outstanding shares or beneficial ownership of such entity.
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
2. License Grants and Conditions
|
|
56
|
+
--------------------------------
|
|
57
|
+
2.1. Grants
|
|
58
|
+
Each Contributor hereby grants You a world-wide, royalty-free, non-exclusive license:
|
|
59
|
+
|
|
60
|
+
under intellectual property rights (other than patent or trademark) Licensable by such Contributor to use, reproduce, make available, modify, display, perform, distribute, and otherwise exploit its Contributions, either on an unmodified basis, with Modifications, or as part of a Larger Work; and
|
|
61
|
+
under Patent Claims of such Contributor to make, use, sell, offer for sale, have made, import, and otherwise transfer either its Contributions or its Contributor Version.
|
|
62
|
+
|
|
63
|
+
2.2. Effective Date
|
|
64
|
+
The licenses granted in Section 2.1 with respect to any Contribution become effective for each Contribution on the date the Contributor first distributes such Contribution.
|
|
65
|
+
|
|
66
|
+
2.3. Limitations on Grant Scope
|
|
67
|
+
The licenses granted in this Section 2 are the only rights granted under this License. No additional rights or licenses will be implied from the distribution or licensing of Covered Software under this License. Notwithstanding Section 2.1(b) above, no patent license is granted by a Contributor:
|
|
68
|
+
|
|
69
|
+
for any code that a Contributor has removed from Covered Software; or
|
|
70
|
+
for infringements caused by: (i) Your and any other third party’s modifications of Covered Software, or (ii) the combination of its Contributions with other software (except as part of its Contributor Version); or
|
|
71
|
+
under Patent Claims infringed by Covered Software in the absence of its Contributions.
|
|
72
|
+
This License does not grant any rights in the trademarks, service marks, or logos of any Contributor (except as may be necessary to comply with the notice requirements in Section 3.4).
|
|
73
|
+
|
|
74
|
+
2.4. Subsequent Licenses
|
|
75
|
+
No Contributor makes additional grants as a result of Your choice to distribute the Covered Software under a subsequent version of this License (see Section 10.2) or under the terms of a Secondary License (if permitted under the terms of Section 3.3).
|
|
76
|
+
|
|
77
|
+
2.5. Representation
|
|
78
|
+
Each Contributor represents that the Contributor believes its Contributions are its original creation(s) or it has sufficient rights to grant the rights to its Contributions conveyed by this License.
|
|
79
|
+
|
|
80
|
+
2.6. Fair Use
|
|
81
|
+
This License is not intended to limit any rights You have under applicable copyright doctrines of fair use, fair dealing, or other equivalents.
|
|
82
|
+
|
|
83
|
+
2.7. Conditions
|
|
84
|
+
Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in Section 2.1.
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
3. Responsibilities
|
|
88
|
+
-------------------
|
|
89
|
+
3.1. Distribution of Source Form
|
|
90
|
+
All distribution of Covered Software in Source Code Form, including any Modifications that You create or to which You contribute, must be under the terms of this License. You must inform recipients that the Source Code Form of the Covered Software is governed by the terms of this License, and how they can obtain a copy of this License. You may not attempt to alter or restrict the recipients’ rights in the Source Code Form.
|
|
91
|
+
|
|
92
|
+
3.2. Distribution of Executable Form
|
|
93
|
+
If You distribute Covered Software in Executable Form then:
|
|
94
|
+
|
|
95
|
+
such Covered Software must also be made available in Source Code Form, as described in Section 3.1, and You must inform recipients of the Executable Form how they can obtain a copy of such Source Code Form by reasonable means in a timely manner, at a charge no more than the cost of distribution to the recipient; and
|
|
96
|
+
You may distribute such Executable Form under the terms of this License, or sublicense it under different terms, provided that the license for the Executable Form does not attempt to limit or alter the recipients’ rights in the Source Code Form under this License.
|
|
97
|
+
|
|
98
|
+
3.3. Distribution of a Larger Work
|
|
99
|
+
You may create and distribute a Larger Work under terms of Your choice, provided that You also comply with the requirements of this License for the Covered Software. If the Larger Work is a combination of Covered Software with a work governed by one or more Secondary Licenses, and the Covered Software is not Incompatible With Secondary Licenses, this License permits You to additionally distribute such Covered Software under the terms of such Secondary License(s), so that the recipient of the Larger Work may, at their option, further distribute the Covered Software under the terms of either this License or such Secondary License(s).
|
|
100
|
+
|
|
101
|
+
3.4. Notices
|
|
102
|
+
You may not remove or alter the substance of any license notices (including copyright notices, patent notices, disclaimers of warranty, or limitations of liability) contained within the Source Code Form of the Covered Software, except that You may alter any license notices to the extent required to remedy known factual inaccuracies.
|
|
103
|
+
|
|
104
|
+
3.5. Application of Additional Terms
|
|
105
|
+
You may choose to offer, and to charge a fee for, warranty, support, indemnity or liability obligations to one or more recipients of Covered Software. However, You may do so only on Your own behalf, and not on behalf of any Contributor. You must make it absolutely clear that any such warranty, support, indemnity, or liability obligation is offered by You alone, and You hereby agree to indemnify every Contributor for any liability incurred by such Contributor as a result of warranty, support, indemnity or liability terms You offer. You may include additional disclaimers of warranty and limitations of liability specific to any jurisdiction.
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
4. Inability to Comply Due to Statute or Regulation
|
|
109
|
+
---------------------------------------------------
|
|
110
|
+
If it is impossible for You to comply with any of the terms of this License with respect to some or all of the Covered Software due to statute, judicial order, or regulation then You must: (a) comply with the terms of this License to the maximum extent possible; and (b) describe the limitations and the code they affect. Such description must be placed in a text file included with all distributions of the Covered Software under this License. Except to the extent prohibited by statute or regulation, such description must be sufficiently detailed for a recipient of ordinary skill to be able to understand it.
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
5. Termination
|
|
114
|
+
--------------
|
|
115
|
+
5.1. The rights granted under this License will terminate automatically if You fail to comply with any of its terms. However, if You become compliant, then the rights granted under this License from a particular Contributor are reinstated (a) provisionally, unless and until such Contributor explicitly and finally terminates Your grants, and (b) on an ongoing basis, if such Contributor fails to notify You of the non-compliance by some reasonable means prior to 60 days after You have come back into compliance. Moreover, Your grants from a particular Contributor are reinstated on an ongoing basis if such Contributor notifies You of the non-compliance by some reasonable means, this is the first time You have received notice of non-compliance with this License from such Contributor, and You become compliant prior to 30 days after Your receipt of the notice.
|
|
116
|
+
|
|
117
|
+
5.2. If You initiate litigation against any entity by asserting a patent infringement claim (excluding declaratory judgment actions, counter-claims, and cross-claims) alleging that a Contributor Version directly or indirectly infringes any patent, then the rights granted to You by any and all Contributors for the Covered Software under Section 2.1 of this License shall terminate.
|
|
118
|
+
|
|
119
|
+
5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user license agreements (excluding distributors and resellers) which have been validly granted by You or Your distributors under this License prior to termination shall survive termination.
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
6. Disclaimer of Warranty
|
|
123
|
+
-------------------------
|
|
124
|
+
Covered Software is provided under this License on an “as is” basis, without warranty of any kind, either expressed, implied, or statutory, including, without limitation, warranties that the Covered Software is free of defects, merchantable, fit for a particular purpose or non-infringing. The entire risk as to the quality and performance of the Covered Software is with You. Should any Covered Software prove defective in any respect, You (not any Contributor) assume the cost of any necessary servicing, repair, or correction. This disclaimer of warranty constitutes an essential part of this License. No use of any Covered Software is authorized under this License except under this disclaimer.
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
7. Limitation of Liability
|
|
128
|
+
--------------------------
|
|
129
|
+
Under no circumstances and under no legal theory, whether tort (including negligence), contract, or otherwise, shall any Contributor, or anyone who distributes Covered Software as permitted above, be liable to You for any direct, indirect, special, incidental, or consequential damages of any character including, without limitation, damages for lost profits, loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses, even if such party shall have been informed of the possibility of such damages. This limitation of liability shall not apply to liability for death or personal injury resulting from such party’s negligence to the extent applicable law prohibits such limitation. Some jurisdictions do not allow the exclusion or limitation of incidental or consequential damages, so this exclusion and limitation may not apply to You.
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
8. Litigation
|
|
133
|
+
-------------
|
|
134
|
+
Any litigation relating to this License may be brought only in the courts of a jurisdiction where the defendant maintains its principal place of business and such litigation shall be governed by laws of that jurisdiction, without reference to its conflict-of-law provisions. Nothing in this Section shall prevent a party’s ability to bring cross-claims or counter-claims.
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
9. Miscellaneous
|
|
138
|
+
----------------
|
|
139
|
+
This License represents the complete agreement concerning the subject matter hereof. If any provision of this License is held to be unenforceable, such provision shall be reformed only to the extent necessary to make it enforceable. Any law or regulation which provides that the language of a contract shall be construed against the drafter shall not be used to construe this License against a Contributor.
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
10. Versions of the License
|
|
143
|
+
---------------------------
|
|
144
|
+
10.1. New Versions
|
|
145
|
+
Mozilla Foundation is the license steward. Except as provided in Section 10.3, no one other than the license steward has the right to modify or publish new versions of this License. Each version will be given a distinguishing version number.
|
|
146
|
+
|
|
147
|
+
10.2. Effect of New Versions
|
|
148
|
+
You may distribute the Covered Software under the terms of the version of the License under which You originally received the Covered Software, or under the terms of any subsequent version published by the license steward.
|
|
149
|
+
|
|
150
|
+
10.3. Modified Versions
|
|
151
|
+
If you create software not governed by this License, and you want to create a new license for such software, you may create and use a modified version of this License if you rename the license and remove any references to the name of the license steward (except to note that such modified license differs from this License).
|
|
152
|
+
|
|
153
|
+
10.4. Distributing Source Code Form that is Incompatible With Secondary Licenses
|
|
154
|
+
If You choose to distribute Source Code Form that is Incompatible With Secondary Licenses under the terms of this version of the License, the notice described in Exhibit B of this License must be attached.
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
Exhibit A – Source Code Form License Notice
|
|
158
|
+
-------------------------------------------
|
|
159
|
+
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
160
|
+
|
|
161
|
+
If it is not possible or desirable to put the notice in a particular file, then You may include the notice in a location (such as a LICENSE file in a relevant directory) where a recipient would be likely to look for such a notice.
|
|
162
|
+
|
|
163
|
+
You may add additional accurate notices of copyright ownership.
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
Exhibit B – “Incompatible With Secondary Licenses” Notice
|
|
167
|
+
---------------------------------------------------------
|
|
168
|
+
This Source Code Form is “Incompatible With Secondary Licenses”, as defined by the Mozilla Public License, v. 2.0.
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
|
index4ai-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: index4ai
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Official Python SDK for index4ai: document indexing and hybrid search with prompt-ready context
|
|
5
|
+
Author: POMA AI GmbH, Berlin
|
|
6
|
+
License-Expression: MPL-2.0
|
|
7
|
+
Project-URL: Homepage, https://index4.ai
|
|
8
|
+
Keywords: rag,retrieval,hybrid-search,indexing,documents,ai
|
|
9
|
+
Requires-Python: >=3.10
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Requires-Dist: httpx>=0.28.1
|
|
13
|
+
Dynamic: license-file
|
|
14
|
+
|
|
15
|
+
# index4ai
|
|
16
|
+
|
|
17
|
+
Official Python SDK for **index4ai**: index documents into a project namespace
|
|
18
|
+
and get prompt-ready retrieval context for RAG from hybrid search.
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
Requires Python 3.10+.
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pip install index4ai
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Authentication
|
|
29
|
+
|
|
30
|
+
index4ai uses a **project API key**. Create one in the index4ai console
|
|
31
|
+
(https://console.index4.ai) and set it in your environment:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
export INDEX4AI_API_KEY="..."
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
or pass `api_key=` to the client. The client checks the key's prefix when it
|
|
38
|
+
is created and raises `InvalidApiKeyError` if you pass a key that is not a
|
|
39
|
+
project key, so a misconfiguration fails locally instead of as a 401/403.
|
|
40
|
+
|
|
41
|
+
## Usage
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from index4ai import Index4AI
|
|
45
|
+
|
|
46
|
+
client = Index4AI() # reads INDEX4AI_API_KEY
|
|
47
|
+
|
|
48
|
+
# 0. List the project's typed attributes BEFORE declaring any.
|
|
49
|
+
# Attribute names (and their types) are permanent per project and capped at
|
|
50
|
+
# `max_names`, so reuse an existing name instead of minting a near-duplicate.
|
|
51
|
+
known = {a.name: a.type for a in client.list_attributes().attributes}
|
|
52
|
+
|
|
53
|
+
# 1. Ingest a document. It is indexed into your project namespace; there is no
|
|
54
|
+
# archive to download. Typed attributes enable query-time filtering:
|
|
55
|
+
# string/int/float/bool are inferred; datetime and encrypted_text must be
|
|
56
|
+
# declared in attribute_schema.
|
|
57
|
+
result = client.ingest(
|
|
58
|
+
"document.pdf",
|
|
59
|
+
attributes={"source": "treasury", "published_at": "2023-01-01T00:00:00Z"},
|
|
60
|
+
attribute_schema={"published_at": {"type": "datetime"}},
|
|
61
|
+
)
|
|
62
|
+
print(result.doc_id, result.status, result.usage)
|
|
63
|
+
|
|
64
|
+
# 1b. Or let the server fetch the file from a public URL — no upload.
|
|
65
|
+
# Optionally POST a webhook when the job finishes; the value is
|
|
66
|
+
# "<url>|<header>:<value>|..." (capped at 1024 chars).
|
|
67
|
+
result = client.ingest(
|
|
68
|
+
remote_url="https://example.com/document.pdf",
|
|
69
|
+
completion="https://example.com/hook|x-api-key:secret",
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
# 2. Hybrid search across all documents in the namespace. Returns a
|
|
73
|
+
# prompt-ready XML+Markdown block — drop it straight into an LLM prompt.
|
|
74
|
+
ctx = client.search(
|
|
75
|
+
"How do I configure retries?",
|
|
76
|
+
target_tokens=2048,
|
|
77
|
+
attribute_filters=[{"attr": "source", "op": "eq", "value": "treasury"}],
|
|
78
|
+
)
|
|
79
|
+
print(ctx.context)
|
|
80
|
+
|
|
81
|
+
# 3. List, inspect, and delete documents. list_docs() follows pagination and
|
|
82
|
+
# returns every document in the namespace.
|
|
83
|
+
for doc in client.list_docs():
|
|
84
|
+
print(doc.doc_id, doc.filename, doc.pages)
|
|
85
|
+
|
|
86
|
+
deleted = client.delete_doc(result.doc_id)
|
|
87
|
+
print(deleted.vectors_deleted, deleted.storage_deleted)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### The document id
|
|
91
|
+
|
|
92
|
+
`ingest()` / `collect()` return an `IngestResult` whose `doc_id` is the id to
|
|
93
|
+
search and manage the document by. If the same content was already indexed,
|
|
94
|
+
the server keeps the existing document instead: `deduplicated` is `True` and
|
|
95
|
+
`doc_id` is the pre-existing document's id. `doc_id` is `None` only when the
|
|
96
|
+
server reported no indexing outcome.
|
|
97
|
+
|
|
98
|
+
### Submitting and waiting separately
|
|
99
|
+
|
|
100
|
+
`ingest()` is `submit()` followed by `collect()`. To submit many files and
|
|
101
|
+
collect later, split them:
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
job_id = client.submit("document.pdf")
|
|
105
|
+
# ... later, even from another process:
|
|
106
|
+
result = client.collect(job_id)
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Waiting can always be resumed with the same `job_id`. Submitting the file
|
|
110
|
+
again instead creates a second job. Two errors tell you which case you are in:
|
|
111
|
+
|
|
112
|
+
- `JobWaitError` — the job exists and keeps running; only the wait failed
|
|
113
|
+
(network, unreadable status). Resume with `client.collect(err.job_id)`.
|
|
114
|
+
- `SubmitUncertainError` — the connection failed mid-submit, so the server may
|
|
115
|
+
or may not have created a job. Check `list_docs()` before submitting again.
|
|
116
|
+
|
|
117
|
+
### Listing attributes: list before you declare
|
|
118
|
+
|
|
119
|
+
`list_attributes()` returns an `AttributeList`: `attributes` (a list of
|
|
120
|
+
`AttributeInfo(name, type)`), `max_names` (the project's cap on distinct names)
|
|
121
|
+
and `note` (the server's guidance).
|
|
122
|
+
|
|
123
|
+
**Attribute names are permanent per project** — the first write of a name fixes
|
|
124
|
+
its type and spends one of the `max_names` slots, and clearing a value does not
|
|
125
|
+
free the name. Call `list_attributes()` before declaring attributes and reuse an
|
|
126
|
+
existing name with its existing type.
|
|
127
|
+
|
|
128
|
+
A `503` means the attribute schema is temporarily unreadable. It is raised as
|
|
129
|
+
`ApiError` with `status_code == 503` — retry later; **never treat it as an
|
|
130
|
+
empty list**, or you will mint duplicate names. A response without an
|
|
131
|
+
`attributes` list raises `InvalidResponseError` for the same reason.
|
|
132
|
+
|
|
133
|
+
### Async
|
|
134
|
+
|
|
135
|
+
`AsyncIndex4AI` mirrors the sync client method for method:
|
|
136
|
+
|
|
137
|
+
```python
|
|
138
|
+
import asyncio
|
|
139
|
+
|
|
140
|
+
from index4ai import AsyncIndex4AI
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
async def main() -> None:
|
|
144
|
+
async with AsyncIndex4AI() as client:
|
|
145
|
+
ctx = await client.search_in_doc("summarize section 3", "doc_abc123")
|
|
146
|
+
print(ctx.context)
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
asyncio.run(main())
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Migrating from `poma.Grill`
|
|
153
|
+
|
|
154
|
+
| Before (`poma` ≤ 0.7) | Now (`index4ai`) |
|
|
155
|
+
|---|---|
|
|
156
|
+
| `pip install poma` | `pip install index4ai` |
|
|
157
|
+
| `from poma import Grill, AsyncGrill` | `from index4ai import Index4AI, AsyncIndex4AI` |
|
|
158
|
+
| `POMA_GRILL_API_KEY` | `INDEX4AI_API_KEY` (same project key) |
|
|
159
|
+
| `GrillIngestResult` (find the doc via `list_docs()`) | `IngestResult` with `doc_id` |
|
|
160
|
+
| `GrillContext` | `SearchContext` |
|
|
161
|
+
| `InvalidGrillApiKeyError` | `InvalidApiKeyError` |
|
|
162
|
+
| `labels` / `meta_int_*`, `labels_any`… / `meta_int_*_gte`… | `attributes` / `attribute_schema`, `attribute_filters` |
|
|
163
|
+
| `return_page_images` | removed |
|
|
164
|
+
|
|
165
|
+
## Configuration
|
|
166
|
+
|
|
167
|
+
| Environment variable | Purpose | Default |
|
|
168
|
+
|---|---|---|
|
|
169
|
+
| `INDEX4AI_API_KEY` | Project API key | — (required) |
|
|
170
|
+
| `INDEX4AI_API_BASE_URL` | API host override | `https://api.index4.ai` |
|
|
171
|
+
|
|
172
|
+
The client targets API version 1 (`https://api.index4.ai/v1`).
|
index4ai-0.1.0/README.md
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
# index4ai
|
|
2
|
+
|
|
3
|
+
Official Python SDK for **index4ai**: index documents into a project namespace
|
|
4
|
+
and get prompt-ready retrieval context for RAG from hybrid search.
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
Requires Python 3.10+.
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
pip install index4ai
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Authentication
|
|
15
|
+
|
|
16
|
+
index4ai uses a **project API key**. Create one in the index4ai console
|
|
17
|
+
(https://console.index4.ai) and set it in your environment:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
export INDEX4AI_API_KEY="..."
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
or pass `api_key=` to the client. The client checks the key's prefix when it
|
|
24
|
+
is created and raises `InvalidApiKeyError` if you pass a key that is not a
|
|
25
|
+
project key, so a misconfiguration fails locally instead of as a 401/403.
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
from index4ai import Index4AI
|
|
31
|
+
|
|
32
|
+
client = Index4AI() # reads INDEX4AI_API_KEY
|
|
33
|
+
|
|
34
|
+
# 0. List the project's typed attributes BEFORE declaring any.
|
|
35
|
+
# Attribute names (and their types) are permanent per project and capped at
|
|
36
|
+
# `max_names`, so reuse an existing name instead of minting a near-duplicate.
|
|
37
|
+
known = {a.name: a.type for a in client.list_attributes().attributes}
|
|
38
|
+
|
|
39
|
+
# 1. Ingest a document. It is indexed into your project namespace; there is no
|
|
40
|
+
# archive to download. Typed attributes enable query-time filtering:
|
|
41
|
+
# string/int/float/bool are inferred; datetime and encrypted_text must be
|
|
42
|
+
# declared in attribute_schema.
|
|
43
|
+
result = client.ingest(
|
|
44
|
+
"document.pdf",
|
|
45
|
+
attributes={"source": "treasury", "published_at": "2023-01-01T00:00:00Z"},
|
|
46
|
+
attribute_schema={"published_at": {"type": "datetime"}},
|
|
47
|
+
)
|
|
48
|
+
print(result.doc_id, result.status, result.usage)
|
|
49
|
+
|
|
50
|
+
# 1b. Or let the server fetch the file from a public URL — no upload.
|
|
51
|
+
# Optionally POST a webhook when the job finishes; the value is
|
|
52
|
+
# "<url>|<header>:<value>|..." (capped at 1024 chars).
|
|
53
|
+
result = client.ingest(
|
|
54
|
+
remote_url="https://example.com/document.pdf",
|
|
55
|
+
completion="https://example.com/hook|x-api-key:secret",
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
# 2. Hybrid search across all documents in the namespace. Returns a
|
|
59
|
+
# prompt-ready XML+Markdown block — drop it straight into an LLM prompt.
|
|
60
|
+
ctx = client.search(
|
|
61
|
+
"How do I configure retries?",
|
|
62
|
+
target_tokens=2048,
|
|
63
|
+
attribute_filters=[{"attr": "source", "op": "eq", "value": "treasury"}],
|
|
64
|
+
)
|
|
65
|
+
print(ctx.context)
|
|
66
|
+
|
|
67
|
+
# 3. List, inspect, and delete documents. list_docs() follows pagination and
|
|
68
|
+
# returns every document in the namespace.
|
|
69
|
+
for doc in client.list_docs():
|
|
70
|
+
print(doc.doc_id, doc.filename, doc.pages)
|
|
71
|
+
|
|
72
|
+
deleted = client.delete_doc(result.doc_id)
|
|
73
|
+
print(deleted.vectors_deleted, deleted.storage_deleted)
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### The document id
|
|
77
|
+
|
|
78
|
+
`ingest()` / `collect()` return an `IngestResult` whose `doc_id` is the id to
|
|
79
|
+
search and manage the document by. If the same content was already indexed,
|
|
80
|
+
the server keeps the existing document instead: `deduplicated` is `True` and
|
|
81
|
+
`doc_id` is the pre-existing document's id. `doc_id` is `None` only when the
|
|
82
|
+
server reported no indexing outcome.
|
|
83
|
+
|
|
84
|
+
### Submitting and waiting separately
|
|
85
|
+
|
|
86
|
+
`ingest()` is `submit()` followed by `collect()`. To submit many files and
|
|
87
|
+
collect later, split them:
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
job_id = client.submit("document.pdf")
|
|
91
|
+
# ... later, even from another process:
|
|
92
|
+
result = client.collect(job_id)
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Waiting can always be resumed with the same `job_id`. Submitting the file
|
|
96
|
+
again instead creates a second job. Two errors tell you which case you are in:
|
|
97
|
+
|
|
98
|
+
- `JobWaitError` — the job exists and keeps running; only the wait failed
|
|
99
|
+
(network, unreadable status). Resume with `client.collect(err.job_id)`.
|
|
100
|
+
- `SubmitUncertainError` — the connection failed mid-submit, so the server may
|
|
101
|
+
or may not have created a job. Check `list_docs()` before submitting again.
|
|
102
|
+
|
|
103
|
+
### Listing attributes: list before you declare
|
|
104
|
+
|
|
105
|
+
`list_attributes()` returns an `AttributeList`: `attributes` (a list of
|
|
106
|
+
`AttributeInfo(name, type)`), `max_names` (the project's cap on distinct names)
|
|
107
|
+
and `note` (the server's guidance).
|
|
108
|
+
|
|
109
|
+
**Attribute names are permanent per project** — the first write of a name fixes
|
|
110
|
+
its type and spends one of the `max_names` slots, and clearing a value does not
|
|
111
|
+
free the name. Call `list_attributes()` before declaring attributes and reuse an
|
|
112
|
+
existing name with its existing type.
|
|
113
|
+
|
|
114
|
+
A `503` means the attribute schema is temporarily unreadable. It is raised as
|
|
115
|
+
`ApiError` with `status_code == 503` — retry later; **never treat it as an
|
|
116
|
+
empty list**, or you will mint duplicate names. A response without an
|
|
117
|
+
`attributes` list raises `InvalidResponseError` for the same reason.
|
|
118
|
+
|
|
119
|
+
### Async
|
|
120
|
+
|
|
121
|
+
`AsyncIndex4AI` mirrors the sync client method for method:
|
|
122
|
+
|
|
123
|
+
```python
|
|
124
|
+
import asyncio
|
|
125
|
+
|
|
126
|
+
from index4ai import AsyncIndex4AI
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
async def main() -> None:
|
|
130
|
+
async with AsyncIndex4AI() as client:
|
|
131
|
+
ctx = await client.search_in_doc("summarize section 3", "doc_abc123")
|
|
132
|
+
print(ctx.context)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
asyncio.run(main())
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
## Migrating from `poma.Grill`
|
|
139
|
+
|
|
140
|
+
| Before (`poma` ≤ 0.7) | Now (`index4ai`) |
|
|
141
|
+
|---|---|
|
|
142
|
+
| `pip install poma` | `pip install index4ai` |
|
|
143
|
+
| `from poma import Grill, AsyncGrill` | `from index4ai import Index4AI, AsyncIndex4AI` |
|
|
144
|
+
| `POMA_GRILL_API_KEY` | `INDEX4AI_API_KEY` (same project key) |
|
|
145
|
+
| `GrillIngestResult` (find the doc via `list_docs()`) | `IngestResult` with `doc_id` |
|
|
146
|
+
| `GrillContext` | `SearchContext` |
|
|
147
|
+
| `InvalidGrillApiKeyError` | `InvalidApiKeyError` |
|
|
148
|
+
| `labels` / `meta_int_*`, `labels_any`… / `meta_int_*_gte`… | `attributes` / `attribute_schema`, `attribute_filters` |
|
|
149
|
+
| `return_page_images` | removed |
|
|
150
|
+
|
|
151
|
+
## Configuration
|
|
152
|
+
|
|
153
|
+
| Environment variable | Purpose | Default |
|
|
154
|
+
|---|---|---|
|
|
155
|
+
| `INDEX4AI_API_KEY` | Project API key | — (required) |
|
|
156
|
+
| `INDEX4AI_API_BASE_URL` | API host override | `https://api.index4.ai` |
|
|
157
|
+
|
|
158
|
+
The client targets API version 1 (`https://api.index4.ai/v1`).
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"""Official Python SDK for index4ai: document indexing and hybrid search."""
|
|
2
|
+
|
|
3
|
+
from .client import AsyncIndex4AI, Index4AI
|
|
4
|
+
from .exceptions import (
|
|
5
|
+
ApiError,
|
|
6
|
+
Index4AIError,
|
|
7
|
+
InvalidApiKeyError,
|
|
8
|
+
InvalidInputError,
|
|
9
|
+
InvalidResponseError,
|
|
10
|
+
JobWaitError,
|
|
11
|
+
SubmitUncertainError,
|
|
12
|
+
)
|
|
13
|
+
from .models import (
|
|
14
|
+
AttributeInfo,
|
|
15
|
+
AttributeList,
|
|
16
|
+
DeleteDocResponse,
|
|
17
|
+
DocInfo,
|
|
18
|
+
IngestResult,
|
|
19
|
+
SearchContext,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
__all__ = [
|
|
23
|
+
"Index4AI",
|
|
24
|
+
"AsyncIndex4AI",
|
|
25
|
+
"IngestResult",
|
|
26
|
+
"SearchContext",
|
|
27
|
+
"DocInfo",
|
|
28
|
+
"AttributeInfo",
|
|
29
|
+
"AttributeList",
|
|
30
|
+
"DeleteDocResponse",
|
|
31
|
+
"Index4AIError",
|
|
32
|
+
"ApiError",
|
|
33
|
+
"InvalidApiKeyError",
|
|
34
|
+
"InvalidInputError",
|
|
35
|
+
"InvalidResponseError",
|
|
36
|
+
"JobWaitError",
|
|
37
|
+
"SubmitUncertainError",
|
|
38
|
+
]
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"""Product-neutral transport helpers for the index4ai clients.
|
|
2
|
+
|
|
3
|
+
Kept free of product names, hosts, env vars and key prefixes: those belong in
|
|
4
|
+
``client.py``. A sibling SDK carries an equivalent module of its own; the
|
|
5
|
+
two are not synchronised automatically, so port a fix to both when it applies.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import json
|
|
11
|
+
import os
|
|
12
|
+
from importlib.metadata import PackageNotFoundError, version as _pkg_version
|
|
13
|
+
from typing import Any
|
|
14
|
+
from urllib.parse import quote
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def package_version(distribution: str) -> str:
|
|
18
|
+
"""Installed version of ``distribution``, or ``"unknown"`` when not installed.
|
|
19
|
+
|
|
20
|
+
Read from installed package metadata, so an editable install reports the
|
|
21
|
+
version it was installed at until it is reinstalled.
|
|
22
|
+
"""
|
|
23
|
+
try:
|
|
24
|
+
return _pkg_version(distribution)
|
|
25
|
+
except PackageNotFoundError:
|
|
26
|
+
return "unknown"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def build_request_origin(
|
|
30
|
+
prefix: str,
|
|
31
|
+
sdk_version: str,
|
|
32
|
+
*,
|
|
33
|
+
is_async: bool = False,
|
|
34
|
+
tags: tuple[str, ...] = (),
|
|
35
|
+
) -> str:
|
|
36
|
+
"""Build an ``X-Request-Origin`` value: ``<prefix>-<version>[-async][-tag...]``.
|
|
37
|
+
|
|
38
|
+
The server's origin enum only matches bare names, so callers must also send
|
|
39
|
+
``X-Is-Sdk: true`` for the request to be classified as SDK traffic; this
|
|
40
|
+
value is telemetry that reaches the server logs.
|
|
41
|
+
"""
|
|
42
|
+
parts = [f"{prefix}-{sdk_version}"]
|
|
43
|
+
if is_async:
|
|
44
|
+
parts.append("async")
|
|
45
|
+
parts.extend(tags)
|
|
46
|
+
return "-".join(parts)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def apply_sdk_user_agent(headers: Any, user_agent: str) -> None:
|
|
50
|
+
"""Set ``user_agent`` on ``headers`` unless the caller chose their own.
|
|
51
|
+
|
|
52
|
+
A client the caller passes in still carries httpx's default
|
|
53
|
+
``python-httpx/<version>``, which makes SDK traffic look like a hand-rolled
|
|
54
|
+
integration in server logs. That default is replaced; any other value is
|
|
55
|
+
the caller's deliberate choice and is kept.
|
|
56
|
+
"""
|
|
57
|
+
current = headers.get("user-agent", "")
|
|
58
|
+
if not current or current.startswith("python-httpx/"):
|
|
59
|
+
headers["user-agent"] = user_agent
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def resolve_base_url(env_var: str, default: str) -> str:
|
|
63
|
+
"""Base URL from ``env_var`` if set, else ``default``, without a trailing slash."""
|
|
64
|
+
base = os.environ.get(env_var) or default
|
|
65
|
+
if not base:
|
|
66
|
+
raise ValueError("API base URL cannot be empty.")
|
|
67
|
+
return base.rstrip("/")
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def format_debug_dump(data: Any) -> str:
|
|
71
|
+
"""Pretty-print ``data`` as JSON for error messages, falling back to ``repr``."""
|
|
72
|
+
try:
|
|
73
|
+
return json.dumps(data, indent=2, sort_keys=True, ensure_ascii=False)
|
|
74
|
+
except TypeError:
|
|
75
|
+
return repr(data)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def build_content_disposition(filename: str) -> str:
|
|
79
|
+
"""``Content-Disposition`` value for a raw upload, with the filename escaped.
|
|
80
|
+
|
|
81
|
+
HTTP headers are ASCII. A non-ASCII name (``résumé.pdf``) is sent as an RFC
|
|
82
|
+
5987 ``filename*`` (UTF-8, percent-encoded to the strict attr-char set)
|
|
83
|
+
alongside an ASCII ``filename`` fallback that keeps the extension.
|
|
84
|
+
"""
|
|
85
|
+
cleaned = filename.replace("\r", "").replace("\n", "")
|
|
86
|
+
if cleaned.isascii():
|
|
87
|
+
safe = cleaned.replace("\\", "\\\\").replace('"', '\\"')
|
|
88
|
+
return f'attachment; filename="{safe}"'
|
|
89
|
+
fallback = "".join(c if c.isascii() and c not in '"\\' else "_" for c in cleaned)
|
|
90
|
+
encoded = quote(cleaned, safe="!#$&+-.^_`|~")
|
|
91
|
+
return f"attachment; filename=\"{fallback}\"; filename*=UTF-8''{encoded}"
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def extract_status_error_details(status_data: dict[str, Any]) -> str:
|
|
95
|
+
"""Best human-readable error from a failed job status payload."""
|
|
96
|
+
for key in ("error", "detail", "details"):
|
|
97
|
+
value = status_data.get(key)
|
|
98
|
+
if not value:
|
|
99
|
+
continue
|
|
100
|
+
if isinstance(value, dict):
|
|
101
|
+
nested_error = value.get("error")
|
|
102
|
+
if nested_error:
|
|
103
|
+
return str(nested_error)
|
|
104
|
+
return format_debug_dump(value)
|
|
105
|
+
return str(value)
|
|
106
|
+
return "No details provided."
|