Resources
Code samples
Python, JavaScript and curl.
Python
import os, requests
def decide(state, questions, model="myles-g-12b", endpoint="https://api.launchables.ai"):
response = requests.post(
f"{endpoint}/v1/decide",
headers={"Authorization": f"Bearer {os.environ['LAUNCHABLES_API_TOKEN']}"},
json={"model": model, "state": state, "questions": questions},
timeout=30,
)
response.raise_for_status()
return response.json()["answers"]
answers = decide(
"Customer bought the item 12 days ago and has no receipt.",
{"refund": {"type": "noul", "instructions": "Is a refund permitted?"}},
)
if answers["refund"]["noul"] >= 0.9:
print("approve")
elif answers["refund"]["noul"] <= 0.1:
print("decline")
else:
print("send to a person")
JavaScript
const response = await fetch('https://api.launchables.ai/v1/decide', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.LAUNCHABLES_API_TOKEN}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'myles-g-12b',
state: { from: 'J. Smith', message: 'Is my parcel lost? It is a week late.' },
questions: {
priority: { type: 'score', instructions: 'How urgent is this?', criteria: ['Routine', 'This week', 'Today'] },
},
}),
});
if (!response.ok) throw new Error((await response.json()).error);
const { answers } = await response.json();
console.log(answers.priority.level, answers.priority.probabilities);
Call the API from your server, not from a web page: the token must stay secret.
The Python SDK and the JavaScript SDK do this for you, with typed answers, retries, error classes and batches.
Retrying with backoff
import time, requests
def decide_with_retry(payload, headers, attempts=4):
for attempt in range(attempts):
response = requests.post("https://api.launchables.ai/v1/decide", json=payload, headers=headers, timeout=30)
if response.status_code not in (429, 502, 503):
response.raise_for_status()
return response.json()
time.sleep(2 ** attempt)
response.raise_for_status()
curl: several questions
curl https://api.launchables.ai/v1/decide \
-H "Authorization: Bearer $LAUNCHABLES_API_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"model": "myles-g-12b",
"state": {"from": "J. Smith", "message": "Charged twice for order 4471, need the extra payment back"},
"questions": {
"route": {"type": "choice", "instructions": "Which team?", "criteria": ["billing", "returns", "technical", "other"]},
"refund": {"type": "noul", "instructions": "Is the customer asking for money back?"},
"priority": {"type": "score", "instructions": "How urgent?", "criteria": ["Routine", "This week", "Today"]}
}
}'