Extracts structured JSON from conversation turns based on memory type (episodic, semantic, procedural). Runs in ~1s on GPU.
1from transformers import AutoTokenizer, AutoModelForCausalLM
2import json
3
4model = AutoModelForCausalLM.from_pretrained(
5 "kurcontko/mnemotree-leaf-v1", torch_dtype="bfloat16", device_map="auto"
6)
7tokenizer = AutoTokenizer.from_pretrained("kurcontko/mnemotree-leaf-v1")
8
9system = (
10 "You are a memory extraction assistant. Given a conversation turn with a "
11 "type prefix (<|semantic|>, <|episodic|>, or <|procedural|>), extract "
12 "structured information as a JSON object. Output ONLY valid JSON, no explanation."
13)
14
15user = "<|semantic|> Python uses the GIL for thread safety in CPython."
16
17messages = [
18 {"role": "system", "content": system},
19 {"role": "user", "content": user},
20]
21inputs = tokenizer.apply_chat_template(messages, return_tensors="pt").to(model.device)
22
23with torch.no_grad():
24 output = model.generate(inputs, max_new_tokens=256, temperature=0.1, top_p=0.95)
25
26result = tokenizer.decode(output[0][inputs.shape[1]:], skip_special_tokens=True)
27parsed = json.loads(result)
28# {"fact": "Python uses GIL for thread safety in CPython", "subject": "Python", "confidence": 0.92}
1from mnemotree import MemoryCoreBuilder
2
3memory = (
4 MemoryCoreBuilder(store)
5 .with_local_models(device="cuda") # auto-downloads root + leaf
6 .build()
7)
8
9item = await memory.remember("Python uses the GIL for thread safety")
10print(item.metadata["extraction"])
11# {"fact": "...", "subject": "Python", "confidence": 0.92}
Pair with
mnemotree-root-v1 (ModernBERT classifier, 149M) for the full pipeline:
1@misc{mnemotree2025,
2 title={mnemotree: Local-first memory for LLM agents},
3 author={kurcontko},
4 year={2025},
5 url={https://github.com/kurcontko/mnemotree}
6}