Quickstart with laya

To get started with laya. 

Laya is a free, open-source AI model designed for fast, focused text classification. It makes simple decisions—such as routing emails, prioritising tickets, or identifying refund requests—by choosing from predefined options and providing a confidence score.

In short: Laya is built for fast, low-cost AI decisions rather than generating text.

To get started, we have to install laya and creating your python environment. Next we going to use it to decide if the ticket belongs to which department.

This is an approach where it uses the model with your local computing resources. There's another option for you use Laya studio which requires an API but you have to sign up first.



import laya
from laya import Router

# Initialize router with preloading (avoids swap delay)
router = Router(preload=True)

# Define complex state
ticket = {
    "ticket_id": "TCK-8821",
    "customer": "enterprise_user",
    "subject": "System downtime and billing dispute",
    "body": "Our production API has been failing since 6 AM. We lost critical transactions. We demand an immediate SLA refund."
}

# Define multiple questions of different primitives
questions = {
    "queue": {
        "type": "choice",
        "instructions": "Which engineering queue owns this ticket?",
        "criteria": {
            "infrastructure": "server outages, network downtime, database failures",
            "billing": "refunds, SLA credits, invoice disputes",
            "security": "breaches, vulnerability reports",
            "support": "general customer inquiries"
        }
    },
    "urgency": {
        "type": "score",
        "instructions": "How urgent is this ticket?",
        "criteria": ["low priority", "medium", "high priority", "critical blocker"]
    },
    "churn_risk": {
        "type": "noul",
        "instructions": "Does the customer threaten to cancel or express severe churn intent?"
    }
}

# Single forward pass: evaluates all questions simultaneously
res = router.predict(ticket, questions)

print("Routing Decision :", res["routing"]["model"])
# -> english

print("Assigned Queue   :", res["answers"]["queue"]["choice"])
# -> infrastructure (confidence: 0.96)

print("Urgency Score    :", res["answers"]["urgency"]["score"])
# -> 2.87 / 3.0

print("Churn Risk       :", f"{res['answers']['churn_risk']['noul']:.1%}")
# -> 91.4%


API approach

Here is example API approach using curl + API Key.

curl https://api.laya.studio/v1/systemone \

  -H "Authorization: Bearer YOUR-API-KEY" \

  -H "Content-Type: application/json" \

  -d '{"state":"Hi, I was charged twice for order #4821 ($129.00). Please refund the duplicate charge before Friday, our books close then. This is the second billing mistake this quarter and we'\''re starting to look at other vendors.","questions":{"intent":{"type":"choice","instructions":"What does the customer want?","criteria":{"refund":"money back or a charge reversed","cancel":"to cancel or stop a subscription","delivery":"to know where an order is","technical_help":"help with a bug or a broken feature","question":"general information or how-to","other":"none of the above"}},"urgent":{"type":"noul","instructions":"Does the message need a reply today because of a deadline or time pressure?"},"churn":{"type":"noul","instructions":"Does the customer suggest they may leave or switch to a competitor?"}}}'


And as you can see here, we are downloading the model from hugging face and running the code. It is quite fast.


The only thing is not sure why it keep on reconstructing and takes 6 seconds to do that. 

Let's try to understand the code and schema here - based on the documentation here (https://nandhakishorm.github.io/laya/structured/)

Here we have multiple questions and the model would look for look at the the ticket and answers all your questions.And the type should batch the decision primitives that is being defined. here :- https://nandhakishorm.github.io/laya/ and if you don't and lets say we get creative, then we will hit an error - notice that i have changed "type" to ratings. 

   
"urgency": {
        "type": "ratings",
        "instructions": "How urgent is this ticket?",
        "criteria": ["low priority", "medium", "high priority", "critical blocker"]
    },
    "churn_risk": {
        "type": "yes_no",
        "instructions": "Does the customer threaten to cancel or express severe churn intent?"
    }

This is a well formatted questions and anwers



ticket = {
    "ticket_id": "TCK-8821",
    "customer": "enterprise_user",
    "subject": "System downtime and billing dispute",
    "body": "Our production API has been failing since 6 AM. We lost critical transactions. We demand an immediate SLA refund."
}


questions = {
    "queue": {
        "type": "choice",
        "instructions": "Which engineering queue owns this ticket?",
        "criteria": {
            "infrastructure": "server outages, network downtime, database failures",
            "billing": "refunds, SLA credits, invoice disputes",
            "security": "breaches, vulnerability reports",
            "support": "general customer inquiries"
        }
    },
    "urgency": {
        "type": "score",
        "instructions": "How urgent is this ticket?",
        "criteria": ["low priority", "medium", "high priority", "critical blocker"]
    },
    "churn_risk": {
        "type": "noul",
        "instructions": "Does the customer threaten to cancel or express severe churn intent?"
    }
}



Question 1 - which engineering team owns this ticket and based on the type which is "choice" here - we have the following outputs 

{'model': 'laya-rl-agent', 'answers': {'queue': {'type': 'choice', 'choice': 'billing', 'probabilities': {'infrastructure': 0.0951, 'billing': 0.822, 'security': 0.0458, 'support': 0.0372}, 'confidence': 0.5322, 'answer_confidence': 0.822, 'action': {'act_probability': 1.0}},

Question 2 - urgency - 2.72 is the score where wew have 0 - low priorty and etc.... as you can see here:-

'urgency': {'type': 'score', 'score': 2.7296, 'legend': {'0': 'low priority', '1': 'medium', '2': 'high priority', '3': 'critical blocker'}, 

Question 3 - churn risk - where the type primitive is noul. And it give an very high confidence that the customer is going to cancel subscription. 

 'churn_risk': {'type': 'noul', 'noul': 0.1925, 'confidence': 0.8075, 'answer_confidence': 0.8075, 'action': {'act_probability': 1.0}}}



Comments

Popular posts from this blog

Windows SSH: Permissions for 'private-key' are too open

NodeJS: Error: spawn EINVAL in window for node version 20.20 and 18.20