Skip to content

armgit / laya

Public Imported from GitHub
0

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.

feat(sdk): expose the per-request controls /v1/systemone forwards

Aashish7f8c75b61f49
diff --git a/sdk/typescript/README.md b/sdk/typescript/README.md
index b3c9b3b2..72acbc80 100644
--- a/sdk/typescript/README.md
+++ b/sdk/typescript/README.md
@@ -144,10 +144,29 @@ await laya.predict(state, questions, { model: 'typed-decisions' });
await laya.predict(state, questions, { model: 'multilingual' });
```
-Laya accepts its local checkpoint aliases. The HTTP endpoint supports `model`;
-it does not expose standalone `route()`, `task`, or `lang` overrides. Laya
-still detects language automatically; set `LAYA_AUTO_TASK=1` on the server to
-enable automatic workflow routing.
+Laya accepts its local checkpoint aliases. The HTTP endpoint forwards the
+per-request controls documented in the [HTTP API](../../docs/http-api.md)
+reference, and this client exposes them as options on `predict()`:
+
+| option | wire field | meaning |
+|---|---|---|
+| `model` | `model` | checkpoint name or alias for this request |
+| `task` | `task` | force a workflow; an unknown name is a 422 naming it |
+| `lang` | `lang` | a language code that skips detection when it names a language |
+| `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 |
+
+```js
+await laya.predict(state, questions, { task: 'typed', minConfidence: 0.8 });
+await laya.predict(state, questions, { lang: 'de', maxLen: 4096, headMaxLen: 512 });
+```
+
+Absent options stay absent, so the deployment's own `Router(...)` settings remain
+in charge. The endpoint does not expose a standalone `route()` call. Laya still
+detects language automatically; set `LAYA_AUTO_TASK=1` on the server to enable
+automatic workflow routing.
Laya returns `model`, `answers`, and `usage`, with optional `routing`, answer
`action`, and Noul `confidence` metadata. Those fields are validated when
diff --git a/sdk/typescript/src/client.ts b/sdk/typescript/src/client.ts
index f75692d9..8b2826e0 100644
--- a/sdk/typescript/src/client.ts
+++ b/sdk/typescript/src/client.ts
@@ -66,8 +66,24 @@ export class Laya {
if (state !== null && typeof state !== 'string' && !Array.isArray(state) && !isRecord(state)) {
throw new LayaValidationError('state must be text, a JSON object, an array, or null');
}
- for (const key of ['task', 'lang']) {
- if (key in options) throw new LayaValidationError(`${key} is not supported by /v1/systemone; use model instead`);
+ if (options.task !== undefined && (typeof options.task !== 'string' || !options.task.trim())) {
+ throw new LayaValidationError('task must be a nonempty string');
+ }
+ for (const key of ['lang', 'langGuess'] as const) {
+ const value = options[key];
+ if (value !== undefined && (typeof value !== 'string' || !value.trim())) {
+ throw new LayaValidationError(`${key} must be a nonempty language code string`);
+ }
+ }
+ for (const key of ['maxLen', 'headMaxLen'] as const) {
+ const value = options[key];
+ if (value !== undefined && (!Number.isInteger(value) || value < 1)) {
+ 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');
}
const model = options.model ?? this.model;
if (model !== undefined && (typeof model !== 'string' || !model.trim())) {
@@ -78,8 +94,21 @@ export class Laya {
? { ...question, criteria: Object.fromEntries(question.criteria.map(label => [label, null])) }
: question,
]));
- const body: { state: State; questions: typeof wireQuestions; model?: string } = { state, questions: wireQuestions };
+ 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;
+ } = { state, questions: wireQuestions };
if (model !== undefined) body.model = model;
+ // The per-request controls `/v1/systemone` forwards when the request sends them (see
+ // docs/http-api.md); absent options stay absent so the deployment's own Router
+ // settings remain in charge.
+ if (options.task !== undefined) body.task = options.task;
+ if (options.lang !== undefined) body.lang = options.lang;
+ if (options.langGuess !== undefined) body.lang_guess = options.langGuess;
+ if (options.maxLen !== undefined) body.max_len = options.maxLen;
+ if (options.headMaxLen !== undefined) body.head_max_len = options.headMaxLen;
+ if (options.minConfidence !== undefined) body.min_confidence = options.minConfidence;
validateJson(body);
return JSON.stringify(body);
}
diff --git a/sdk/typescript/src/types.ts b/sdk/typescript/src/types.ts
index 68fe12ce..d6ea74a9 100644
--- a/sdk/typescript/src/types.ts
+++ b/sdk/typescript/src/types.ts
@@ -32,6 +32,22 @@ export interface RequestOptions {
export interface PredictOptions extends RequestOptions {
/** Local Laya checkpoint or alias. Overrides the client's configured model. */
model?: string;
+ /** Force a checkpoint by workflow name instead of letting routing decide; the server
+ * answers 422 naming an unknown one. */
+ task?: string;
+ /** A language code (`de`, `en-US`) that skips detection when it names a language. */
+ lang?: string;
+ /** A language code from the client's own identifier, consulted after `lang` and
+ * before detection. */
+ langGuess?: string;
+ /** Total token window for this request, capped by the deployment's
+ * `LAYA_MAX_TOKEN_BUDGET` (422 above it). */
+ 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;
}
interface AnswerBase {
diff --git a/sdk/typescript/test/client.test.mjs b/sdk/typescript/test/client.test.mjs
index 021fde6f..e222b024 100644
--- a/sdk/typescript/test/client.test.mjs
+++ b/sdk/typescript/test/client.test.mjs
@@ -86,14 +86,40 @@ test('client model default and prediction overrides select local checkpoints', a
assert.deepEqual(models, ['english', 'multilingual']);
});
-test('unsupported routing options and invalid models fail before sending a request', async () => {
+test('invalid model and control values fail before sending a request', async () => {
const client = new Laya({ fetch: async () => { assert.fail('must not send'); } });
- for (const options of [{ task: 'typed' }, { lang: 'hi' }, { model: '' }, { model: 1 }]) {
- await assert.rejects(client.predict('hello', questions, options), LayaValidationError);
+ for (const options of [
+ { model: '' }, { model: 1 },
+ { task: '' }, { task: 12 },
+ { lang: 12 }, { lang: ' ' }, { langGuess: 42 },
+ { maxLen: 0 }, { maxLen: 2.5 }, { headMaxLen: -1 },
+ { minConfidence: -0.1 }, { minConfidence: 1.5 }, { minConfidence: NaN },
+ ]) {
+ await assert.rejects(client.predict('hello', questions, options), LayaValidationError, JSON.stringify(options));
}
for (const model of ['', ' ', 1]) assert.throws(() => new Laya({ model }), LayaValidationError);
});
+test('per-request controls reach the wire under their server names', async () => {
+ const client = new Laya({ fetch: async (_url, init) => {
+ assert.deepEqual(JSON.parse(init.body), {
+ state: 'hello',
+ questions,
+ task: 'typed',
+ lang: 'de',
+ lang_guess: 'fr',
+ max_len: 2048,
+ head_max_len: 256,
+ min_confidence: 0.8,
+ });
+ return json(prediction);
+ } });
+ await client.predict('hello', questions, {
+ task: 'typed', lang: 'de', langGuess: 'fr',
+ maxLen: 2048, headMaxLen: 256, minConfidence: 0.8,
+ });
+});
+
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.ts
index 51230d9f..7bbd0aa8 100644
--- a/sdk/typescript/test/types.ts
+++ b/sdk/typescript/test/types.ts
@@ -22,11 +22,15 @@ result.routing.model;
result.answers.team.action.act_probability;
// @ts-expect-error /v1/systemone does not expose standalone routing.
client.route('hello');
-// @ts-expect-error /v1/systemone supports model, not task.
-client.predict('hello', questions, { task: 'typed' });
-// @ts-expect-error /v1/systemone supports model, not lang.
-client.predict('hello', questions, { lang: 'hi' });
+// @ts-expect-error task must be a string.
+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.
+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 });
new Laya({ model: 'english' });
// @ts-expect-error A score answer has no choice field.
result.answers.priority.choice;