micro-lm 0.1.1
Trust layer for domain-specific AI
Requires Python: >=3.9
Keywords: micro-llm, ai, defi, deterministic, ngf

# micro-lm

Micro-LMs are lightweight, domain-specialized AIs that run on NGF rails, turning natural language into deterministic, auditable actions with built-in safety and abstain guarantees. We are piloting this idea first on **ARC** (Abstraction & Reasoning Corpus) testing to highlight its reasoning power, then for **DeFi** (Decentralized Finance) to highlight it applicability (one of many verticals) — both built on top of the `ngeodesic` Python package.

---

## Comparing LLMs vs. micro-LMs 

| Dimension | LLMs (ChatGPT, Claude, Meta, Perplexity, etc.) | **micro-LMs (ARC, DeFi)** |
|-----------|-------------------------------------------------------|-----------------------------|
| **Domain accuracy** | Broad coverage, but DeFi primitives are not a training focus. Accuracy drifts under phrasing changes. | Mapper trained on 1k–5k usecase prompts (eg. DeFi, ARC). Benchmarked accuracy > 98% on 8 DeFi primitives; abstains correctly when uncertain. |
| **Determinism** | Outputs vary run-to-run (sampling drift). Even `temperature=0` doesn’t guarantee identical results. | Stage-11 NGF rails (Warp → Detect → Denoise) yield reproducible traces. Perturbation tests confirm stable decisions. |
| **Safety / Policy enforcement** | Can be prompted with “stay under LTV 0.75,” but no hard guarantees — may still propose unsafe actions. | Built-in verifiers: Loan-to-Value (LTV), Health Factor (HF), Oracle freshness. Unsafe paths always block or abstain. |
| **Abstain behavior** | Rarely abstains — tends to “make something up” even when uncertain. | Explicit abstain mode: non-exec prompts (balance checks, nonsense) → abstain with clear reason (`abstain_non_exec`). |
| **Auditability** | Opaque; no structured rationale. | Every run produces machine-readable artifacts: mapper score, abstain reason, verifier tags, plan trace. Auditable for compliance. |
| **Efficiency / Cost** | 10s–100s of billions of params; inference is slow/expensive. | SBERT (~22M params) + lightweight classifier. Fast, cheap, deployable in CI. |
| **Regulatory / Compliance fit** | Hard to certify (stochastic, unexplainable). | Deterministic + auditable by design. Built for domains where regulators demand safety. |

---

### **Summary**
- **LLM = generalist**: broad knowledge, flexible language, but *stochastic and unsafe* for mission-critical execution.  
- **micro-LM = specialist**: slim, deterministic, auditable, and **more accurate where it matters** (DeFi/Finance, Manufacturing & Robotics, Industrial Robotics, Supply Chain & Logistics, Energy & Grid Management, etc).


---

## What’s included
- **ARC micro-LM (stress test usecase) :** a compact, NGF-style classifier that detects and orders latent “primitives” on SBERT ARC-like traces. It demonstrates the **Adapter → Detect** path and stable metrics.
- **DeFi micro-LM (business usecase):** same skeleton, different adapter — turn market features into latent traces and reuse the exact parser/denoiser stack.

> NGF’s repeatable pipeline: **Adapter → Warp → Detect → Denoise → Execute → Verify**. Here we focus on Adapter→Detect (+optional Denoise) for a small, reliable sidecar you can pair with a larger LLM.


### Foundation: `ngeodesic` (NGF Stage-10/11)

- **Stage-10 (Parser):** matched-filter parsing with dual thresholds (absolute vs null; relative vs best channel), then ordering by peak time.
- **Stage-11 (Denoise):** stabilization via hybrid EMA+median smoothing, confidence gates, seed-jitter averaging — the Warp→Detect→Denoise doctrine to suppress phantoms.

These are provided by the `ngeodesic` package and reused here without modification.

---

## Micro-LM: Tiered Plan of Attack

This repo hosts experiments in **micro-scale language models** with **domain-specific reasoning**. Our current focus is the DeFi domain for the usecase, and ARC to highlight the extent of its potential, and yet the architecture generalizes to other verticals. Each tier represents an increasing level of capability and integration. 

---

### **Tier-0: Baseline Deterministic Rails (✔ Secured)**  
- **Stock matched filter + parser** pipeline.  
- Supports core DeFi primitives with deterministic abstain paths.  
- Sandbox verified and benchmarked with stable execution.

**Status:** ✅ Complete — foundation secured.

### **Tier-1: Micro-LM on SBERT Latents (✔ Secured)**  
- Replace hashmap lookups with a **trained micro-LM encoder**.  
- Train against **2–5k SBERT latent prompts**.
- Audit results to return ABSTAIN / PASS with auditable trace
- Benchmark with full Stage-11 runner on DeFi suites (**1% hallucination / 0.98 F1 Score** across 8 primitives)

**Status:** ✅ Complete — MVP secured.

### **Tier-2: Incorporate WDD with SBERT Latents (✔ Secured)**  
The current release implements **Warp → Detect → Denoise (WDD)** on SBERT embeddings.
- Core Features
    - Deterministic mapper + verifier with abstain-first behavior.
    - Handles both DeFi prompts (financial primitives) and ARC prompts (cognitive/aptitude tasks).
    - Auditable traces: every PASS/ABSTAIN decision includes reasons + confidence.
    - Stress-tested on SBERT latents: validated signal separation + denoising.

- Status: ✅ Complete — Tier-2 is fully open under Apache 2.0.
- Purpose: **Community Edition**, deterministic & auditable safety (but scoped), SBERT + WDD — Apache 2.0.

