Skip to content

laya-ts: per-language temperature overrides (lang_temperatures) + Router lang forwarding - #398

Merged
NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
aashish254:feat-ts-lang-temperatures
Sep 24, 2026
Merged

NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
aashish254:feat-ts-lang-temperatures

Conversation

@aashish254

@aashish254 aashish254 commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Why

The Python Agent accepts lang_temperatures (laya/agent.py:357-366) — per-language
temperature overrides fitted on multilingual evals — and Router.predict forwards the
request language (lang argument, else the language detected for routing) into
system_one so the override actually applies (laya/router.py:532-534). The TS port had
zero language-aware calibration: AgentOptions had no lang_temperatures,
systemOne had no lang, and Router.predict dropped the language on the floor after
routing. This PR ports the whole path.

What

  • AgentOptions.lang_temperatures — { de: { temperature: [t_choice, t_score, t_noul], temperature_by_options: {...} } }, same shape as Python. Constructor
    normalisation mirrors Agent.__init__: keys normalise to the base subtag
    (de-AT -> de), an omitted temperature inherits the base raw temperature, every
    value is clampTemperature-clamped, and a temperature that is not a list of 3 floats
    throws immediately.
  • Decode site (_decode_answers equivalent) — a per-request lang selects the
    matching override, normalised to its base subtag on lookup too (de-CH -> de).
    Parity with Python: a matching override replaces the temperature scale wholesale
    (its own temperature_by_options first, then its 3-slot temperature) — it is not
    merged with the base config, so an override without buckets intentionally ignores the
    base per-bucket entries.
  • PredictOptions.lang on systemOne / predict — routing alone never sets it,
    exactly like Python.
  • Agent.load(..., { lang_temperatures }) — passthrough so a loaded agent can be
    calibrated the same way as a constructed one.
  • Router.predict — forwards opts.lang ?? detected language into systemOne,
    matching router.py predict. Two parity notes:
    • TS analyse() names English "en" where Python's analyse returns None (it only
      ever names non-English languages, laya/lang.py:344,368). A detected "en" is
      therefore forwarded as null; only an explicit lang="en" can select an "en"
      override, exactly as in Python. (The "en"-vs-None asymmetry in the detection
      payload itself is pre-existing and untouched here.)
    • Python's TypeError retry for duck-typed agents without a lang kwarg is
      unnecessary in TS — extra arguments are ignored, so Router.predict keeps working
      with plain { systemOne(s, q) } agent-like objects (covered by the stub-agent tests).

predictBatch (#329) is intentionally unchanged; per-request lang grouping there is the
TS counterpart of #381 and lands as a follow-up once both settle.

Merge-order note (verified with git merge-tree): clean against #386
(answer_confidence — same file, different hunks). Textual conflict with #329 in
agent.ts only, and it is mechanical, not semantic: #329 refactors _systemOneCore
into _systemOneMany / _encodeItems / _decodeAnswers while this PR edits
_systemOneCore in place. Whichever lands second, I rebase — resolution is just moving
the lang param and the scale-selection lines into the refactored decode function.

Tests

tests/lang-temperature.test.ts (11 tests, mirroring tests/test_system_one_lang.py):
key normalisation on both sides, default-to-base temperature, clamping, wholesale scale
replacement (base temperature_by_options must not leak into an overridden language),
buckets-only override inheriting the base raw temperature, invalid-shape throw, null
entry as empty override, and Router forwarding (explicit wins, detected non-English
forwarded, detected English -> null, explicit "en" verbatim).

Verified with npx vitest run (176/176) and npx tsc --noEmit (clean).

@NandhaKishorM

Copy link
Copy Markdown
Owner

Thank you, this is a faithful port. TypeScript users can now apply per-language temperature overrides, and the Router passes the request language through, just like Python. The tests that pin the full scale replacement and the English case make the matching behaviour easy to trust. Merging, and #329 can rebase on top so its decode helper takes the lang argument.

@NandhaKishorM
NandhaKishorM merged commit cd53df3 into NandhaKishorM:main Sep 24, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants