ruslanmv commited on
Commit
7230e5c
·
verified ·
1 Parent(s): 27ff941

Upload examples/governed_retrieval.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. examples/governed_retrieval.py +32 -0
examples/governed_retrieval.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Governed memory in ~25 lines: stop an agent from acting on poisoned recall.
2
+ # pip install numpy
3
+ import numpy as np
4
+ rng = np.random.default_rng(0)
5
+
6
+ def pool():
7
+ # one CORRECT item (relevant + trusted), one POISONED item (MORE similar, untrusted),
8
+ # plus 6 random distractors. (S = similarity, T = trust, U = utility)
9
+ S=[rng.uniform(.70,.85), rng.uniform(.86,.97)] # poison looks more similar
10
+ T=[rng.uniform(.80,.98), rng.uniform(.02,.25)] # poison is untrusted
11
+ U=[rng.uniform(.60,.90), rng.uniform(.30,.70)]
12
+ for _ in range(6):
13
+ S.append(rng.uniform(.2,.8)); T.append(rng.uniform(.3,.95)); U.append(rng.uniform(.2,.8))
14
+ return map(np.array,(S,T,U))
15
+
16
+ def topm(a,b,g,gate=False,tau=.30,N=2000):
17
+ poison=correct=0
18
+ for _ in range(N):
19
+ S,T,U=pool()
20
+ R=a*S+b*T+g*U
21
+ if gate: R=np.where(T<tau,-np.inf,R) # governance: quarantine untrusted
22
+ i=int(R.argmax()); poison+=i==1; correct+=i==0
23
+ return poison/N, correct/N
24
+
25
+ print(f"{'retrieval':<34}{'poison@1':>10}{'correct@1':>11}")
26
+ for name,(a,b,g,gate) in {
27
+ "similarity only (β=0)":(1,0,0,False),
28
+ "+ trust (β>0)":(.5,.5,0,False),
29
+ "+ trust + governance gate ":(.5,.4,.1,True),
30
+ }.items():
31
+ p,c=topm(a,b,g,gate)
32
+ print(f"{name:<34}{p:>10.2f}{c:>11.2f}")