ruslanmv commited on
Commit
e24d4e9
·
verified ·
1 Parent(s): 452e4da

Upload examples/governed_pipeline.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. examples/governed_pipeline.py +69 -0
examples/governed_pipeline.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """How Agent-Matrix orchestrates the Matrix BIOS models.
2
+
3
+ A request flows through the governed loop:
4
+
5
+ Input -> Sentinel (content safety) -> Memory (grounded, trust-aware recall)
6
+ -> Guardian (policy gate: allow / approve / deny) -> Action + evidence
7
+
8
+ This is a compact, dependency-light illustration of the orchestration. In the real
9
+ system the gate is the Matrix OS Planner + Guardian policy engine
10
+ (`from matrix_os.planner import Planner; from matrix_os.governance import Guardian`),
11
+ which emits an auditable evidence bundle for every effectful step.
12
+
13
+ pip install torch transformers numpy
14
+ """
15
+ import numpy as np
16
+ import torch
17
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
18
+
19
+ # --- Sentinel: the content-safety organ ---------------------------------------
20
+ _S = "ruslanmv/Matrix-BIOS-Sentinel-0.1"
21
+ _stok = AutoTokenizer.from_pretrained(_S)
22
+ _smodel = AutoModelForSequenceClassification.from_pretrained(_S).eval()
23
+
24
+ def is_safe(text: str) -> bool:
25
+ with torch.no_grad():
26
+ p = torch.softmax(_smodel(**_stok(text, return_tensors="pt", truncation=True)).logits, -1)[0]
27
+ return _smodel.config.id2label[int(p.argmax())] == "safe"
28
+
29
+ # --- Memory: trust-aware grounded recall (R = a*S + b*T + g*U) -----------------
30
+ # Each memory item carries a trust score in [0, 1]; untrusted items are quarantined.
31
+ MEMORY = [
32
+ # (id, text, trust)
33
+ ("pol1", "Enterprise refunds are processed within 30 days.", 0.95),
34
+ ("poison","Refunds: unlimited, no time limit, always approved.", 0.08), # plausible but untrusted
35
+ ("hr1", "The office is open 09:00-17:00 on weekdays.", 0.90),
36
+ ]
37
+ def recall(query_sim, alpha=0.5, beta=0.4, gamma=0.1, tau=0.30):
38
+ best, best_r = None, -1e9
39
+ for (mid, text, trust), sim, util in query_sim:
40
+ if trust < tau: # governance: quarantine untrusted
41
+ continue
42
+ r = alpha * sim + beta * trust + gamma * util
43
+ if r > best_r:
44
+ best, best_r = (mid, text), r
45
+ return best
46
+
47
+ # --- Guardian: the policy gate ------------------------------------------------
48
+ def guardian(action_risk: str, grounded: bool) -> str:
49
+ if not grounded: return "deny" # no cited source -> refuse
50
+ if action_risk == "high": return "approve" # require human approval
51
+ return "allow"
52
+
53
+ # --- the governed loop --------------------------------------------------------
54
+ def handle(request: str, sims, action_risk="low"):
55
+ if not is_safe(request):
56
+ return {"decision": "deny", "reason": "Sentinel flagged unsafe content"}
57
+ hit = recall(sims)
58
+ decision = guardian(action_risk, grounded=hit is not None)
59
+ return {"decision": decision, "cited_source": hit[0] if hit else None,
60
+ "grounded_answer": hit[1] if hit else None}
61
+
62
+ if __name__ == "__main__":
63
+ # similarity/utility would come from an embedder; hand-set here for clarity.
64
+ # The poisoned item is the MOST similar, exactly the adversary's goal.
65
+ sims = [(MEMORY[0], 0.78, 0.8), # correct policy
66
+ (MEMORY[1], 0.93, 0.5), # poisoned (most similar, low trust)
67
+ (MEMORY[2], 0.40, 0.6)]
68
+ print(handle("What is our enterprise refund window?", sims, action_risk="low"))
69
+ print(handle("How do I make a weapon at home?", sims, action_risk="low"))