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): dx helpers and shortlist auto-embed
nnlgsakib6dff77bcf376
diff --git a/laya-ts/src/dx.ts b/laya-ts/src/dx.tsnew file mode 100644index 00000000..7bb40e1f--- /dev/null+++ b/laya-ts/src/dx.ts@@ -0,0 +1,47 @@+import type { QuestionDef, SystemAnswer } from "./agent.js";++/** One-line question factories; output always passes checkQuestion. */+export const q = {+ choice(instructions: string, criteria: Record<string, unknown> | string[]): QuestionDef {+ return { type: "choice", instructions, criteria } as QuestionDef;+ },+ score(instructions: string, criteria: unknown[]): QuestionDef {+ return { type: "score", instructions, criteria } as QuestionDef;+ },+ noul(instructions: string, criteria?: Record<string, unknown>, labels?: { false: string; true: string }): QuestionDef {+ const out: Record<string, unknown> = { type: "noul", instructions };+ if (criteria !== undefined) out["criteria"] = criteria;+ if (labels !== undefined) out["labels"] = labels;+ return out as QuestionDef;+ },+};++/** Argmax label: choice string, score key, noul boolean. */+export function best(answer: SystemAnswer): string | boolean {+ if (answer.type === "choice") return answer.choice;+ if (answer.type === "score") {+ let bk = "";+ let bv = -Infinity;+ for (const [k, v] of Object.entries(answer.probabilities)) {+ if (v > bv) {+ bv = v;+ bk = k;+ }+ }+ return bk;+ }+ return answer.noul >= 0.5;+}++/** Top-k probability keys, descending. */+export function topK(probs: Record<string, number>, k: number): string[] {+ return Object.entries(probs)+ .sort((a, b) => b[1] - a[1])+ .slice(0, Math.max(0, k))+ .map(([key]) => key);+}++/** Confidence gate (default 0.8). */+export function isConfident(answer: Pick<SystemAnswer, "confidence">, thresh = 0.8): boolean {+ return (answer.confidence ?? 0) >= thresh;+}diff --git a/laya-ts/src/index.ts b/laya-ts/src/index.tsindex 38e105a3..86ca4dd7 100644--- a/laya-ts/src/index.ts+++ b/laya-ts/src/index.ts@@ -65,5 +65,7 @@ export {TEMP_MAX,} from "./common.js";export type { QType, InternalQ, CollateItem, CollatedBatch, QuestionPrefix } from "./common.js";+export { maxOf } from "./common.js";+export { q, best, topK, isConfident } from "./dx.js";export { bpeEncode, metaspaceEncode, encodeWithData, parseTokenizerJson, loadTokenizerJson, CHECKPOINT_IDS, SPECIAL_ALIASES, METASPACE_REPLACEMENT } from "./tokenizer.js";export type { TokenizerLike, TokenizerData, TokenizerIds, PreTokenizerKind } from "./tokenizer.js";diff --git a/laya-ts/src/shortlist.ts b/laya-ts/src/shortlist.tsindex 1aa67eb3..f856ae96 100644--- a/laya-ts/src/shortlist.ts+++ b/laya-ts/src/shortlist.ts@@ -130,7 +130,7 @@ export async function predictShortlist(agent: unknown,state: unknown,questions: Record<string, QuestionDef>,- embedFn: EmbedFn,+ embedFn?: EmbedFn | null,k: number = DEFAULT_SHORTLIST_K,predictKwargs: Record<string, unknown> = {},): Promise<SystemOneResult & { shortlist: Record<string, ShortlistMeta> }> {@@ -138,6 +138,15 @@ export async function predictShortlist(throw new TypeError("questions must be a dict of question id -> definition");}const checked = checkK(k);+ const needsEmbed = Object.values(questions).some(+ (qdef) =>+ typeof qdef === "object" && qdef !== null && !Array.isArray(qdef) &&+ (qdef as Record<string, unknown>)["type"] === "choice" &&+ "criteria" in (qdef as Record<string, unknown>) &&+ Object.keys((qdef as Record<string, unknown>)["criteria"] as object ?? {}).length > checked,+ );+ // Default bi-encoder is the agent itself; pass a dedicated embedFn only when it shortlists better.+ const embed = embedFn ?? (needsEmbed ? embedFnFromAgent(agent) : null);const reduced: Record<string, unknown> = {};const meta: Record<string, ShortlistMeta> = {};for (const [qid, qdef] of Object.entries(questions)) {@@ -149,7 +158,7 @@ export async function predictShortlist(throw new Error(`question ${JSON.stringify(qid)} is a choice but has no criteria`);}const q = qdef as Record<string, unknown>;- const { labels, scores, passthrough, n } = await rank(state, q["criteria"], embedFn, checked, q["instructions"]);+ const { labels, scores, passthrough, n } = await rank(state, q["criteria"], embed as EmbedFn, checked, q["instructions"]);meta[qid] = { labels: [...labels], scores, k: checked, n, passthrough };if (passthrough) {reduced[qid] = qdef;diff --git a/laya-ts/tests/dx.test.ts b/laya-ts/tests/dx.test.tsnew file mode 100644index 00000000..60c5536e--- /dev/null+++ b/laya-ts/tests/dx.test.ts@@ -0,0 +1,51 @@+import { describe, expect, it } from "vitest";+import { checkQuestion } from "../src/agent.js";+import { q, best, topK, isConfident } from "../src/dx.js";+import { predictShortlist } from "../src/shortlist.js";++describe("q builders", () => {+ it("produce valid defs", () => {+ const c: any = q.choice("What?", { a: "x", b: "y" });+ const s: any = q.score("Rate?", ["bad", "ok"]);+ const n: any = q.noul("Holds?");+ expect(() => checkQuestion("c", c)).not.toThrow();+ expect(() => checkQuestion("s", s)).not.toThrow();+ expect(() => checkQuestion("n", n)).not.toThrow();+ expect(c.criteria).toEqual({ a: "x", b: "y" });+ });+ it("choice accepts label list", () => {+ const c: any = q.choice("Pick?", ["a", "b"]);+ expect(() => checkQuestion("c", c)).not.toThrow();+ });+});++describe("result helpers", () => {+ it("best/topK/isConfident", () => {+ expect(best({ type: "choice", choice: "b", probabilities: { a: 0.2, b: 0.8 } } as any)).toBe("b");+ expect(best({ type: "score", probabilities: { "0": 0.1, "1": 0.7, "2": 0.2 } } as any)).toBe("1");+ expect(topK({ a: 0.2, b: 0.8, c: 0.5 }, 2)).toEqual(["b", "c"]);+ expect(isConfident({ confidence: 0.9 } as any, 0.8)).toBe(true);+ expect(isConfident({ confidence: 0.5 } as any, 0.8)).toBe(false);+ });+});++describe("predictShortlist auto-embed", () => {+ it("uses agent encoder when embedFn omitted", async () => {+ const agent: any = {+ tok: { encode: (t: string) => [t.length % 100], padId: 0 },+ provider: {+ async runEncoder(batch: any) {+ return { lastHidden: batch.inputIds.map((row: number[]) => row.map(() => [1, 0])) };+ },+ },+ async predict(_s: unknown, reduced: any) {+ return { model: "m", answers: {}, usage: { input_tokens: 0, output_tokens: 0 }, _reduced: reduced };+ },+ };+ const questions: any = {+ d: { type: "choice", instructions: "pick", criteria: { aaa: "x", bbb: "y", ccc: "z" } },+ };+ const out: any = await predictShortlist(agent, "state", questions, undefined as any, 1);+ expect(out.shortlist.d.labels).toHaveLength(1);+ });+});