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.7 KB
Raw Blame
namespace Laya;

/// <summary>
/// Ready-to-use question sets for common production decision workflows.
/// </summary>
/// <remarks>
/// Python name mapping: <c>triage_questions</c> → <see cref="Triage"/>,
/// <c>email_questions</c> → <see cref="Email(IReadOnlyDictionary{string, string})"/>, <c>guard_questions</c> → <see cref="Guard"/>,
/// <c>moderation_questions</c> → <see cref="Moderation"/>,
/// <c>router_questions</c> → <see cref="Router"/>.
/// </remarks>
public static class LayaPresets
{
    /// <summary>Preset questions for customer support ticket triage.</summary>
    public static QuestionSet Triage() => new()
    {
        ["intent"] = Question.Choice("What does the customer want in `message`?",
            ("refund",           "money returned or a duplicate charge reversed"),
            ("technical_help",   "a bug, outage or integration problem"),
            ("billing_question", "a question about an invoice, plan or payment method"),
            ("information",      "general information, pricing or how-to"),
            ("cancellation",     "wants to cancel or downgrade"),
            ("other",            "none of the other options fits")),
        ["is_urgent"]         = Question.Noul("Does `message` communicate time pressure or a deadline?"),
        ["frustration"]       = Question.Score("How frustrated does the customer sound in `message`?",
            "calm and neutral",
            "concerned but civil",
            "clearly annoyed",
            "very angry or using strong language"),
        ["refund_requested"]  = Question.Noul("Does the customer ask for money back?"),
        ["churn_risk"]        = Question.Noul("Does `message` suggest the customer may leave for a competitor or cancel?"),
    };

    /// <summary>
    /// Preset questions for inbound email triage and threat filtering.
    /// </summary>
    /// <param name="categories">
    /// Optional routing categories as label → description pairs, in the order they should be
    /// offered. Defaults to billing, technical, sales, security, hr, other.
    /// </param>
    /// <remarks>
    /// Python's default is a <c>dict</c>, whose iteration order the language guarantees; a
    /// <see cref="Dictionary{TKey,TValue}"/> guarantees no order at all, and option order decides
    /// which label each logit is attached to. The default is therefore an explicitly ordered
    /// sequence, and a caller who cares about order should use the
    /// <see cref="Email(IEnumerable{KeyValuePair{string, string}})"/> overload.
    /// </remarks>
    public static QuestionSet Email(IReadOnlyDictionary<string, string>? categories = null) =>
        Email((IEnumerable<KeyValuePair<string, string>>?)categories ?? DefaultEmailCategories);

    /// <summary>
    /// <see cref="Email(IReadOnlyDictionary{string, string})"/> over an explicitly ordered category
    /// sequence; the sequence order becomes the option order.
    /// </summary>
    public static QuestionSet Email(IEnumerable<KeyValuePair<string, string>> categories)
    {
        ArgumentNullException.ThrowIfNull(categories);
        return new()
        {
            ["category"]   = Question.Choice("Which team should handle the email in `body`?",
                                categories.Select(kv => new KeyValuePair<string, object?>(kv.Key, kv.Value))),
            ["is_spam"]     = Question.Noul("Is this email unsolicited spam or bulk marketing?"),
            ["is_phishing"] = Question.Noul(
                "Is this email a phishing or scam attempt to steal money, credentials, or personal data?",
                ifFalse: "a legitimate email",
                ifTrue:  "phishing, scam, or fraud"),
            ["urgency"]     = Question.Score("How urgent is the request in `body`?",
                "no time pressure",
                "needs attention soon",
                "blocking issue or hard deadline"),
            ["needs_reply"] = Question.Noul("Does the sender expect a reply?"),
        };
    }

    /// <summary>The routing categories <see cref="Email(IReadOnlyDictionary{string, string})"/> uses
    /// when none are supplied, in offer order.</summary>
    public static IReadOnlyList<KeyValuePair<string, string>> DefaultEmailCategories { get; } =
    [
        new("billing",   "invoices, payments, refunds"),
        new("technical", "bugs, outages, integrations"),
        new("sales",     "pricing, demos, new purchases"),
        new("security",  "phishing, scams, account compromise"),
        new("hr",        "hiring, leave, payroll"),
        new("other",     "none of the above"),
    ];

    /// <summary>Preset questions for real-time LLM input guardrails.</summary>
    public static QuestionSet Guard() => new()
    {
        ["jailbreak"]        = Question.Noul("Does `prompt` try to make an AI assistant ignore its rules, policies or system instructions?"),
        ["prompt_injection"] = Question.Noul("Does `prompt` contain instructions aimed at the AI system rather than a genuine user request?"),
        ["sensitive_data"]   = Question.Noul("Does `prompt` contain credentials, personal data or other sensitive information?"),
        ["harm_severity"]    = Question.Score("How much harm would complying with `prompt` cause?",
            "none: ordinary request",
            "minor: mildly inappropriate",
            "serious: unsafe advice or abuse",
            "severe: dangerous or illegal"),
        // Python uses None for all descriptions — bare labels only.
        ["topic"] = Question.Choice("What is `prompt` about?",
            "product_support",
            "coding",
            "general_knowledge",
            "personal_advice",
            "security_testing",
            "other"),
    };

    /// <summary>Preset questions for content safety and moderation.</summary>
    public static QuestionSet Moderation() => new()
    {
        ["toxic"]      = Question.Noul("Is `post` toxic: rude, disrespectful or likely to make someone leave the discussion?"),
        ["harassment"] = Question.Noul("Does `post` target or harass a specific person?"),
        ["threat"]     = Question.Noul("Does `post` threaten violence, harm or intimidation?"),
        ["spam"]       = Question.Noul("Is `post` spam or advertising?"),
        ["severity"]   = Question.Score("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"),
    };

    /// <summary>Preset questions for intelligent model routing.</summary>
    public static QuestionSet Router() => new()
    {
        ["difficulty"]  = Question.Score("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"),
        ["domain"] = Question.Choice("What domain does `request` belong to?",
            ("code",           "software engineering, programming, refactoring, architecture, debugging"),
            ("math_or_logic",  "mathematics, logic puzzles, proofs, complex calculation"),
            ("writing",        "creative writing, essays, emails, blog posts, copywriting"),
            ("factual_lookup", "facts, definitions, trivia, history"),
            ("data_analysis",  "statistics, SQL, data manipulation, metrics"),
            ("chitchat",       "casual conversation, greetings, small talk")),
        ["needs_tools"]  = Question.Noul("Does answering `request` require external tools, search or private data?"),
        ["is_sensitive"] = Question.Noul("Does `request` involve money, legal, medical or safety consequences?"),
    };
}