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
19 changes: 8 additions & 11 deletions laya-ts/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -639,13 +639,13 @@ export class Router extends HookRegistry {
checkQuestion(qid, qdef);
}
// Python parity (router.py predict): the request's language also shapes the answer
// distribution through the agent's lang_temperatures. An explicit lang wins;
// otherwise forward the language the router detected for the routing decision.
// TS analyse() names English "en" where Python's analyse returns None (it only
// ever names non-English), so a detected "en" forwards as null — in Python only an
// explicit lang="en" can select an "en" override.
// distribution through the agent's lang_temperatures. An explicit lang wins; otherwise
// forward the language the router detected for the routing decision, "en" included --
// Python's `analyse` names English "en" too (laya/lang.py, `_latin_profile`), and
// `lang_temperatures` may carry an "en" entry, so replacing it with null made that
// override dead code on this path while the same agent honoured it when called directly.
const detected = decision.detection?.language;
const effectiveLang = opts.lang ?? (detected && detected !== "en" ? detected : null);
const effectiveLang = opts.lang ?? detected ?? null;
const agentOpts: PredictOptions = { lang: effectiveLang };
if (mc !== null) agentOpts.minConfidence = mc;
markDefaultsRan(agentOpts);
Expand Down Expand Up @@ -900,12 +900,9 @@ export class Router extends HookRegistry {
continue;
}
// An explicit `lang` wins; otherwise forward the language the router detected for the
// routing decision. TS analyse() names English "en" where Python's returns None (it
// only ever names non-English), so a detected "en" forwards as null — in Python only
// an explicit lang="en" can select an "en" override. Same rule as `predict`.
// routing decision, "en" included, exactly as `predict` and Python's predict_batch do.
const detectedLang = decisions[i].detection?.language ?? null;
const effectiveLang =
requests[i].lang ?? (detectedLang && detectedLang !== "en" ? detectedLang : null);
const effectiveLang = requests[i].lang ?? detectedLang;
const langKey = hasLangOverrides ? effectiveLang : null;
const schema = questionSchema(ctx.questions);
const found = questionGroups.find(
Expand Down
27 changes: 23 additions & 4 deletions laya-ts/tests/lang-temperature.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,15 @@ describe("Router.predict lang forwarding", () => {
expect(seen).toEqual([{ lang: "bn" }]);
});

it("forwards null when nothing non-English was detected", async () => {
it("forwards the detected \"en\", so a lang_temperatures.en override is applied", async () => {
// Python parity (laya/lang.py): `analyse` names English "en", and router.py forwards
// `decision.detection.language` without a special case, so an `en` entry in
// `lang_temperatures` is applied there. This path replaced "en" with null, which made that
// override dead code on the Router while the same agent honoured it when called directly.
const seen: any[] = [];
const r = routerRecording(seen);
await r.predict("please refund my ticket", questions);
expect(seen).toEqual([{ lang: null }]);
expect(seen).toEqual([{ lang: "en" }]);
});

it("an explicit lang is forwarded verbatim, even \"en\"", async () => {
Expand All @@ -203,6 +207,21 @@ describe("Router.predict lang forwarding", () => {
await r.predict("please refund my ticket", questions, { lang: "en" });
expect(seen).toEqual([{ lang: "en" }]);
});

it("an \"en\" override changes the answer through Router.predict", async () => {
// End-to-end, not just the forwarded option: the same agent answers 0.7311 at temperature
// 2 and 0.8808 at the base 1.0 (see the fixed logit row above), so a dropped "en" is a
// visible difference in the reported probabilities rather than an unused option.
const agent = new Agent({
provider: fakeProvider(),
lang_temperatures: { en: { temperature: [2, 2, 2] } },
} as any);
const r = new Router({ loader: () => agent } as any);
const viaRouter: any = await r.predict("please refund my ticket", CHOICE);
const direct: any = await agent.systemOne("please refund my ticket", CHOICE, { lang: "en" });
expect(viaRouter.answers.d.probabilities.x).toBeCloseTo(0.7311, 4);
expect(viaRouter.answers.d.probabilities.x).toBeCloseTo(direct.answers.d.probabilities.x, 10);
});
});

describe("Router.predictBatch lang forwarding", () => {
Expand Down Expand Up @@ -242,11 +261,11 @@ describe("Router.predictBatch lang forwarding", () => {
expect(seen.map((c) => c.opts.lang)).toEqual(["bn"]);
});

it("forwards no lang when nothing non-English was detected", async () => {
it("forwards the detected \"en\" on the batch path too", async () => {
const seen: any[] = [];
const r = routerRecording(seen);
await r.predictBatch([req("please refund my ticket")]);
expect(seen.map((c) => c.opts.lang)).toEqual([undefined]);
expect(seen.map((c) => c.opts.lang)).toEqual(["en"]);
});

it("does not share a forward pass across languages", async () => {
Expand Down
Loading