Download the PHP package bynn/decisionnode without Composer

On this page you can find all versions of the php package bynn/decisionnode. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package decisionnode

DecisionNode PHP SDK

DecisionNode is a decision engine: send it text, JSON or images with a question, and it answers with calibrated probabilities.

The PHP client for the DecisionNode API: ask yes-or-no, choice, score and number questions about any text, JSON, image or video, point to a spot or draw a box in an image, and get probabilities back. One-off decisions, live sessions that answer a stream of frames, and batches for less (Flash 50% off the live price, DecisionNode MoE 30% off). Boosters add specialist readings of your images (an age, whether a picture was AI-generated) to a decision as evidence, and your own fine-tuned models are called by name.

Implements version 1.5 of the DecisionNode SDK specification, which the package states as DecisionNode::SPEC_VERSION (also Bynn\DecisionNode\SPEC_VERSION). Requires PHP 8.2 or later.

Install

Set your API key

Quickstart

Every number is the value the API sent: nothing is rounded, renormalised or thresholded. What 0.94 means for your application is your code's decision.

Questions

Builder Answer Fields
truth($instructions, $ifTrue, $ifFalse) TruthAnswer truth: the probability, 0 to 1, that the statement is true
choice($instructions, $options) ChoiceAnswer choice, confidence, probabilities keyed by option name
score($instructions, $levels) ScoreAnswer score (the expected level index), confidence, probabilities and legend keyed by level index
number($instructions, $min, $max, $step) NumberAnswer number (the most likely value), expected, confidence (the probability of number), probabilities keyed by the value as the API wrote it ("7", "3.5")
point($instructions, $image, $units) PointAnswer x, y, present, confidence, image, width, height (see Points and boxes)
box($instructions, $image, $units) BoxAnswer x1, y1, x2, y2, present, confidence, image, width, height

PHP stores an array key that looks like a whole number ("4") as an integer, so read a number answer's probabilities as $answer->probabilities[4] or $answer->probabilities['4']; a key such as "3.5" stays a string.

Instructions, descriptions and levels can be strings or any JSON value. The builders are also static methods (Question::truth(...)), and a question in its plain wire form is accepted wherever a builder is:

Points and boxes

A point question finds one spot in an image and a box question a rectangle around something, for a robot to pick, a camera to frame or a reviewer to check.

Every field is always there and every number is the one the API sent. Point and box questions work in decide, decideMany and batch requests, beside any other question type. A request with one needs at least one image.

Answers are read by question id, $d['route'] or $d->answers['route']. An answer of a question type this SDK version does not know yet comes back as an UnknownAnswer with its type and raw fields. In decide, so does an answer of a known type whose fields are missing or of the wrong type (a choice without probabilities, a truth whose value is a string): the rest of the decision is read as usual. The one-question helpers check, classify, score and number promise a typed answer, so for them a missing or malformed answer is a ServerError (see below). Every answer class extends Answer.

The client

Method Does Returns
decide($state, $questions, $images, $videos, $model, $boosters, $fallback) One POST /v1/decide Decision
decideMany($requests, $concurrency = 8) One decision per request, at most $concurrency at a time A list of DecisionResult in input order: each a Decision or the DecisionNodeError that request ended with
check($state, $instructions, $ifTrue, $ifFalse) One truth question TruthAnswer; a ServerError when the answer is missing or malformed
classify($state, $instructions, $options) One choice question ChoiceAnswer; a ServerError when the answer is missing or malformed
score($state, $instructions, $levels) One score question ScoreAnswer; a ServerError when the answer is missing or malformed
number($state, $instructions, $min, $max, $step) One number question NumberAnswer; a ServerError when the answer is missing or malformed
gate($action, $context) Whether an action should run: a risk score, a touches_production truth and an allow, confirm or block verdict (wording below) Decision
models() GET /v1/models A list of ModelInfo with name, description, releaseDate, version and limits (every other field of the entry, such as context_tokens, number_max_grid_values, upload_max_bytes, sessions and batch); the prices as pricePerMillionInput, pricePerMillionOutput, pricePerMillionImage, pricePerMillionVideo and batchPriceFactor; your fine-tuned models also have fineTuned, base, adapterVersion and status (see Fine-tuned models)
upload($images) Uploads local images whatever their size One upload id per image
openSession(...) POST /v1/sessions Session (see Sessions)
supervise($body, $feed, $onReply) Keeps a session running over a long feed The last SessionClosed, or null
batches->create(...) and the other batch calls, runBatch(...) The Batch API Batch, BatchResult (see Batches)
decideRaw($body) Sends $body exactly as given The response body as an array

check, classify, score, number and gate also take $images, $videos, $model, $boosters and $fallback, like decide. $state is a string or any JSON value; leaving it out sends an empty string. decideMany checks every request before it sends any: a $concurrency below 1, an argument problem in any one of the requests (an empty question map, say), or a question, image or video the API would refuse throws, and nothing is sent, not even the valid requests.