**Status:** ✅ Complete — WWD secured.

### **Tier-3: LLM Latents + WDD (🔮 Future / Enterprise)**  
The end-goal is to extend WDD beyond SBERT into large language model hidden states.
- Planned Features
    - Swap SBERT latents for LLM internal latents.
    - Apply WDD rails to noisy LLM embeddings → restore determinism.
    - Package as a sidecar system: LLM provides fluency, micro-LM provides deterministic safety.
    - Designed for enterprise use: auditability, compliance, SLAs.
    
- Status: 🔮 Planning stage — not required for MVP, proprietary development path.
- Purpose: **Enterprise Edition**: gold standard, LLM Latents + WDD — proprietary.

#### Licensing
- Tier-1 / Tier-2 code in this repo is released under Apache 2.0.
- Tier-3 integrations (LLM latents + WDD) are proprietary and not part of this release.
---

## Quickstart

### Test DeFi Prompt

``` python
micro-defi -p "deposit 10 ETH into aave" \
  --rails stage11 \
  --policy '{"audit":{"backend":"wdd"},"mapper":{"confidence_threshold":-1.0}}' \
  --verbose
```

#### ✅ Example Output

``` python
{
  "prompt": "deposit 10 ETH into aave",
  "domain": "defi",
  "rails": "stage11",
  "T": 180,
  "top1": "deposit_asset",
  "sequence": [
    "deposit_asset"
  ],
  "plan": {
    "sequence": [
      "deposit_asset"
    ]
  },
  "verify": {
    "ok": true,
    "reason": "shim:accept:stage-4",
    "tags": [
      "rails:stage11",
      "wdd:on",
      "audit:wdd"
    ]
  },
  "flags": {},
  "aux": {
    "stage11": {
      "wdd": {
        "decision": "PASS",
        "sigma": 4,
        "proto_w": 13,
        "which_prior": "deposit(L-5)",
        "mf_peak": 6.953530481900707,
        "keep": []
      }
    }
  },
  "det_hash": "f1378c645f25",
  "wdd_summary": {
    "decision": "PASS",
    "keep": [],
    "sigma": 4,
    "proto_w": 13,
    "which_prior": "deposit(L-5)",
    "note": "fallback: MF_peak=6.953530481900707"
  },
  "abstained": false
}
```
------------------------------------------------------------------------

### Test ARC Prompt

``` python
micro-arc -p "rotate the grid 90 degrees, then flip the grid vertically" \
    --grid '[[1,2],[3,4]]' \
    --rails stage11 \
    --policy '{"audit":{"backend":"wdd"},"mapper":{"confidence_threshold":-1.0}}' \
    --verbose
```

#### ✅ Example Output

``` python
{
  "prompt": "rotate the grid 90 degrees, then flip the grid vertically",
  "domain": "arc",
  "rails": "stage11",
  "T": 180,
  "top1": null,
  "sequence": [],
  "plan": {
    "sequence": []
  },
  "verify": {
    "ok": true,
    "reason": "shim:accept:stage-4",
    "tags": [
      "audit:wdd",
      "rails:stage11",
      "wdd:on"
    ]
  },
  "flags": {
    "wdd_family": false
  },
  "aux": {
    "stage11": {
      "wdd": {
        "arc": {
          "mode": "detector",
          "results": {
            "flip_h": {
              "ok": false,
              "info": {
                "t_peak": {
                  "flip_h": 8
                },
                "corr_max": 0.31952728711321754,
                "area": 5.187295037956119e-12,
                "window": [
                  1,
                  19
                ],
                "z_abs": -0.5486818421429215,
                "sigma": null,
                "proto_w": null,
                "which_prior": "arc:flip_h"
              },
              "which": "flip_h",
              "layer": null,
              "mf_peak": 0.31952728711321754
            },
            "flip_v": {
              "ok": true,
              "info": {
                "t_peak": {
                  "flip_v": 64
                },
                "corr_max": 0.45721985028076,
                "area": 1.0890134473208314e-11,
                "window": [
                  39,
                  88
                ],
                "z_abs": 0.8531804989704017,
                "sigma": null,
                "proto_w": null,
                "which_prior": "arc:flip_v"
              },
              "which": "flip_v",
              "layer": null,
              "mf_peak": 0.45721985028076
            },
            "rotate": {
              "ok": true,
              "info": {
                "t_peak": {
                  "rotate": 0
                },
                "corr_max": 0.28304979559563553,
                "area": 1.7010950696184626,
                "window": [
                  0,
                  8
                ],
                "z_abs": -2.903935438960599,
                "sigma": null,
                "proto_w": null,
                "which_prior": "arc:rotate"
              },
              "which": "rotate",
              "layer": null,
              "mf_peak": 0.28304979559563553
            }
          }
        }
      }
    }
  },
  "det_hash": "73c8ffe9553f",
  "wdd_summary": {
    "decision": "PASS",
    "keep": [
      "rotate",
      "flip_v"
    ],
    "order": [],
    "which_prior": {
      "rotate": "arc:rotate",
      "flip_v": "arc:flip_v"
    },
    "sigma": {
      "rotate": null,
      "flip_v": null
    },
    "proto_w": {
      "rotate": null,
      "flip_v": null
    },
    "note": "mode=detector"
  },
  "abstained": true
}
```

------------------------------------------------------------------------

## Install

```bash
# 1) Install the NGF core
python3 -m pip install -U ngeodesic

# 2) (optional) install this repo in editable mode
git clone https://github.com/ngeodesic-ai/micro-lm.git
cd micro-lm
python3 -m pip install -e .
```