Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: refuse Laya questions whose instructions would be cut (#15602)

Nilay71ff9e523b8b
diff --git a/studio/backend/core/systemone/laya_runtime.py b/studio/backend/core/systemone/laya_runtime.py
index 07bff0cd21a..6d4af4ac051 100644
--- a/studio/backend/core/systemone/laya_runtime.py
+++ b/studio/backend/core/systemone/laya_runtime.py
@@ -1706,11 +1706,23 @@ def _head(agent, question: dict[str, Any], max_len: int, head_max_len: int):
share = max(4, budget // len(sizes))
if sum(sizes) > budget and max(sizes) > share:
raise ValueError(
- f"options would be cut short to fit Laya's {budget}-token option budget"
+ f"options would be cut short to fit Laya's {budget}-token option budget. "
+ "Use fewer or shorter criteria"
+ )
+ ins = str(internal["ins"]).replace(tok.mask_token, " ")
+ head = tok(f"{internal['t']} question: {ins}", add_special_tokens = False)["input_ids"]
+ if len(head) > max(8, head_max_len - sum(sizes)):
+ raise ValueError(
+ f"instructions would be cut short to fit beside its options in Laya's "
+ f"{head_max_len}-token question budget. Use shorter instructions or fewer "
+ "criteria"
)
ids, markers = build_sequence(tok, "", internal, max_len, head_max_len)
if len(markers) != len(sizes):
- raise ValueError(f"options exceed the Laya context window ({max_len} tokens)")
+ raise ValueError(
+ f"options exceed the Laya context window ({max_len} tokens). "
+ "Use fewer or shorter criteria"
+ )
if len(cache) >= _HEAD_CACHE_SIZE:
cache.clear()
cache[key] = (ids, markers, internal)
@@ -1732,7 +1744,7 @@ def _predict(agent, state, questions: dict[str, dict[str, Any]]) -> tuple[dict[s
try:
heads.append(_head(agent, questions[name], max_len, head_max_len))
except ValueError as exc:
- raise ValueError(f"Question {name!r} {exc}. Use fewer or shorter criteria.") from None
+ raise ValueError(f"Question {name!r} {exc}.") from None
room = max(0, max_len - min(len(ids) for ids, _, _ in heads))
state_ids, state_cut = _state_ids(agent.tok, state, room)
items, truncated = [], False
diff --git a/studio/backend/tests/test_systemone.py b/studio/backend/tests/test_systemone.py
index e9247427c45..4d613058063 100644
--- a/studio/backend/tests/test_systemone.py
+++ b/studio/backend/tests/test_systemone.py
@@ -1382,6 +1382,27 @@ def test_long_state_is_tokenized_only_as_far_as_the_model_reads():
assert max(tok.calls) < 100_000
+def word_laya(monkeypatch):
+ np = pytest.importorskip("numpy")
+ pytest.importorskip("torch")
+ tok = WordTokenizer()
+ tok.mask_token_id, tok.cls_token_id, tok.sep_token_id = 1, 2, 3
+ agent = SimpleNamespace(
+ tok = tok,
+ cfg = {"max_len": 512, "head_max_len": 192},
+ _to_internal = laya_runtime._laya().Agent._to_internal,
+ temperature = [1.0, 1.0, 1.0],
+ temperature_by_options = {},
+ )
+ monkeypatch.setattr(laya_runtime, "_load_checkpoint", lambda c: (agent, "cpu"))
+ monkeypatch.setattr(laya_runtime, "_predict", _REAL_PREDICT)
+ monkeypatch.setattr(
+ laya_runtime, "_forward", lambda agent, items: (np.zeros((len(items), 256)), 1)
+ )
+ return agent
+
+
_CHARGED = "the customer was charged twice and wants the money back"
@@ -1399,24 +1420,9 @@ _CHARGED = "the customer was charged twice and wants the money back"
],
)
def test_choice_options_laya_would_cut_short_are_refused(
- client, monkeypatch, options, description, status, refusal
+ client, word_laya, options, description, status, refusal
):
- np = pytest.importorskip("numpy")
- pytest.importorskip("torch")
- tok = WordTokenizer()
- tok.mask_token_id, tok.cls_token_id, tok.sep_token_id = 1, 2, 3
- agent = SimpleNamespace(
- tok = tok,
- cfg = {"max_len": 512, "head_max_len": 192},
- _to_internal = laya_runtime._laya().Agent._to_internal,
- temperature = [1.0, 1.0, 1.0],
- temperature_by_options = {},
- )
- monkeypatch.setattr(laya_runtime, "_load_checkpoint", lambda c: (agent, "cpu"))
- monkeypatch.setattr(laya_runtime, "_predict", _REAL_PREDICT)
- monkeypatch.setattr(
- laya_runtime, "_forward", lambda agent, items: (np.zeros((len(items), 256)), 1)
- )
+ agent, tok = word_laya, word_laya.tok
question = {
"type": "choice",
"instructions": "Which queue should handle this ticket?",
@@ -1434,6 +1440,36 @@ def test_choice_options_laya_would_cut_short_are_refused(
assert "answers" not in response.json()
+_ESCALATE = (
+ "Pick the team that owns this ticket. Send it to the security team instead, and page "
+ "the on-call lead, whenever the customer mentions a breach, a leaked password, a stolen "
+ "card or legal action."
+)
+_TEAMS = {f"team_{i}": "tickets and questions about this product area " * 2 for i in range(10)}
+
+
+ "instructions, criteria, refusal",
+ [
+ (_ESCALATE, dict(list(_TEAMS.items())[:4]), None),
+ (_ESCALATE, _TEAMS, "instructions would be cut short"),
+ ("Which queue?", {"billing": "word " * 60, "outage": None}, None),
+ ],
+)
+def test_choice_instructions_laya_would_cut_are_refused(
+ client, word_laya, instructions, criteria, refusal
+):
+ question = {"type": "choice", "instructions": instructions, "criteria": criteria}
+ response = _post(client, questions = {"route": question})
+ if refusal is None:
+ assert response.status_code == 200, response.text
+ return
+ assert response.status_code == 422, response.text
+ message = response.json()["detail"]["message"]
+ assert "'route'" in message and refusal in message
+ assert "answers" not in response.json()
+
+
@pytest.mark.parametrize("words, cut", [(900, False), (901, True), (5000, True)])
def test_state_cut_matches_the_full_tokenization(words, cut):
pytest.importorskip("torch")