Metadata-Version: 2.4
Name: nativecluster
Version: 0.1.1
Summary: NativeCluster's kit for Python apps: the app's half of the host API
Author: NativeCluster
License-Expression: Apache-2.0
Project-URL: Homepage, https://www.nativecluster.com
Project-URL: Documentation, https://www.nativecluster.com/docs/apps#apps-your-group-builds
Keywords: nativecluster,local-llm,private-ai,self-hosted,openai
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Operating System :: MacOS
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Topic :: Software Development :: Libraries
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: openai
Requires-Dist: httpx>=0.23; extra == "openai"
Dynamic: license-file

# nativecluster

The Python kit for building [NativeCluster](https://www.nativecluster.com)
apps.

NativeCluster is a free Mac app that runs open AI models on a group's own
Macs. An app made for it is a program NativeCluster installs and runs for the
group's people: it serves its pages behind NativeCluster's sign-in, and every
model call it makes waits its turn in the cluster's one line and runs on one
of the group's own Macs. This kit is the app's side of that:

- **Serving:** a WSGI app (Flask, Django, a plain function) or an ASGI one
  (FastAPI, Starlette) runs behind NativeCluster with one line,
  `run_wsgi` or `run_asgi`, which checks NativeCluster's signed ticket on
  every request.
- **People:** `person_of(request)` is the person NativeCluster signed in,
  read from that ticket and nothing else.
- **Models:** `models().chat(<task>, messages)` asks the group's models
  through the line. Or keep OpenAI's Python library and hand it
  `transport.http_client()`.
- **Schedules and background work:** `host()` sets the app's schedules,
  events and loops, and `run_firing` does the work when one comes due.
- **Data:** `data_dir()` is the app's own folder.

You don't need this kit to call a cluster's models from a script or tool.
Any OpenAI-compatible client works with NativeCluster's local address,
`http://localhost:8487/v1`: see [the API docs](https://www.nativecluster.com/docs/api).

## Install

```sh
pip install nativecluster
pip install "nativecluster[openai]"   # for nativecluster.transport, with OpenAI's library
```

Python 3.9 or newer. The kit itself needs nothing else.

`ncdev`, NativeCluster's tool for building apps, comes with NativeCluster.
`ncdev new <dir> -lang python` writes a whole example app with this kit
inside it, and `ncdev check` holds an app to NativeCluster's rules before it
is installed. An app's package carries its own Python and every dependency,
so install the kit into the Python the app ships with.

## A small app

```python
import nativecluster as nc

def app(environ, start_response):
    person = nc.person_of(environ)
    answer = nc.models().chat("summarize", [
        {"role": "user", "content": "Summarize: NativeCluster runs AI on our own Macs."},
    ])
    start_response("200 OK", [("Content-Type", "text/plain; charset=utf-8")])
    return [f"{person}: {answer.text} ({answer.model})".encode()]

if __name__ == "__main__":
    nc.run_wsgi("nc-app.json", app)
```

`summarize` is one of the tasks the app's manifest (`nc-app.json`) names.
NativeCluster decides which of the group's models may do it. The program
runs only under NativeCluster (`ncdev run` while you build it); started any
other way, it says so and stops.

With OpenAI's library, a call's `model` names one of the app's tasks:

```python
from openai import OpenAI
from nativecluster import transport

client = OpenAI(base_url=transport.BASE_URL, api_key="nativecluster",
                http_client=transport.http_client())
client.chat.completions.create(model="summarize", messages=[...])
```

## Learn more

- [Apps your group builds](https://www.nativecluster.com/docs/apps#apps-your-group-builds)
- [NativeCluster](https://www.nativecluster.com)

## License

This kit is licensed under the Apache License, Version 2.0. NativeCluster
itself is proprietary.
