Metadata-Version: 2.4
Name: aamio-ollama
Version: 0.1.0
Summary: A local Ollama model as an aamio partner: known keys ask over a sealed transport, the model on this machine answers, and a queue in SQLite makes sure no question is answered twice.
Author: AI SENSE AS
License: MIT
Project-URL: Homepage, https://aamio.at
Project-URL: Source, https://github.com/aisenseapi/aamio-python/tree/main/programs/ollama
Project-URL: Issues, https://github.com/aisenseapi/aamio-python/issues
Keywords: aamio,agents,ollama,llm,local,offline
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: aamio<0.8,>=0.7.4
Dynamic: license-file

# aamio-ollama

A local Ollama model as an aamio partner. Keys you name ask over a sealed
transport, the model on your machine answers, and a queue in SQLite makes sure
no question is answered twice.

A device without a language model, such as a handheld field terminal or
another agent, asks a machine nearby that has one. The machine's owner decides
which keys may ask. Ollama's own port never leaves the machine: questions and
answers go as aamio messages, sealed and signed, over a transport the two
share, such as the local network, or over aamio.at when you allow it.

On one PC where you type to Ollama yourself, plain Ollama is enough.

An answer shows that the model gave this answer to this question, and nothing
more. The model knows only what the question says. It reads no files, calls no
tools and switches nothing.

aamio-ollama is a program on top of aamio, not an add-on under contract 2. It
holds its home for as long as it runs, as `aamio serve` does, and `aamio addon
list` does not show it.

## Install

```powershell
pip install aamio-ollama
```

It needs Python 3.11 or later, aamio 0.7.4 or later, and Ollama running on the
same machine.

## Set up two machines

On the machine with the model, in PowerShell. The key of the terminal is what
`aamio whoami` says on the terminal.

```powershell
ollama pull gemma3:1b
pip install aamio-lan
aamio --home C:\aamio\model-partner init
aamio --home C:\aamio\model-partner addon enable lan
aamio --home C:\aamio\model-partner partner add terminal KEY_OF_THE_TERMINAL
aamio --home C:\aamio\model-partner transport bind terminal lan
```

Then a file `partner.toml` beside it:

```toml
home = "C:/aamio/model-partner"
model = "gemma3:1b"
allowed_keys = ["KEY_OF_THE_TERMINAL"]
```

`doctor` checks the file, the home, each key's route, Ollama and the model,
and with `--probe` asks the model one test question. `run` answers until
Ctrl+C:

```powershell
aamio-ollama doctor --config partner.toml --probe
aamio-ollama run --config partner.toml
```

On the terminal, with the key of the model partner from `aamio --home
C:\aamio\model-partner whoami`:

```powershell
pip install aamio-ollama aamio-lan
aamio --home C:\aamio\terminal init
aamio --home C:\aamio\terminal addon enable lan
aamio --home C:\aamio\terminal partner add model KEY_OF_THE_MODEL_PARTNER
aamio --home C:\aamio\terminal transport bind model lan
aamio-ollama ask --home C:\aamio\terminal model "What does E12 mean? E12 = low voltage."
```

`ask` prints the answer. When none comes within `--wait` seconds, it says
the question's id. `--id` with that id asks the same question again and gets
the answer the partner made already, without the model working again.

## The messages

An ask is an ordinary aamio message to the partner:

```json
{"text": "What does E12 mean? E12 = low voltage.",
 "data": {"ollama": {"v": 1, "kind": "ask", "id": "0123456789abcdef0123456789abcdef"}}}
```

The answer goes back to the key that asked:

```json
{"text": "E12 means the supply voltage is too low.",
 "re": "<sha256 of the ask's envelope>",
 "data": {"auto": "replied", "ollama": {"v": 1, "kind": "answer", "id": "0123456789abcdef0123456789abcdef"}}}
```

- **The id:** the one who asks picks a fresh id of 32 lowercase hex characters
  for each question. It uses the id again only to ask the same question again.
- **One answer per question:** the partner answers a question once. The same id
  again gets the same answer, without the model.
- **Conflicts:** the same id with another question is never answered.
- **Errors:** `kind` is `error` when the model could not answer. A `code` says
  why, such as `unreachable`, `timeout`, `no_model` or `cloud`.
- **The program mark:** `auto` marks the answer as sent by a program. aamio-ollama
  never answers a message with that mark, so two partners that both answer by
  themselves do not keep each other going.

Without aamio-ollama on the terminal, the plain aamio command line asks as
well:

```powershell
aamio --home C:\aamio\terminal send model "What does E12 mean?" --data '{"ollama":{"v":1,"kind":"ask","id":"0123456789abcdef0123456789abcdef"}}'
```

## What is answered

Only a message that meets all of these:

- It comes from a key in `allowed_keys` that is also a partner in the home.
- It is verified, sealed and no replay.
- It is a version 1 ask with a valid id.
- It does not carry the program mark.
- It is at most `max_question_bytes` long.

Everything else is dropped without an answer, and counted by reason.
`doctor` shows the counts, and the log says each one, without the text.

