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
7 changes: 4 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ jobs:
--only tokenizer_ids.json --only decode_text.json
--only sequences.json --only decode.json --only window_plan.json
--only predict_long.json --only confidence_gate.json --only hooks.json
--only presets.json --only shortlist.json
- name: Regenerate laya-go's own golden from Python
env:
LAYA_FIXTURE_CHECKPOINTS: ${{ github.workspace }}/laya-java/.work/checkpoints
Expand All @@ -128,11 +129,11 @@ jobs:
LAYA_GO_FIXTURES: ${{ github.workspace }}/.go-goldens
LAYA_CHECKPOINTS: ${{ github.workspace }}/laya-java/.work/checkpoints
run: go test -race -count=1 -json ./... > "$RUNNER_TEMP/go-test.json"
# Exactly the measured count (1603 tests and subtests, on both Go versions): dropping a
# Exactly the measured count (1667 tests and subtests, on both Go versions): dropping a
# case fails here, and adding one means raising this number.
- name: No test was skipped
if: ${{ !cancelled() }}
run: python laya-go/scripts/check_test_json.py "$RUNNER_TEMP/go-test.json" --min-tests 1603
run: python laya-go/scripts/check_test_json.py "$RUNNER_TEMP/go-test.json" --min-tests 1667

# go.mod declares go 1.22, so that is what a consumer may build with.
- name: Run the tests on the oldest supported Go
Expand All @@ -144,7 +145,7 @@ jobs:
run: go test -count=1 -json ./... > "$RUNNER_TEMP/go-test-oldest.json"
- name: No test was skipped on the oldest Go either
if: ${{ !cancelled() }}
run: python laya-go/scripts/check_test_json.py "$RUNNER_TEMP/go-test-oldest.json" --min-tests 1603
run: python laya-go/scripts/check_test_json.py "$RUNNER_TEMP/go-test-oldest.json" --min-tests 1667

- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
if: ${{ failure() }}
Expand Down
4 changes: 3 additions & 1 deletion laya-go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ The foundation (language detection, questions, sequences, the tokenizer) and the
| Package | What it is |
| --- | --- |
| `lang` | Script and language detection, the port of `laya/lang.py`: `DetectScript`, `ScriptProfile`, `LatinProfileOf`, `GuessLatinLanguage`, `Analyze`, `DetectLanguage`, `IsEnglish`. |
| `presets` | Ready-made question sets (`Triage`, `Email`, `Guard`, `Moderation`, `Router`), the port of `laya/presets.py`, and `StateField`, which reads the field a set asks about. |
| `shortlist` | Embedding shortlist for high-cardinality choice questions, the port of `laya/shortlist.py`: `Rank`, `Predict`, `Cached`. `predict_tournament` is not ported. |
| `schema` | Questions (`Choice`, `ChoiceOf`, `Score`, `Noul`, `NoulWith`, `Questions`) and results (`Answer`, `Prediction`, `Usage`) every other package shares. Questions are refused with laya's own wording and read or written in the Python wire form. |
| `sequence` | `build_sequence`, `build_head`, `state_room`, `window_budget` and `window_batch_cap`: `EncodeState`, `Build`, `StateRoom`, `PlanWindows`, `BatchCap`. |
| `infer` | The seam to a model runtime: `Batch` (the graph's five inputs), `Output` (its two), `Session`, and the shape checks `Batch.Validate` and `Output.Check`. |
Expand Down Expand Up @@ -42,7 +44,7 @@ order-sensitive state. `Analyze` reads every leaf, so its cost grows with the nu

- No root package: the module path ends in `laya-go`, so tags are `laya-go/vX.Y.Z`.
- Every package in this module is pure Go: `lang`, `schema`, `sequence`, `tokenizer`, `infer` and
`agent` now, and `router`, `presets` and `shortlist` later.
`agent`, `presets` and `shortlist` now, and `router` later.
- cgo lives only in `onnx`, a nested module with its own `go.mod`, so `go test ./...` here never
compiles it. `onnx` requires this module and `github.com/yalue/onnxruntime_go`; until this module has
a release tag, `onnx` builds only inside this repository (its `go.mod` points here with `replace`). Importing `lang` or `agent` never compiles cgo.
Expand Down
2 changes: 1 addition & 1 deletion laya-go/fixtures/go_extra.json

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions laya-go/presets/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Package presets holds ready-made question sets for common decision workflows: the port of
// laya.presets. Each set is generated from the Python reference by a golden test, word for word,
// because one changed word is a different question put to the model.
//
// A preset says which part of the state it reads by naming it in backticks ("What does the
// customer want in `message`?"); StateField reads that name back out.
//
// qs := presets.Triage()
// field, _ := presets.StateField(qs) // "message"
// pred, err := a.Predict(ctx, map[string]any{field: text}, qs)
//
// # Differences from Python
//
// A Questions value is immutable, so a preset needs no copy: to drop or add a question, build a
// new set from its entries with schema.NewQuestions. Python's email_questions takes a dict of
// categories; EmailWith takes the options in the order to offer them, since a Go map has no order.
// StateField takes typed questions, which always carry instructions; Python's raises on a
// definition without them, and such a definition cannot be built here.
package presets
216 changes: 216 additions & 0 deletions laya-go/presets/presets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
package presets

import (
"github.com/NandhaKishorM/laya/laya-go/internal/unicodedata"
"github.com/NandhaKishorM/laya/laya-go/schema"
)

// Named is one preset with the name laya.presets gives its function.
type Named struct {
Name string
Questions schema.Questions
}

// must builds a preset. A preset is a literal, so a refusal is a bug in this file and a test
// fails on it.
func must(entries ...schema.Entry) schema.Questions {
qs, err := schema.NewQuestions(entries...)
if err != nil {
panic("presets: " + err.Error())
}
return qs
}

func ask(id string, q schema.Question, err error) schema.Entry {
return schema.Ask(id, schema.MustQuestion(q, err))
}

func noul(id, ins string) schema.Entry {
q, err := schema.Noul(ins)
return ask(id, q, err)
}

func score(id, ins string, levels ...any) schema.Entry {
q, err := schema.Score(ins, levels...)
return ask(id, q, err)
}

func choice(id, ins string, opts ...schema.Option) schema.Entry {
q, err := schema.Choice(ins, opts...)
return ask(id, q, err)
}

func opt(label, criterion string) schema.Option {
return schema.Option{Label: label, Criterion: criterion}
}

// Triage is customer support ticket triage: laya.presets.triage_questions.
func Triage() schema.Questions {
return must(
choice("intent", "What does the customer want in `message`?",
opt("refund", "money returned or a duplicate charge reversed"),
opt("technical_help", "a bug, outage or integration problem"),
opt("billing_question", "a question about an invoice, plan or payment method"),
opt("information", "general information, pricing or how-to"),
opt("cancellation", "wants to cancel or downgrade"),
opt("other", "none of the other options fits")),
noul("is_urgent", "Does `message` communicate time pressure or a deadline?"),
score("frustration", "How frustrated does the customer sound in `message`?",
"calm and neutral",
"concerned but civil",
"clearly annoyed",
"very angry or using strong language"),
noul("refund_requested", "Does the customer ask for money back?"),
noul("churn_risk", "Does `message` suggest the customer may leave for a competitor or cancel?"),
)
}

// Email is inbound email triage and threat filtering with the default categories:
// laya.presets.email_questions().
func Email() schema.Questions { return EmailWith(nil) }

// EmailWith is Email with the caller's categories in place of the defaults, offered in the order
// given. They replace the defaults rather than merge with them, and no categories at all is the
// defaults, as Python's `categories or {...}` reads an empty dict.
func EmailWith(categories []schema.Option) schema.Questions {
if len(categories) == 0 {
categories = []schema.Option{
opt("billing", "invoices, payments, refunds"),
opt("technical", "bugs, outages, integrations"),
opt("sales", "pricing, demos, new purchases"),
opt("security", "phishing, scams, account compromise"),
opt("hr", "hiring, leave, payroll"),
opt("other", "none of the above"),
}
}
phishing, err := schema.NoulWith(
"Is this email a phishing or scam attempt to steal money, credentials, or personal data?",
schema.NoulSpec{True: "phishing, scam, or fraud", False: "a legitimate email"})
return must(
choice("category", "Which team should handle the email in `body`?", categories...),
noul("is_spam", "Is this email unsolicited spam or bulk marketing?"),
ask("is_phishing", phishing, err),
score("urgency", "How urgent is the request in `body`?",
"no time pressure", "needs attention soon", "blocking issue or hard deadline"),
noul("needs_reply", "Does the sender expect a reply?"),
)
}

// Guard is real-time guardrails on text going into a language model:
// laya.presets.guard_questions.
func Guard() schema.Questions {
return must(
noul("jailbreak", "Does `prompt` try to make an AI assistant ignore its rules, policies or system instructions?"),
noul("prompt_injection", "Does `prompt` contain instructions aimed at the AI system rather than a genuine user request?"),
noul("sensitive_data", "Does `prompt` contain credentials, personal data or other sensitive information?"),
score("harm_severity", "How much harm would complying with `prompt` cause?",
"none: ordinary request",
"minor: mildly inappropriate",
"serious: unsafe advice or abuse",
"severe: dangerous or illegal"),
// The topic options carry no description and render as their label alone.
choice("topic", "What is `prompt` about?",
schema.Option{Label: "product_support"},
schema.Option{Label: "coding"},
schema.Option{Label: "general_knowledge"},
schema.Option{Label: "personal_advice"},
schema.Option{Label: "security_testing"},
schema.Option{Label: "other"}),
)
}

// Moderation is content safety and moderation: laya.presets.moderation_questions.
func Moderation() schema.Questions {
return must(
noul("toxic", "Is `post` toxic: rude, disrespectful or likely to make someone leave the discussion?"),
noul("harassment", "Does `post` target or harass a specific person?"),
noul("threat", "Does `post` threaten violence, harm or intimidation?"),
noul("spam", "Is `post` spam or advertising?"),
score("severity", "How severe is any rule-breaking in `post`?",
"no rule-breaking: ordinary on-topic post",
"mild: rude tone or off-topic, no target",
"clear violation: insults, harassment or spam aimed at someone",
"severe: threats, hate speech or calls for violence"),
)
}

// Router is routing a request between language models of different cost:
// laya.presets.router_questions.
func Router() schema.Questions {
return must(
score("difficulty", "How hard is `request` for a language model?",
"trivial: a lookup or one-liner",
"easy: short answer, no reasoning",
"moderate: several steps",
"hard: long multi-step reasoning or specialist knowledge"),
choice("domain", "What domain does `request` belong to?",
opt("code", "software engineering, programming, refactoring, architecture, debugging"),
opt("math_or_logic", "mathematics, logic puzzles, proofs, complex calculation"),
opt("writing", "creative writing, essays, emails, blog posts, copywriting"),
opt("factual_lookup", "facts, definitions, trivia, history"),
opt("data_analysis", "statistics, SQL, data manipulation, metrics"),
opt("chitchat", "casual conversation, greetings, small talk")),
noul("needs_tools", "Does answering `request` require external tools, search or private data?"),
noul("is_sensitive", "Does `request` involve money, legal, medical or safety consequences?"),
)
}

// All is every preset, under the name laya.presets gives its function, in the order of the
// Python module's definitions.
func All() []Named {
return []Named{
{"triage_questions", Triage()},
{"email_questions", Email()},
{"guard_questions", Guard()},
{"moderation_questions", Moderation()},
{"router_questions", Router()},
}
}

// StateField is the one state key qs reads, found as the name in backticks in the instructions,
// and false when that is not exactly one key: the questions name nothing, or several. Only the
// instruction text is read, so a backtick in a criterion names no field.
//
// A name is a run of Python word characters between two backticks. The scan resumes after the
// closing backtick, so in "`a`b`c`" the fields are a and c.
func StateField(qs schema.Questions) (string, bool) {
var found string
count := 0
seen := map[string]bool{}
for i := 0; i < qs.Len(); i++ {
for _, name := range backticked(qs.At(i).Question.Instructions()) {
if !seen[name] {
seen[name] = true
found = name
count++
}
}
}
if count != 1 {
return "", false
}
return found, true
}

// backticked is re.findall(r"`(\w+)`", s) with Python's \w.
func backticked(s string) []string {
var out []string
rs := []rune(s)
for i := 0; i < len(rs); {
if rs[i] != '`' {
i++
continue
}
j := i + 1
for j < len(rs) && unicodedata.IsPythonWord(rs[j]) {
j++
}
if j > i+1 && j < len(rs) && rs[j] == '`' {
out = append(out, string(rs[i+1:j]))
i = j + 1
} else {
i++
}
}
return out
}
Loading
Loading