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.

laya-go samples: pin policy branch order, extract policy from the example

Adeshina Lasisi4dce0fabdff6
diff --git a/laya-go/samples/internal/gateway/gateway_test.go b/laya-go/samples/internal/gateway/gateway_test.go
index 84c6700b..7db39f5f 100644
--- a/laya-go/samples/internal/gateway/gateway_test.go
+++ b/laya-go/samples/internal/gateway/gateway_test.go
@@ -32,7 +32,7 @@ func TestPolicyAndRowMatchPython(t *testing.T) {
if err := json.Unmarshal(data, &cases); err != nil {
t.Fatal(err)
}
- if len(cases) != 7 {
+ if len(cases) != 10 {
t.Fatalf("%d cases", len(cases))
}
for _, c := range cases {
diff --git a/laya-go/samples/internal/gateway/testdata/policy.json b/laya-go/samples/internal/gateway/testdata/policy.json
index 99a77ea9..c7576163 100644
--- a/laya-go/samples/internal/gateway/testdata/policy.json
+++ b/laya-go/samples/internal/gateway/testdata/policy.json
@@ -101,6 +101,57 @@
"why": "sensitive: money/legal/medical/safety (p=0.50)",
"row": " label 0.20 / 3 writing (0.30) 0.900 0.500"
},
+ {
+ "name": "tools-just-under",
+ "difficulty": 0.2,
+ "domains": {
+ "code": 0.1,
+ "math_or_logic": 0.2,
+ "writing": 0.3,
+ "factual_lookup": 0.3,
+ "data_analysis": 0.05,
+ "chitchat": 0.05
+ },
+ "needs_tools": 0.4999,
+ "is_sensitive": 0.1,
+ "tier": "small",
+ "why": "difficulty 0.20 < 1.5, no tools, not sensitive",
+ "row": " label 0.20 / 3 writing (0.30) 0.500 0.100"
+ },
+ {
+ "name": "tools-overrides-hard",
+ "difficulty": 2.2,
+ "domains": {
+ "code": 0.1,
+ "math_or_logic": 0.2,
+ "writing": 0.3,
+ "factual_lookup": 0.3,
+ "data_analysis": 0.05,
+ "chitchat": 0.05
+ },
+ "needs_tools": 0.9,
+ "is_sensitive": 0.1,
+ "tier": "frontier+tools",
+ "why": "needs external data (p=0.90)",
+ "row": " label 2.20 / 3 writing (0.30) 0.900 0.100"
+ },
+ {
+ "name": "sensitive-overrides-hard",
+ "difficulty": 2.5,
+ "domains": {
+ "code": 0.1,
+ "math_or_logic": 0.2,
+ "writing": 0.3,
+ "factual_lookup": 0.3,
+ "data_analysis": 0.05,
+ "chitchat": 0.05
+ },
+ "needs_tools": 0.1,
+ "is_sensitive": 0.9,
+ "tier": "frontier",
+ "why": "sensitive: money/legal/medical/safety (p=0.90)",
+ "row": " label 2.50 / 3 writing (0.30) 0.100 0.900"
+ },
{
"name": "sensitive-easy",
"difficulty": 0.1,
diff --git a/laya-go/samples/scripts/gen_router_policy_golden.py b/laya-go/samples/scripts/gen_router_policy_golden.py
index 4ee02cd6..fe2ab54d 100644
--- a/laya-go/samples/scripts/gen_router_policy_golden.py
+++ b/laya-go/samples/scripts/gen_router_policy_golden.py
@@ -6,32 +6,52 @@ examples/29_presets_model_router.py on synthetic answers, with the example's own
python laya-go/samples/scripts/gen_router_policy_golden.py --check # fail if the file differs
"""
import argparse
+import ast
import json
import pathlib
import sys
+EXAMPLE = pathlib.Path(__file__).resolve().parents[3] / "examples" / "29_presets_model_router.py"
OUT = pathlib.Path(__file__).resolve().parents[1] / "internal" / "gateway" / "testdata" / "policy.json"
-# Copied from examples/29_presets_model_router.py.
-def route_tier(answers):
- difficulty = answers["difficulty"]["score"]
- sensitive = answers["is_sensitive"]["noul"]
- tools = answers["needs_tools"]["noul"]
- if sensitive >= 0.5:
- return "frontier", "sensitive: money/legal/medical/safety (p=%.2f)" % sensitive
- if tools >= 0.5:
- return "frontier+tools", "needs external data (p=%.2f)" % tools
- if difficulty >= 1.5:
- return "frontier", "difficulty %.2f >= 1.5" % difficulty
- return "small", "difficulty %.2f < 1.5, no tools, not sensitive" % difficulty
+def _str(node):
+ try:
+ v = ast.literal_eval(node)
+ except ValueError:
+ return ""
+ return v if isinstance(v, str) else ""
-def row(label, a):
- domain = max(a["domain"]["probabilities"].items(), key=lambda kv: kv[1])
- return " %-17s %.2f / 3 %-16s %-11.3f %.3f" % (
- label, a["difficulty"]["score"], "%s (%.2f)" % (domain[0], domain[1]),
- a["needs_tools"]["noul"], a["is_sensitive"]["noul"])
+def _load_example():
+ """Take route_tier and the table-row expression from the example's own source via ast."""
+ src = EXAMPLE.read_text(encoding="utf-8")
+ tree = ast.parse(src)
+ fn = [n for n in tree.body if isinstance(n, ast.FunctionDef) and n.name == "route_tier"]
+ assert len(fn) == 1, "route_tier not found in %s" % EXAMPLE
+ ns = {}
+ exec(compile(ast.Module(body=fn, type_ignores=[]), str(EXAMPLE), "exec"), ns)
+ dom = row_expr = None
+ for n in ast.walk(tree):
+ if (isinstance(n, ast.Assign) and len(n.targets) == 1 and isinstance(n.targets[0], ast.Name)
+ and n.targets[0].id == "domain"):
+ dom = ast.Expression(n.value)
+ if (isinstance(n, ast.Call) and isinstance(n.func, ast.Name) and n.func.id == "print" and n.args
+ and isinstance(n.args[0], ast.BinOp) and _str(n.args[0].left).startswith(" %-17s %.2f / 3")):
+ row_expr = ast.Expression(n.args[0])
+ assert dom is not None and row_expr is not None, "table row expression not found in %s" % EXAMPLE
+ dom_c = compile(ast.fix_missing_locations(dom), str(EXAMPLE), "eval")
+ row_c = compile(ast.fix_missing_locations(row_expr), str(EXAMPLE), "eval")
+
+ def row(label, a):
+ env = {"label": label, "a": a}
+ env["domain"] = eval(dom_c, {}, env)
+ return eval(row_c, {}, env)
+
+ return ns["route_tier"], row
+
+
+route_tier, row = _load_example()
def case(d, probs, tools, sens):
@@ -48,6 +68,9 @@ CASES = [
("just-under-threshold", case(1.4999, DOMAINS, 0.2, 0.3)),
("tools", case(0.2, DOMAINS, 0.5, 0.49)),
("sensitive-overrides-tools", case(0.2, DOMAINS, 0.9, 0.5)),
+ ("tools-just-under", case(0.2, DOMAINS, 0.4999, 0.1)),
+ ("tools-overrides-hard", case(2.2, DOMAINS, 0.9, 0.1)),
+ ("sensitive-overrides-hard", case(2.5, DOMAINS, 0.1, 0.9)),
("sensitive-easy", case(0.1, DOMAINS, 0.0, 0.97)),
]