Metadata-Version: 2.4
Name: causilo-client
Version: 1.0.6
Summary: Python client for the Causilo inference API
Author: Nums AI
License-Expression: LicenseRef-Proprietary
Project-URL: Homepage, https://nums.world
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Operating System :: OS Independent
Classifier: Intended Audience :: Developers
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Typing :: Typed
Requires-Python: >=3.11
Description-Content-Type: text/markdown
Requires-Dist: pandas>=2.0
Requires-Dist: pyarrow>=15
Requires-Dist: requests>=2.32
Requires-Dist: google-auth>=2.28

# causilo-client

Python client for the Causilo inference API. Send a table with the answers
filled in and a table with them blank; get a prediction for every blank row.

## Install

```bash
pip install -U causilo-client
```

Python 3.11 or newer. `pandas`, `pyarrow`, `requests` and `google-auth` are
pulled in automatically.

The import name is `causilo_client`. `causilo` on PyPI is the model package,
and the two can be installed side by side. Code written against the earlier
`radix` name changes the import and the class name: `from causilo_client import
Causilo`, and `Radix(...)` becomes `Causilo(...)`. Nothing else moved.

## Use

```python
import os

from causilo_client import Causilo

cx = Causilo(
    os.environ["CAUSILO_ENDPOINT"],   # https://api.nums.world
    token=os.environ["CAUSILO_KEY"],  # the csl_... key
)

pred = cx.predict(context_df, query_df, target="churned", model="causilo-clf")
```

`context_df` holds the target column; `query_df` does not. The feature columns
must match in name and order across the two.

The hosted API is `https://api.nums.world`, and a key for it (`csl_...`) is
created in the console at https://platform.nums.world; pass it as `token=`. A deployment
of the container that sits behind Google IAM takes a service account key file
instead (`key_file=`), and its endpoint and credential come from whoever
operates it.

For a large answer -- class probabilities for hundreds of thousands of rows,
say -- pass `as_frame=True`. The answer then comes as Parquet and is returned
as a DataFrame (`prediction`, or `prediction_0`, `prediction_1`, ...), which is
several times less to download and much less memory than the same numbers
parsed out of JSON. If an answer is cut off while it is being sent, the client
raises `CausiloIncompleteResponse` and does not retry: the prediction ran and
was counted, so sending it again counts it again.

## Moving from another tabular API

The shapes are close enough that most code moves in an afternoon. `fit` and
`predict` collapse into one `predict(context, query, ...)`: the context table
is the training data with the target column filled in, the query table the
rows to predict without it, and nothing is stored between calls unless you ask
for a context to be kept. The `output_type` names are the usual ones
(`probas`, `preds`, `mean`, `median`, `quantiles`); `quantiles` must lie
strictly inside (0, 1), and some APIs accept the endpoints. Tables travel as
Parquet rather than CSV or JSON, so pass DataFrames and let the client encode
them. Datetime, timedelta, Period and decimal columns are refused with a clear
message rather than silently mishandled; split dates into numeric parts first.
One model knob is a request parameter: `n_estimators=` (1 to 8) on
`predict`, `predict_with_metadata`, `predict_cached` and `to_batch_parquet`
asks for a narrower ensemble than the server's default of 8. It is faster and
changes the numbers, it does not change the charge, and a `CachedContext`
sends it with every later query. Other knobs are not parameters; the server
reports what it applied in `metadata.served_by`.

## Where it works

The client talks to the hosted API and to any deployment of the container
that sits behind a plain HTTPS address: an Azure ML managed endpoint, a load
balancer in front of ECS. It does not talk to a SageMaker
endpoint. SageMaker's runtime API requires AWS-signed requests and routes
only `/invocations` and `/ping`, so `/usage`, `/limits` and `/cancel` do not
exist there; use `boto3` as shown in `deploy/aws/README.md`.

Every path this client calls carries `/v1` as of 0.11.0: `POST /v1/predict`
and the rest. A container older than image 0.12.17 does not answer those
names, so pointing 0.11.0 or later at one gives a 404 on the first call --
update the container. The releases that used the unversioned names (0.10.1,
0.10.2) are no longer on PyPI. The hosted API at
`https://api.nums.world` answers the `/v1` names only.

## What else is there

| | |
|---|---|
| `predict_with_metadata(...)` | The prediction and the metadata the server attached: the settings it applied, `classes` for the column order of a probability matrix, `served_by` naming the image and the weights that answered, `quota` with what is left of the month |
| `predict_cached(...)` | The same first call, plus a handle. Use it when the same context table will be queried again: the server keeps the context table and later queries send only the query rows. That saves the upload, not the encoding and not the charge: the table is encoded again on every call, and cells are counted over context and query rows together. Returns `(prediction, handle)` |
| `CachedContext` | The handle. `handle.predict(query)` for each later question, `handle.release()` when finished, or use it as a context manager. If the server's copy has gone -- it expired, or the account stored more contexts than it may hold and this was the oldest -- the handle re-sends the table and carries on rather than raising |
| `list_contexts()`, `release_context(id)` | What this credential is holding, and letting one go by id |
| `usage()` | The month's figures for this credential without spending any of it; `metered: false` when no cap applies |
| `health()`, `wake()` | Whether the service is ready; `wake()` waits for a cold instance to come up before a first call |
| `to_batch_parquet(...)` | Packs a context and a query table into the single Parquet file a batch job takes |
| `MODELS` | The model keys this version knows |

Every failure is a `CausiloError` with `.status`, `.error_code` and
`.request_id`, the identifier to quote when reporting a problem. The subclasses
say what to do: `CausiloValidationError` (fix the request), `CausiloTooLarge`
(the message names what fits), `CausiloQuotaExceeded` (a cell quota is spent;
`.scope` is `caller`, `daily` or `tenant`, and for an organisation's key also
`member`, `workspace` or `workspace_daily`, with `.limit`, `.used`,
`.resets_at`), `CausiloRateLimited` (too many requests in one minute or hour;
the client already waited out `Retry-After` three times before raising it, and
`.scope`, `.limit`, `.used`, `.resets_at` say which window), `CausiloOverloaded`
(retry once the queue drains; `CausiloRateLimited` is a subclass of it),
`CausiloUnavailable` (already retried three times), `CausiloAuthError` (the
credential was refused) and `CausiloTimeout`. A `402 payment_required` (a
personal account's free cells are spent and its prepaid credits do not cover
the call) arrives as a plain `CausiloError` with that `.error_code`. Timeouts are the one failure the
client will not retry: the server is still computing, so instead it tells the
server the call was abandoned (`POST /v1/cancel`), and `.refunded` says how
many cells came back. A call whose prediction had not started yet is not run
and is not counted; one already running when your timeout fires runs to the
end and is counted in full, and the message says which. A connection that
breaks after the table went out is handled the same way: cancelled, not sent
again, since a resend would be charged again. A `502` or a `504` is never
resent either. The server keeps no answer to send twice. The server's own
limit is different: a call that cannot finish within the time a request waits
(900 seconds, the server stopping the work about 90 seconds before that) is
stopped, nothing is counted, and the client raises `CausiloTimeout` from the
server's 504. The default `timeout` is
900 seconds, so that 504 arrives before it fires. The `RadixError` family of names from
releases before 0.8.19 remains importable as aliases of the same classes, so an
`except RadixError` written earlier still catches everything.

The integration guide covers the call arguments, the input rules, the response
metadata, what each error means, and what the client does on your behalf when a
call times out.
