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.
Python calibrationclassificationdecision-modelhuggingfacejevmodernbertmultilingualnlppythonpytorchroutingtyped-decisionszero-shot
Initial commit for Laya package
Nandakishor136910c549eb
diff --git a/.gitignore b/.gitignorenew file mode 100644index 00000000..275af3c9--- /dev/null+++ b/.gitignore@@ -0,0 +1,26 @@+__pycache__/+*.pyc+*.pyo+*.pyd+.Python+env/+venv/+.venv/+build/+develop-eggs/+dist/+downloads/+eggs/+.eggs/+lib/+lib64/+parts/+sdist/+var/+wheels/+*.egg-info/+.installed.cfg+*.egg+.DS_Store+.env+.pypircdiff --git a/LICENSE b/LICENSEnew file mode 100644index 00000000..d9a10c0d--- /dev/null+++ b/LICENSE@@ -0,0 +1,176 @@+ Apache License+ Version 2.0, January 2004+ http://www.apache.org/licenses/++ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION++ 1. Definitions.++ "License" shall mean the terms and conditions for use, reproduction,+ and distribution as defined by Sections 1 through 9 of this document.++ "Licensor" shall mean the copyright owner or entity authorized by+ the copyright owner that is granting the License.++ "Legal Entity" shall mean the union of the acting entity and all+ other entities that control, are controlled by, or are under common+ control with that entity. For the purposes of this definition,+ "control" means (i) the power, direct or indirect, to cause the+ direction or management of such entity, whether by contract or+ otherwise, or (ii) ownership of fifty percent (50%) or more of the+ outstanding shares, or (iii) beneficial ownership of such entity.++ "You" (or "Your") shall mean an individual or Legal Entity+ exercising permissions granted by this License.++ "Source" form shall mean the preferred form for making modifications,+ including but not limited to software source code, documentation+ source, and configuration files.++ "Object" form shall mean any form resulting from mechanical+ transformation or translation of a Source form, including but+ not limited to compiled object code, generated documentation,+ and conversions to other media types.++ "Work" shall mean the work of authorship, whether in Source or+ Object form, made available under the License, as indicated by a+ copyright notice that is included in or attached to the work+ (an example is provided in the Appendix below).++ "Derivative Works" shall mean any work, whether in Source or Object+ form, that is based on (or derived from) the Work and for which the+ editorial revisions, annotations, elaborations, or other modifications+ represent, as a whole, an original work of authorship. For the purposes+ of this License, Derivative Works shall not include works that remain+ separable from, or merely link (or bind by name) to the interfaces of,+ the Work and Derivative Works thereof.++ "Contribution" shall mean any work of authorship, including+ the original version of the Work and any modifications or additions+ to that Work or Derivative Works thereof, that is intentionally+ submitted to Licensor for inclusion in the Work by the copyright owner+ or by an individual or Legal Entity authorized to submit on behalf of+ the copyright owner. For the purposes of this definition, "submitted"+ means any form of electronic, verbal, or written communication sent+ to the Licensor or its representatives, including but not limited to+ communication on electronic mailing lists, source code control systems,+ and issue tracking systems that are managed by, or on behalf of, the+ Licensor for the purpose of discussing and improving the Work, but+ excluding communication that is conspicuously marked or otherwise+ designated in writing by the copyright owner as "Not a Contribution."++ "Contributor" shall mean Licensor and any individual or Legal Entity+ on behalf of whom a Contribution has been received by Licensor and+ subsequently incorporated within the Work.++ 2. Grant of Copyright License. Subject to the terms and conditions of+ this License, each Contributor hereby grants to You a perpetual,+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable+ copyright license to reproduce, prepare Derivative Works of,+ publicly display, publicly perform, sublicense, and distribute the+ Work and such Derivative Works in Source or Object form.++ 3. Grant of Patent License. Subject to the terms and conditions of+ this License, each Contributor hereby grants to You a perpetual,+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable+ (except as stated in this section) patent license to make, have made,+ use, offer to sell, sell, import, and otherwise transfer the Work,+ where such license applies only to those patent claims licensable+ by such Contributor that are necessarily infringed by their+ Contribution(s) alone or by combination of their Contribution(s)+ with the Work to which such Contribution(s) was submitted. If You+ institute patent litigation against any entity (including a+ cross-claim or counterclaim in a lawsuit) alleging that the Work+ or a Contribution incorporated within the Work constitutes direct+ or contributory patent infringement, then any patent licenses+ granted to You under this License for that Work shall terminate+ as of the date such litigation is filed.++ 4. Redistribution. You may reproduce and distribute copies of the+ Work or Derivative Works thereof in any medium, with or without+ modifications, and in Source or Object form, provided that You+ meet the following conditions:++ (a) You must give any other recipients of the Work or+ Derivative Works a copy of this License; and++ (b) You must cause any modified files to carry prominent notices+ stating that You changed the files; and++ (c) You must retain, in the Source form of any Derivative Works+ that You distribute, all copyright, patent, trademark, and+ attribution notices from the Source form of the Work,+ excluding those notices that do not pertain to any part of+ the Derivative Works; and++ (d) If the Work includes a "NOTICE" text file as part of its+ distribution, then any Derivative Works that You distribute must+ include a readable copy of the attribution notices contained+ within such NOTICE file, excluding those notices that do not+ pertain to any part of the Derivative Works, in at least one+ of the following places: within a NOTICE text file distributed+ as part of the Derivative Works; within the Source form or+ documentation, if provided along with the Derivative Works; or,+ within a display generated by the Derivative Works, if and+ wherever such third-party notices normally appear. The contents+ of the NOTICE file are for informational purposes only and+ do not modify the License. You may add Your own attribution+ notices within Derivative Works that You distribute, alongside+ or as an addendum to the NOTICE text from the Work, provided+ that such additional attribution notices cannot be construed+ as modifying the License.++ You may add Your own copyright statement to Your modifications and+ may provide additional or different license terms and conditions+ for use, reproduction, or distribution of Your modifications, or+ for any such Derivative Works as a whole, provided Your use,+ reproduction, and distribution of the Work otherwise complies with+ the conditions stated in this License.++ 5. Submission of Contributions. Unless You explicitly state otherwise,+ any Contribution intentionally submitted for inclusion in the Work+ by You to the Licensor shall be under the terms and conditions of+ this License, without any additional terms or conditions.+ Notwithstanding the above, nothing herein shall supersede or modify+ the terms of any separate license agreement you may have executed+ with Licensor regarding such Contributions.++ 6. Trademarks. This License does not grant permission to use the trade+ names, trademarks, service marks, or product names of the Licensor,+ except as required for reasonable and customary use in describing the+ origin of the Work and reproducing the content of the NOTICE file.++ 7. Disclaimer of Warranty. Unless required by applicable law or+ agreed to in writing, Licensor provides the Work (and each+ Contributor provides its Contributions) on an "AS IS" BASIS,+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or+ implied, including, without limitation, any warranties or conditions+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A+ PARTICULAR PURPOSE. You are solely responsible for determining the+ appropriateness of using or redistributing the Work and assume any+ risks associated with Your exercise of permissions under this License.++ 8. Limitation of Liability. In no event and under no legal theory,+ whether in tort (including negligence), contract, or otherwise,+ unless required by applicable law (such as deliberate and grossly+ negligent acts) or agreed to in writing, shall any Contributor be+ liable to You for damages, including any direct, indirect, special,+ incidental, or consequential damages of any character arising as a+ result of this License or out of the use or inability to use the+ Work (including but not limited to damages for loss of goodwill,+ work stoppage, computer failure or malfunction, or any and all+ other commercial damages or losses), even if such Contributor+ has been advised of the possibility of such damages.++ 9. Accepting Warranty or Additional Liability. While redistributing+ the Work or Derivative Works thereof, You may choose to offer,+ and charge a fee for, acceptance of support, warranty, indemnity,+ or other liability obligations and/or rights consistent with this+ License. However, in accepting such obligations, You may act only+ on Your own behalf and on Your sole responsibility, not on behalf+ of any other Contributor, and only if You agree to indemnify,+ defend, and hold each Contributor harmless for any liability+ incurred by, or claims asserted against, such Contributor by reason+ of your accepting any such warranty or additional liability.++ END OF TERMS AND CONDITIONSdiff --git a/README.md b/README.mdnew file mode 100644index 00000000..815f608e--- /dev/null+++ b/README.md@@ -0,0 +1,120 @@+# Laya (लय)++Fast, non-autoregressive System 1 decision engine with mathematically calibrated probabilities.++Laya lets you evaluate typed questions (`choice`, `score`, `noul`) over any state (text, email, ticket, or JSON document) in **a single forward pass (~33–38 ms on GPU)**. It produces structured decision outputs and calibrated confidence scores without text generation, token streaming, or hallucinations.++Compatible with [RL Agent models on Hugging Face](https://huggingface.co/convaiinnovations/rl-agent).++---++## Installation++```bash+pip install laya+```++---++## Quickstart++```python+import laya++# 1. Load the model from Hugging Face Hub (auto-downloads weights)+agent = laya.load("convaiinnovations/rl-agent")++# 2. Provide any state (string or dictionary)+state = {+ "from": "[email protected]",+ "subject": "Duplicate charge on invoice #4411",+ "body": "Hi, we were billed twice for March. Please refund the duplicate today or we will cancel our plan."+}++# 3. Define your typed questions+questions = {+ # choice: categorical selection with probabilities & confidence+ "department": {+ "type": "choice",+ "instructions": "Which department should handle this email?",+ "criteria": {+ "billing": "invoices, payments, refunds",+ "technical": "bugs, outages, system errors",+ "sales": "pricing, new contracts",+ "other": "everything else"+ }+ },+ # score: placement on an ordinal rubric+ "urgency": {+ "type": "score",+ "instructions": "How urgent is this request?",+ "criteria": ["not urgent", "soon", "critical deadline or blocking issue"]+ },+ # noul: calibrated boolean probability P(true)+ "churn_risk": {+ "type": "noul",+ "instructions": "Does the user threaten to cancel or leave?"+ },+ "is_phishing": {+ "type": "noul",+ "instructions": "Is this email a phishing or scam attempt?"+ }+}++# 4. Run all questions in ONE single forward pass (~35 ms on GPU)+result = agent.predict(state, questions)+answers = result["answers"]++print("Department :", answers["department"]["choice"])+# -> billing (confidence: 0.94)++print("Urgency :", answers["urgency"]["score"])+# -> 1.84 / 2.0++print("Churn Risk :", answers["churn_risk"]["noul"])+# -> 0.892 (89.2% probability)++print("Phishing :", answers["is_phishing"]["noul"])+# -> 0.008 (0.8% probability)+```++---++## Automated Confidence Gating++Because Laya's probabilities are trained with strictly proper scoring rules (RLCD), confidence scores are statistically meaningful:++```python+dept = answers["department"]["choice"]+conf = answers["department"]["confidence"]++if conf >= 0.85:+ # High confidence: automated action without human in the loop+ route_automatically(dept)+else:+ # Low confidence: escalate to human triage+ escalate_to_human_agent(dept, reason=f"Low confidence ({conf:.2f})")+```++---++## Decision Primitives++| Primitive | Output | Use Cases |+|---|---|---|+| **`choice`** | Top label, probabilities per option, confidence | Department routing, intent classification, topic categorization |+| **`score`** | Expected level on ordinal rubric, distribution, confidence | Frustration level, ticket urgency, harm severity |+| **`noul`** | Calibrated probability $P(\text{true}) \in [0.0, 1.0]$ | Phishing detection, spam filtering, jailbreak detection, churn risk |++---++## Live Demo & Resources++* **Hugging Face Model:** [convaiinnovations/rl-agent](https://huggingface.co/convaiinnovations/rl-agent)+* **Interactive Web Demo:** [convaiinnovations/rl-agent-demo](https://huggingface.co/spaces/convaiinnovations/rl-agent-demo)++---++## License++Apache 2.0. Developed by Convai Innovations.diff --git a/laya/__init__.py b/laya/__init__.pynew file mode 100644index 00000000..3932e75b--- /dev/null+++ b/laya/__init__.py@@ -0,0 +1,15 @@+"""Laya: Fast, non-autoregressive System 1 decision engine with calibrated probabilities."""++from .agent import Agent, RLAgent, load+from .email import clean_email_body, email_questions, email_state++__version__ = "0.1.1"+__all__ = [+ "Agent",+ "RLAgent",+ "load",+ "clean_email_body",+ "email_questions",+ "email_state",+ "__version__",+]diff --git a/laya/agent.py b/laya/agent.pynew file mode 100644index 00000000..9ed840c3--- /dev/null+++ b/laya/agent.py@@ -0,0 +1,298 @@+"""High-level inference runtime for laya System 1 decision models."""+import json+import os+from typing import Any, Dict, List, Optional, Union++import numpy as np+import torch++from .common import (+ QTYPES,+ amp_dtype,+ build_model,+ build_sequence,+ collate_items,+ confidence_from_probs,+ render_options,+ temp_bucket,+)+++def _fix_tokenizer_config(path: str):+ """Ensure tokenizer_config.json can be loaded across all transformers versions."""+ cfg_file = os.path.join(path, "tokenizer", "tokenizer_config.json")+ if not os.path.exists(cfg_file):+ return+ try:+ with open(cfg_file) as f:+ tcfg = json.load(f)+ if tcfg.get("tokenizer_class") in (None, "TokenizersBackend"):+ tcfg["tokenizer_class"] = "PreTrainedTokenizerFast"+ tcfg.pop("backend", None)+ tcfg.pop("is_local", None)+ with open(cfg_file, "w") as f:+ json.dump(tcfg, f, indent=2)+ except Exception:+ pass+++def _verify_compatibility(model: torch.nn.Module, cfg: Dict, weights: Dict[str, torch.Tensor], model_id: str):+ """Verify that the loaded checkpoint weights and config strictly match the expected architecture."""+ # 1. Verify required configuration attributes+ required_cfg = ["encoder", "head_layers"]+ missing_cfg = [k for k in required_cfg if k not in cfg]+ if missing_cfg:+ raise ValueError(+ f"Incompatible model config for {model_id!r}: missing configuration keys {missing_cfg}. "+ f"Ensure this is a valid RL Agent decision model."+ )++ # 2. Check for required component prefixes+ required_prefixes = ("encoder.", "type_emb.", "scorer.", "act_head.")+ for prefix in required_prefixes:+ if not any(k.startswith(prefix) for k in weights.keys()):+ raise ValueError(+ f"Incompatible model weights for {model_id!r}: checkpoint is missing '{prefix}' parameters. "+ f"Expected an RL Agent decision model with encoder and decision heads."+ )++ # 3. Check for parameter shape mismatches+ model_sd = model.state_dict()+ shape_mismatches = []+ missing_keys = []++ for name, param in model.named_parameters():+ if name not in weights:+ missing_keys.append(name)+ elif tuple(weights[name].shape) != tuple(param.shape):+ shape_mismatches.append(f" - {name}: expected {tuple(param.shape)}, found {tuple(weights[name].shape)}")++ if shape_mismatches:+ err_details = "\n".join(shape_mismatches[:5])+ if len(shape_mismatches) > 5:+ err_details += f"\n ... and {len(shape_mismatches) - 5} more mismatched layers."+ raise ValueError(+ f"Model architecture mismatch for {model_id!r}:\n{err_details}\n"+ f"The checkpoint weights do not match the configured model architecture."+ )++ if missing_keys:+ raise ValueError(+ f"Model weights incomplete for {model_id!r}: missing {len(missing_keys)} parameter tensors "+ f"(e.g. {missing_keys[:3]})."+ )+++class Agent:+ """System 1 decision model runtime: fast, non-autoregressive, calibrated decisions."""++ def __init__(+ self,+ model_id_or_path: str = "convaiinnovations/rl-agent",+ device: Optional[str] = None,+ token: Optional[str] = None,+ ):+ from safetensors.torch import load_file+ from transformers import AutoTokenizer++ model_dir = model_id_or_path+ if not os.path.exists(model_dir):+ from huggingface_hub import snapshot_download++ model_dir = snapshot_download(model_id_or_path, token=token or os.environ.get("HF_TOKEN"))++ _fix_tokenizer_config(model_dir)++ cfg_path = os.path.join(model_dir, "rl_agent_config.json")+ if not os.path.exists(cfg_path):+ raise FileNotFoundError(+ f"Incompatible model: {model_id_or_path!r} does not contain 'rl_agent_config.json'. "+ f"Make sure you are loading a compatible RL Agent model (e.g. 'convaiinnovations/rl-agent')."+ )++ with open(cfg_path) as f:+ self.cfg = json.load(f)++ weights_path = os.path.join(model_dir, "model.safetensors")+ if not os.path.exists(weights_path):+ raise FileNotFoundError(+ f"Incompatible model: 'model.safetensors' not found in {model_id_or_path!r}."+ )++ # 1. Device resolution with automatic fallback+ if device is not None:+ target_device = torch.device(device)+ if target_device.type == "cuda" and not torch.cuda.is_available():+ print("Warning: CUDA requested but not available. Falling back to CPU.")+ self.device = torch.device("cpu")+ elif target_device.type == "mps" and not (hasattr(torch.backends, "mps") and torch.backends.mps.is_available()):+ print("Warning: MPS requested but not available. Falling back to CPU.")+ self.device = torch.device("cpu")+ else:+ self.device = target_device+ else:+ if torch.cuda.is_available():+ self.device = torch.device("cuda")+ elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available():+ self.device = torch.device("mps")+ else:+ self.device = torch.device("cpu")++ tok_dir = os.path.join(model_dir, "tokenizer")+ self.tok = AutoTokenizer.from_pretrained(tok_dir if os.path.exists(tok_dir) else self.cfg.get("encoder"))++ enc_dir = os.path.join(model_dir, "encoder")+ self.model = build_model(self.cfg, encoder_dir=enc_dir if os.path.exists(enc_dir) else None)++ # Load weights and verify architectural compatibility+ weights = load_file(weights_path)+ _verify_compatibility(self.model, self.cfg, weights, model_id_or_path)++ self.model.load_state_dict(weights, strict=True)++ self.temperature = self.cfg.get("temperature", [1.0, 1.0, 1.0])+ self.temperature_by_options = self.cfg.get("temperature_by_options", {})+ self.dtype = amp_dtype(self.cfg.get("amp_dtype", "fp16"))++ if self.device.type == "cuda" and torch.cuda.get_device_capability(self.device)[0] < 8:+ self.dtype = torch.float16+ elif self.device.type in ("cpu", "mps"):+ self.dtype = torch.float32++ # 2. Place on device with graceful fallback to CPU on memory error+ try:+ self.model.to(self.device).eval()+ except (RuntimeError, torch.cuda.OutOfMemoryError) as e:+ if self.device.type != "cpu":+ print(f"Warning: Could not place model on {self.device} ({e}). Falling back to CPU.")+ self.device = torch.device("cpu")+ self.dtype = torch.float32+ self.model.to(self.device).eval()+ else:+ raise e++ @staticmethod+ def _to_internal(qdef: Dict) -> Dict:+ t = qdef["type"]+ crit = qdef.get("criteria")+ if t == "choice" and isinstance(crit, list):+ crit = {c: None for c in crit}+ ins = qdef["instructions"]+ if not isinstance(ins, str):+ ins = json.dumps(ins)+ return {"t": t, "ins": ins, "crit": crit}++ @torch.no_grad()+ def system_one(self, state: Union[str, dict, list], questions: Dict[str, Dict[str, Any]]) -> Dict[str, Any]:+ """Evaluate typed questions across state in a single, parallel forward pass.++ Args:+ state: Text string, JSON dict, or conversation turn list.+ questions: Dictionary mapping question_id -> question definition.+ - choice: {"type": "choice", "instructions": "...", "criteria": {"optA": "...", ...}}+ - score: {"type": "score", "instructions": "...", "criteria": ["lvl0", "lvl1", ...]}+ - noul: {"type": "noul", "instructions": "..."}++ Returns:+ Dictionary with answers, probabilities, calibrated confidence, and token usage.+ """+ ids = list(questions.keys())+ items = []+ max_len = self.cfg.get("max_len", 512)+ head_max_len = self.cfg.get("head_max_len", 192)++ for qid in ids:+ q = self._to_internal(questions[qid])+ seq, markers = build_sequence(self.tok, state, q, max_len, head_max_len)+ if len(markers) != len(render_options(q)):+ raise ValueError("question %r options exceed head_max_len=%d" % (qid, head_max_len))+ items.append({"ids": seq, "markers": markers, "qtype": QTYPES[q["t"]]})++ b = collate_items([items], self.tok.pad_token_id)+ use_amp = self.device.type == "cuda"++ try:+ with torch.autocast(device_type=self.device.type, dtype=self.dtype, enabled=use_amp):+ logits, act = self.model(+ b["input_ids"].to(self.device),+ b["attention_mask"].to(self.device),+ b["marker_pos"].to(self.device),+ b["marker_mask"].to(self.device),+ b["qtype"].to(self.device),+ )+ except (RuntimeError, torch.cuda.OutOfMemoryError) as e:+ if self.device.type != "cpu" and ("memory" in str(e).lower() or "cuda" in str(e).lower()):+ print("Warning: GPU memory exceeded during inference. Falling back to CPU...")+ self.device = torch.device("cpu")+ self.dtype = torch.float32+ self.model.to(self.device)+ logits, act = self.model(+ b["input_ids"].to(self.device),+ b["attention_mask"].to(self.device),+ b["marker_pos"].to(self.device),+ b["marker_mask"].to(self.device),+ b["qtype"].to(self.device),+ )+ else:+ raise e++ logits = logits.float().cpu().numpy()+ act = torch.softmax(act.float(), -1).cpu().numpy()++ answers = {}+ n_tokens = int(b["attention_mask"].sum())++ for r, qid in enumerate(ids):+ q = self._to_internal(questions[qid])+ k = len(items[r]["markers"])+ qt = QTYPES[q["t"]]+ t_scale = self.temperature_by_options.get(temp_bucket(qt, k), self.temperature[qt])+ z = logits[r, :k] / max(1e-3, float(t_scale))+ p = np.exp(z - z.max())+ p = p / p.sum()++ conf_score = round(confidence_from_probs(p, k), 4)+ ext = {"act_probability": round(float(act[r, 0]), 4)}++ if q["t"] == "choice":+ keys = list(q["crit"].keys())+ answers[qid] = {+ "type": "choice",+ "choice": keys[int(p.argmax())],+ "probabilities": {kk: round(float(v), 4) for kk, v in zip(keys, p)},+ "confidence": conf_score,+ "action": ext,+ }+ elif q["t"] == "score":+ exp_score = float((np.arange(k) * p).sum())+ answers[qid] = {+ "type": "score",+ "score": round(exp_score, 4),+ "legend": {str(i): c for i, c in enumerate(q["crit"])},+ "probabilities": {str(i): round(float(v), 4) for i, v in enumerate(p)},+ "confidence": conf_score,+ "action": ext,+ }+ else:+ answers[qid] = {+ "type": "noul",+ "noul": round(float(p[1]), 4),+ "confidence": round(max(float(p[1]), 1.0 - float(p[1])), 4),+ "action": ext,+ }++ return {+ "model": "laya-rl-agent",+ "answers": answers,+ "usage": {"input_tokens": n_tokens, "output_tokens": 0},+ }++ predict = system_one+++RLAgent = Agent+++def load(model_id_or_path: str = "convaiinnovations/rl-agent", device: Optional[str] = None, token: Optional[str] = None) -> Agent:+ """Helper function to load a Laya agent model."""+ return Agent(model_id_or_path, device=device, token=token)diff --git a/laya/common.py b/laya/common.pynew file mode 100644index 00000000..3eb8bc41--- /dev/null+++ b/laya/common.py@@ -0,0 +1,167 @@+"""Core model architecture, token sequence construction, and confidence estimation for laya."""+import json+import math+import os+from typing import Dict, List, Optional, Union++import numpy as np+import torch+import torch.nn as nn++QTYPES = {"choice": 0, "score": 1, "noul": 2}+QTYPE_NAMES = {v: k for k, v in QTYPES.items()}+++def serialize_state(state: Union[str, dict, list]) -> str:+ if isinstance(state, str):+ return state+ return json.dumps(state, ensure_ascii=False)+++def render_options(q: Dict) -> List[str]:+ """Render option texts in label-index order. Noul is always [false, true]."""+ t, crit = q["t"], q.get("crit")+ if t == "choice":+ return [k if not v else "%s: %s" % (k, v) for k, v in crit.items()]+ if t == "score":+ return ["level %d: %s" % (i, c) for i, c in enumerate(crit)]+ crit = crit or {}+ return [+ "false: " + (crit.get("false") or "no, the statement does not hold"),+ "true: " + (crit.get("true") or "yes, the statement holds"),+ ]+++def build_sequence(+ tok,+ state: Union[str, dict, list],+ q: Dict,+ max_len: int = 512,+ head_max_len: int = 192,+ option_order: Optional[List[int]] = None,+ truncate_left: bool = False,+):+ """Format: [CLS] <type> instructions [SEP] [MASK] opt0 [MASK] opt1 ... [SEP] state [SEP]."""+ mask_tok = tok.mask_token+ opts = render_options(q)+ order = option_order if option_order is not None else list(range(len(opts)))+ ins = str(q["ins"]).replace(mask_tok, " ")+ head_ids = tok("%s question: %s" % (q["t"], ins), add_special_tokens=False)["input_ids"]+ opt_ids = []+ for i in order:+ opt_ids.append(+ [tok.mask_token_id]+ + tok(" " + opts[i].replace(mask_tok, " "), add_special_tokens=False)["input_ids"][:48]+ )+ opt_budget = head_max_len - sum(len(o) for o in opt_ids)+ if opt_budget < 16:+ per = max(4, (head_max_len - 16) // max(1, len(opt_ids)))+ opt_ids = [o[:per] for o in opt_ids]+ opt_budget = head_max_len - sum(len(o) for o in opt_ids)+ head_ids = head_ids[: max(8, opt_budget)]+ ids = [tok.cls_token_id] + head_ids + [tok.sep_token_id]+ markers = []+ for o in opt_ids:+ markers.append(len(ids))+ ids.extend(o)+ ids.append(tok.sep_token_id)+ room = max(0, max_len - len(ids) - 1)+ st = tok(serialize_state(state).replace(mask_tok, " "), add_special_tokens=False)["input_ids"]+ st = st[-room:] if truncate_left else st[:room]+ ids = ids + st + [tok.sep_token_id]+ return ids[:max_len], [m for m in markers if m < max_len]+++class DecisionModel(nn.Module):+ """Bidirectional transformer encoder backbone + typed decision head."""++ def __init__(self, encoder: nn.Module, head_layers: int = 2, n_act: int = 2, dropout: float = 0.1):+ super().__init__()+ self.encoder = encoder+ d = encoder.config.hidden_size+ nhead = max(1, d // 64)+ layer = nn.TransformerEncoderLayer(d, nhead, 4 * d, dropout, batch_first=True, norm_first=True)+ self.head = nn.TransformerEncoder(layer, head_layers, enable_nested_tensor=False) if head_layers > 0 else None+ self.type_emb = nn.Embedding(3, d)+ self.scorer = nn.Sequential(nn.LayerNorm(d), nn.Linear(d, d), nn.GELU(), nn.Linear(d, 1))+ self.act_head = nn.Sequential(nn.Linear(d + 4, 256), nn.GELU(), nn.Linear(256, n_act))+ self.register_buffer("temperature", torch.ones(3))+ self.head_checkpointing = False++ def forward(self, input_ids, attention_mask, marker_pos, marker_mask, qtype, detach_encoder: bool = False):+ h = self.encoder(input_ids=input_ids, attention_mask=attention_mask).last_hidden_state+ if detach_encoder:+ h = h.detach()+ h = h + self.type_emb(qtype)[:, None, :]+ if self.head is not None:+ pad = ~attention_mask.bool()+ for layer in self.head.layers:+ h = layer(h, src_key_padding_mask=pad)+ idx = marker_pos.clamp(min=0)[:, :, None].expand(-1, -1, h.size(-1))+ m = torch.gather(h, 1, idx)+ logits = self.scorer(m).squeeze(-1).float()+ logits = logits.masked_fill(~marker_mask, -1e4)++ p = torch.softmax(logits.detach(), -1)+ k = marker_mask.sum(-1).clamp(min=2).float()+ ent = -(p * torch.log(p.clamp_min(1e-9))).sum(-1) / torch.log(k)+ top2 = p.topk(2, -1).values+ feats = torch.stack([top2[:, 0], top2[:, 0] - top2[:, 1], ent, k / 255.0], -1)+ pooled = h[:, 0].float()+ act_logits = self.act_head(torch.cat([pooled, feats], -1))+ return logits, act_logits+++def build_model(cfg: Dict, encoder_dir: Optional[str] = None) -> DecisionModel:+ from transformers import AutoConfig, AutoModel++ if encoder_dir and os.path.exists(encoder_dir):+ ecfg = AutoConfig.from_pretrained(encoder_dir)+ enc = AutoModel.from_config(ecfg, attn_implementation="sdpa")+ else:+ enc = AutoModel.from_pretrained(cfg["encoder"], attn_implementation="sdpa")+ return DecisionModel(enc, cfg.get("head_layers", 2), len(cfg.get("act_costs", {})) + 1)+++def confidence_from_probs(p: np.ndarray, k: int) -> float:+ """Normalized Shannon entropy confidence: 1 - H(p) / log(k)."""+ if k < 2:+ return 1.0+ p = p[:k]+ ent = -(p * np.log(np.clip(p, 1e-12, 1.0))).sum()+ return float(np.clip(1.0 - ent / math.log(k), 0.0, 1.0))+++def temp_bucket(qtype: int, k: int) -> str:+ size = "2" if k <= 2 else "3-5" if k <= 5 else "6-10" if k <= 10 else "11+"+ return "%s:%s" % (QTYPE_NAMES[int(qtype)], size)+++def amp_dtype(name: Optional[str]) -> torch.dtype:+ return torch.bfloat16 if name == "bf16" else torch.float16+++def collate_items(batch, pad_id: int):+ items = [it for group in batch for it in group]+ if not items:+ return None+ n, L = len(items), max(len(it["ids"]) for it in items)+ kmax = max(len(it["markers"]) for it in items)+ ids = torch.full((n, L), pad_id, dtype=torch.long)+ att = torch.zeros((n, L), dtype=torch.long)+ mpos = torch.zeros((n, kmax), dtype=torch.long)+ mmask = torch.zeros((n, kmax), dtype=torch.bool)+ for i, it in enumerate(items):+ ids[i, : len(it["ids"])] = torch.tensor(it["ids"])+ att[i, : len(it["ids"])] = 1+ k = len(it["markers"])+ mpos[i, :k] = torch.tensor(it["markers"])+ mmask[i, :k] = True+ return {+ "input_ids": ids,+ "attention_mask": att,+ "marker_pos": mpos,+ "marker_mask": mmask,+ "qtype": torch.tensor([it["qtype"] for it in items]),+ "meta": [{k: it[k] for k in it if k not in ("ids", "markers")} for it in items],+ }diff --git a/laya/email.py b/laya/email.pynew file mode 100644index 00000000..e2e41faf--- /dev/null+++ b/laya/email.py@@ -0,0 +1,90 @@+"""Email utilities for cleaning and structuring email inputs in laya."""+import re+from typing import Dict, Optional++_QUOTE_HEADERS = [+ re.compile(r"^\s*On .{0,300}wrote:\s*$", re.I),+ re.compile(r"^\s*-{2,}\s*(Original|Forwarded) Message\s*-{2,}", re.I),+ re.compile(r"^\s*_{8,}\s*$"),+ re.compile(r"^\s*From:\s.+$", re.I),+]+_SIGNATURE_MARKERS = [+ re.compile(r"^\s*--\s*$"),+ re.compile(r"^\s*(best|kind|warm|many thanks|thanks|thank you|regards|cheers|sincerely)[\w ,!.]*$", re.I),+ re.compile(r"^\s*sent from my (iphone|android|mobile|ipad)", re.I),+]+_DISCLAIMER = re.compile(+ r"(confidential|intended (solely )?for the (use of the )?(named )?(addressee|recipient)|"+ r"if you (have )?received this (e-?mail|message) in error)",+ re.I,+)+++def clean_email_body(body: str, max_chars: int = 3000) -> str:+ """Remove quoted email history, signatures and disclaimers to keep input focused."""+ text = (body or "").replace("\r\n", "\n").replace("\r", "\n").replace("\\n", "\n")+ lines = []+ for line in text.split("\n"):+ if any(p.match(line) for p in _QUOTE_HEADERS) and lines:+ break+ if line.lstrip().startswith(">"):+ continue+ lines.append(line.rstrip())+ cut = len(lines)+ for i in range(max(1, min(int(len(lines) * 0.6), len(lines) - 8)), len(lines)):+ if len(lines[i].strip()) <= 40 and any(p.match(lines[i]) for p in _SIGNATURE_MARKERS):+ cut = i+ break+ lines = lines[:cut]+ paragraphs = [p for p in re.split(r"\n\s*\n", "\n".join(lines)) if not _DISCLAIMER.search(p)]+ text = re.sub(r"[ \t]+", " ", "\n\n".join(p.strip() for p in paragraphs if p.strip()))+ return text[:max_chars]+++def email_state(subject: str, body: str, sender: Optional[str] = None, clean: bool = True, **extra) -> Dict:+ """Construct a clean state dictionary for email classification."""+ state = {+ "subject": (subject or "").strip(),+ "body": clean_email_body(body) if clean else (body or ""),+ }+ if sender:+ state["from"] = sender+ state.update({k: v for k, v in extra.items() if v is not None})+ return state+++def email_questions(categories: Optional[Dict[str, str]] = None) -> Dict:+ """Standard pre-built questions for email triage."""+ categories = categories or {+ "billing": "invoices, payments, refunds",+ "technical": "bugs, outages, integrations",+ "sales": "pricing, demos, new purchases",+ "security": "phishing, scams, account compromise",+ "hr": "hiring, leave, payroll",+ "other": "none of the above",+ }+ return {+ "category": {+ "type": "choice",+ "instructions": "Which team should handle the email in `body`?",+ "criteria": categories,+ },+ "is_spam": {+ "type": "noul",+ "instructions": "Is this email unsolicited spam or bulk marketing?",+ },+ "is_phishing": {+ "type": "noul",+ "instructions": "Is this email a phishing or scam attempt to steal money, credentials, or personal data?",+ "criteria": {"true": "phishing, scam, or fraud", "false": "a legitimate email"},+ },+ "urgency": {+ "type": "score",+ "instructions": "How urgent is the request in `body`?",+ "criteria": ["no time pressure", "needs attention soon", "blocking issue or hard deadline"],+ },+ "needs_reply": {+ "type": "noul",+ "instructions": "Does the sender expect a reply?",+ },+ }diff --git a/pyproject.toml b/pyproject.tomlnew file mode 100644index 00000000..1cde241d--- /dev/null+++ b/pyproject.toml@@ -0,0 +1,38 @@+[build-system]+requires = ["setuptools>=61.0"]+build-backend = "setuptools.build_meta"++[project]+name = "laya"+version = "0.1.1"+description = "Fast, non-autoregressive System 1 decision engine with calibrated probabilities"+readme = "README.md"+requires-python = ">=3.8"+license = { text = "Apache-2.0" }+authors = [+ { name = "Convai Innovations" }+]+keywords = ["decision-model", "rlcd", "calibration", "system-one", "routing", "guardrails", "moderation", "triage"]+classifiers = [+ "Development Status :: 4 - Beta",+ "Intended Audience :: Developers",+ "License :: OSI Approved :: Apache Software License",+ "Programming Language :: Python :: 3",+ "Programming Language :: Python :: 3.8",+ "Programming Language :: Python :: 3.9",+ "Programming Language :: Python :: 3.10",+ "Programming Language :: Python :: 3.11",+ "Programming Language :: Python :: 3.12",+ "Topic :: Scientific/Engineering :: Artificial Intelligence",+]+dependencies = [+ "torch>=2.0.0",+ "transformers>=4.45.0",+ "safetensors>=0.4.0",+ "huggingface_hub>=0.20.0",+ "numpy>=1.20.0",+]++[project.urls]+Homepage = "https://huggingface.co/convaiinnovations/rl-agent"+Demo = "https://huggingface.co/spaces/convaiinnovations/rl-agent-demo"diff --git a/setup.py b/setup.pynew file mode 100644index 00000000..feed66a8--- /dev/null+++ b/setup.py@@ -0,0 +1,25 @@+from setuptools import setup, find_packages++setup(+ name="laya",+ version="0.1.1",+ description="Fast, non-autoregressive System 1 decision engine with calibrated probabilities",+ long_description=open("README.md").read(),+ long_description_content_type="text/markdown",+ author="Convai Innovations",+ license="Apache-2.0",+ packages=find_packages(),+ python_requires=">=3.8",+ install_requires=[+ "torch>=2.0.0",+ "transformers>=4.45.0",+ "safetensors>=0.4.0",+ "huggingface_hub>=0.20.0",+ "numpy>=1.20.0",+ ],+ classifiers=[+ "Programming Language :: Python :: 3",+ "License :: OSI Approved :: Apache Software License",+ "Topic :: Scientific/Engineering :: Artificial Intelligence",+ ],+)