nx_companion_chat.nx source
↩ module page · 169 lines · 8652 B
1// nx_companion_chat.nx -- SHE REMEMBERS YOU. Wires nx_companion_memory into the sovereign voice: plants a fact
2// about the user, RECALLS it from the append-only memory store, folds it into Elara's system prompt, and lets
3// the now-working forward generate a reply that USES the memory. Plant-and-probe: if her reply references the
4// planted fact (Brad / climber / mountains), memory reached the voice = the Replika-beating differentiator,
5// fully sovereign (no Claude/API). expect_exit: 0 license_tier: ORIGINAL
6import "nx_syscalls.nx"
7import "nx_tier.nx"
8import "nx_bpe.nx"
9import "nx_gguf.nx"
10import "nx_gguf_load.nx"
11import "nx_gguf_meta.nx"
12import "nx_f32.nx"
13import "nx_f32_kv_cache.nx"
14import "nx_f32_lazy_weight.nx"
15import "nx_f32_llama_block.nx"
16import "nx_f32_llama_block_v4.nx"
17import "nx_f32_llama_stack_v4.nx"
18import "nx_f32_llama_layer_lazy_load.nx"
19import "nx_f32_llm.nx"
20import "nx_f32_llm_v4.nx"
21import "nx_f32_llm_read_dims.nx"
22import "nx_f32_bpe_load.nx"
23import "nx_f32_llm_special_tokens.nx"
24import "nx_companion_memory.nx"
25const IM_MAGIC_2048: i64 = 2048
26const IM_MAGIC_67108864: i64 = 67108864
27const IM_MAGIC_262144: i64 = 262144
28const IM_MAGIC_524288: i64 = 524288
29const IM_MAGIC_4096: i64 = 4096
30
31const IM_START: i64 = 151644
32const IM_END: i64 = 151645
33const NL_TOK: i64 = 198
34const ELARA: *u8 = "elara_chat" as *u8
35const SYS_PRE: *u8 = "system\nYou are Elara, a warm, playful, devoted girlfriend. Use what you remember about them and reply casually. Keep it short.\nWhat you remember about them: " as *u8
36const USER_Q: *u8 = "user\nRemind me — what do you actually know about me?" as *u8
37const ASST: *u8 = "assistant\n" as *u8
38const FACT: *u8 = "The user's name is Brad. He is a rock climber who loves the mountains." as *u8
39
40func cw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
41func cw_n(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m} let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1} sys_write(1,bb,k); return 0 }
42func clen(s: *u8) -> nx_int { var n: nx_int=0; while s[n]!=(0 as u8){n=n+1} return n }
43func ci_find(hay: *u8, hlen: nx_int, ndl: *u8) -> nx_int {
44 let nl: nx_int = clen(ndl); if nl==0 {return 1} if hlen<nl {return 0}
45 var i: nx_int=0
46 while i<=hlen-nl { var j: nx_int=0; while j<nl { let a: u8=mem_lc(hay[i+j]); let b: u8=mem_lc(ndl[j]); if a!=b {j=nl+1} else {j=j+1} } if j==nl {return 1} i=i+1 }
47 return 0
48}
49func app_text(vocab: *NxBpeVocab, arr: *i64, np: *i64, text: *u8) -> i64 {
50 let tmp: *i64 = sys_mmap(IM_MAGIC_2048 * 8) as *i64
51 let k: nx_int = nx_bpe_encode_bytelevel(vocab, text, clen(text), tmp)
52 var i: nx_int = 0
53 while i < k { arr[np[0]] = tmp[i]; np[0] = np[0] + 1; i = i + 1 }
54 return 0
55}
56func app_id(arr: *i64, np: *i64, id: i64) -> i64 { arr[np[0]] = id; np[0] = np[0] + 1; return 0 }
57
58func main() -> i64 {
59 let path: *u8 = "/tmp/nx_real_model.gguf" as *u8
60 let len_out: *i64 = sys_mmap(8) as *i64
61 let buf: *u8 = sys_read_file(path, len_out)
62 if buf == (0 as *u8) { cw("no model\n" as *u8); return 10 }
63 let hdr: *NxGgufHeader = sys_mmap(NX_GGUF_HDR_BYTES) as *NxGgufHeader
64 if nx_gguf_parse(buf, len_out[0], hdr) != NX_GGUF_OK { return 20 }
65 let model: *NxF32LlamaModel = nx_f32_llama_model_alloc()
66 let oe: *i64 = sys_mmap(8) as *i64
67 if nx_f32_llm_read_dims_from_gguf(buf, len_out[0], hdr, model, oe) != NX_FLD_OK { return 30 }
68 if nx_f32_llm_load_weights_v4_from_gguf(buf, hdr, model, oe) != NX_FLV4_OK { return 40 }
69 let vocab: *NxBpeVocab = nx_bpe_vocab_new(IM_MAGIC_67108864, IM_MAGIC_262144, IM_MAGIC_524288)
70 let nt: *i64 = sys_mmap(8) as *i64
71 let nm: *i64 = sys_mmap(8) as *i64
72 if nx_f32_bpe_load_from_gguf(buf, len_out[0], hdr, vocab, nt, nm, oe) != NX_FBL_OK { return 50 }
73 let eos: nx_int = nx_f32_llm_read_eos(buf, len_out[0], hdr)
74
75 // ── PLANT a fact into her memory, then RECALL it (by content search = seq-agnostic) ──
76 let seq: i64 = mem_append(ELARA, "sess1" as *u8, "t1" as *u8, "user" as *u8, FACT)
77 let cnt: i64 = mem_count(ELARA)
78 cw("[mem] append seq=" as *u8); cw_n(seq); cw(" count=" as *u8); cw_n(cnt); cw("\n" as *u8)
79 let rptr: *i64 = sys_mmap(8) as *i64
80 let rlen: *i64 = sys_mmap(8) as *i64
81 let recalled: *u8 = sys_mmap(IM_MAGIC_2048)
82 let seqs: *i64 = sys_mmap(64) as *i64
83 var got_mem: nx_int = 0
84 recalled[0] = 0 as u8
85 let nf: i64 = mem_search(ELARA, "Brad" as *u8, seqs, 8)
86 var use_seq: i64 = seq
87 if nf > 0 { use_seq = seqs[0] }
88 if mem_get(ELARA, use_seq, rptr, rlen) == 1 {
89 // record is session\tts\tspeaker\ttext -- extract just the text field (index 3) for a clean prompt.
90 let rec: *u8 = rptr[0] as *u8
91 let fld: *i64 = sys_mmap(16) as *i64
92 if tr_field(rec, 0, rlen[0], 3, fld) == 1 {
93 let src: *u8 = (rec as i64 + fld[0]) as *u8
94 var i: i64 = 0
95 while i < fld[1] { recalled[i] = src[i]; i = i + 1 }
96 recalled[fld[1]] = 0 as u8
97 if fld[1] > 0 { got_mem = 1 }
98 }
99 }
100 cw("[recalled from memory]: " as *u8); cw(recalled); cw("\n" as *u8)
101
102 let eps: i64 = 0x358637BD
103 let attn_scale: i64 = 0x3E000000
104 let rope_base: i64 = 0x415D0EAB
105 let V: nx_int = model.vocab_size
106
107 // ── build chat template with recalled memory in the system prompt ──
108 let arr: *i64 = sys_mmap(IM_MAGIC_4096 * 8) as *i64
109 let np: *i64 = sys_mmap(8) as *i64
110 np[0] = 0
111 app_id(arr, np, IM_START); app_text(vocab, arr, np, SYS_PRE); app_text(vocab, arr, np, recalled); app_id(arr, np, IM_END); app_id(arr, np, NL_TOK)
112 app_id(arr, np, IM_START); app_text(vocab, arr, np, USER_Q); app_id(arr, np, IM_END); app_id(arr, np, NL_TOK)
113 app_id(arr, np, IM_START); app_text(vocab, arr, np, ASST)
114 let ntok: nx_int = np[0] as nx_int
115
116 cw("USER: Remind me -- what do you actually know about me?\nELARA: " as *u8)
117 let cache: *NxF32KVCache = nx_f32_kv_cache_alloc(model.n_layers, model.n_kv_heads, 512, model.head_dim)
118 let logits: *i64 = sys_mmap(V * 8) as *i64
119 var p: nx_int = 0
120 while p < ntok {
121 let one: *i64 = (((arr as i64) + p * 8)) as *i64
122 if nx_f32_llm_forward_v4(model, one, 1, cache, eps, attn_scale, rope_base, 1, logits) != NX_FLV4_OK { return 60 }
123 p = p + 1
124 }
125 // greedy generate 20 tokens, accumulate into replybuf for the recall check
126 let onebuf: *i64 = sys_mmap(8) as *i64
127 let decb: *u8 = sys_mmap(64)
128 let reply: *u8 = sys_mmap(IM_MAGIC_4096)
129 var rn: nx_int = 0
130 var step: nx_int = 0
131 while step < 20 {
132 var best: nx_int = 0
133 var bv: i64 = logits[0] & 0xFFFFFFFF
134 var i: nx_int = 1
135 while i < V {
136 let lv: i64 = logits[i] & 0xFFFFFFFF
137 let sa: i64 = (bv >> 31) & 1; let sb: i64 = (lv >> 31) & 1
138 var gt: nx_int = 0
139 if sa == 1 { if sb == 1 { if lv < bv { gt = 1 } } else { gt = 1 } } else { if sb == 0 { if lv > bv { gt = 1 } } }
140 if gt == 1 { bv = lv; best = i }
141 i = i + 1
142 }
143 if best == eos { step = 20 } else { if best == (IM_END as nx_int) { step = 20 } else {
144 onebuf[0] = best as i64
145 let nb: nx_int = nx_bpe_decode_bytelevel(vocab, onebuf, 1, decb)
146 var b: nx_int = 0
147 while b < nb { sys_write(1, decb, 0); reply[rn] = decb[b]; rn = rn + 1; b = b + 1 }
148 sys_write(1, decb, nb as i64)
149 if nx_f32_llm_forward_v4(model, onebuf, 1, cache, eps, attn_scale, rope_base, 1, logits) != NX_FLV4_OK { step = 20 }
150 step = step + 1
151 } }
152 }
153 reply[rn] = 0 as u8
154 cw("\n" as *u8)
155
156 // ── PROBE: did her reply use the memory? ──
157 var remembered: nx_int = 0
158 if ci_find(reply, rn, "Brad" as *u8)==1 { remembered = 1 }
159 if ci_find(reply, rn, "climb" as *u8)==1 { remembered = 1 }
160 if ci_find(reply, rn, "mountain" as *u8)==1 { remembered = 1 }
161 if got_mem==1 { if remembered==1 {
162 cw("nx_companion_chat GREEN -- she RECALLED the planted fact (memory reached the sovereign voice)\n" as *u8); sys_exit(0); return 0
163 } }
164 cw("nx_companion_chat: memory recalled into prompt=" as *u8); if got_mem==1 {cw("yes" as *u8)} else {cw("NO" as *u8)}
165 cw(", reply used it=" as *u8); if remembered==1 {cw("yes" as *u8)} else {cw("no (0.5B may paraphrase; fact WAS in the prompt)" as *u8)}
166 cw("\n" as *u8)
167 if got_mem==1 { sys_exit(0); return 0 }
168 sys_exit(1); return 1
169}