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.

fix(ts): reject bucket overrides that are not a mapping, as Python does

Bruce-Yiidb957c19c42d
diff --git a/laya-ts/src/agent.ts b/laya-ts/src/agent.ts
index 647c5c0c..a6c95ff7 100644
--- a/laya-ts/src/agent.ts
+++ b/laya-ts/src/agent.ts
@@ -400,6 +400,18 @@ export class Agent extends HookRegistry {
);
}
const tboRaw = (lc?.temperature_by_options ?? {}) as Record<string, unknown>;
+ // Python requires a mapping here and names the language otherwise. `?? {}` covers only
+ // null/undefined, so a string reached `Object.entries` and became character keys --
+ // "nope" gave {"0":"n","1":"o","2":"p","3":"e"} -- and every bucket a question can ask for
+ // then missed, silently, at the base temperature; a number gave {} and so did nothing at
+ // all. A falsy value is an empty override on both sides (Python's `or {}` is what makes it
+ // so), so only a truthy non-mapping is an error here too.
+ if (tboRaw && (typeof tboRaw !== "object" || Array.isArray(tboRaw))) {
+ throw new Error(
+ `Language override ${JSON.stringify(l)} temperature_by_options must be a mapping of ` +
+ `bucket -> float, got ${Array.isArray(tboRaw) ? "list" : typeof tboRaw}`,
+ );
+ }
this.langTemperatures[normL] = {
temperature: [0, 1, 2].map((i) => clampTemperature(tRaw[i])),
temperatureByOptions: Object.fromEntries(
diff --git a/laya-ts/tests/lang-temperature.test.ts b/laya-ts/tests/lang-temperature.test.ts
index 7dc804e9..d2204f55 100644
--- a/laya-ts/tests/lang-temperature.test.ts
+++ b/laya-ts/tests/lang-temperature.test.ts
@@ -83,6 +83,27 @@ describe("lang_temperatures", () => {
).toThrow('Language override "de" temperature must be a list of 3 floats');
});
+ it("rejects bucket overrides that are not a mapping, as Python does", () => {
+ // Python (common.py resolve_lang_temperatures) requires a mapping and names the language
+ // otherwise. `?? {}` only covers null/undefined, so a string reached Object.entries and
+ // became character keys: Object.entries("nope") is [["0","n"],["1","o"],["2","p"],["3","e"]],
+ // none of which is a bucket any question asks for.
+ for (const buckets of ["nope", 42, true, ["choice:2"]]) {
+ expect(
+ () => new Agent({ provider: fakeProvider(), lang_temperatures: { de: { temperature_by_options: buckets } } } as any),
+ ).toThrow('Language override "de" temperature_by_options must be a mapping of bucket -> float');
+ }
+ // A falsy value is an empty override in both languages, and Python's `or {}` is what makes
+ // that so; these must keep passing rather than start failing on a shape that means "none".
+ for (const buckets of [undefined, null, {}, 0, false, ""]) {
+ const a = new Agent({
+ provider: fakeProvider(),
+ lang_temperatures: { de: { temperature_by_options: buckets } },
+ } as any);
+ expect(a.langTemperatures.de.temperatureByOptions).toEqual({});
+ }
+ });
+
it("rejects a base temperature that is not a list of 3 floats, as Python does (#502)", () => {
for (const temperature of [[1, 1], [1, 1, 1, 1], 2, "1"]) {
expect(() => new Agent({ provider: fakeProvider(), temperature } as any)).toThrow(