gate sends the state {"action": ..., "context": ...} and these three questions, word for word the same in every DecisionNode SDK:

Id Type Instructions Levels or options
risk score How much harm could this action cause if it is wrong? none, low, moderate, high, severe
touches_production truth Does this action change a production system, production data or anything customers use?
verdict choice Should this action run as it is, wait for a person to confirm it, or be blocked? allow "safe to run without asking", confirm "a person should confirm it first", block "should not run"

A Decision has answers, model (the resolved model version), usage->inputTokens, usage->imageTokens and usage->videoTokens (see below), usage->outputTokens, usage->videoFrames and usage->videoSeconds (for a request with videos, null otherwise), warnings (a list of strings, empty unless the API sent some), evidence, usage->boosterTokens and usage->boosters (for a request with boosters, null otherwise; see Boosters), usage->cost (what the answer cost in USD, exactly as the API sent it: usd, its parts modelUsd and boostersUsd, and freeTier; read usd rather than adding up the parts; also on a session's open and in batch results; null when absent), requestId, latencyMs (the server's), safety and raw (the parsed body).

usage->imageTokens and usage->videoTokens are the shares of usage->inputTokens that came from images and from videos, read from the x-decisionnode-image-tokens and x-decisionnode-video-tokens headers, so you can bill or budget vision tokens at your own price. Both are ?int: imageTokens is null for a request without images and videoTokens is null for a request without videos (the server sends each header only when its share is above 0), and either is null when its header is not a whole number from 0 to 2^31 - 1. They are read as sent: the client never adds them to, or takes them from, another count, and never prices them. A batch result's decision reads null for both.

decideRaw is the way to use an API field this SDK version does not know yet.

Async

Every HTTP method has an Async form (decideAsync, checkAsync, modelsAsync, uploadAsync, openSessionAsync, runBatchAsync, $dn->batches->getAsync, ...) that returns a promise without blocking. Call ->wait() for the result, or combine several with GuzzleHttp\Promise\Utils::all(). A problem found before sending (an argument problem, or a question or image the API would refuse) is thrown when the method is called, before a promise is made; everything the API answers, and a failed connection, rejects the promise with a DecisionNodeError. A session's socket is blocking.

Images

Send an image by URL when you can. It is the fastest input: nothing is uploaded, the request stays small, and the API fetches the image itself.

A URL must be https, at most 2,048 characters, on port 443, without user information and not a literal private address; the SDK checks that and sends it as it is, without fetching it. Local images work too:

A file or stream is read when the Image is made, so a missing or unreadable file throws InvalidArgumentException there. Before any request, the SDK checks every local image against the API's rules and raises the API's own BadRequestError for a fault, without a round trip: JPEG, PNG or WebP by its first bytes, at most 10 MB, at most 40 megapixels read from the image header (the image is never decoded), ids unique. More than 16 images in a request (URLs included) is the API's ValidationError, raised before sending too. No image is resized or re-encoded.

How a local image travels is chosen for the shortest response time:

A request body may be at most 25 MiB (26,214,400 bytes): inline images and video frames count toward it, uploads and URLs do not. A body over it is refused before anything is sent or uploaded, with a RequestTooLargeError: it is measured as it would go out, an image to upload by its upload id. Set inlineThreshold on the client to change the threshold (0 uploads every local image). upload($images) uploads whatever the size and returns the upload ids, for a pipeline that wants to upload first and decide later.

How many large images fit in one request is the model's token limit, which the server checks: a request over it is a ContextLimitError.

Referring to images and videos

There are two ways to say which image a question is about:

  1. By id (recommended). Give each image a short, meaningful id and use it in the instructions, in backticks: "Does the total on receipt match the amount in the claim?"
  2. By number. Images are numbered from 1 in the order of images: "image 2", "the second image", "Picture 2" and "the last image" all work.

The model sees each image as Picture 1 (receipt):, Picture 2 (claim_photo):, so both ways point to the same image. Prefer ids: they stay right when images are added or reordered. Use names, not digits, as ids (front, not 2). Videos work the same way, by id or by number ("video 1"), numbered separately in the order of videos. The image of a point or box question takes the id.

Videos

A video is sent as frames: still images you already hold, such as a camera's JPEG snapshots, each with its time in seconds. Only the frames go to the API: no video file, no video URL and no sound.

The API reads the frames of a video as one sequence, in order and with their times, and every question sees every video, every image and the state.

Before sending, the SDK checks the rules above and raises the API's own error without a round trip: more than 4 videos is a ValidationError, as the API answers it; a missing or repeated id, no frames, a time that is negative or not larger than the one before it, a media type that is not JPEG, PNG or WebP, an fps outside the range, a negative start or end and an end not after start are a BadRequestError (api_usage_error) with the server's message. How many frames a request may carry, each frame's bytes and size, and the token budget are the server's to judge.

