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: router sample on the presets package

Adeshina Lasisi012bfef194e3
diff --git a/laya-go/samples/internal/gateway/gateway.go b/laya-go/samples/internal/gateway/gateway.go
new file mode 100644
index 00000000..01670a2f
--- /dev/null
+++ b/laya-go/samples/internal/gateway/gateway.go
@@ -0,0 +1,119 @@
+// Package gateway is the policy half of the router sample: it turns the answers of
+// presets.Router into a model tier and prints them as examples/29_presets_model_router.py does.
+package gateway
+
+import (
+ "fmt"
+ "io"
+
+ "github.com/NandhaKishorM/laya/laya-go/schema"
+)
+
+// Request is a labelled request to route.
+type Request struct{ Label, Text string }
+
+// Requests are the three requests of Python example 29, in its order.
+var Requests = []Request{
+ {"trivial lookup", "What is the capital of Portugal?"},
+ {"hard refactor", "Refactor this 900-line Django view into service classes with dependency " +
+ "injection, keeping all existing tests green and preserving transaction " +
+ "boundaries."},
+ {"sensitive legal", "My employer is withholding my final paycheck after I reported a safety " +
+ "violation. Should I sue, and can I claim damages under California law?"},
+}
+
+// Router is the four router answers, read by their types.
+type Router struct {
+ Difficulty float64
+ Domain string
+ DomainP float64
+ NeedsTools float64
+ IsSensitive float64
+}
+
+// Read pulls the router answers out of a prediction.
+func Read(answers schema.Answers) (Router, error) {
+ var r Router
+ d, ok := answers.Get("difficulty")
+ if !ok {
+ return r, fmt.Errorf("gateway: no difficulty answer")
+ }
+ ds, ok := d.(*schema.ScoreAnswer)
+ if !ok {
+ return r, fmt.Errorf("gateway: difficulty is %T, not a score", d)
+ }
+ r.Difficulty = ds.Score
+ c, ok := answers.Get("domain")
+ if !ok {
+ return r, fmt.Errorf("gateway: no domain answer")
+ }
+ cs, ok := c.(*schema.ChoiceAnswer)
+ if !ok {
+ return r, fmt.Errorf("gateway: domain is %T, not a choice", c)
+ }
+ // The first label with the highest probability, as Python's max over the dict.
+ for i, p := range cs.Probabilities {
+ if i == 0 || p.P > r.DomainP {
+ r.Domain, r.DomainP = p.Label.String(), p.P
+ }
+ }
+ for _, f := range []struct {
+ id string
+ dst *float64
+ }{{"needs_tools", &r.NeedsTools}, {"is_sensitive", &r.IsSensitive}} {
+ a, ok := answers.Get(f.id)
+ if !ok {
+ return r, fmt.Errorf("gateway: no %s answer", f.id)
+ }
+ n, ok := a.(*schema.NoulAnswer)
+ if !ok {
+ return r, fmt.Errorf("gateway: %s is %T, not a noul", f.id, a)
+ }
+ *f.dst = n.Noul
+ }
+ return r, nil
+}
+
+// Tier is the gateway policy: the cheap model unless the request is sensitive, needs tools or is
+// hard. Sensitivity and tools override difficulty, never the other way round.
+func Tier(r Router) (tier, why string) {
+ switch {
+ case r.IsSensitive >= 0.5:
+ return "frontier", fmt.Sprintf("sensitive: money/legal/medical/safety (p=%.2f)", r.IsSensitive)
+ case r.NeedsTools >= 0.5:
+ return "frontier+tools", fmt.Sprintf("needs external data (p=%.2f)", r.NeedsTools)
+ case r.Difficulty >= 1.5:
+ return "frontier", fmt.Sprintf("difficulty %.2f >= 1.5", r.Difficulty)
+ }
+ return "small", fmt.Sprintf("difficulty %.2f < 1.5, no tools, not sensitive", r.Difficulty)
+}
+
+// Row is one line of the side-by-side table.
+func Row(label string, r Router) string {
+ return fmt.Sprintf(" %-17s %.2f / 3 %-16s %-11.3f %.3f", label, r.Difficulty,
+ fmt.Sprintf("%s (%.2f)", r.Domain, r.DomainP), r.NeedsTools, r.IsSensitive)
+}
+
+// Report prints the side-by-side table, the policy output and the closing notes. results is in
+// Requests order.
+func Report(w io.Writer, results []Router) {
+ p := func(format string, a ...any) { fmt.Fprintf(w, format+"\n", a...) }
+ p("-- side by side --")
+ p(" %-17s %-10s %-16s %-11s %s", "request", "difficulty", "domain", "needs_tools", "is_sensitive")
+ for i, req := range Requests {
+ p("%s", Row(req.Label, results[i]))
+ }
+ p("-- policy output --")
+ for i, req := range Requests {
+ tier, why := Tier(results[i])
+ p(" %-17s -> %-13s %s", req.Label, tier, why)
+ }
+ triv, sens := results[0], results[2]
+ p(" The threshold 1.5 sits between the lookup cluster and the reasoning end of the scale.")
+ p(" Two honest notes. First, `difficulty` is an expected level over four buckets, so even")
+ p(" a one-line lookup scores %.2f -- the zero point is \"trivial\", not \"easy\".", triv.Difficulty)
+ p(" Second, the sensitive request scores only %.2f on difficulty, so difficulty alone", sens.Difficulty)
+ p(" would send it to the cheap model; `is_sensitive` at %.3f is what promotes it. A", sens.IsSensitive)
+ p(" gateway should always let sensitivity and tool needs override the difficulty score,")
+ p(" never the other way round.")
+}
diff --git a/laya-go/samples/internal/gateway/gateway_test.go b/laya-go/samples/internal/gateway/gateway_test.go
new file mode 100644
index 00000000..84c6700b
--- /dev/null
+++ b/laya-go/samples/internal/gateway/gateway_test.go
@@ -0,0 +1,97 @@
+package gateway
+
+import (
+ "bytes"
+ "encoding/json"
+ "os"
+ "strings"
+ "testing"
+
+ "github.com/NandhaKishorM/laya/laya-go/schema"
+)
+
+type golden struct {
+ Name string
+ Difficulty float64
+ Domains json.RawMessage
+ NeedsTools float64 `json:"needs_tools"`
+ IsSensitive float64 `json:"is_sensitive"`
+ Tier string
+ Why string
+ Row string
+}
+
+// The golden is the tier policy and the table row of Python example 29 on the same numbers,
+// recorded by scripts/gen_router_policy_golden.py.
+func TestPolicyAndRowMatchPython(t *testing.T) {
+ data, err := os.ReadFile("testdata/policy.json")
+ if err != nil {
+ t.Fatal(err)
+ }
+ var cases []golden
+ if err := json.Unmarshal(data, &cases); err != nil {
+ t.Fatal(err)
+ }
+ if len(cases) != 7 {
+ t.Fatalf("%d cases", len(cases))
+ }
+ for _, c := range cases {
+ var probs schema.Probabilities
+ if err := json.Unmarshal(c.Domains, &probs); err != nil {
+ t.Fatal(err)
+ }
+ answers := schema.Answers{
+ {ID: "difficulty", Answer: &schema.ScoreAnswer{Score: c.Difficulty}},
+ {ID: "domain", Answer: &schema.ChoiceAnswer{Choice: probs[0].Label, Probabilities: probs}},
+ {ID: "needs_tools", Answer: &schema.NoulAnswer{Noul: c.NeedsTools}},
+ {ID: "is_sensitive", Answer: &schema.NoulAnswer{Noul: c.IsSensitive}},
+ }
+ r, err := Read(answers)
+ if err != nil {
+ t.Fatalf("%s: %v", c.Name, err)
+ }
+ tier, why := Tier(r)
+ if tier != c.Tier || why != c.Why {
+ t.Errorf("%s: tier = %q %q, want %q %q", c.Name, tier, why, c.Tier, c.Why)
+ }
+ if got := Row("label", r); got != c.Row {
+ t.Errorf("%s: row\n got %q\nwant %q", c.Name, got, c.Row)
+ }
+ }
+}
+
+func TestReadRejectsWrongAnswerTypes(t *testing.T) {
+ good := schema.Answers{
+ {ID: "difficulty", Answer: &schema.ScoreAnswer{}},
+ {ID: "domain", Answer: &schema.ChoiceAnswer{}},
+ {ID: "needs_tools", Answer: &schema.NoulAnswer{}},
+ {ID: "is_sensitive", Answer: &schema.NoulAnswer{}},
+ }
+ if _, err := Read(good); err != nil {
+ t.Fatal(err)
+ }
+ for i, wrong := range []schema.Answer{&schema.NoulAnswer{}, &schema.NoulAnswer{}, &schema.ScoreAnswer{}, &schema.ScoreAnswer{}} {
+ bad := append(schema.Answers(nil), good...)
+ bad[i].Answer = wrong
+ if _, err := Read(bad); err == nil {
+ t.Errorf("%s as %T: no error", bad[i].ID, wrong)
+ }
+ if _, err := Read(append(schema.Answers(nil), good[:i]...)); err == nil {
+ t.Errorf("missing %s: no error", good[i].ID)
+ }
+ }
+}
+
+func TestReportShape(t *testing.T) {
+ var b bytes.Buffer
+ r := Router{Difficulty: 0.5, Domain: "code", DomainP: 0.9}
+ Report(&b, []Router{r, r, {Difficulty: 0.9, IsSensitive: 0.97}})
+ out := b.String()
+ for _, want := range []string{"trivial lookup", "hard refactor", "sensitive legal",
+ "sensitive legal -> frontier sensitive: money/legal/medical/safety (p=0.97)",
+ "even\n a one-line lookup scores 0.50", "only 0.90 on difficulty", "`is_sensitive` at 0.970"} {
+ if !strings.Contains(out, want) {
+ t.Errorf("report lacks %q:\n%s", want, out)
+ }
+ }
+}
diff --git a/laya-go/samples/internal/gateway/testdata/policy.json b/laya-go/samples/internal/gateway/testdata/policy.json
new file mode 100644
index 00000000..99a77ea9
--- /dev/null
+++ b/laya-go/samples/internal/gateway/testdata/policy.json
@@ -0,0 +1,121 @@
+[
+ {
+ "name": "small",
+ "difficulty": 0.4,
+ "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.1,
+ "tier": "small",
+ "why": "difficulty 0.40 < 1.5, no tools, not sensitive",
+ "row": " label 0.40 / 3 writing (0.30) 0.100 0.100"
+ },
+ {
+ "name": "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.2,
+ "is_sensitive": 0.3,
+ "tier": "frontier",
+ "why": "difficulty 2.20 >= 1.5",
+ "row": " label 2.20 / 3 writing (0.30) 0.200 0.300"
+ },
+ {
+ "name": "at-difficulty-threshold",
+ "difficulty": 1.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.2,
+ "is_sensitive": 0.3,
+ "tier": "frontier",
+ "why": "difficulty 1.50 >= 1.5",
+ "row": " label 1.50 / 3 writing (0.30) 0.200 0.300"
+ },
+ {
+ "name": "just-under-threshold",
+ "difficulty": 1.4999,
+ "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.2,
+ "is_sensitive": 0.3,
+ "tier": "small",
+ "why": "difficulty 1.50 < 1.5, no tools, not sensitive",
+ "row": " label 1.50 / 3 writing (0.30) 0.200 0.300"
+ },
+ {
+ "name": "tools",
+ "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.5,
+ "is_sensitive": 0.49,
+ "tier": "frontier+tools",
+ "why": "needs external data (p=0.50)",
+ "row": " label 0.20 / 3 writing (0.30) 0.500 0.490"
+ },
+ {
+ "name": "sensitive-overrides-tools",
+ "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.9,
+ "is_sensitive": 0.5,
+ "tier": "frontier",
+ "why": "sensitive: money/legal/medical/safety (p=0.50)",
+ "row": " label 0.20 / 3 writing (0.30) 0.900 0.500"
+ },
+ {
+ "name": "sensitive-easy",
+ "difficulty": 0.1,
+ "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.0,
+ "is_sensitive": 0.97,
+ "tier": "frontier",
+ "why": "sensitive: money/legal/medical/safety (p=0.97)",
+ "row": " label 0.10 / 3 writing (0.30) 0.000 0.970"
+ }
+]
diff --git a/laya-go/samples/router/main.go b/laya-go/samples/router/main.go
new file mode 100644
index 00000000..3f49a11c
--- /dev/null
+++ b/laya-go/samples/router/main.go
@@ -0,0 +1,65 @@
+// Router routes three requests to a model tier with the router preset, as Python example 29 does.
+//
+// go run ./router <checkpoint-dir> <graph-dir-or-laya.onnx> [onnxruntime-library]
+package main
+
+import (
+ "context"
+ "fmt"
+ "os"
+ "path/filepath"
+
+ "github.com/NandhaKishorM/laya/laya-go/agent"
+ "github.com/NandhaKishorM/laya/laya-go/lang"
+ "github.com/NandhaKishorM/laya/laya-go/onnx"
+ "github.com/NandhaKishorM/laya/laya-go/presets"
+ "github.com/NandhaKishorM/laya/laya-go/samples/internal/gateway"
+)
+
+func main() {
+ if err := run(os.Args[1:]); err != nil {
+ fmt.Fprintln(os.Stderr, "router:", err)
+ os.Exit(1)
+ }
+}
+
+func run(args []string) error {
+ if len(args) < 2 {
+ return fmt.Errorf("usage: router <checkpoint-dir> <graph-dir-or-laya.onnx> [onnxruntime-library]")
+ }
+ graph := args[1]
+ if fi, err := os.Stat(graph); err == nil && fi.IsDir() {
+ graph = filepath.Join(graph, "laya.onnx")
+ }
+ var opts []onnx.Option
+ if len(args) > 2 {
+ opts = append(opts, onnx.WithLibrary(args[2]))
+ }
+ sess, err := onnx.Open(graph, opts...)
+ if err != nil {
+ return err
+ }
+ // The agent owns the session; closing the agent closes it.
+ a, err := agent.Open(os.DirFS(args[0]), sess)
+ if err != nil {
+ _ = sess.Close()
+ return err
+ }
+ defer a.Close()
+
+ qs := presets.Router()
+ ctx := context.Background()
+ results := make([]gateway.Router, len(gateway.Requests))
+ for i, req := range gateway.Requests {
+ // The state is an ordered object: the model sees keys in the order given.
+ pred, err := a.Predict(ctx, lang.Object{{Key: "request", Value: req.Text}}, qs)
+ if err != nil {
+ return fmt.Errorf("%s: %w", req.Label, err)
+ }
+ if results[i], err = gateway.Read(pred.Answers); err != nil {
+ return fmt.Errorf("%s: %w", req.Label, err)
+ }
+ }
+ gateway.Report(os.Stdout, results)
+ return nil
+}
diff --git a/laya-go/samples/scripts/gen_router_policy_golden.py b/laya-go/samples/scripts/gen_router_policy_golden.py
new file mode 100644
index 00000000..4ee02cd6
--- /dev/null
+++ b/laya-go/samples/scripts/gen_router_policy_golden.py
@@ -0,0 +1,81 @@
+#!/usr/bin/env python3
+"""Record internal/gateway/testdata/policy.json: the tier policy and table row of
+examples/29_presets_model_router.py on synthetic answers, with the example's own formatting.
+
+ python laya-go/samples/scripts/gen_router_policy_golden.py # write
+ python laya-go/samples/scripts/gen_router_policy_golden.py --check # fail if the file differs
+"""
+import argparse
+import json
+import pathlib
+import sys
+
+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 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 case(d, probs, tools, sens):
+ return {"difficulty": {"score": d}, "domain": {"probabilities": probs},
+ "needs_tools": {"noul": tools}, "is_sensitive": {"noul": sens}}
+
+
+DOMAINS = {"code": 0.1, "math_or_logic": 0.2, "writing": 0.3, "factual_lookup": 0.3, "data_analysis": 0.05,
+ "chitchat": 0.05}
+CASES = [
+ ("small", case(0.4, DOMAINS, 0.1, 0.1)),
+ ("hard", case(2.2, DOMAINS, 0.2, 0.3)),
+ ("at-difficulty-threshold", case(1.5, DOMAINS, 0.2, 0.3)),
+ ("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)),
+ ("sensitive-easy", case(0.1, DOMAINS, 0.0, 0.97)),
+]
+
+
+def render():
+ out = []
+ for name, a in CASES:
+ tier, why = route_tier(a)
+ out.append({"name": name, "difficulty": a["difficulty"]["score"], "domains": a["domain"]["probabilities"],
+ "needs_tools": a["needs_tools"]["noul"], "is_sensitive": a["is_sensitive"]["noul"],
+ "tier": tier, "why": why, "row": row("label", a)})
+ return json.dumps(out, ensure_ascii=False, indent=1) + "\n"
+
+
+def main():
+ parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
+ parser.add_argument("--check", action="store_true")
+ args = parser.parse_args()
+ text = render()
+ if args.check:
+ if not OUT.exists() or OUT.read_text(encoding="utf-8") != text:
+ print("%s differs" % OUT)
+ return 1
+ print("%s matches" % OUT)
+ return 0
+ OUT.write_text(text, encoding="utf-8")
+ return 0
+
+
+if __name__ == "__main__":
+ sys.exit(main())