The problem
Volta Gear, a made-up brand in Coimbatore, sells phone chargers, cables and earbuds online. A few hundred messages a day arrive by email and WhatsApp, and one person reads them all: where is my order, the refund has not come, the charger is dead, the payment failed, will this work with my laptop, and now and then a customer at the end of their patience.
The costly mistakes are slow ones: an angry customer waiting behind thirty tracking questions, a weekend backlog nobody sorted, a reply that promises a refund window the policy does not offer. The system sorts each message, reads its mood, routes it with a deadline and drafts a reply grounded in the policy, so the team only checks and sends.
The data
400 past messages, each with the intent an agent gave it: order status 72, return or refund 64, damaged or faulty 64, payment problem 52, product question 56, complaint 44, other 48. I generated them from templates with a fixed seed, in the English, Indian English and Tanglish customers write ("refund innum varala", "return pannanum"), with typos, shorthand, rough WhatsApp messages and emails with a subject and a sign-off. One in ten carries a second topic, as real ones do.
This morning's 40 messages, written by hand: emails and WhatsApp messages from Saturday afternoon to Monday 09:55, each with a customer, a tier (new, regular or VIP), and an order and its value. I gave each the intent I would file it under, only to check the classifier; the system never sees those labels.
The support policy: 17 short passages on returns, refunds, warranty, faults, delivery, tracking, cash on delivery, failed and double payments, invoices and complaints. For each of this morning's messages I noted the passages I would quote, to check the search. Made up, like everything about Volta Gear.
The model
Stage 1, machine learning: what do they want? Naive Bayes over words and word pairs, from my own library, trained in your browser on 320 messages in about a tenth of a second; 80 are held back to test it. Order numbers, amounts and long numbers become placeholders, so it learns "mentions an amount" rather than one price. Naive Bayes is over-confident, so its scores are divided by 2, the value that fitted held-out folds best, before they become percentages.
Stage 2, deep learning: how do they feel? DistilBERT, a 66-million-parameter network fine-tuned on film reviews (SST-2), reads each message without its greeting and sign-off. It calls almost every support message negative, because people write in about problems, so the label alone says little. What separates them is how sure it is: on this morning's messages the angry ones scored 8.1 to 8.5 in log-odds, plain questions 5.1 to 6.5, and the thank-you note -7.4. I set four bands in the gaps: upset from 7.6, unhappy from 7.2, neutral from 0, positive.
Stage 3, automation: who handles it, and by when? 6 rules, in order, that the team can edit: they look at the intent, the mood, the order value, the customer's tier and how long the message has waited, and send it to a desk at a priority. The priority sets the reply deadline in working hours (urgent 2 working hours, high 4 working hours, normal 1 working day, low 2 working days), counted from 09:00 to 18:00, Monday to Friday, so a weekend message's clock starts on Monday morning. A rule that needs the mood waits until it is read, and the inbox is routed again.
Stage 4, generative AI: what do we say? The search finds up to 3 policy passages for the message: by shared key words until MiniLM loads, then by meaning, with a boost for passages about the intent from stage 1. An escalated message always brings the complaints passage. From those passages the code picks the one or two sentences that answer the message, and quoted word for word they already make a usable reply. SmolLM2 360M then rewrites them as a short answer to this customer, and two checks read the rewrite: Copy Writer's fact check flags any number or claim the policy does not give, and a second check flags any number from the quoted policy that the rewrite leaves out.
Each stage hands its result to the next: the intent steers the rules and the search, the mood moves messages between desks and decides whether the reply opens with an apology, and the desk decides whether the language model drafts at all. Messages sent to Escalations get the quoted template only: a small model should not be the one answering the angriest customers.
The pipeline
- 01Train
Naive Bayes learns from 320 labelled messages in your browser; 80 are held back to test it.
- 02Sort
Each message gets an intent and how sure the classifier is, with the words that decided it.
- 03Read the mood
When you load it, DistilBERT scores each message; the score becomes upset, unhappy, neutral or positive.
- 04Route
The rules turn intent, mood, order value, tier and wait into a desk, a priority and a reply-by time.
- 05Find the policy
Key words, then MiniLM, pick the passages and the sentences that answer the message.
- 06Draft and check
The template quotes the policy; SmolLM2 rewrites it as a reply; the fact check flags what the policy does not say.
Results
The first four are measured when this page is built, with the same code the demo runs; the moods come from DistilBERT run on the same messages with the same model file, and the drafts from a script that runs stage 4 with the demo's prompt. A draft that passes both checks is not always right: they catch numbers and named claims, not every wrong sentence.
For a client
For a real brand I would train the classifier on their own tickets, a few hundred per intent to start, and retrain each month as products and questions change. Messages it is unsure about would go to a person to label, which is how the training set grows.
I would replace the film-review mood model with one fine-tuned on support messages, ideally one that also reads Tamil and Hindi, and keep the bands tuned on the client's own angry and calm messages.
The rules would live where the team works, in the help desk or in an n8n workflow, with the deadlines and the queue board on a shared screen. Drafts would land as internal notes for a person to check; nothing would be sent automatically.
The policy passages would come from the client's help-centre pages and be re-indexed when they change. On a server, a larger language model would write better replies; the search, the quoted template and the fact check would stay as they are.
Stack
| Part | What it does here | Licence |
|---|---|---|
| My machine-learning library | Naive Bayes, the seeded data generator and the metrics, in TypeScript and unit-tested | Mine |
| DistilBERT SST-2 | Stage 2: the mood of each message | Apache 2.0 |
| all-MiniLM-L6-v2 | Stage 4: finding passages and sentences by meaning | Apache 2.0 |
| SmolLM2 360M Instruct | Stage 4: rewriting the quoted policy as a reply | Apache 2.0 |
| Transformers.js | Runs the three models in your browser | Apache 2.0 |
| Ask the Docs and Copy Writer code | Keyword index and sentence splitting; the fact check | Mine |
| Preact and Astro | The interactive demo and the static page | MIT |
Privacy
Everything runs in your browser. The classifier trains here; the three models download once from Hugging Face's public servers and then run on your device. The messages, the rules and the policy you edit never leave it.
Nothing is sent to customers: Copy reply puts the draft on your clipboard, and the CSV export downloads to your computer.
Limits
- The history is generated from my templates, so the held-out accuracy says how well the classifier learns my patterns. This morning's hand-written messages are the fairer test, and even those are mine.
- Tanglish words shared by two intents can tip a message the wrong way: "innum varala" (not come yet) is said of orders and of refunds.
- DistilBERT learnt sentiment from film reviews. It read a polite cancellation ("ordered by mistake") as upset, and a calm, detailed email about a neckband that stopped working as positive. The mood is a signal for the rules, not a verdict.
- The same model file gives slightly different scores in different runtimes. In Edge, on the processor, the scores came within 1.2 of the ones above, and one refund chase (7.43 here, 7.15 in Edge) fell on the other side of a band, so it stayed at high priority instead of urgent.
- SmolLM2 360M is small. It sometimes adds a day, a step or a promise of its own, or leaves out the very number the customer asked about. The checks flag numbers and named claims, not every wrong sentence, so a person reads every draft and the quoted template is always there.
- The deadlines use one set of working hours and no holidays.