Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.
Python calibrationclassificationdecision-modelhuggingfacejevmodernbertmultilingualnlppythonpytorchroutingtyped-decisionszero-shot
Merge pull request #1117
Nandakishor853ca315de17
diff --cc laya-ts/README.mdindex 864c5e60,82e6738f..2a7b0bb3--- a/laya-ts/README.md+++ b/laya-ts/README.md@@@ -170,63 -170,16 +170,65 @@@ Python's `sort_by_length` grouping is n## Shortlist (many labels)```ts- import { shortlistChoice, predictShortlist, embedFnFromAgent } from "laya-ts";+ import { shortlistChoice, predictShortlist, embedFnFromAgent, cached } from "laya-ts";- const keep = await shortlistChoice(state, bigCriteriaDict, embedFn, 20);- const out = await predictShortlist(agent, state, questions, embedFn, 20);+ const cachedEmbed = cached(embedFn);+ const keep = await shortlistChoice(state, bigCriteriaDict, cachedEmbed, 20);+ const out = await predictShortlist(agent, state, questions, cachedEmbed, 20);// out.shortlist[qid] = { labels, scores, k, n, passthrough }// embedFnFromAgent(agent) mean-pools the loaded encoder; a dedicated bi-encoder usually shortlists better.+ // cached(embedFn) keeps an LRU cache across calls; cachedEmbed.cacheInfo() reports size/hits/misses.```+### Tournament (without an embedder)++For a large tool or intent catalog, `predictTournament` lets the decision model narrow the+options itself. It ports Python's `predict_tournament` to the local Node/browser runtime:++```ts+import { predictTournament } from "laya-ts";++const out = await predictTournament(agent, state, questions);+console.log(out.answers.intent);+console.log(out.tournament.intent); // { labels: [...finalists], n: originalCount, rounds: 1 }++// Pick a group size and forward options to every prediction, including the final one.+const routed = await predictTournament(router, state, questions, 16, { model: "english" });+```++Each oversized choice is split into balanced groups of at most 16 labels. All groups from+all questions share one `predict` call per round; their winners advance until each choice+fits. Up to 256 labels therefore take one elimination call and one final call. Small choices+and non-choice questions reach the final call unchanged, and a request needing no+elimination makes exactly one call. The helper does not mutate the supplied questions.++The returned `tournament` metadata covers every choice, including `rounds: 0` for a+passthrough. Probabilities and confidences describe **only the finalists**; `usage`,+`routing`, and other prediction fields come from the final call, not all rounds combined.+Hooks run for every underlying call. Calibrate thresholds on the strategy you actually use;+a threshold fitted on the full catalog does not automatically transfer to a tournament.+Widening the head budget or using a dedicated shortlist embedder can still be a better fit.++A narrowed choice drops its original `option_order` with one warning per question, matching+Python. Passthrough definitions keep it. This does not add `option_order` handling to the+TypeScript `Agent`, whose prediction path does not currently apply that field.++For a reproducible local comparison, build the package and export a checkpoint as above,+then run from the repository root:++```bash+python laya-ts/scripts/tournament_parity.py --model-dir CHECKPOINT --onnx-dir ./model \+ --fixture ./tournament-python.json+node laya-ts/examples/tournament.mjs --model-dir ./model \+ --fixture ./tournament-python.json --report ./tournament-node.json+```++The Python script records fresh predictions for fixed inputs; Node independently runs+the same inputs through the split ONNX export. The check compares each round's questions,+winners, probabilities and final metadata, including a two-round case. Reports include+artifact hashes, call counts and warmed timings. These few cases check runtime parity;+they do not establish accuracy gains or production latency. Model files stay local.+## Per-language calibration (`lang_temperatures`)Port of the Python `Agent(lang_temperatures=...)` knob. A language override replaces thediff --cc laya-ts/src/index.tsindex fec9d250,f08738db..8f881334--- a/laya-ts/src/index.ts+++ b/laya-ts/src/index.ts@@@ -17,8 -17,8 +17,8 @@@ export { createNodeProvider, createWebPexport type { Batch, SessionProvider, ProviderOptions, NodeBundle, WebBundle } from "./providers.js";export { Router, normaliseName, DEFAULT_MODELS, digestsFromEnv, _digestsFromEnv } from "./router.js";export type { RoutedResult, RouteDecision, ModelName, ModelSpec, BatchRequest, PredictBatchOptions, RouterOptions, RouteOptions } from "./router.js";- export { shortlistChoice, predictShortlist, predictTournament, embedFnFromAgent, DEFAULT_SHORTLIST_K, DEFAULT_TOURNAMENT_GROUP } from "./shortlist.js";- export type { EmbedFn, ShortlistMeta, TournamentMeta } from "./shortlist.js";-export { shortlistChoice, predictShortlist, embedFnFromAgent, DEFAULT_SHORTLIST_K, cached, cachedEmbedFn, DEFAULT_SHORTLIST_MAXSIZE } from "./shortlist.js";-export type { EmbedFn, ShortlistMeta, CacheInfo, CachedEmbedFn } from "./shortlist.js";++export { shortlistChoice, predictShortlist, predictTournament, embedFnFromAgent, cached, cachedEmbedFn, DEFAULT_SHORTLIST_K, DEFAULT_SHORTLIST_MAXSIZE, DEFAULT_TOURNAMENT_GROUP } from "./shortlist.js";++export type { EmbedFn, ShortlistMeta, TournamentMeta, CacheInfo, CachedEmbedFn } from "./shortlist.js";export { analyse, isEnglish, guessLatinLanguage, detectScript } from "./lang.js";export type { AnalyseResult, LatinProfile } from "./lang.js";export { cleanEmailBody, emailState } from "./email.js";diff --cc laya-ts/src/shortlist.tsindex 9571d922,5e3f0815..1d1fdca7--- a/laya-ts/src/shortlist.ts+++ b/laya-ts/src/shortlist.ts@@@ -3,7 -3,7 +3,8 @@@ import type { Batch } from "./providersimport type { QuestionDef, SystemOneResult } from "./agent.js";export const DEFAULT_SHORTLIST_K = 20;+export const DEFAULT_TOURNAMENT_GROUP = 16;+ export const DEFAULT_SHORTLIST_MAXSIZE = 4096;export type EmbedFn = (texts: string[]) => Promise<number[][]> | number[][];@@@ -15,12 -15,21 +16,27 @@@ export interface ShortlistMetapassthrough: boolean;}+export interface TournamentMeta {+ labels: string[];+ n: number;+ rounds: number;+}++ export interface CacheInfo {+ size: number;+ maxsize: number;+ hits: number;+ misses: number;+ }++ export interface CachedEmbedFn {+ (texts: string[]): Promise<number[][]>;+ cacheInfo(): CacheInfo;+ cache_info(): CacheInfo;+ cacheClear(): void;+ cache_clear(): void;+ }+function checkK(k: unknown): number {if (typeof k !== "number" || !Number.isInteger(k) || k < 1) {throw new Error(`k must be a positive integer, got ${JSON.stringify(k)}`);