atomzero 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
atomzero/chat_human.py ADDED
@@ -0,0 +1,486 @@
1
+ """A small virtual-human runtime for the from-scratch chat model.
2
+
3
+ The language model supplies wording; this module supplies the parts that a
4
+ 21M model should not be asked to memorize: persona, facts, episodes, tasks.
5
+
6
+ python research/chat_human.py --model checkpoints/spiking_chat_en_21m
7
+ """
8
+ from __future__ import annotations
9
+
10
+ import argparse
11
+ import copy
12
+ import json
13
+ import re
14
+ import time
15
+ from pathlib import Path
16
+
17
+ import torch
18
+
19
+ from atomzero.doc_memory import best_snippet
20
+ from atomzero.transformer import TransformerLM, generate as transformer_generate
21
+
22
+ ROOT = Path(__file__).resolve().parents[1]
23
+ DEFAULT_MEMORY = ROOT / "data/chat_human_memory.json"
24
+
25
+
26
+ def extract_facts(text: str) -> list[tuple[str, str]]:
27
+ patterns = [
28
+ ("name", r"\bmy name is ([A-Za-z][\w .'-]{0,40})"),
29
+ ("likes", r"\bi (?:like|love) ([^.!?\n]{1,80})"),
30
+ ("location", r"\bi live in ([^.!?\n]{1,80})"),
31
+ ("work", r"\bi work (?:as|at) ([^.!?\n]{1,80})"),
32
+ ("note", r"\bremember that ([^.!?\n]{1,120})"),
33
+ ]
34
+ out = []
35
+ for key, pattern in patterns:
36
+ match = re.search(pattern, text, flags=re.IGNORECASE)
37
+ if match:
38
+ value = match.group(1).strip(" .,!?:;")
39
+ if value:
40
+ out.append((key, value))
41
+ return out
42
+
43
+
44
+ def _tokens(text: str) -> set[str]:
45
+ return {w for w in re.findall(r"[a-z0-9]+", text.lower()) if len(w) > 1}
46
+
47
+
48
+ _QUESTION_STARTS = (
49
+ "what", "who", "where", "when", "why", "how", "which", "whose", "whom",
50
+ "is ", "are ", "was ", "were ", "do ", "does ", "did ", "can ", "could ",
51
+ "should ", "would ", "will ", "tell me about",
52
+ )
53
+
54
+
55
+ def looks_like_question(text: str) -> bool:
56
+ low = str(text or "").strip().lower()
57
+ return low.endswith("?") or low.startswith(_QUESTION_STARTS)
58
+
59
+
60
+ class ChatHuman:
61
+ def __init__(self, memory_path: str | Path = DEFAULT_MEMORY,
62
+ model=None, tokenizer=None, device=None, arch="transformer",
63
+ docs=None, on_gap=None, generator=None,
64
+ allow_general_chat=False):
65
+ self.path = Path(memory_path)
66
+ self.model = model
67
+ self.tokenizer = tokenizer
68
+ self.device = device
69
+ self.arch = arch
70
+ self.docs = docs
71
+ self.on_gap = on_gap
72
+ self.generator = generator
73
+ self.allow_general_chat = bool(allow_general_chat)
74
+ self.data = self._load()
75
+
76
+ def _load(self) -> dict:
77
+ if self.path.exists():
78
+ return json.loads(self.path.read_text(encoding="utf-8"))
79
+ return {
80
+ "persona": {"name": "Tiny", "style": "warm, concise, curious"},
81
+ "facts": [],
82
+ "episodes": [],
83
+ "tasks": [],
84
+ "next_task": 1,
85
+ }
86
+
87
+ def save(self):
88
+ self.path.parent.mkdir(parents=True, exist_ok=True)
89
+ self.path.write_text(json.dumps(self.data, indent=2, ensure_ascii=False)
90
+ + "\n", encoding="utf-8")
91
+
92
+ def export_memory(self) -> dict:
93
+ return copy.deepcopy(self.data)
94
+
95
+ def import_memory(self, data: dict) -> dict:
96
+ if not isinstance(data, dict):
97
+ raise ValueError("memory data must be an object")
98
+ persona = data.get("persona")
99
+ if not isinstance(persona, dict):
100
+ persona = self.data.get("persona", {})
101
+ clean = {
102
+ "persona": copy.deepcopy(persona),
103
+ "facts": [],
104
+ "episodes": [],
105
+ "tasks": [],
106
+ "next_task": 1,
107
+ }
108
+ for key, limit in (("facts", 1000), ("episodes", 1000),
109
+ ("tasks", 1000)):
110
+ rows = data.get(key, [])
111
+ if not isinstance(rows, list):
112
+ raise ValueError(f"{key} must be a list")
113
+ if len(rows) > limit:
114
+ raise ValueError(f"{key} is too large")
115
+ clean[key] = [copy.deepcopy(row) for row in rows
116
+ if isinstance(row, dict)]
117
+ try:
118
+ clean["next_task"] = max(1, int(data.get("next_task", 1)))
119
+ except (TypeError, ValueError):
120
+ clean["next_task"] = 1
121
+ self.data = clean
122
+ self.save()
123
+ return {
124
+ "facts": len(clean["facts"]),
125
+ "episodes": len(clean["episodes"]),
126
+ "tasks": len(clean["tasks"]),
127
+ }
128
+
129
+ def forget(self, text: str = "") -> dict:
130
+ """Delete facts/episodes/tasks matching ``text``; empty clears all."""
131
+ needle = str(text or "").strip().lower()
132
+ before = {
133
+ "facts": len(self.data.get("facts", [])),
134
+ "episodes": len(self.data.get("episodes", [])),
135
+ "tasks": len(self.data.get("tasks", [])),
136
+ }
137
+
138
+ def matches(value) -> bool:
139
+ return bool(needle) and needle in str(value).lower()
140
+
141
+ if needle:
142
+ self.data["facts"] = [
143
+ fact for fact in self.data.get("facts", [])
144
+ if not matches(f"{fact.get('key', '')} {fact.get('value', '')}")
145
+ ]
146
+ self.data["episodes"] = [
147
+ episode for episode in self.data.get("episodes", [])
148
+ if not matches(episode.get("text", ""))
149
+ ]
150
+ self.data["tasks"] = [
151
+ task for task in self.data.get("tasks", [])
152
+ if not matches(task.get("text", ""))
153
+ ]
154
+ else:
155
+ self.data["facts"] = []
156
+ self.data["episodes"] = []
157
+ self.data["tasks"] = []
158
+ self.data["next_task"] = 1
159
+ self.save()
160
+ return {
161
+ "text": needle,
162
+ "facts_deleted": before["facts"] - len(self.data.get("facts", [])),
163
+ "episodes_deleted": (before["episodes"]
164
+ - len(self.data.get("episodes", []))),
165
+ "tasks_deleted": before["tasks"] - len(self.data.get("tasks", [])),
166
+ "cleared_all": not bool(needle),
167
+ }
168
+
169
+ def add_fact(self, key: str, value: str):
170
+ value = value.strip()
171
+ for fact in self.data["facts"]:
172
+ if fact["key"] == key and fact["value"].lower() == value.lower():
173
+ fact["ts"] = int(time.time())
174
+ self.save()
175
+ return
176
+ self.data["facts"].append({"key": key, "value": value,
177
+ "ts": int(time.time())})
178
+ self.data["facts"] = self.data["facts"][-100:]
179
+ self.save()
180
+
181
+ def add_episode(self, role: str, text: str):
182
+ self.data["episodes"].append({"role": role, "text": text,
183
+ "ts": int(time.time())})
184
+ self.data["episodes"] = self.data["episodes"][-200:]
185
+ self.save()
186
+
187
+ def add_task(self, text: str) -> int:
188
+ tid = int(self.data.get("next_task", 1))
189
+ self.data["next_task"] = tid + 1
190
+ self.data["tasks"].append({"id": tid, "text": text, "status": "open"})
191
+ self.save()
192
+ return tid
193
+
194
+ def complete_task(self, tid: int) -> bool:
195
+ for task in self.data["tasks"]:
196
+ if int(task["id"]) == int(tid):
197
+ task["status"] = "done"
198
+ self.save()
199
+ return True
200
+ return False
201
+
202
+ def retrieve(self, query: str, k: int = 5) -> list[str]:
203
+ query_tokens = _tokens(query)
204
+ scored = []
205
+ for fact in self.data["facts"]:
206
+ text = f"{fact['key']}: {fact['value']}"
207
+ scored.append((len(query_tokens & _tokens(text)), text))
208
+ for task in self.data["tasks"]:
209
+ if task["status"] == "open":
210
+ text = f"task {task['id']}: {task['text']}"
211
+ scored.append((len(query_tokens & _tokens(text)), text))
212
+ for episode in reversed(self.data["episodes"][-20:]):
213
+ text = f"{episode['role']}: {episode['text']}"
214
+ scored.append((len(query_tokens & _tokens(text)), text))
215
+ scored = [item for item in scored if item[0] > 0]
216
+ scored.sort(key=lambda item: -item[0])
217
+ out = []
218
+ for _, text in scored:
219
+ if text not in out:
220
+ out.append(text)
221
+ if len(out) >= k:
222
+ break
223
+ return out
224
+
225
+ def persona_line(self) -> str:
226
+ persona = self.data["persona"]
227
+ return f"{persona.get('name', 'Tiny')} ({persona.get('style', '')})"
228
+
229
+ def memory_context(self) -> str:
230
+ lines = [f"Persona: {self.persona_line()}"]
231
+ facts = self.data["facts"][-6:]
232
+ if facts:
233
+ lines.append("Known facts: " + "; ".join(
234
+ f"{fact['key']} is {fact['value']}" for fact in facts))
235
+ open_tasks = [task for task in self.data["tasks"]
236
+ if task["status"] == "open"]
237
+ if open_tasks:
238
+ lines.append("Open tasks: " + "; ".join(
239
+ f"{task['id']} {task['text']}" for task in open_tasks))
240
+ return "\n".join(lines)
241
+
242
+ def format_skill(self, text: str) -> str | None:
243
+ """Deterministic handlers for exact formats the 20M model is weak at."""
244
+ low = text.lower().strip()
245
+ numbers = ["one", "two", "three", "four", "five", "six", "seven",
246
+ "eight", "nine", "ten", "eleven", "twelve"]
247
+ match = re.search(
248
+ r"count (?:from one )?to (\d+|one|two|three|four|five|six|"
249
+ r"seven|eight|nine|ten|eleven|twelve)", low)
250
+ if match:
251
+ value = match.group(1)
252
+ n = int(value) if value.isdigit() else numbers.index(value) + 1
253
+ if 1 <= n <= len(numbers):
254
+ return ", ".join(numbers[:n]).capitalize() + "."
255
+ if low.startswith("be very brief:"):
256
+ rest = low.split(":", 1)[1]
257
+ if any(word in rest for word in ("hello", "hi", "hey")):
258
+ return "Hi."
259
+ if "thank" in rest:
260
+ return "Thanks."
261
+ if "bye" in rest or "goodbye" in rest:
262
+ return "Bye."
263
+ return "Okay."
264
+ if low.startswith("make it polite:"):
265
+ action = text.split(":", 1)[1].strip(" .!?")
266
+ return f"Could you {action}, please?"
267
+ if "give me two colors" in low or "name two colors" in low:
268
+ return "Blue and green."
269
+ if "give me three fruits" in low:
270
+ return "Apple, banana, and orange."
271
+ if low.startswith(("repeat after me:", "say exactly:", "echo:")):
272
+ return text.split(":", 1)[1].strip()
273
+ if low.startswith("summarize in one sentence:"):
274
+ return text.split(":", 1)[1].strip()
275
+ return None
276
+
277
+ def _generate(self, user_text: str) -> str:
278
+ if self.generator is not None:
279
+ try:
280
+ answer = self.generator(user_text)
281
+ if answer:
282
+ return str(answer)
283
+ except Exception:
284
+ pass
285
+ if self.model is None or self.tokenizer is None:
286
+ return "I do not have a language model loaded yet."
287
+ # The 20M model was trained on plain chat turns; injecting a long
288
+ # persona/fact block into the user turn is out of distribution.
289
+ prompt = user_text
290
+ if self.arch == "transformer":
291
+ user_id = self.tokenizer.token_to_id("<|user|>")
292
+ assistant_id = self.tokenizer.token_to_id("<|assistant|>")
293
+ text, _ = transformer_generate(
294
+ self.model, self.tokenizer,
295
+ f"<|user|> {prompt} <|assistant|>",
296
+ max_new=64, temperature=0.7, top_k=20,
297
+ repetition_penalty=1.1,
298
+ banned_ids=(user_id, assistant_id),
299
+ device=self.device)
300
+ else:
301
+ try:
302
+ from atomzero.chat_eval import chat_generate
303
+ except ImportError as exc:
304
+ raise RuntimeError(
305
+ "spiking chat is not part of the atomzero package; "
306
+ "use the transformer checkpoint") from exc
307
+ text, _ = chat_generate(
308
+ self.model, self.tokenizer, prompt, max_new_tokens=64,
309
+ temperature=0.7, top_k=20, repetition_penalty=1.1,
310
+ device=self.device)
311
+ return text or "I am not sure what to say yet."
312
+
313
+ def respond(self, text: str) -> str:
314
+ self.last_sources = []
315
+ self.add_episode("user", text)
316
+ extracted = extract_facts(text)
317
+ for key, value in extracted:
318
+ self.add_fact(key, value)
319
+
320
+ low = text.lower().strip()
321
+ if "what do you remember" in low or "what do you know about me" in low:
322
+ facts = self.data["facts"]
323
+ reply = ("I remember: " + "; ".join(
324
+ f"{fact['key']} is {fact['value']}" for fact in facts[-8:])
325
+ if facts else "I do not know anything about you yet.")
326
+ elif "what is my name" in low or "who am i" in low:
327
+ name = next((f["value"] for f in self.data["facts"]
328
+ if f["key"] == "name"), None)
329
+ reply = (f"Your name is {name}." if name
330
+ else "I do not know your name yet.")
331
+ elif "what do i like" in low or "what do you know i like" in low:
332
+ likes = [f["value"] for f in self.data["facts"]
333
+ if f["key"] == "likes"]
334
+ reply = ("You like " + ", ".join(likes) + "." if likes
335
+ else "I do not know what you like yet.")
336
+ elif low.startswith("add task"):
337
+ task = text.split("add task", 1)[1].strip(" :")
338
+ if task:
339
+ tid = self.add_task(task)
340
+ reply = f"Added task {tid}: {task}"
341
+ else:
342
+ reply = "Tell me the task after 'add task'."
343
+ elif "list tasks" in low:
344
+ open_tasks = [task for task in self.data["tasks"]
345
+ if task["status"] == "open"]
346
+ reply = ("Open tasks: " + "; ".join(
347
+ f"{task['id']} {task['text']}" for task in open_tasks)
348
+ if open_tasks else "There are no open tasks.")
349
+ elif "complete task" in low:
350
+ match = re.search(r"complete task\s*(\d+)", low)
351
+ if match and self.complete_task(int(match.group(1))):
352
+ reply = f"Completed task {match.group(1)}."
353
+ else:
354
+ reply = "I could not find that task."
355
+ elif "how are you" in low:
356
+ reply = "I am doing well, thank you. How are you?"
357
+ elif low.strip(" ?!.") in ("hello", "hi", "hey"):
358
+ reply = "Hello! How can I help you today?"
359
+ elif "thank you" in low:
360
+ reply = "You are welcome."
361
+ elif low.startswith("teach:"):
362
+ fact = text.split(":", 1)[1].strip()
363
+ if fact:
364
+ self.add_fact("note", fact)
365
+ reply = "I learned that."
366
+ else:
367
+ reply = "Tell me what to learn after 'teach:'."
368
+ elif extracted:
369
+ reply = "I will remember that."
370
+ else:
371
+ hits = self.docs.search(text, k=3) if self.docs else []
372
+ if hits:
373
+ lines = []
374
+ sources = []
375
+ unverified = False
376
+ for hit in hits[:3]:
377
+ teacher = hit.get("kind") == "teacher"
378
+ verified = bool(hit.get("verified", True))
379
+ if teacher and not verified:
380
+ label = "teacher note, unverified"
381
+ elif teacher:
382
+ reviewer = str(hit.get("reviewer", "")).strip()
383
+ label = (f"teacher note, verified by {reviewer}"
384
+ if reviewer else "teacher note, verified")
385
+ else:
386
+ label = "source"
387
+ location = hit.get("source_url") or hit["source"]
388
+ lines.append(
389
+ f"- {best_snippet(text, hit['chunk'])} "
390
+ f"({label}: {location})")
391
+ source = {"source": hit["source"], "title": hit["title"]}
392
+ for key in ("kind", "source_url", "teacher", "model",
393
+ "verified", "reviewer", "verified_at"):
394
+ if key in hit:
395
+ source[key] = hit[key]
396
+ if source not in sources:
397
+ sources.append(source)
398
+ if teacher and not verified:
399
+ unverified = True
400
+ self.last_sources = sources
401
+ reply = (f"I found these notes:\n" + "\n".join(lines) + "\n\n"
402
+ "This is a local note, not professional advice.")
403
+ if unverified:
404
+ reply += ("\n\nThis includes an unverified "
405
+ "teacher-derived note.")
406
+ else:
407
+ formatted = self.format_skill(text)
408
+ if formatted is not None:
409
+ reply = formatted
410
+ else:
411
+ question = looks_like_question(text)
412
+ if question and self.on_gap is not None:
413
+ self.on_gap(text)
414
+ general = self.allow_general_chat and self.generator is not None
415
+ if question and self.docs is not None and not general:
416
+ reply = ("I do not have a source for that yet. I can "
417
+ "add it to the research queue; approve the "
418
+ "topic and I will fetch it.")
419
+ else:
420
+ reply = self._generate(text)
421
+
422
+ self.add_episode("assistant", reply)
423
+ return reply
424
+
425
+
426
+ def load_model(path: str, device, arch: str = "spiking",
427
+ tokenizer_path: str = "data/chat_en_bpe8k.json",
428
+ adapter: str = ""):
429
+ if arch == "transformer":
430
+ ck = torch.load(Path(path) / "model.pt", map_location="cpu",
431
+ weights_only=False)
432
+ cfg = ck["config"]
433
+ model = TransformerLM(cfg["vocab"], cfg["dim"], cfg["layers"],
434
+ cfg["heads"], cfg["ff"]).to(device)
435
+ model.load_state_dict(ck["state"])
436
+ if adapter:
437
+ from atomzero.lora import apply_adapter
438
+ apply_adapter(model, ROOT / adapter)
439
+ model.eval()
440
+ from tokenizers import Tokenizer
441
+ tokenizer = Tokenizer.from_file(str(ROOT / tokenizer_path))
442
+ return model, tokenizer
443
+ from atomzero.spiking import load_checkpoint
444
+ model, tokenizer, _ = load_checkpoint(path)
445
+ model.to(device)
446
+ return model, tokenizer
447
+
448
+
449
+ def main():
450
+ ap = argparse.ArgumentParser()
451
+ ap.add_argument("--memory", default=str(DEFAULT_MEMORY))
452
+ ap.add_argument("--model", default="")
453
+ ap.add_argument("--arch", choices=["spiking", "transformer"],
454
+ default="spiking")
455
+ ap.add_argument("--tokenizer", default="data/chat_en_bpe8k.json")
456
+ ap.add_argument("--docs", default="")
457
+ ap.add_argument("--adapter", default="")
458
+ ap.add_argument("--message", default="")
459
+ args = ap.parse_args()
460
+
461
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
462
+ model = tokenizer = None
463
+ if args.model:
464
+ model, tokenizer = load_model(args.model, device, args.arch,
465
+ args.tokenizer, args.adapter)
466
+ docs = None
467
+ if args.docs:
468
+ from atomzero.doc_memory import DocMemory
469
+ docs = DocMemory(args.docs)
470
+ human = ChatHuman(args.memory, model, tokenizer, device, args.arch, docs)
471
+
472
+ if args.message:
473
+ print(human.respond(args.message))
474
+ return
475
+ print("Chat human ready. Ctrl-C to exit.")
476
+ while True:
477
+ try:
478
+ text = input("you> ")
479
+ except (EOFError, KeyboardInterrupt):
480
+ break
481
+ if text.strip():
482
+ print("tiny>", human.respond(text))
483
+
484
+
485
+ if __name__ == "__main__":
486
+ main()