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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ Installing the package also installs a `laya` command for quick local testing, n
laya "I was charged twice, please refund" # routing decision only; works offline, no download
laya "Refactor this service" --predict # full answers (downloads the checkpoint on first use)
laya "Mein Konto wurde zweimal belastet" --lang de # force a language instead of detecting it
laya "My payment failed twice" --model ml # pin a checkpoint: names, aliases and casing all resolve as the SDK resolves them
laya "My payment failed twice" --preset triage # answer a ready-made preset (triage, email, guard, moderation, router)
laya # interactive mode
```
Expand Down
26 changes: 24 additions & 2 deletions laya/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -39,6 +40,26 @@
}


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",
Expand All @@ -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",
Expand Down
62 changes: 62 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,68 @@ def route(self, state, **kwargs):
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):
Expand Down
Loading