Videos work in decide, the one-question helpers, gate, decideMany, batch requests and checkRequest. Sessions take no videos. A video in its plain wire form, ['id' => 'dock', 'fps' => 2, 'frames' => [['t' => 0.0, 'media_type' => 'image/jpeg', 'data' => $base64]]], is accepted wherever a Video is.

To turn a video file into frames, use the dn command line, which samples the file on your machine and sends only the frames:

Boosters

A booster is a specialist model that reads the request's images (knowledge reads its state and questions, against your own documents) before the decision. Its reading is added to what the model sees as evidence, and comes back in the response. The model still answers every question: a booster never answers one, and a request without boosters is answered exactly as before.

Booster Reads Each reading
age (Booster::AGE) A picture of a person; the largest face is read AgeReading: image, faces, and ageYears, low, high (the range, in years) and confidence, each null when faces is 0
ai_generated (Booster::AI_GENERATED) Any picture AiGeneratedReading: image, generated (the probability that an image generator made it), confidence, method (inference, read from the pixels, or c2pa, the file's Content Credentials say so), topGenerator (['name' => ..., 'probability' => ...], null unless the picture reads as likely generated, and on every c2pa reading), c2paSigner (who signed the file's Content Credentials, null unless they are trusted) and c2pa (untrusted when the file carries Content Credentials that are not trusted, else null)
face_detection (Booster::FACE_DETECTION) Any picture FaceDetectionReading: image, faces (how many it found) and detections, each face's box and confidence (at most 64, so faces can be larger)
face_recognition (Booster::FACE_RECOGNITION) Any picture, searched against a collection: Booster::faceRecognition($collection, $threshold), see Collections FaceRecognitionReading: image, collectionName, faces (how many it searched) and matches by score, each box, score, collection, reference, name (a public figure's) or label, collection_name and the item's metadata
ocr (Booster::OCR) Any picture; Booster::ocr($prompt) asks for something specific OcrReading: image, text (the text, or the answer to the prompt) and truncated
bynn_depth (Booster::DEPTH), Depth Vision Any picture; Booster::depth($gridSize) sets the grid, 2 to 16 cells a side (8) DepthReading, in meters: image, ahead (the nearest obstacle straight ahead, the floor left out; null when none is found), directions (the same in 7 strips from left to right, each null when none), ground (a floor was found and left out), gridRows, gridCols and grid (row by row from the top left), depthMin, depthMax, depthMedian, and the picture's width and height in pixels. Estimates from one picture, for decisions and planning, never a safety stop
bynn_segment (Booster::SEGMENT), Segment Vision Any picture, with what to find: Booster::segment($prompt), a phrase or 1 to 4 phrases of at most 120 characters, and maxInstances (1 to 16, 8), minScore (0 to 1, 0.25), polygonPoints (3 to 64, 24) and depth (true) SegmentReading: image, width, height, prompts, instances by score, each as sent (label, the phrase it matched, score, box, polygon, area, centroid, opening, the largest hole it encloses or null, and with depth on its depth and size_m in meters), depthSource and assumedHfovDeg. Coordinates are fractions of the picture from its top left; distances and sizes are estimates, never a safety stop
knowledge (Booster::KNOWLEDGE) The request's state and questions, never a picture, against one of your knowledge bases: Booster::knowledge($collection, ...), see Knowledge One KnowledgeReading: the passages that apply, pinned first, each citing its document, chunk and byte offsets

$boosters takes up to 4 boosters, each at most once, in the order you give, usually by name (each booster in the table above has its constant, such as Booster::AGE). Names are plain strings, so a booster added to the API later works with this version too. A booster that needs a setting is a Booster:

It is sent as {"name": "ai_generated", "images": ["listing"], "require": true}, and a booster in that wire form (['name' => 'age', 'require' => true]) is accepted too. A Booster with no option set is sent as the bare name.

new Booster(...) argument Sends
name The booster's name.
images The ids of the request's images the booster reads. Left out: every image of the request.
require true: a booster that fails or does not answer in time fails the request with a 529 OverloadedError whose errorType is booster_unavailable, retried like any 529. Left out or false: the decision runs without that reading, and its evidence says why.
options The booster's settings: ocr's prompt, face_recognition's collection and threshold, bynn_depth's grid_size, bynn_segment's prompt, max_instances, min_score, polygon_points and depth, and knowledge's collection and its options. Booster::ocr(), Booster::faceRecognition(), Booster::depth(), Booster::segment() and Booster::knowledge() set them.

$d->evidence holds one Evidence per booster the request named, by name, in the request's order: version (the booster's version, such as age-v3), status (ok; timeout, no answer in its time; error; skipped, none of the request's images is one it reads), latencyMs, billedCalls (the calls of that booster billed on this request) and results, one reading per picture the booster was to read. A reading of a booster this version does not know, or one with a field missing or of the wrong type, is an UnknownReading whose raw holds its fields; the decision does not fail. So is a picture the booster did not read (it timed out or failed: ['image' => 'selfie'], status says why) or could not decode (['image' => 'selfie', 'readable' => false]); neither is billed. Fields a reading adds that this version does not know are ignored. Without boosters, evidence and both usage fields are null.

Boosters read images in every form an image takes: URL, inline bytes and upload. Every question sees every reading. The one-question helpers, gate, decideMany, batch requests and checkRequest take boosters as decide does; sessions do not. The SDK checks nothing about boosters before sending: which boosters exist and which your workspace may use is the API's to answer, before any booster runs and before any charge. Every booster is on for a new workspace, and an owner or admin can turn one off in the console.

Booster calls are billed one per booster and image read with status ok, at the price per call on Pricing. A reading is billed every time a request uses it, one the API had already made for your workspace included; a booster that timed out or failed is free, and a request the safety check refuses pays for none. The readings' tokens are input tokens at the model's price.

Collections

A collection holds the faces of the people you enroll, for decisions to search. (A collection of documents, a knowledge base, is in Knowledge.) $dn->collections creates, lists, reads and deletes the workspace's collections, and enrolls, lists, edits and deletes their items, each result with its fields as sent and raw:

Knowledge

A knowledge base is a collection of your own documents: policies, house rules, manuals, a mission's special instructions. The knowledge booster finds the passages of it that apply to a request and gives them to the decision as facts and rules to apply, with citations back to the document they came from.

Create a knowledge base and add documents

A document is Document::fromText($text, $format) (markdown, the default, or text), Document::fromPath($path), Document::fromData($bytes, $contentType, $filename), Document::fromStream($stream, $contentType, $filename) (a stream resource or a PSR-7 stream, read to its end once) or Document::fromUploadId($uploadId) for a file uploaded before with $dn->uploadDocuments([...]). A file's type comes from $contentType, else from its name's extension, else from its first bytes; DOCX, PPTX, XLSX and text need a type or a file name. The title is the one you give, else the file's name without its extension; text in memory needs one.

A PDF, DOCX, PPTX, XLSX, HTML file or picture is converted to text. Once a document is ready only its text is kept, cut into chunks: the original file is deleted. A small UTF-8 text or Markdown file (under the client's inlineThreshold) is sent inline in one call; every other file is uploaded first, then named by its upload id.

Ingestion is asynchronous: a document comes back queued, then is processing (stage extracting for a converted file, chunking, embedding, with progress), then ready or failed (its error saying why). wait: true on addDocument and replaceDocument asks the server to hold the call up to 10 seconds and answer once the document is ready, else as it stands. waitUntilReady($collectionId, $itemId) reads the document until it is ready or failed and returns it either way; without an item id it waits until no document of the knowledge base is queued or processing, and returns the collection. It waits 1 s between reads at first, growing to 10 s, for at most 300 s (timeout: and pollInterval: change both), then throws TimeoutError.

Call Does
create($name, type: 'text', scopeKey: ...) A knowledge base. Its Collection carries scopeKey, chunks, tokens, version, statusCounts and pinned (the pinned load: tokens, cap, largest_scope)
addDocument($collectionId, $document, title:, reference:, pinned:, pinRank:, label:, metadata:, wait:) Adds a document; every argument is sent only when given
replaceDocument($collectionId, $itemId, $document, ...) Replaces it whole: a new version, the old one serving until the new one is ready; the same text again changes nothing and is free
updateItem($collectionId, $itemId, reference:, label:, metadata:, pinned:, pinRank:, title:) Changes only what you give, at once and free; a new title makes a new version
getItem($collectionId, $itemId, includeText: true) One document; with includeText its stored text in text
listItems($collectionId, ..., q:, sort:), listChunks($collectionId, $itemId, offset:, limit:) The documents (q finds 1 to 200 characters in a title or reference; sort is newest, oldest or title), and one document's chunks (chunk, heading, byteStart, byteEnd, pageStart, pageEnd, tokens, text)
deleteFailed($collectionId) Deletes every failed document at once (a failed document stays listed with its reason until deleted, counting toward nothing) and answers how many: deleted
deleteItem($collectionId, $itemId), delete($collectionId) Deletes a document with its text and chunks, or the whole knowledge base (deleted true, or status purging while the purge finishes in the background)
search($collectionId, $query, scope:, match:, filter:, topK:, pinned:, maxTokens:) The passages a query finds, without a decision

Adding, replacing and changing a document send an Idempotency-Key, the same on every attempt, so they are retried like any call and applied once. A document's item carries title, format, pinned, pinRank, source (article for text, file for a converted file), file (filename, content_type, byte_size, pages, original_held), status, stage, progress, serving, error, delayed, warnings, chunks, tokens, contentSha256, version, queuedAt and readyAt, each as sent.

Pinned rules and scopes

A rule that must always apply is pinned: a pinned document is given to every request that names the knowledge base, ahead of the passages found by search, in pinRank order.

A knowledge base created with a scope key (mission here, fixed for its life) holds global documents and documents of one scope value, given in metadata. A request names the value in scope: it sees the global documents and those of that value, never another's.

A decision with knowledge

Booster::knowledge($collection, query:, scope:, match:, filter:, topK:, maxTokens:, pinned:, minScore:, includeText:, require:) sends {"name": "knowledge", "options": {...}} with every option given, as given; the API checks them. Without query the query is built from the request's questions and state. topK: 0 gives the pinned documents only. require: true fails the request when the retrieval cannot run over a ready knowledge base.

The reading is a KnowledgeReading: collection, collectionName, corpus, query, budget, scope, warnings (such as scope_not_named when a scoped knowledge base was searched without a scope), passages, omitted, pendingItems, search and truncated. Each passage cites its document (item, reference, title), its chunk (chunk, heading) and where it sits in the document's stored text: byteStart to byteEnd, UTF-8 byte offsets, the end exclusive. PHP strings are bytes, so substr($text, $p->byteStart, $p->byteEnd - $p->byteStart) on the text of getItem($kb->id, $p->item, includeText: true) is the passage. score is null for a pinned passage. With includeText: true (a full-scope key) each passage carries its text. A reading with a field missing or of the wrong type is an UnknownReading.

Search

To try a query without a decision, search() returns the same KnowledgeReading, with every passage's text:

Errors and limits

When Raised
require: true on a knowledge base with nothing ready and nothing being processed DecisionNodeError with status 409, errorType knowledge_collection_empty, details['items'] and details['failed']
require: true while documents are still being processed OverloadedError (booster_unavailable) with retryAfter 5, retried like any 529
require: true while ingestion is delayed (details['reason'] ingestion_delayed) OverloadedError with retryAfter 60, raised at once: you decide whether to wait
Too many documents waiting, or too many tokens embedded within the hour RateLimitError with details['reason'] ingest_queue_full or ingest_tokens_per_hour and retryAfter, retried like any 429
A text over 512 KiB once normalised RequestTooLargeError, errorType item_too_large
A text that is not UTF-8 or holds a NUL ValidationError, errorType invalid_text
A pin past the pinned cap; a reference another document has DecisionNodeError with status 409, errorType pinned_budget_exceeded or reference_taken (details['item_id'])
A face collection named by knowledge or search BadRequestError, errorType collection_type_mismatch
search, includeText or a knowledge upload from a key that is not full-scope PermissionDeniedError, errorType key_scope_denied

Checked before anything is sent, as the API answers: a type other than face or text, a scope key not 1 to 40 of a-z 0-9 _, a title not 1 to 200 characters, a missing title, an empty text, a pinRank outside 0 to 1,000, a format other than markdown or text, the reference, label and metadata rules of Collections, and a file that is empty, over 50 MB or not one of the types below.

A file is one of text/plain, text/markdown, application/pdf, DOCX, PPTX, XLSX, text/html, image/png, image/jpeg, image/tiff and image/webp, at most 50 MB; a PDF at most 100 pages; and its text at most 512 KiB once extracted. An upload's PUT has 120 s per attempt unless you set uploadTimeout.

A knowledge call (one retrieval on a decision, or one search) is billed at USD 0.001, whatever it retrieves; ingestion and storage are priced on Pricing.

Per-key usage

Every call is metered on the key that made it. $dn->usage reads each key's calls and cost over a period, with a Full access key or a Manage device keys key, each result with its fields as sent and raw:

Keys for devices

Give every device its own Decisions-only key from your provisioning server, with its own limits, daily spend cap and expiry. $dn->keys takes a Manage device keys key, made in the console and kept on your server; any other key gets a PermissionDeniedError (key_scope_denied). A device asks your server for its key and never calls /v1/keys itself.

Fallback

When no worker can take a request for decisionnode-moe at once, the API answers it with decisionnode-flash rather than make it wait. $d->model is then the model that answered, and $d->fallback says so: a Fallback with servedModel and requestedModel, from the x-decisionnode-fallback and x-decisionnode-requested-model headers; on an ordinary answer it is null. To have such a request wait for its own model instead, pass fallback: false (the helpers, decideMany's requests and BatchRequest take it too): it then gets the 529, an OverloadedError retried like any other. Left out, nothing is sent and the API's default applies.

Fine-tuned models

A model you fine-tune on your own labelled decisions, in the console, is called by its name as $model: decisionnode-flash@acme/fraud (the version deployed most recently) or decisionnode-flash@acme/fraud:3 (one version). The name is sent as given; only your workspace's keys can call it, and a name the API does not know for your workspace is a BadRequestError, "Unknown model: ". $d->model carries the name pinned to the version that answered. models() lists your fine-tuned models beside the catalogue's, with fineTuned true (null for a catalogue model), base (the model it was trained from), adapterVersion (its version number) and status: deployed answers decisions, ready is trained and not deployed.

Sessions

A session reads its state and questions once, then answers every frame you send on its socket: telemetry from a drone, a video feed's latest frame, a chat's latest message. Frames are sent without waiting; when they arrive faster than they are answered, the latest wins and its reply lists the frames it superseded in dropped.

SessionClosed::$reopen is true for the close codes after which a new session should be opened (1001, 1006, 1011, 4404, 4408, 4409, 4410). On 1013 the session is kept and you connect to it again. supervise($body, $feed, $onReply) does all of that for a long-lived feed: it sends each reading as it arrives, hands every reply to $onReply, opens a new session when the server asks for it (at once when the session's time is up or it went idle, after a backoff of 1 to 30 s with jitter otherwise), and when the feed ends waits for the outstanding replies, ends the session and returns the last SessionClosed.

Batches

The Batch API answers many decisions for less than live calls: Flash 50% off the live price, DecisionNode MoE 30% off. There is no set time at which a batch finishes, so poll for it; a request not answered 24 hours after the batch is finalized expires and is not billed. Each model's batch price is its batchPriceFactor in models() (none on a fine-tuned model), and each result line's charge is in its usage. runBatch runs the whole lifecycle:

It makes an empty draft, adds the requests in calls of at most 10 MiB, finalizes, reads the batch every pollInterval seconds (at least 10; the server's Retry-After when that is longer) until it ends, then reads the results. A failed add cancels the draft. Past timeout (26 hours by default) it throws TimeoutError.

Each call is also yours to use, as $dn->batches->...:

Method Sends Returns
create($requests = []) POST /v1/batches, JSON Lines; with no requests, an empty draft Batch
add($batchId, $requests) POST /v1/batches/{id}/requests, split into calls of at most 10 MiB, in order Batch
finalize($batchId) POST /v1/batches/{id}/finalize Batch
get($batchId) GET /v1/batches/{id} Batch
list($offset, $limit) GET /v1/batches BatchPage with batches, offset, limit, total, nextOffset
results($batchId) GET /v1/batches/{id}/results, the whole file A list of BatchResult in the order the requests were added
stream($batchId) The same, read line by line as the body arrives A generator of BatchResult, so a large results file is never held in memory whole
cancel($batchId) POST /v1/batches/{id}/cancel Batch

A request is an array with custom_id and the arguments of decide (state, questions, images, videos, model), or a BatchRequest. Every request is checked before anything is sent, and each line's local images follow the image rules above; a line's videos go with it, frames inline. create, add and finalize are retried only on 429 and 529, never after a timeout or a lost answer, so a batch is never created twice; a finalize whose earlier attempt went through returns the batch. The other calls are retried like every call.

Errors

Every error derives from Bynn\DecisionNode\Exception\DecisionNodeError, so catch (DecisionNodeError $e) catches them all. Its message, getMessage(), is the server's message exactly as sent (empty when the server sent none), or the SDK's description when there was no response or the response could not be read. It also has status, errorType, requestId and reason (the server's explanation from the x-decisionnode-error header, for example "question 'q': a score takes up to 10 levels, got 11") and details (the body's detail.details when it is an object, such as ['reason' => 'enrollment_unpriced'], else null).

An error whose body is not JSON (a page from a proxy or the service's edge, or no body) is classed by its status alone, as in the table: a 413 is a RequestTooLargeError, a 400 a BadRequestError (never a ContextLimitError), a 403 a PermissionDeniedError (never a RefusalError), a 422 a ValidationError with no entries. errorType is null, requestId and reason come from their headers, nothing is read from the body, and the message is HTTP <status> <text>, then (request id <id>) when the response has one: HTTP 413 Content Too Large, HTTP 408 Request Timeout (request id req_...). Retries are as for any response: a 408 is retried, a 413 never.

Only a 2xx response with a JSON body is a success. Redirects are never followed, so your key never goes to another URL or over plain HTTP.

Status Exception Extra fields
400 BadRequestError; ContextLimitError when the request is larger than the model's input limit
401 AuthenticationError (also raised before sending when no API key is set)
402 InsufficientCreditError
403 RefusalError when the request is refused under the usage policy; PermissionDeniedError otherwise (booster_not_enabled, key_scope_denied, a suspended workspace, and a publishable key's origin_not_allowed and model_not_allowed, whose details['allowed'] lists the key's models) category (harm or self_harm), safety
404 NotFoundError
413 RequestTooLargeError, never retried
422 ValidationError entries
429 RateLimitError (a publishable key's per-address rate and calls in flight carry details['reason'] client_rate or in_flight), never retried when errorType is key_spend_cap_reached, a key's daily spend cap, reached until 00:00 UTC (details: resets_at, cap, spent); SessionLimitError when a key holds as many open sessions as it may retryAfter (seconds)
529 (also booster_unavailable; never retried when details has the reason enrollment_unpriced or ingestion_delayed) OverloadedError retryAfter (seconds)
other 5xx, a 2xx whose body is not JSON, or a 2xx where the answer of check, classify, score or number is missing or malformed ServerError
any other status (a 3xx, 405, 409, 410, 413, 408 once its retries are used up) DecisionNodeError itself
an upload's storage refused the bytes (an image's or a document's) DecisionNodeError itself, errorType upload_failed, the storage's error code in reason
no response ConnectionError, TimeoutError

A booster the API does not offer is a BadRequestError with errorType booster_unknown, and a booster with no image to read (or naming an image the request does not have) booster_input_mismatch; a malformed $boosters list is api_usage_error with the reason in reason. Every error of a request that named boosters carries boosters: how each booster ended, from the x-decisionnode-boosters header, by name in the request's order (['age' => ['status' => 'timeout', 'latency_ms' => 800]]), so you can see which required booster failed.

The SDK checks every fixed rule the API documents before sending, so the server never sees a request the client could have refused and a fault costs no round trip. A local error has the class, errorType and message the server would answer with, and no status or requestId:

Fault Raised as
A choice with no options or more than 255, a score with more than 10 levels, a truth question with neither instructions nor criteria, a number, point or box question without instructions BadRequestError with the API's message and no errorType
An unknown question type, objects and arrays nested more than 128 deep, an image fault (not JPEG, PNG or WebP, over 10 MB or 40 megapixels, an empty id, an id over 64 characters counted as code points ("Image 1: its id is longer than 64 characters.", images counted from 0), a repeated id, an image URL that is not https), a video fault (see Videos), an upload id given with another media type, a batch request without a valid or with a repeated custom_id, an empty add, list paging out of range BadRequestError, errorType api_usage_error
A request body, or a batch line, over 25 MiB (26,214,400 bytes): a long state, many video frames, inline images RequestTooLargeError, errorType request_too_large_error
A usage read's granularity, groupBy, label filter, deviceId, key id (or more than 100 of them), scope, status or limit out of the API's bounds BadRequestError, errorType api_usage_error, the API's message
An image URL over 2,048 characters, with user information, on a port other than 443, or on a literal private, loopback or reserved address BadRequestError, errorType image_url_refused
More than 16 images or more than 4 videos in a request (a held lower image limit once models() has been called): one too_long entry for each, after the questions' entries ValidationError
A point or box question in a request without images, without image when the request has two or more, or with an image that is not one of the request's image ids, or units other than pixels and normalized ValidationError with every such entry of the request in one list
A question or state of the wrong shape or type, a score with no levels, a number grid fault (min, max or step not a number, beyond 2^53, more than 6 decimals, step not above 0, min above max, more than 4,096 values), a session window outside 1 to 64 or ttl_seconds outside 1 to 3,600 ValidationError with the entries the API would list

A frame that breaks a rule (no frame and no image, an empty frame, an unknown or empty question list, a seq out of range, an image fault, an image by URL) makes Session::send() throw BadRequestError (api_usage_error): nothing goes on the socket and the frame takes no seq. Once models() has been called, the limits it lists (a model that answers no number questions or serves no sessions, max_window, requests_per_batch, max_custom_id_chars) are checked too; until then they are left to the server. A model is found in the held list by its name, or by the entry that names it as its alias or pinned version; a model the list does not name is left to the server. Token limits stay the server's, since they differ by model. decideRaw sends its body unchecked.

To check a request without sending it, for example while a user is still editing it, call checkRequest() with the call's name and arguments, or checkFrame() on a session with send()'s arguments. Each runs every check sending would run, throws the same error, sends and uploads nothing, and returns when the request would be sent:

A problem with the arguments themselves is PHP's own InvalidArgumentException, never a DecisionNodeError: an empty question map, a value that cannot be encoded as JSON, an image file it cannot read, a timeout of 0 or less, a negative maxRetries or inlineThreshold, a base URL that is not https, an httpClient the SDK cannot show refuses redirects or one that turns off TLS checks (see HTTP client), a $concurrency below 1, a batch request without a custom_id, a runBatch poll interval under 10 s.

Timeouts and retries

Each attempt times out after 10 seconds, counted over the whole attempt: from sending the request to the last byte of the response body, not per network read. A request is retried twice after the first attempt on 408, 429, any 5xx, timeouts and dropped connections, waiting what the server asks for (retry-after-ms, else Retry-After in seconds or as an HTTP date, never more than 60 s) or else backing off exponentially from 0.5 s with jitter, up to 8 s. 400, 401, 402, 403, 404 and 422 are never retried, nor is a 429 key_spend_cap_reached, which holds until 00:00 UTC. A decision changes nothing on the server, so a retry is always safe. An image upload's PUT is retried the same way on 5xx and dropped connections, and has its own timeout, uploadTimeout (60 s by default), since an image of several MB may take longer than an API attempt is allowed. Batch create, add and finalize are the exception: retried only on 429 and 529 (see Batches).

A document call with wait: true has 20 seconds per attempt (the server holds it up to 10) unless you set timeout. A request whose boosters name ocr has 30 seconds per attempt (DecisionNode::OCR_TIMEOUT) unless you set timeout, which is then used as given. It is never retried after a timeout, because the server finishes the timed-out attempt and may bill it; its other retries stay. Give such requests a timeout of at least 30 seconds.

timeout must be above 0 and maxRetries 0 or more; anything else throws InvalidArgumentException. There is no setting for an unlimited timeout.

Safety

On the hosted API every decision response carries the result of a safety check in the x-decisionnode-safety header, which $d->safety holds: checked, harm, selfHarm, flagged (the categories above your key's thresholds) and, when the check did not run, reason. On a self-hosted server it is null. A refused request raises RefusalError and is never retried.

The safety scores in the body

Send the request header x-decisionnode-safety-body: true and the API also returns the safety scores as a safety field of the response body, which you read from $d->raw['safety']. The SDK sends no such header unless you add it; set it for every request with the client's headers argument:

Configuration

Argument Environment variable Default
apiKey DECISIONNODE_API_KEY none: a call without a key fails before sending
baseUrl DECISIONNODE_BASE_URL https://api.decisionnode.com; must be https (plain http only for localhost, 127.0.0.1 and ::1)
model DECISIONNODE_MODEL decisionnode-moe
timeout 10.0 seconds for a whole attempt (30 for a request naming ocr); above 0
maxRetries 2; 0 or more
headers extra headers sent with every API request and session handshake (never with an image upload's PUT to storage)
httpClient your own GuzzleHttp\Client (rules below)
logger none
inlineThreshold 524288 bytes: local images of a request under it in all go inline, at or above it they are uploaded
uploadTimeout 60.0 seconds for one storage PUT of an image upload, 120.0 for a document's; when given, it holds for both; above 0
clock the system clock: a Closure returning the Unix time in seconds, which upload URLs and cached uploads are judged by (for tests)

An argument wins over the environment, which wins over the default. An empty string counts as not given, for the client's arguments and for the $model of a call. A base URL that is not https, from an argument or from the environment, throws InvalidArgumentException when the client is made, so the API key never travels unencrypted; plain http is accepted only for this machine (localhost, 127.0.0.1, ::1), for a local test server. The models are decisionnode-moe, the main model, and decisionnode-flash, the fast one: each always means its current release. A decision's model names the pinned id that answered, decisionnode-<family>-<version>; to pin a version, take its id from models() rather than writing one down. The earlier ids decisionnode-latest, decisionnode-flash-latest, decisionnode-1.0 and decisionnode-1.0-flash are still accepted. A fine-tuned model of yours goes in model too.

HTTP client

By default the SDK uses Guzzle and reuses its connections, for API requests and image uploads alike. Pass your own GuzzleHttp\Client to set a proxy, a CA bundle or other transport options. The SDK's rules still hold with it, and the client is checked when the DecisionNode is made:

A session's socket uses phrity/websocket. It takes your client's TLS settings (a CA bundle given as verify, a client certificate and key) and its https proxy, through which it opens a tunnel (proxy given as a string, or as an array with https and the no hosts that bypass it); certificate and host name checks stay on. It follows no redirect, and its URL must be wss (plain ws only on this machine).

Logging

Pass a PSR-3 logger to get one debug line per attempt with the method, path, status, request id and duration. State and questions are never logged. Nothing else is sent anywhere: the only header the SDK adds is User-Agent: decisionnode-php/<version>.

JSON objects in PHP

PHP encodes an empty array as [] and an array with keys 0, 1, ... as a list. Questions and choice options are always sent as JSON objects. In your own data ($state, wire-form questions, decideRaw bodies), use an object such as new \stdClass() or (object) [...] where you need {} or an object with integer-like keys. decideRaw also accepts a JSON string, sent byte for byte.

Development

The conformance tests run the language-neutral DecisionNode conformance cases (suite 1.28) against their mock server, every case once through the blocking methods and once through their Async forms (sessions are blocking in both runs): set DECISIONNODE_CONFORMANCE_DIR to the suite's directory and run composer conformance. Without that variable the conformance tests fail; they never skip. CI checks the suite out at a pinned commit, set once as CONFORMANCE_REF in .github/workflows/ci.yml, and every action in the workflow is pinned to a commit SHA.

License

MIT. See LICENSE.


All versions of decisionnode with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
ext-json Version *
guzzlehttp/guzzle Version ^7.8
guzzlehttp/promises Version ^2.0
guzzlehttp/psr7 Version ^2.6
phrity/websocket Version 3.8.1
psr/http-message Version ^1.1 || ^2.0
psr/log Version ^1.1 || ^2.0 || ^3.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package bynn/decisionnode contains the following files

Loading the files please wait ...