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.

7.9 KB
Raw Blame
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
}