> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sqwish.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Run Kettei on your Mac

> Run Kettei Core and Kettei Dot on your own Mac with the Python client and ippon. No API key, and your requests stay on the Mac.

The Python client can run Kettei Core and Kettei Dot on your own Mac with Apple silicon. `Kettei` takes the same `decide` call as `OneHot`, `model` included, and gives the answer in the same shape. You need no API key, your requests stay on the Mac, and each call is free.

[ippon](https://github.com/sqwishai/ippon), Sqwish's open-source runtime for decision models, runs the model on the Mac's GPU. The client installs it for you.

| | On your Mac | Through the API |
| - | - | - |
| Models | Kettei Core and Kettei Dot | Kettei Core, Kettei Chub, Kettei Fat and your fine-tuned models |
| API key | Not needed | Needed |
| Your requests | Stay on the Mac | Go to Sqwish |
| Cost | Free | [Per input token](https://docs.sqwish.ai/guides/pricing.md) |
| Images | Not read | With Kettei Chub and Kettei Fat |

## What you need

* A Mac with Apple silicon (M1 or later).
* Python 3.11 or newer, running natively on Apple silicon. A Python that runs under Rosetta can't use the GPU. To check, run `python3 -c "import platform; print(platform.machine())"`. It prints `arm64` when Python runs natively.
* Space on disk and in memory for the model. [ippon's models table](https://github.com/sqwishai/ippon#models) gives the size of each model and precision.

## Install

```bash theme={null}
pip install "sqwishai[mac]"
```

The `mac` extra installs ippon and MLX, Apple's array library, beside the client. On other computers the extra installs nothing more. To run Kettei on Linux or Windows, see [Linux and Windows](#linux-and-windows).

## Make a first call

This loads Kettei Core, asks one yes-or-no question and prints the probability of yes.

```python theme={null}
from sqwishai import Kettei

with Kettei("kettei-core") as kettei:
    result = kettei.decide(
        context="I was charged twice for my subscription this month.",
        decisions=[{
            "id": "refund", "kind": "binary",
            "question": "Should this customer get a refund?",
        }],
    )

print(result["decisions"]["refund"]["probabilities"]["yes"])
```

The first call downloads the model from Hugging Face ([Sqwish/Kettei-Core](https://huggingface.co/Sqwish/Kettei-Core)) into the Hugging Face cache. After that, the model loads from the cache. [Licences](#licences) names the licence it comes under.

Loading takes a few seconds, so load the model once and keep it for all your calls. The `with` block frees the model's memory at its end. Without `with`, call `kettei.close()`.

The decisions take the same kinds and fields as on the API. [Decision types](https://docs.sqwish.ai/guides/decision-types.md) and [Write decisions](https://docs.sqwish.ai/guides/writing-decisions.md) explain them.

## Choose Kettei Core or Kettei Dot

| Model | Name |
| - | - |
| Kettei Core | `kettei-core` |
| Kettei Dot | `kettei-dot` |

* Use Kettei Core for the best answers. It is the model that the API answers with when a request names no `model`.
* Use Kettei Dot when speed or memory counts most. It is smaller and faster than Kettei Core.

`Kettei()` with no name loads Kettei Dot. [ippon's benchmarks](https://github.com/sqwishai/ippon/blob/main/docs/benchmarks.md) give the time of a call on each model.

## Choose a precision

`precision` sets how the model is stored and computed in memory.

| `precision` | What it does |
| - | - |
| `"auto"` | ippon computes in fp16 when the model's values fit in fp16, and in bf16 when they do not. If a request overflows fp16, ippon answers it again in bf16. This is the default. |
| `"q8"` | 8-bit weights, in about half the memory |
| `"fp16"`, `"fp32"` | The model in that format |
| `"bf16"` | The model in bf16. Its probabilities are further from the creators' reference than `"auto"`'s, past ippon's bound on some Kettei Dot requests, so use `"auto"` when you can. |

A name with `:q8`, such as `Kettei("kettei-core:q8")`, does the same as `precision="q8"`. Use 8-bit weights when the model does not fit your Mac's memory. Their probabilities are further from the creators' reference, past ippon's bound on some requests, such as Kettei Core's questions with the most options. The top answer stayed the same in every request ippon checked.

ippon checks each model against its creators' code. [ippon's accuracy page](https://github.com/sqwishai/ippon/blob/main/docs/accuracy.md) gives how closely the probabilities match at each precision.

## Move between your Mac and the API

`Kettei` and `OneHot` take the same request and give the answer in the same shape, so one piece of code can use either. This example uses the Mac when `SQWISH_LOCAL` is set, and the API when it is not.

```python theme={null}
import os

from sqwishai import Kettei, OneHot


def decision_client():
    if os.environ.get("SQWISH_LOCAL"):
        return Kettei("kettei-core")
    return OneHot()


with decision_client() as client:
    result = client.decide(
        context="I was charged twice for my subscription this month.",
        decisions=[{
            "id": "refund", "kind": "binary",
            "question": "Should this customer get a refund?",
        }],
        model="kettei-core",
    )
```

`Kettei` takes `model` too, so shared code can keep it. A `Kettei` answers only for the model that it loaded. It refuses the other Kettei size with `invalid_request`, and a name that it does not run with `not_found`, such as a typo, `kettei-chub` or `kettei-fat`.

The answer's `model` is the name that the API uses, such as `kettei-core`.

The API may apply a default temperature, and its answer's `temperature` names it. A model on your Mac applies none unless the request gives one. To get comparable probabilities in both places, send the same `temperature` in each request. [Temperature](https://docs.sqwish.ai/guides/interpreting-results.md#temperature) explains it.

## Share one model

One `Kettei` can be shared by the threads of a program, and it answers their calls one at a time.

To serve many callers at once, or callers in other programs and languages, run the model as a local server. The `ippon` command installs with the client.

```bash theme={null}
ippon serve kettei-core
```

The server answers the same decide request at `http://127.0.0.1:8000/v1/decide`, with no key.

```bash theme={null}
curl -s http://127.0.0.1:8000/v1/decide -H 'Content-Type: application/json' -d '{
  "context": "I was charged twice for my subscription this month.",
  "decisions": [{"id": "refund", "kind": "binary", "question": "Should this customer get a refund?"}]
}'
```

[ippon's README](https://github.com/sqwishai/ippon#readme) explains the server and its other commands.

## Licences

The weights download under the [Kettei Community Model License](https://huggingface.co/Sqwish/Kettei-Core/blob/main/LICENSE). When you download or use the model, you accept that licence. Nothing asks you to confirm it, so scripts and agents never wait for input.

## Errors

A refused request or a model that can't run raises `KetteiError`, with a `code` and a `message`. Where the API refuses the same request, the code is the API's code.

| Code | What happened | What to do |
| - | - | - |
| `invalid_request` | The request is malformed, it names the other Kettei size, or this computer can't run the model (an Intel Mac, or Python under Rosetta) | Correct what the message names. On an Intel Mac, use `OneHot` |
| `not_found` | Kettei does not run a model with this name, or the model runs only through the API | Check the name. For `kettei-chub` and `kettei-fat`, use `OneHot` |
| `context_too_long` | The request has more than 65,536 input tokens, or a decision is longer than the model takes on this Mac | Shorten the context or the decision that the message names. Nothing is cut for you |
| `images_not_supported` | The request has images | Use `OneHot` with `kettei-chub` or `kettei-fat` |
| `insufficient_memory` | The model does not fit this Mac's memory at this precision | Use `precision="q8"`, Kettei Dot, or close other apps |
| `insufficient_disk` | The disk has no space for the download | Free space on the disk, then load again |
| `download_failed` | The download stopped or is incomplete | Load again. The download continues from where it stopped |
| `runtime_unavailable` | Part of ippon or MLX is missing or can't load | Install or repair what the message names |
| `runtime_error` | ippon stopped for another reason | Read the message, which names the cause |

When ippon is not installed, loading a model raises `ImportError` with the command to install it.

## Linux and Windows

On Linux and Windows, `Kettei` runs Kettei Core and Kettei Dot with PyTorch, on an NVIDIA GPU when the computer has one.

```bash theme={null}
pip install "sqwishai[local]"
```

`device` chooses where the model runs. `"auto"` (the default) uses CUDA when it is available and the CPU when it is not. `"cpu"`, `"cuda"` and `"cuda:N"` ask for one. PyTorch runs the weights as published, so `precision` and the `:q8` names are for Macs only. On a Mac, `device` takes `"auto"` or `"mlx"`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.