Expand description
Core model-independent logic: state serialization (Python json.dumps parity),
option rendering, token-sequence construction, collation, and calibration helpers.
Ported from laya/common.py (source of truth) and mirrored against laya-ts/src/common.ts.
Structs§
- Collated
Batch - A padded batch ready to be turned into tensors.
- InternalQ
- A validated + normalized question, ready for sequence building and decoding.
critis kept as a JSON value in the shape the type expects (choice: object, score: array, noul: object-or-null), matching the normalization inAgent::to_internal. - Item
- One tokenized (state, question) pair before collation.
Enums§
- QType
- The three question types. Numeric indices match Python
QTYPES(choice=0, score=1, noul=2) — they select the model’s type embedding row.
Constants§
Traits§
- Tokenizer
- Abstraction over a checkpoint tokenizer. Mirrors
TokenizerLikein laya-ts.encodecorresponds to HFtokenizer(text, add_special_tokens=False)["input_ids"].
Functions§
- answer_
confidence - Probability mass on the answer being reported:
max(p[:k]), clamped to[0, 1]. - build_
sequence - Build one token sequence + option marker positions.
- clamp_
temperature - A usable temperature:
tconfined to[TEMP_MIN, TEMP_MAX], falling back to 1.0 if not finite. - collate_
items - Collate groups of items into one padded batch.
groupsis a list of per-state item lists, flattened in order (mirrorscollate_itemsinlaya/common.py). ReturnsNoneif empty. - confidence_
from_ probs - Normalized Shannon-entropy confidence:
1 - H(p)/log(k), clamped to[0, 1].pis expected to already hold thekoption probabilities. - drop_
stale_ option_ order - Remove an
option_ordera narrowed choice question can no longer honour, warning once perqid. Returnstrueif an order was dropped. Mirrors upstreamshortlist._drop_stale_order. - jev_
confidence - Jev’s
confidencefor achoice/scoreanswer’sprobabilities, orNone(upstream #302). - py_json
- Replica of Python
json.dumps(value, ensure_ascii=False, separators=(", ", ": ")). - render_
criterion - Render one criterion value as text (strings pass through, structured values become JSON).
- render_
options - Render option texts in label-index order. Noul semantic order is always
[false, true]. - resolve_
noul_ labels - Resolve noul labels to
(false_label, true_label), validating the mapping. - round4
- Round to 4 decimals like Python
round(x, 4)(the precision Laya publishes). - round_
half_ even - Round to
ndigitsdecimals using round-half-to-even, matching Python’s built-inround. - serialize_
state - A state as the model sees it: raw strings pass through, everything else becomes compact JSON.
- softmax
- Numerically stable softmax.
- temp_
bucket - Temperature bucket key, e.g.
"choice:3-5". - unpermute_
probs - Put a slot-ordered probability row back into the caller’s option order.