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(cli): --model takes core's checkpoint registry, aliases included

Aashish98d95f022f7f
diff --git a/laya/cli.py b/laya/cli.py
index 9f6f9868..a9d23b63 100644
--- a/laya/cli.py
+++ b/laya/cli.py
@@ -4,6 +4,7 @@
laya "Refactor this service" --predict # full answers, loads the checkpoint
laya # interactive mode
laya "Mein Konto wurde zweimal belastet" --lang de # explicit language
+ laya "My payment failed twice" --model ml # pin a checkpoint, by name or alias
laya "My payment failed twice" --preset triage # a ready-made question preset
Routing (the default) never downloads a checkpoint, so it works offline and
@@ -39,6 +40,26 @@ PRESET_STATE_KEYS = {
}
+def model_name(value):
+ """Resolve a `--model` argument the way core resolves it: same names, same aliases, same casing.
+
+ ``argparse``'s ``choices=`` compared strings exactly, so ``--model en`` was rejected with
+ "invalid choice" even though the call this flag feeds -- ``router.predict(model=...)`` --
+ has always accepted it. ``laya.router.normalise_name`` is the one registry of checkpoints and
+ aliases, so delegating here cannot fall behind it, and the name that reaches the router is
+ already canonical: what a caller prints is the checkpoint, not its spelling. ``auto`` means
+ "do not pin one", which is what omitting the flag means, so it maps to ``None``.
+ """
+ if value.strip().lower() == "auto":
+ return None
+ from laya.router import normalise_name
+
+ try:
+ return normalise_name(value)
+ except ValueError as error:
+ raise argparse.ArgumentTypeError("%s, or 'auto'" % error) from None
+
+
def build_parser():
parser = argparse.ArgumentParser(
prog="laya",
@@ -47,8 +68,9 @@ def build_parser():
parser.add_argument("text", nargs="*", help="the request text (omit for interactive mode)")
parser.add_argument("--predict", action="store_true",
help="run the full prediction, not just the routing decision (downloads the checkpoint on first use)")
- parser.add_argument("--model", choices=sorted(laya.DEFAULT_MODELS),
- help="force a checkpoint instead of auto-routing")
+ parser.add_argument("--model", type=model_name, metavar="NAME",
+ help="force a checkpoint instead of auto-routing: a checkpoint name or any of "
+ "core's aliases, in any casing, or 'auto' to route it (the default)")
parser.add_argument("--lang", help="force a language, e.g. en or de, instead of detecting it")
parser.add_argument("--task", help="force a typed-decisions workflow instead of detecting it")
parser.add_argument("--preset", choices=sorted(PRESETS), metavar="NAME",
diff --git a/tests/test_cli.py b/tests/test_cli.py
index a4b3262a..afeeb3fd 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -84,6 +84,68 @@ check("error: points at the fix", "Hugging Face hub" in err, err)
code, out, err, stub = run_cli(["--model", "english", "charged twice"])
check("flags: --model forwarded", stub.route_calls[0][1]["model"] == "english")
+# ------------------------------------------------- --model is core's registry, not a copy of it
+# `choices=` compared strings exactly, so the flag knew three spellings while the router it feeds
+# has always resolved a dozen more. The names and aliases are imported from `laya.router` rather
+# than written out, so this sweep follows the registry instead of drifting from it.
+from laya.router import DEFAULT_MODELS, _ALIASES # noqa: E402
+
+
+def parse_model(argv):
+ """What `--model` settles to, or "REJECTED" when argparse exits 2 on it.
+
+ The value is produced by the real parser `main()` uses, so these checks cover the flag as a
+ user types it; `run_cli` cannot see it, because argparse stops before the router is called.
+ """
+ try:
+ with redirect_stderr(io.StringIO()):
+ return cli.build_parser().parse_args(argv).model
+ except SystemExit:
+ return "REJECTED"
+
+
+for name in sorted(DEFAULT_MODELS):
+ got = parse_model(["t", "--model", name])
+ check("model: %s accepted" % name, got == name, repr(got))
+for alias, canonical in sorted(_ALIASES.items()):
+ got = parse_model(["t", "--model", alias])
+ check("model: alias %s is %s" % (alias, canonical), got == canonical, repr(got))
+for spelling, want in (("EN", "english"), (" Multilingual ", "multilingual"),
+ ("Typed-Decisions", "typed-decisions"), ("laya-MULTILINGUAL", "multilingual")):
+ got = parse_model(["t", "--model", spelling])
+ check("model: casing and spacing %r" % spelling, got == want, repr(got))
+for auto in ("auto", "AUTO", " Auto "):
+ got = parse_model(["t", "--model", auto])
+ check("model: %r pins nothing" % auto, got is None, repr(got))
+check("model: omitting it pins nothing either", parse_model(["t"]) is None)
+for bad in ("gpt4", "english-ish", "laya-typed", ""):
+ got = parse_model(["t", "--model", bad])
+ check("model: %r rejected" % bad, got == "REJECTED", repr(got))
+
+# The message is now core's, so it has to carry everything a user can name.
+err = io.StringIO()
+try:
+ with redirect_stderr(err):
+ cli.build_parser().parse_args(["t", "--model", "gpt4"])
+except SystemExit:
+ pass
+text = err.getvalue()
+check("model: error names the checkpoints",
+ all(n in text for n in sorted(DEFAULT_MODELS)), text)
+check("model: error names the aliases", "alias" in text and "'en'" in text, text)
+check("model: error names auto", "'auto'" in text, text)
+
+# And what reaches the router is the canonical name, whichever spelling was typed.
+code, out, err, stub = run_cli(["charged twice", "--model", "en"])
+check("model: alias forwarded canonical to route",
+ code == 0 and stub.route_calls[0][1]["model"] == "english", str(stub.route_calls))
+code, out, err, stub = run_cli(["charged twice", "--predict", "--model", "ML"])
+check("model: alias forwarded canonical to predict",
+ code == 0 and stub.predict_calls[0][1]["model"] == "multilingual", str(stub.predict_calls))
+code, out, err, stub = run_cli(["charged twice", "--model", "auto"])
+check("model: auto forwarded as no pin",
+ code == 0 and stub.route_calls[0][1]["model"] is None, str(stub.route_calls))
+
# --------------------------------------------------------------------- presets
class QuestionRecorder:
def __init__(self):