Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions laya-ts/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,13 @@ export class Agent extends HookRegistry {
this.tok = opts.tok ?? defaultTokenizer();
const raw = (cfg.temperature ?? [1.0, 1.0, 1.0]) as unknown;
this.temperatureRaw = raw;
const rawList = Array.isArray(raw) ? raw : [raw, raw, raw];
this.temperature = [0, 1, 2].map((i) => clampTemperature(rawList[i] ?? 1.0));
// The decode indexes this by question type, so refuse any other shape here, as Python's
// Agent.__init__ does (#502): a short list silently fell back to 1.0 per missing type.
if (!Array.isArray(raw) || raw.length !== 3) {
throw new Error(`Incompatible model: temperature must be a list of 3 floats, got ${JSON.stringify(raw)}`);
}
const rawList: unknown[] = raw;
this.temperature = rawList.map((t) => clampTemperature(t));
this.temperatureByOptionsRaw = (cfg.temperature_by_options ?? {}) as Record<string, unknown>;
this.temperatureByOptions = Object.fromEntries(
Object.entries(this.temperatureByOptionsRaw).map(([k, v]) => [k, clampTemperature(v)]),
Expand All @@ -316,7 +321,7 @@ export class Agent extends HookRegistry {
([k, v]) => [k, v, this.temperatureByOptions[k]] as [string, unknown, number],
),
...[0, 1, 2].map(
(i) => [`temperature[${i}]`, rawList[i] ?? 1.0, this.temperature[i]] as [string, unknown, number],
(i) => [`temperature[${i}]`, rawList[i], this.temperature[i]] as [string, unknown, number],
),
];
const rejected: string[] = [];
Expand Down
10 changes: 10 additions & 0 deletions laya-ts/tests/lang-temperature.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ describe("lang_temperatures", () => {
).toThrow('Language override "de" temperature must be a list of 3 floats');
});

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(
"Incompatible model: temperature must be a list of 3 floats",
);
}
expect(() => new Agent({ provider: fakeProvider(), cfg: { temperature: [1, 2] } } as any)).toThrow("list of 3");
expect(new Agent({ provider: fakeProvider(), temperature: [1, 2, 3] } as any).temperature).toEqual([1, 2, 3]);
});

it("a null entry is an empty override (base raw temperature, no buckets)", async () => {
const a = new Agent({
provider: fakeProvider(),
Expand Down
Loading