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
fix(ts): write small numbers the way Python's json.dumps does
Ludwig J. Marx2b59ea2daf0e
diff --git a/laya-ts/src/common.ts b/laya-ts/src/common.tsindex 323bad34..ebc3d42a 100644--- a/laya-ts/src/common.ts+++ b/laya-ts/src/common.ts@@ -1,6 +1,15 @@import type { TokenizerLike } from "./tokenizer.js";export type QType = "choice" | "score" | "noul";export interface InternalQ { t: QType; ins: string; crit: unknown; labels?: { false: string; true: string } }+/** Python `json.dumps` for a finite number. JavaScript prints the same shortest round-trip+digits, but Python switches to exponent notation below 1e-4 (JavaScript: below 1e-6) and pads the+exponent to two digits (`5e-05`, not `5e-5`). An integer-valued number prints as before, which+matches a Python int below 1e21: JavaScript cannot tell Python's `49.0` from `49`. */+function pyNumber(v: number): string {+ const [mant, exp] = v.toExponential().split("e");+ const e = Number(exp);+ return e >= -4 ? String(v) : `${mant}e-${String(-e).padStart(2, "0")}`;+}/** Python `json.dumps(v, ensure_ascii=False)` replica: separators (", ", ": "),unicode raw, unknown types fall back to undefined (caller applies str()). */function pyJson(v: unknown): string | undefined {@@ -10,7 +19,7 @@ function pyJson(v: unknown): string | undefined {if (Number.isNaN(v)) return "NaN";if (v === Infinity) return "Infinity";if (v === -Infinity) return "-Infinity";- return JSON.stringify(v);+ return pyNumber(v);}if (Array.isArray(v)) return `[${v.map((x) => pyJson(x) ?? "null").join(", ")}]`;if (typeof v === "object") {diff --git a/laya-ts/tests/parity-sequence.test.ts b/laya-ts/tests/parity-sequence.test.tsindex bbe12cd2..bbca5bae 100644--- a/laya-ts/tests/parity-sequence.test.ts+++ b/laya-ts/tests/parity-sequence.test.ts@@ -21,6 +21,26 @@ describe("common parity", () => {it("serializes dict state as JSON", () => {expect(serializeState({ body: "x" })).toBe('{"body": "x"}');});+ it("formats non-integer numbers like Python's float repr", () => {+ // Expected strings are Python's json.dumps({"x": v}, ensure_ascii=False). Python switches+ // to exponent notation below 1e-4 and pads the exponent to two digits; JavaScript's+ // Number#toString switches only below 1e-6 and never pads, so the model read other tokens.+ const cases: [number, string][] = [+ [0.5, '{"x": 0.5}'],+ [0.0001, '{"x": 0.0001}'],+ [0.00005, '{"x": 5e-05}'],+ [1.25e-5, '{"x": 1.25e-05}'],+ [3e-7, '{"x": 3e-07}'],+ [-2.5e-8, '{"x": -2.5e-08}'],+ [1.5e-10, '{"x": 1.5e-10}'],+ [5e-324, '{"x": 5e-324}'],+ ];+ for (const [v, want] of cases) expect(serializeState({ x: v })).toBe(want);+ expect(renderOptions({ t: "score", ins: "x", crit: [0.00005] })).toEqual(["level 0: 5e-05"]);+ // an integer-valued number reads as a Python int, which keeps plain digits+ expect(serializeState({ n: 49 })).toBe('{"n": 49}');+ expect(serializeState({ n: 1e16 })).toBe('{"n": 10000000000000000}');+ });it("truncateLeft keeps tail of state (py parity)", () => {const tok = {clsId: 101, sepId: 102, maskId: 103, maskToken: "[MASK]",