# Quickstart

> Start here: install the SDK, measure base models on your data, train a model in a custom project, evaluate it, promote it and call it.

Part of the Endor docs. Index of every page: https://endor.thetokencompany.com/llms.txt. HTML version: https://endor.thetokencompany.com/docs/quickstart.

This guide trains a model on labeled data in a **custom project**, checks it on rows it didn't train on, puts it in production and calls it. Each code block runs alone.

## 1. Install the SDK

1. Create an API key under **API keys** in the dashboard.
2. Add credits under **Billing**. A new organization has a balance of $0.
3. Install the SDK (0.9.0 or later), and set the key and the API URL:

```bash
pip install endor
export ENDOR_API_KEY=edk_...
export ENDOR_BASE_URL=https://api.endor.thetokencompany.com
```

`ENDOR_BASE_URL` is the API that these docs belong to. Without it, the SDK uses `https://api.endor.thetokencompany.com`. In code, you can give it as `endor.EndorClient(base_url="https://api.endor.thetokencompany.com")`.

## 2. Prepare the data

Download the sample [train.jsonl](https://endor.thetokencompany.com/docs/samples/train.jsonl): 20 support tickets, each with a `department` (choice) and an `urgent` (noul) label. Your own rows have the same shape (see [Data format](https://endor.thetokencompany.com/docs/data-format)).

Keep some rows out of training, to check models on:

```python
import endor

client = endor.EndorClient()
rows = endor.data.load_rows("train.jsonl")
train_rows, heldout_rows = endor.data.split(rows, holdout=0.2)  # the same split each time
client.datasets.upload("heldout", heldout_rows)
```

## 3. Measure base models

```python
import endor

client = endor.EndorClient()
for base in ["mapika/decider-2b", "mapika/decider-4b", "autotrust/gev-26b"]:
    ev = client.evaluations.create(base, "heldout").result()
    print(base, ev.results["overall"]["accuracy"])
```

This is the baseline that training must beat. Smaller base models are faster and cheaper; larger ones start more accurate. See [Choosing a base model](https://endor.thetokencompany.com/docs/training#choosing-a-base-model). A base model without decisions for a while takes a few minutes to answer its first one (see [Cold starts](https://endor.thetokencompany.com/docs/decisions#cold-starts)).

## 4. Train

```python
import endor

client = endor.EndorClient()
train_rows, _ = endor.data.split(endor.data.load_rows("train.jsonl"), holdout=0.2)
train = endor.data.rows_to_datums(train_rows)

with client.custom_models.create("router-1", base_model="mapika/decider-2b") as run:  # waits for a GPU
    steps = endor.Steps()
    for epoch in range(3):
        for batch in endor.data.batches(train, 8, seed=epoch):
            steps.add(run.forward_backward(batch), run.optim_step(learning_rate=1e-4))
    steps.wait()
    run.save_checkpoint().result()  # saves the model "router-1", archived
# the end of the "with" block closes the run and releases the GPU
```

The run takes seconds to start when a GPU for its base model is warm, and a few minutes when one has to start. Endor bills the GPU time of your training calls, by the second.

Each save is a new model with its own name. To train again, save under a new name, for example `router-2`.

## 5. Evaluate it

```python
import endor

client = endor.EndorClient()
print(client.evaluations.create("router-1", "heldout").result().results["overall"]["accuracy"])
```

Compare it with the baseline from step 3.

## 6. Promote it

A saved model is **archived**: you can evaluate and download it, but decisions can't call it yet. Promote it to **production**:

```python
import endor

client = endor.EndorClient()
client.custom_models.promote("router-1")
```

Decisions can call it at once. The first ones may wait while Endor loads it onto a GPU.

## 7. Call it

```python
import endor
from endor import Choice, Noul

client = endor.EndorClient()
res = client.system_one(
    {"body": "My card was charged twice."},
    {"department": Choice(instructions="Which team must handle `body`?",
                          criteria={"billing": "Payments, invoices, refunds", "technical": "Bugs, errors, outages",
                                    "sales": "Plans, prices, new accounts"}),
     "urgent": Noul(instructions="Does the customer need an answer today?")},
    model="router-1",
)
print(res.choices["department"].choice, res.nouls["urgent"].noul)
```

Ask the questions as the model was trained on them: the same instructions and options.

## Next steps

- [Custom projects](https://endor.thetokencompany.com/docs/training): the training loop, production and archive, save and resume, limits.
- [Base models](https://endor.thetokencompany.com/docs/base-models): limits, rules and prices of each base model.
- [Decisions](https://endor.thetokencompany.com/docs/decisions): question types, images, errors.
- Something unexpected or unclear on the way? `endor.user_report("what you did, what happened, what you expected")` tells the Endor team (see [Reporting problems](https://endor.thetokencompany.com/docs/sdk#reporting-problems)).

## Other pages

- [Overview](https://endor.thetokencompany.com/docs/index.md): What Endor does: you train decision models on your labeled data in custom projects with the SDK, Endor runs the GPUs, and you call them by name. Continuous models are for invited organizations.
- [Core concepts](https://endor.thetokencompany.com/docs/concepts.md): Organizations, continuous models and their production and preview versions, names, base models, custom projects, datasets, runs, evaluations and futures.
- [Custom projects](https://endor.thetokencompany.com/docs/training.md): Custom projects (custom_models in the SDK and API): train a model with the SDK, save, promote to production, call, download; the training loop in detail.
- [Continuous models](https://endor.thetokencompany.com/docs/continuous-learning.md): Continuous models (projects in the SDK and API), only available to invited organizations: create one, call its production version or its preview, auto-promote, promote and roll back versions, feedback, price.
- [Decisions](https://endor.thetokencompany.com/docs/decisions.md): Decisions with the SDK and over HTTP, model names, giving feedback on them, TypeSafe clients, cold starts, errors.
- [Base models](https://endor.thetokencompany.com/docs/base-models.md): The 18 base models: limits, rules, prices and cold starts.
- [Billing & limits](https://endor.thetokencompany.com/docs/billing.md): Prices, the prepaid balance, usage and limits.
- [Data format](https://endor.thetokencompany.com/docs/data-format.md): Decision rows, question types, hard and soft labels, datums and limits: the data for custom projects.
- [API reference](https://endor.thetokencompany.com/docs/api.md): Each /v1 endpoint: method, path, body fields, response fields and errors.
- [Python SDK](https://endor.thetokencompany.com/docs/sdk.md): The Python SDK (0.9.0): client, continuous models (client.projects) and versions, custom projects (client.custom_models), datasets, runs, evaluations, futures, data helpers, recipes, errors, CLI.
- [Updates](https://endor.thetokencompany.com/docs/updates.md): Changes to the API and SDK, newest first, and when old names and endpoints stop working.
- [Index](https://endor.thetokencompany.com/llms.txt): every page and the API schema.