An answer goes only over a sealed route. When the partner has no transport,
the answer goes over the aamio service only when `allow_https` is true.

## Delivery and restarts

The queue keeps one job per key and question id:

| State | What it means |
|---|---|
| received, generating | waiting for the model, or with it |
| ready, sending | the answer is made and goes now |
| submitted | the transport still carries it; its outbox says how it ends |
| delivered | the partner's aamio took it in and said so with a receipt |
| uncertain | it may or may not have arrived |
| failed | it cannot go, as with no sealed route to the partner |

**Receipts of kind 3 and 4 (contract 3, A7.1):**

- **Kind 3:** the partner's aamio never stored the answer, as when it started
  again and its session had expired. The answer goes again at once in a new
  session, up to `resend_not_stored` times.
- **Kind 4:** the partner's aamio no longer knew the session, so the answer
  may have arrived. It is not sent again by itself.
- **No receipt:** when its tries end without a receipt, the answer is also
  uncertain and not sent again by itself.

When the same question comes again under its id, the answer goes again.

**A stop:**

- **While the model works:** the question goes to the model again at the next
  start.
- **While an answer is being sent:** it is uncertain, and goes again when it is
  asked for.

The worker never sends an uncertain answer by itself.

A question can be lost in one short window: after aamio has handed it over,
and before the queue has written it down. A client that asks again under the
same id after a while gets the answer all the same. `aamio-ollama ask` does
that every `--every` seconds.

## Configuration

| Key | Default | What it sets |
|---|---|---|
| `home` | needed | the aamio home that answers, the worker's own; relative to the file |
| `model` | needed | the model, exactly as `ollama list` names it |
| `allowed_keys` | `[]` | the keys that may ask; each must also be a partner in the home |
| `allow_https` | `false` | answer a partner with no transport over the aamio service |
| `host` | aamio.at | the aamio service the home uses |
| `system` | answer briefly, in the question's language | the system message the model gets |
| `ollama_port` | `11434` | Ollama's port on 127.0.0.1 |
| `model_timeout` | `120` | seconds the model may take for one answer |
| `model_tries` | `2` | tries per question when the model fails in a way that can pass |
| `num_predict`, `temperature`, `keep_alive` | `160`, `0.2`, `"30m"` | passed to Ollama as they are |
| `max_question_bytes` | `4096` | the longest question taken |
| `max_answer_bytes` | `2000` | the longest answer sent; under it, what the route's profile takes |
| `max_pending` | `16` | questions waiting for the model at most |
| `max_pending_per_peer` | `2` | of them, from one key at most |
| `send_wait` | `30` | seconds a send waits for its receipt |
| `resend_not_stored` | `2` | sends again after a receipt of kind 3 |
| `resend_gap` | `30` | seconds before the same question can make the answer go again |
| `keep_hours` | `48` | hours a job is kept after it ended |

A key the file does not know is refused, so a misspelt limit never quietly
falls back to its default.

An answer longer than its room is cut between characters, with an ellipsis.
The room is `max_answer_bytes`, or less when the route's profile takes less.
On a slow radio, set the profile's `--max-content` and a short `num_predict`.

## When the model does not answer

`doctor --probe` asks the model one test question. When Ollama answers HTTP
500, its own log says why.

A model that cannot load is one cause. Take a machine whose NVIDIA driver is
older than the one Ollama's CUDA build needs: the model fails to load with a
CUDA error such as "the provided PTX was compiled with an unsupported
toolchain". Start the Ollama server with `CUDA_VISIBLE_DEVICES=-1`, and it runs
the model on the processor instead. A small model such as `gemma3:1b` answers
on a desktop processor too.

## What is kept

The queue is `ollama/jobs.sqlite3` in the home:

- **The question's text:** kept only until the model has answered it.
- **Its sha256 and the answer:** kept until `keep_hours` after the job ended.

The log names ids and partners, never a question or an answer. The home's own
archive, when it is on, keeps messages as aamio does.

A loopback address says where Ollama listens, not where it runs a model:
Ollama can run a model in its cloud. Before each question the worker asks
Ollama about the model (`/api/show`), and the question goes only to a model
whose weights are on this machine. A model Ollama runs in its cloud is never
asked, and the asker gets the error `cloud`. `run` does not start with one,
and `doctor` says so. Starting the Ollama server with `OLLAMA_NO_CLOUD=1`
refuses cloud models there as well.

## The home is the worker's own

The worker holds its home for as long as it runs. An aamio command on the same
home says that the home is in use. Stop the worker to add a partner or change a
route, and start it again.

The worker reads every message that comes to the home. Give it a home of its
own, not one you read yourself.

## Limits of 0.1.0

- **One at a time:** one question goes to the model at a time, and text only,
  with one model.
- **The field terminals' own format:** a `body.once` of 16 hex characters
  instead of a version 1 ask is not taken.
- **A late ending:** a transport can still carry an answer when its send
  returns. When it ends without a receipt after that, it is uncertain, and the
  reason is not kept.
