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
- Create an API key under API keys in the dashboard.
- Add credits under Billing. A new organization has a balance of $0.
- Install the SDK (0.9.0 or later), and set the key and the API URL:
pip install endor
export ENDOR_API_KEY=edk_...
export ENDOR_BASE_URL=https://api.endor.thetokencompany.comENDOR_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: 20 support tickets, each with a department (choice) and an urgent (noul) label. Your own rows have the same shape (see Data format).
Keep some rows out of training, to check models on:
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
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. A base model without decisions for a while takes a few minutes to answer its first one (see Cold starts).
4. Train
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 GPUThe 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
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:
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
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: the training loop, production and archive, save and resume, limits.
- Base models: limits, rules and prices of each base model.
- 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).