nativecluster 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.
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,5 @@
1
+ # The source distribution on PyPI carries the kit, its license and its
2
+ # PyPI page (PYPI.md) alone: the tests need a fake host and the repository's
3
+ # spec vectors, and README.md is for the repository.
4
+ prune tests
5
+ exclude README.md
@@ -0,0 +1,107 @@
1
+ Metadata-Version: 2.4
2
+ Name: nativecluster
3
+ Version: 0.1.0
4
+ Summary: NativeCluster's kit for Python apps: the app's half of the host API
5
+ Author: NativeCluster
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Homepage, https://www.nativecluster.com
8
+ Project-URL: Documentation, https://www.nativecluster.com/docs/apps#apps-your-group-builds
9
+ Keywords: nativecluster,local-llm,private-ai,self-hosted,openai
10
+ Classifier: Development Status :: 4 - Beta
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: Operating System :: MacOS
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3 :: Only
15
+ Classifier: Topic :: Software Development :: Libraries
16
+ Requires-Python: >=3.9
17
+ Description-Content-Type: text/markdown
18
+ License-File: LICENSE
19
+ Provides-Extra: openai
20
+ Requires-Dist: httpx>=0.23; extra == "openai"
21
+ Dynamic: license-file
22
+
23
+ # nativecluster
24
+
25
+ The Python kit for building [NativeCluster](https://www.nativecluster.com)
26
+ apps.
27
+
28
+ NativeCluster is a free Mac app that runs open AI models on a group's own
29
+ Macs. An app made for it is a program NativeCluster installs and runs for the
30
+ group's people: it serves its pages behind NativeCluster's sign-in, and every
31
+ model call it makes waits its turn in the cluster's one line and runs on one
32
+ of the group's own Macs. This kit is the app's side of that:
33
+
34
+ - **Serving:** a WSGI app (Flask, Django, a plain function) or an ASGI one
35
+ (FastAPI, Starlette) runs behind NativeCluster with one line,
36
+ `run_wsgi` or `run_asgi`, which checks NativeCluster's signed ticket on
37
+ every request.
38
+ - **People:** `person_of(request)` is the person NativeCluster signed in,
39
+ read from that ticket and nothing else.
40
+ - **Models:** `models().chat(<task>, messages)` asks the group's models
41
+ through the line. Or keep OpenAI's Python library and hand it
42
+ `transport.http_client()`.
43
+ - **Schedules and background work:** `host()` sets the app's schedules,
44
+ events and loops, and `run_firing` does the work when one comes due.
45
+ - **Data:** `data_dir()` is the app's own folder.
46
+
47
+ You don't need this kit to call a cluster's models from a script or tool.
48
+ Any OpenAI-compatible client works with NativeCluster's local address,
49
+ `http://localhost:8487/v1`: see [the API docs](https://www.nativecluster.com/docs/api).
50
+
51
+ ## Install
52
+
53
+ ```sh
54
+ pip install nativecluster
55
+ pip install "nativecluster[openai]" # for nativecluster.transport, with OpenAI's library
56
+ ```
57
+
58
+ Python 3.9 or newer. The kit itself needs nothing else.
59
+
60
+ `ncdev`, NativeCluster's tool for building apps, comes with NativeCluster.
61
+ `ncdev new <dir> -lang python` writes a whole example app with this kit
62
+ inside it, and `ncdev check` holds an app to NativeCluster's rules before it
63
+ is installed. An app's package carries its own Python and every dependency,
64
+ so install the kit into the Python the app ships with.
65
+
66
+ ## A small app
67
+
68
+ ```python
69
+ import nativecluster as nc
70
+
71
+ def app(environ, start_response):
72
+ person = nc.person_of(environ)
73
+ answer = nc.models().chat("summarize", [
74
+ {"role": "user", "content": "Summarize: NativeCluster runs AI on our own Macs."},
75
+ ])
76
+ start_response("200 OK", [("Content-Type", "text/plain; charset=utf-8")])
77
+ return [f"{person}: {answer.text} ({answer.model})".encode()]
78
+
79
+ if __name__ == "__main__":
80
+ nc.run_wsgi("nc-app.json", app)
81
+ ```
82
+
83
+ `summarize` is one of the tasks the app's manifest (`nc-app.json`) names.
84
+ NativeCluster decides which of the group's models may do it. The program
85
+ runs only under NativeCluster (`ncdev run` while you build it); started any
86
+ other way, it says so and stops.
87
+
88
+ With OpenAI's library, a call's `model` names one of the app's tasks:
89
+
90
+ ```python
91
+ from openai import OpenAI
92
+ from nativecluster import transport
93
+
94
+ client = OpenAI(base_url=transport.BASE_URL, api_key="nativecluster",
95
+ http_client=transport.http_client())
96
+ client.chat.completions.create(model="summarize", messages=[...])
97
+ ```
98
+
99
+ ## Learn more
100
+
101
+ - [Apps your group builds](https://www.nativecluster.com/docs/apps#apps-your-group-builds)
102
+ - [NativeCluster](https://www.nativecluster.com)
103
+
104
+ ## License
105
+
106
+ This kit is licensed under the Apache License, Version 2.0. NativeCluster
107
+ itself is proprietary.
@@ -0,0 +1,85 @@
1
+ # nativecluster
2
+
3
+ The Python kit for building [NativeCluster](https://www.nativecluster.com)
4
+ apps.
5
+
6
+ NativeCluster is a free Mac app that runs open AI models on a group's own
7
+ Macs. An app made for it is a program NativeCluster installs and runs for the
8
+ group's people: it serves its pages behind NativeCluster's sign-in, and every
9
+ model call it makes waits its turn in the cluster's one line and runs on one
10
+ of the group's own Macs. This kit is the app's side of that:
11
+
12
+ - **Serving:** a WSGI app (Flask, Django, a plain function) or an ASGI one
13
+ (FastAPI, Starlette) runs behind NativeCluster with one line,
14
+ `run_wsgi` or `run_asgi`, which checks NativeCluster's signed ticket on
15
+ every request.
16
+ - **People:** `person_of(request)` is the person NativeCluster signed in,
17
+ read from that ticket and nothing else.
18
+ - **Models:** `models().chat(<task>, messages)` asks the group's models
19
+ through the line. Or keep OpenAI's Python library and hand it
20
+ `transport.http_client()`.
21
+ - **Schedules and background work:** `host()` sets the app's schedules,
22
+ events and loops, and `run_firing` does the work when one comes due.
23
+ - **Data:** `data_dir()` is the app's own folder.
24
+
25
+ You don't need this kit to call a cluster's models from a script or tool.
26
+ Any OpenAI-compatible client works with NativeCluster's local address,
27
+ `http://localhost:8487/v1`: see [the API docs](https://www.nativecluster.com/docs/api).
28
+
29
+ ## Install
30
+
31
+ ```sh
32
+ pip install nativecluster
33
+ pip install "nativecluster[openai]" # for nativecluster.transport, with OpenAI's library
34
+ ```
35
+
36
+ Python 3.9 or newer. The kit itself needs nothing else.
37
+
38
+ `ncdev`, NativeCluster's tool for building apps, comes with NativeCluster.
39
+ `ncdev new <dir> -lang python` writes a whole example app with this kit
40
+ inside it, and `ncdev check` holds an app to NativeCluster's rules before it
41
+ is installed. An app's package carries its own Python and every dependency,
42
+ so install the kit into the Python the app ships with.
43
+
44
+ ## A small app
45
+
46
+ ```python
47
+ import nativecluster as nc
48
+
49
+ def app(environ, start_response):
50
+ person = nc.person_of(environ)
51
+ answer = nc.models().chat("summarize", [
52
+ {"role": "user", "content": "Summarize: NativeCluster runs AI on our own Macs."},
53
+ ])
54
+ start_response("200 OK", [("Content-Type", "text/plain; charset=utf-8")])
55
+ return [f"{person}: {answer.text} ({answer.model})".encode()]
56
+
57
+ if __name__ == "__main__":
58
+ nc.run_wsgi("nc-app.json", app)
59
+ ```
60
+
61
+ `summarize` is one of the tasks the app's manifest (`nc-app.json`) names.
62
+ NativeCluster decides which of the group's models may do it. The program
63
+ runs only under NativeCluster (`ncdev run` while you build it); started any
64
+ other way, it says so and stops.
65
+
66
+ With OpenAI's library, a call's `model` names one of the app's tasks:
67
+
68
+ ```python
69
+ from openai import OpenAI
70
+ from nativecluster import transport
71
+
72
+ client = OpenAI(base_url=transport.BASE_URL, api_key="nativecluster",
73
+ http_client=transport.http_client())
74
+ client.chat.completions.create(model="summarize", messages=[...])
75
+ ```
76
+
77
+ ## Learn more
78
+
79
+ - [Apps your group builds](https://www.nativecluster.com/docs/apps#apps-your-group-builds)
80
+ - [NativeCluster](https://www.nativecluster.com)
81
+
82
+ ## License
83
+
84
+ This kit is licensed under the Apache License, Version 2.0. NativeCluster
85
+ itself is proprietary.
@@ -0,0 +1,139 @@
1
+ """NativeCluster's kit for Python apps: the app's half of the host API
2
+ (spec/hostapi.v1.md), as sdk/go/app is for Go, held to the same ticket
3
+ vectors (spec/ticket-vectors.v1.json) and the same `ncdev check`.
4
+
5
+ A WSGI app (Flask, Django, a plain function) or an ASGI one (FastAPI,
6
+ Starlette) runs behind NativeCluster with one line, its main:
7
+
8
+ nativecluster.run_wsgi(<nc-app.json's path>, app, on_wake=on_firing)
9
+
10
+ It answers `<program> manifest`, reads what the host handed the app, puts
11
+ one ticket check in front of every route, answers the host's readiness
12
+ check, its firings and the conformance probe, serves on the app's socket,
13
+ and leaves when the host does. A handler takes the person from the ticket
14
+ (person_of) and nowhere else, keeps its data in data_dir(), and reaches
15
+ the models only through the host: models().chat(<task>, messages), or
16
+ OpenAI's own library with transport.http_client().
17
+ """
18
+
19
+ from __future__ import annotations
20
+
21
+ import threading
22
+ from typing import Optional
23
+
24
+ from ._firings import TICK_PATH, WAKE_PATH, Firing, WakeAnswer, answer_wake, run_firing
25
+ from ._gate import SCOPE_KEY, Gate, WSGIGate, from_host, person_of, ticket_of
26
+ from ._host import (
27
+ FIRING_DONE_PATH,
28
+ FIRING_RENEW_PATH,
29
+ HAND_MIN_HOST,
30
+ HAND_PATH,
31
+ MAX_SECRET_VALUE_BYTES,
32
+ MAX_SECRETS_STORE_BYTES,
33
+ SECRETS_MIN_HOST,
34
+ SECRETS_PATH,
35
+ Away,
36
+ HostClient,
37
+ NoMain,
38
+ SecretsFull,
39
+ SecretTooLarge,
40
+ SecretUnreadable,
41
+ StopFiring,
42
+ at,
43
+ cron,
44
+ every,
45
+ on_event,
46
+ schedule,
47
+ weekly,
48
+ )
49
+ from ._hostsock import BASE_URL, HostError, HostSocket
50
+ from ._launch import (
51
+ APP_SOCKET_ENV,
52
+ BASE_ENV,
53
+ DATA_ENV,
54
+ EXIT_WITH_PARENT_ENV,
55
+ HEALTH_PATH,
56
+ HOST_SOCKET_ENV,
57
+ MANIFEST_ARG,
58
+ PERSON_ENV,
59
+ SECRETS_STDIN_ENV,
60
+ SOCKETS_ENV,
61
+ WHOAMI_PATH,
62
+ Launch,
63
+ NotBehindHost,
64
+ answer_manifest,
65
+ base_path,
66
+ behind_host,
67
+ data_dir,
68
+ launch,
69
+ process_person,
70
+ read_launch,
71
+ sockets_dir,
72
+ watch_parent,
73
+ )
74
+ from ._models import (
75
+ CONTEXT_LENGTH_EXCEEDED,
76
+ KIND_CHAT,
77
+ KIND_EMBEDDING,
78
+ KIND_IMAGE,
79
+ KIND_TRANSCRIPTION,
80
+ KIND_TTS,
81
+ Answer,
82
+ ContextTooLong,
83
+ Grant,
84
+ ModelError,
85
+ Models,
86
+ NoModel,
87
+ NoOrigin,
88
+ Refused,
89
+ Stopped,
90
+ )
91
+ from ._origin import (
92
+ Origin,
93
+ acting_for,
94
+ bind,
95
+ carry_origin_into_threads,
96
+ conversation,
97
+ current_conversation,
98
+ current_origin,
99
+ for_firing,
100
+ for_job,
101
+ for_request,
102
+ otherwise_acting_for,
103
+ when_away,
104
+ )
105
+ from ._serve import listen, run_asgi, run_wsgi, serve_asgi, serve_wsgi
106
+ from ._ticket import TICKET_HEADER, TICKET_TTL_MS, NoTicket, Ticket, TicketKey
107
+
108
+ __version__ = "0.1.0"
109
+
110
+ # MIN_HOST is the oldest NativeCluster whose host answers every route this
111
+ # kit calls (the schedules' put and remove, a firing's lease, loops): an
112
+ # app's manifest that uses them sets "min_host" at least this.
113
+ MIN_HOST = "0.3.5"
114
+
115
+ # The process's clients (not _models or _host: those are this package's
116
+ # modules).
117
+ _lock = threading.Lock()
118
+ _the_models: Optional[Models] = None
119
+ _the_host: Optional[HostClient] = None
120
+
121
+
122
+ def models() -> Models:
123
+ """The models client for this process's launch."""
124
+ global _the_models
125
+ with _lock:
126
+ if _the_models is None:
127
+ l = launch()
128
+ _the_models = Models(HostSocket(l.host_socket, l.credential), test=l.test)
129
+ return _the_models
130
+
131
+
132
+ def host() -> HostClient:
133
+ """The host's other routes for this process's launch."""
134
+ global _the_host
135
+ with _lock:
136
+ if _the_host is None:
137
+ l = launch()
138
+ _the_host = HostClient(HostSocket(l.host_socket, l.credential))
139
+ return _the_host