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
feat(ts): accept the per-bucket min_confidence map
Bruce-Yii3642b101ad90
diff --git a/docs/typescript-sdk.md b/docs/typescript-sdk.mdindex 7f8a2e39..e400521b 100644--- a/docs/typescript-sdk.md+++ b/docs/typescript-sdk.md@@ -63,7 +63,8 @@ sent only when the caller supplied the option -- an absent option leaves the dep| `minConfidence` | `min_confidence` |An option that cannot mean anything is refused locally, before the request goes out: a blank `task`, a-budget that is not a positive integer, a threshold outside `[0, 1]`. Nothing is silently ignored. Laya's+budget that is not a positive integer, a threshold outside `[0, 1]`, or a threshold map that is empty+or holds a value outside `[0, 1]`. Nothing is silently ignored. Laya'spublic `/health` returns `status`, `loaded`, and `device`. Prediction never probes health first.FastAPI detail strings and validation arrays are preserved as `LayaAPIError`diff --git a/sdk/typescript/README.md b/sdk/typescript/README.mdindex 4f7dc5f7..1369a23c 100644--- a/sdk/typescript/README.md+++ b/sdk/typescript/README.md@@ -170,7 +170,7 @@ reference, and this client exposes them as options on `predict()`:| `langGuess` | `lang_guess` | a code from your own LID, consulted before detection || `maxLen` | `max_len` | total token window for this request (server-capped) || `headMaxLen` | `head_max_len` | token window the option prompt shares, same cap |-| `minConfidence` | `min_confidence` | abstention threshold in `[0, 1]`; low-confidence answers are marked, not dropped |+| `minConfidence` | `min_confidence` | abstention gate: one threshold in `[0, 1]`, or a per-bucket map of option-count bucket to threshold; low-confidence answers are marked, not dropped |```jsawait laya.predict(state, questions, { task: 'typed', minConfidence: 0.8 });diff --git a/sdk/typescript/src/client.ts b/sdk/typescript/src/client.tsindex 8b2826e0..b5f14e69 100644--- a/sdk/typescript/src/client.ts+++ b/sdk/typescript/src/client.ts@@ -6,6 +6,21 @@ import type { Health, Prediction, PredictOptions, Questions, RequestOptions, Staimport { isRecord, validateJson, validateQuestions, validateTimeout } from './validation.js';import { validateHealth, validatePrediction } from './response.js';+/** One threshold in `[0, 1]`: booleans are not thresholds even though they read as 0/1. */+function isThreshold(value: unknown): value is number {+ return typeof value === 'number' && Number.isFinite(value) && value >= 0 && value <= 1;+}++/** Mirror core's `check_min_confidence`: one threshold for every answer, or a non-empty+ * per-bucket map whose values are all thresholds. Anything else is refused before the+ * request goes out, so a caller cannot pay for inference with a gate the server would 422. */+function validateMinConfidence(value: unknown): asserts value is number | Record<string, number> {+ if (isThreshold(value)) return;+ if (isRecord(value) && Object.keys(value).length > 0 && Object.values(value).every(isThreshold)) return;+ throw new LayaValidationError(+ 'minConfidence must be a number between 0 and 1, or a non-empty map of bucket to threshold');+}+export interface LayaOptions {/** Server root URL, optionally including a reverse-proxy path prefix. */baseURL?: string;@@ -81,10 +96,7 @@ export class Laya {throw new LayaValidationError(`${key} must be a positive integer`);}}- if (options.minConfidence !== undefined &&- (!Number.isFinite(options.minConfidence) || options.minConfidence < 0 || options.minConfidence > 1)) {- throw new LayaValidationError('minConfidence must be a number between 0 and 1');- }+ if (options.minConfidence !== undefined) validateMinConfidence(options.minConfidence);const model = options.model ?? this.model;if (model !== undefined && (typeof model !== 'string' || !model.trim())) {throw new LayaValidationError('model must be a nonempty string');@@ -97,7 +109,7 @@ export class Laya {const body: {state: State; questions: typeof wireQuestions; model?: string; task?: string;lang?: string; lang_guess?: string; max_len?: number; head_max_len?: number;- min_confidence?: number;+ min_confidence?: number | Record<string, number>;} = { state, questions: wireQuestions };if (model !== undefined) body.model = model;// The per-request controls `/v1/systemone` forwards when the request sends them (seediff --git a/sdk/typescript/src/types.ts b/sdk/typescript/src/types.tsindex 45c93b41..0e012d7a 100644--- a/sdk/typescript/src/types.ts+++ b/sdk/typescript/src/types.ts@@ -23,6 +23,15 @@ export type ModelName = 'english' | 'multilingual' | 'typed-decisions';export type ModelAlias = ModelName | 'en' | 'laya' | 'default' | 'multi' | 'ml' | 'laya-multilingual'| 'typed' | 'typed_decisions' | 'laya-typed-decisions' | 'decisions';+/** Per-option-count abstention thresholds: `bucket -> threshold`, keyed by core's `temp_bucket`+ * spelling (`"choice:2"`, `"choice:3-5"`, `"score:6-10"`, `"noul:2"`, ...) plus an optional+ * `"default"` for buckets the map does not name. One threshold does not transfer across option+ * counts, so each bucket is gated at the level its own calibration earns; fit one with+ * `laya.calibrate.fit_abstention_thresholds`. Every value is a threshold in `[0, 1]`. */+export type MinConfidenceMap = Record<string, number>;+/** The abstention gate: one threshold for every answer, or a per-bucket map. */+export type MinConfidence = number | MinConfidenceMap;+export interface RequestOptions {/** Abort waiting for the response. Running inference may still finish on the server. */signal?: AbortSignal;@@ -45,9 +54,11 @@ export interface PredictOptions extends RequestOptions {maxLen?: number;/** Token window the option prompt shares, same cap. */headMaxLen?: number;- /** Abstention threshold in `[0, 1]`; an answer whose `answer_confidence` falls below- * it comes back marked `low_confidence`, with the answer itself kept. */- minConfidence?: number;+ /** Abstention gate: either one threshold in `[0, 1]` for every answer, or a+ * per-bucket map so each option count is gated at the level its own calibration earns.+ * An answer whose `answer_confidence` falls below its threshold comes back marked+ * `low_confidence`, with the answer itself kept. */+ minConfidence?: MinConfidence;}interface AnswerBase {diff --git a/sdk/typescript/test/client.test.mjs b/sdk/typescript/test/client.test.mjsindex 0fa1a144..fc4ae4ab 100644--- a/sdk/typescript/test/client.test.mjs+++ b/sdk/typescript/test/client.test.mjs@@ -112,6 +112,9 @@ test('invalid model and control values fail before sending a request', async (){ lang: 12 }, { lang: ' ' }, { langGuess: 42 },{ maxLen: 0 }, { maxLen: 2.5 }, { headMaxLen: -1 },{ minConfidence: -0.1 }, { minConfidence: 1.5 }, { minConfidence: NaN },+ { minConfidence: true }, { minConfidence: [] }, { minConfidence: {} },+ { minConfidence: { 'choice:2': 1.5 } }, { minConfidence: { 'choice:2': 'high' } },+ { minConfidence: { 'choice:2': NaN } },]) {await assert.rejects(client.predict('hello', questions, options), LayaValidationError, JSON.stringify(options));}@@ -138,6 +141,18 @@ test('per-request controls reach the wire under their server names', async () =>});});+test('a per-bucket threshold map reaches the wire as min_confidence', async () => {+ // The server's abstention gate resolves each answer's threshold from its own option-count+ // bucket (#394), so the map must arrive verbatim: re-keying or dropping `default` here+ // would silently change which answers abstain.+ const map = { 'choice:2': 0.9, default: 0.3 };+ const client = new Laya({ fetch: async (_url, init) => {+ assert.deepEqual(JSON.parse(init.body).min_confidence, map);+ return json(prediction);+ } });+ await client.predict('hello', questions, { minConfidence: map });+});+test('bad JavaScript inputs fail before fetch without lossy serialization', async () => {let calls = 0;const client = new Laya({ fetch: async () => { calls++; return json({}); } });diff --git a/sdk/typescript/test/types.ts b/sdk/typescript/test/types.tsindex 5dc34bd3..5931132c 100644--- a/sdk/typescript/test/types.ts+++ b/sdk/typescript/test/types.ts@@ -64,11 +64,15 @@ client.route('hello');client.predict('hello', questions, { task: 12 });// @ts-expect-error maxLen must be a number.client.predict('hello', questions, { maxLen: '1024' });-// @ts-expect-error minConfidence must be a number.+// @ts-expect-error minConfidence must be a threshold or a threshold map.client.predict('hello', questions, { minConfidence: '0.9' });client.predict('hello', questions, { model: 'multilingual' });client.predict('hello', questions, { task: 'typed', lang: 'de', langGuess: 'fr',maxLen: 2048, headMaxLen: 256, minConfidence: 0.8 });+// A per-bucket map gates each option count at the level its own calibration earns (#394).+client.predict('hello', questions, { minConfidence: { 'choice:2': 0.9, default: 0.3 } });+// @ts-expect-error A threshold map with a non-threshold value is not a gate.+client.predict('hello', questions, { minConfidence: { 'choice:2': 'high' } });new Laya({ model: 'english' });// @ts-expect-error A score answer has no choice field.result.answers.priority.choice;