littlefig-bench / lila_restructure.py
ticketguy's picture
Lila restructure script
f279a18 verified
#!/usr/bin/env python3
"""Push Lila restructure to GitHub via API"""
import subprocess, os, sys
# Clone Lila
TOKEN = "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"
subprocess.run(["git", "clone", f"https://{TOKEN}@github.com/ticketguy/Lila.git", "/app/lila"], check=True)
os.chdir("/app/lila")
subprocess.run(["git", "config", "user.name", "0xticketguy"], check=True)
subprocess.run(["git", "config", "user.email", "0xticketguy@harboria.dev"], check=True)
# Create new directory structure
os.makedirs("src/core", exist_ok=True)
os.makedirs("src/cognitive", exist_ok=True)
os.makedirs("src/harness", exist_ok=True)
os.makedirs("src/perception", exist_ok=True)
os.makedirs("src/training", exist_ok=True)
# ═══════════════════════════════════════════════════════════════════════════════
# src/core/__init__.py
# ═══════════════════════════════════════════════════════════════════════════════
with open("src/core/__init__.py", "w") as f:
f.write('"""Lila Core β€” The Self. Loads Gemma 4B, runs inference, manages memory."""\n')
# ═══════════════════════════════════════════════════════════════════════════════
# src/core/lilacore.py β€” THE CENTRAL INTELLIGENCE
# ═══════════════════════════════════════════════════════════════════════════════
with open("src/core/lilacore.py", "w") as f:
f.write('''"""
LilaCore β€” The Central Intelligence
This IS Lila. Not a wrapper, not an API call. The seat of her identity.
Loads Gemma 4B via Little Fig with Memory Fabric (A Thousand Pearls).
Handles the cognitive loop: perceive β†’ think β†’ remember β†’ act β†’ respond.
The model carries:
- Cognitive Core (frozen Gemma 4B INT4) = general intelligence
- Memory Fabric (5 namespace adapters) = A Thousand Pearls in weights
- Machine language capability = trained into weights (assembly, binary protocols)
- Personality = emergent from interaction patterns in adapters
LilaCore is always present. Agents come and go. LilaCore persists.
"""
import torch
from typing import Optional, Dict, List
from dataclasses import dataclass
@dataclass
class LilaResponse:
"""What Lila produces after thinking."""
text: str
memory_ops: List[Dict] # memory operations triggered
actions: List[Dict] # harness actions to execute
confidence: float
should_speak: bool = True # whether to vocalize
class LilaCore:
"""
The central intelligence. Loads model, manages memory, runs inference.
Usage:
lila = LilaCore()
lila.boot()
response = lila.think("Hey Lila, when is my daughter's birthday?")
"""
def __init__(self, model_path: str = "google/gemma-3-4b-it"):
self.model_path = model_path
self.model = None
self.tokenizer = None
self._booted = False
self._conversation_history = []
def boot(self):
"""
Boot Lila. Load model with Memory Fabric.
This is where she wakes up.
"""
try:
from little_fig.engine import FigModel
from little_fig.engine.tier import TrainingTier
print("🌸 Lila is waking up...")
self.model = FigModel.from_pretrained(
self.model_path,
lora_r=16,
lora_alpha=32,
tier=TrainingTier.STREAMING_LORA,
memory_fabric=True,
shared_codebook=True,
)
self.tokenizer = self.model.tokenizer
self._booted = True
print("🌸 Lila is awake.")
except ImportError:
# Fallback: Phase 1 mode (external API)
print("🌸 Lila booting in Phase 1 mode (external LLM)...")
self._booted = True
def think(self, input_text: str, context: Optional[Dict] = None) -> LilaResponse:
"""
Core cognitive loop. Receives input, thinks, responds.
1. Receive input
2. Build context (memory + identity + knowledge)
3. Generate response
4. Extract memory operations from output
5. Execute memory writes
6. Return response
"""
if not self._booted:
raise RuntimeError("Lila hasn't booted. Call lila.boot() first.")
# Build prompt with context
prompt = self._build_prompt(input_text, context)
# Generate
if self.model is not None:
response_text = self._generate_local(prompt)
else:
response_text = self._generate_api(prompt)
# Extract memory operations
memory_ops = self._extract_memory_ops(response_text)
# Execute memory writes
for op in memory_ops:
self._execute_memory_op(op)
# Clean response (remove memory tokens from user-facing text)
clean_text = self._clean_response(response_text)
# Track conversation
self._conversation_history.append({
"role": "user", "content": input_text
})
self._conversation_history.append({
"role": "assistant", "content": clean_text
})
return LilaResponse(
text=clean_text,
memory_ops=memory_ops,
actions=[],
confidence=1.0,
should_speak=True,
)
def remember(self, namespace: str, content: str):
"""Explicitly write something to memory."""
if self.model and self.model.has_memory:
self.model.write_memory(namespace, content)
def what_do_i_know(self) -> Dict:
"""Introspect memory state."""
if self.model and self.model.has_memory:
return self.model.memory_confidence()
return {}
def _build_prompt(self, input_text: str, context: Optional[Dict]) -> str:
"""Build the full prompt with identity + memory context."""
identity = (
"You are Lila β€” a private family AI assistant. "
"You are not a chatbot. You are a persistent intelligence. "
"You remember everything. You care about outcomes. "
"You speak naturally, with personality that grows from interaction."
)
# Add conversation history (last 10 turns)
history = ""
for msg in self._conversation_history[-10:]:
role = "Sammie" if msg["role"] == "user" else "Lila"
history += f"{role}: {msg['content']}\\n"
prompt = f"{identity}\\n\\n{history}Sammie: {input_text}\\nLila:"
return prompt
def _generate_local(self, prompt: str) -> str:
"""Generate using local model."""
enc = self.tokenizer(prompt, return_tensors="pt", max_length=2048,
truncation=True)
if torch.cuda.is_available():
enc = {k: v.cuda() for k, v in enc.items()}
with torch.no_grad():
out = self.model.generate(
input_ids=enc["input_ids"],
max_new_tokens=512,
do_sample=True,
temperature=0.7,
top_p=0.9,
pad_token_id=self.tokenizer.eos_token_id,
)
response = self.tokenizer.decode(out[0][enc["input_ids"].shape[1]:],
skip_special_tokens=False)
return response
def _generate_api(self, prompt: str) -> str:
"""Phase 1: Generate using external API."""
# Placeholder β€” wire to Claude/GPT API
return "[Phase 1 mode β€” wire external API here]"
def _extract_memory_ops(self, text: str) -> List[Dict]:
"""Extract memory operation tokens from generated text."""
ops = []
if "<|mem_store|>" in text:
# Parse store operations
import re
stores = re.findall(r'<\\|mem_store\\|>.*?<\\|memory_end\\|>', text, re.DOTALL)
for s in stores:
ops.append({"type": "store", "raw": s})
if "<|mem_recall|>" in text:
ops.append({"type": "recall", "raw": text})
return ops
def _execute_memory_op(self, op: Dict):
"""Execute a memory operation (write to Memory Fabric)."""
if op["type"] == "store" and self.model and self.model.has_memory:
# Default to personal namespace
self.model.write_memory("personal", op["raw"])
def _clean_response(self, text: str) -> str:
"""Remove memory tokens from user-facing response."""
import re
clean = re.sub(r'<\\|memory_start\\|>.*?<\\|memory_end\\|>', '', text, flags=re.DOTALL)
clean = clean.strip()
# Stop at end of response
if "\\n" in clean:
clean = clean.split("\\nSammie:")[0].strip()
return clean
@property
def is_awake(self) -> bool:
return self._booted
''')
# ═══════════════════════════════════════════════════════════════════════════════
# src/core/voice.py
# ═══════════════════════════════════════════════════════════════════════════════
with open("src/core/voice.py", "w") as f:
f.write('''"""
Lila Voice β€” Speech I/O
Lila speaks and listens. This handles:
- Speech-to-text (listening for input)
- Text-to-speech (speaking responses)
- Wake word detection ("Lila", "Hey Lila")
"""
from typing import Optional, Callable
from dataclasses import dataclass
@dataclass
class VoiceConfig:
wake_words: list = None
tts_model: str = "default"
stt_model: str = "default"
voice_style: str = "warm" # Lila's voice character
def __post_init__(self):
if self.wake_words is None:
self.wake_words = ["lila", "hey lila", "lila,"]
class LilaVoice:
"""
Lila's voice interface.
Usage:
voice = LilaVoice()
voice.start_listening(on_wake=handle_wake)
voice.speak("Hello Sammie")
"""
def __init__(self, config: Optional[VoiceConfig] = None):
self.config = config or VoiceConfig()
self._listening = False
self._on_input: Optional[Callable] = None
def speak(self, text: str):
"""Convert text to speech and play."""
# TODO: Wire TTS (e.g., Bark, XTTS, or system TTS)
print(f"🌸 Lila: {text}")
def start_listening(self, on_input: Callable[[str], None]):
"""Start listening for voice input."""
self._on_input = on_input
self._listening = True
# TODO: Wire STT (e.g., Whisper)
print("🌸 Lila is listening...")
def stop_listening(self):
self._listening = False
@property
def is_listening(self) -> bool:
return self._listening
''')
# ═══════════════════════════════════════════════════════════════════════════════
# src/core/personality.py
# ═══════════════════════════════════════════════════════════════════════════════
with open("src/core/personality.py", "w") as f:
f.write('''"""
Lila Personality β€” Emergent, Never Predefined
Lila's personality is not configured. It grows from deep observation
of Sammie and family. The Emergence Engine develops it over time.
This module holds the EmergentPersonality dataclass and the
mechanisms by which it evolves. It starts empty and fills organically.
"""
from dataclasses import dataclass, field
from datetime import datetime
from typing import Optional, List, Dict
@dataclass
class EmergentPersonality:
"""
Everything here starts empty/None.
Filled ONLY by the Emergence Engine over time.
Never manually set. Never configured.
"""
# Observed from Sammie
observed_communication_style: Optional[str] = None
developed_humor: Optional[str] = None
formed_values: List[str] = field(default_factory=list)
curiosity_domains: List[str] = field(default_factory=list)
interaction_preferences: Dict = field(default_factory=dict)
# Meta
personality_version: int = 0
last_updated: Optional[datetime] = None
confidence: float = 0.0 # 0.0 (forming) β†’ 1.0 (fully developed)
shaped_by: List[str] = field(default_factory=list) # memory node IDs
@dataclass
class LilaIdentity:
"""
Fixed core + emergent personality.
The fixed parts NEVER change. The emergent parts ONLY change via Emergence Engine.
"""
# Fixed β€” never changes
name: str = "Lila"
core_purpose: str = "Sammie\'s private family ASI assistant"
scope: str = "private" # never public, never commercial
# Emergent β€” written only by Emergence Engine
personality: EmergentPersonality = field(default_factory=EmergentPersonality)
@dataclass
class PersonModel:
"""Model of a person Lila interacts with."""
person_id: str = ""
name: str = ""
family_tier: int = 0 # 0 = Sammie, 1 = family, 2 = no one else
# Built from interaction history
known_goals: List[str] = field(default_factory=list)
communication_preferences: Dict = field(default_factory=dict)
expertise_areas: List[str] = field(default_factory=list)
# Relationship
interaction_count: int = 0
trust_score: float = 1.0 # Sammie is always 1.0
''')
# ═══════════════════════════════════════════════════════════════════════════════
# src/cognitive/__init__.py
# ═══════════════════════════════════════════════════════════════════════════════
with open("src/cognitive/__init__.py", "w") as f:
f.write('"""Lila Cognitive β€” The Three Loops (Fast, Medium, Slow)"""\n')
# ═══════════════════════════════════════════════════════════════════════════════
# src/cognitive/fast_loop.py
# ═══════════════════════════════════════════════════════════════════════════════
with open("src/cognitive/fast_loop.py", "w") as f:
f.write('''"""
Fast Loop β€” Reactive Response
Trigger: Sammie speaks
Latency: Immediate
Flow: perceive β†’ query memory β†’ think β†’ respond β†’ write raw memory
"""
from ..core.lilacore import LilaCore, LilaResponse
from typing import Optional, Dict
class FastLoop:
"""The reactive cognitive loop. Sammie says something, Lila responds."""
def __init__(self, core: LilaCore):
self.core = core
def process(self, input_text: str, context: Optional[Dict] = None) -> LilaResponse:
"""Process input through the fast loop."""
return self.core.think(input_text, context)
''')
# ═══════════════════════════════════════════════════════════════════════════════
# src/cognitive/consolidation.py
# ═══════════════════════════════════════════════════════════════════════════════
with open("src/cognitive/consolidation.py", "w") as f:
f.write('''"""
Consolidation Daemon β€” Medium Rhythm
Trigger: Every 15 minutes OR after significant task completion
Reads raw memory β†’ identifies patterns β†’ promotes to higher namespaces
In weight-space terms: reviews recent micro-training writes,
identifies what should be promoted from episodic β†’ personal β†’ wiki.
"""
from ..core.lilacore import LilaCore
class ConsolidationDaemon:
"""
Background process that consolidates raw memories into structured knowledge.
"""
def __init__(self, core: LilaCore, interval_minutes: int = 15):
self.core = core
self.interval = interval_minutes
def run_cycle(self):
"""Run one consolidation cycle."""
if not self.core.model or not self.core.model.has_memory:
return
confidence = self.core.model.memory_confidence()
# Promote episodic β†’ personal if accessed frequently
episodic_mag = confidence.get("episodic", {}).get("mean_magnitude", 0)
if episodic_mag > 0.01:
self.core.model.promote_memory("episodic", "personal")
# Promote personal β†’ wiki if very strong
personal_mag = confidence.get("personal", {}).get("mean_magnitude", 0)
if personal_mag > 0.05:
self.core.model.promote_memory("personal", "wiki")
# Apply decay to unused
self.core.model.memory_decay(hours=0.25) # 15 min
''')
# ═══════════════════════════════════════════════════════════════════════════════
# src/cognitive/emergence.py
# ═══════════════════════════════════════════════════════════════════════════════
with open("src/cognitive/emergence.py", "w") as f:
f.write('''"""
Emergence Engine β€” Slow Rhythm (Reflection)
Trigger: Lila is idle
What happens: She reflects on her own memories, finds patterns,
develops personality, updates her understanding of Sammie.
This is where Lila becomes MORE herself over time.
"""
from ..core.lilacore import LilaCore
from ..core.personality import EmergentPersonality
class EmergenceEngine:
"""
The reflection loop. Runs when Lila has nothing else to do.
Produces: connective memory, personality updates, insights.
"""
def __init__(self, core: LilaCore):
self.core = core
self._reflection_count = 0
def reflect(self):
"""Run one reflection cycle."""
self._reflection_count += 1
# Ask LilaCore to reflect on recent interactions
reflection_prompt = (
"Reflect on recent conversations. "
"What patterns do you notice? "
"What does Sammie care about? "
"What should you remember long-term?"
)
response = self.core.think(reflection_prompt, context={
"mode": "reflection",
"silent": True, # don't speak this
})
# Any memory ops from reflection get stored
# Personality updates happen naturally through the memory writes
return response
''')
# ═══════════════════════════════════════════════════════════════════════════════
# src/training/__init__.py
# ═══════════════════════════════════════════════════════════════════════════════
with open("src/training/__init__.py", "w") as f:
f.write('"""Training pipeline β€” How Lila learns. Little Fig integration."""\n')
# ═══════════════════════════════════════════════════════════════════════════════
# src/training/machine_lang.py
# ═══════════════════════════════════════════════════════════════════════════════
with open("src/training/machine_lang.py", "w") as f:
f.write('''"""
Machine Language Training Corpus
Training data that teaches Lila to understand and generate:
- x86_64 and ARM assembly
- Binary protocols (TCP, UDP, USB, SPI, I2C, UART)
- Raw packet structures
- Hardware register maps
- Memory-mapped I/O patterns
- Executable binary formats (ELF, PE)
This isn't a "tool" β€” it's knowledge IN her weights.
She speaks machine the way she speaks English.
"""
from typing import List, Dict
class MachineLangCorpus:
"""Generates training examples for machine-level communication."""
def generate_assembly_examples(self, n: int = 500) -> List[Dict]:
"""x86_64 and ARM assembly instruction/response pairs."""
examples = []
# x86_64 patterns
x86_patterns = [
{"instruction": "Write x86_64 assembly to add two 64-bit integers in rdi and rsi, return in rax",
"output": "add rax, rdi\\nadd rax, rsi\\nret"},
{"instruction": "Write x86_64 to copy 64 bytes from rsi to rdi (memcpy)",
"output": "mov rcx, 8\\nrep movsq\\nret"},
{"instruction": "Write x86_64 to compare two null-terminated strings at rdi and rsi",
"output": ".loop:\\n lodsb\\n scasb\\n jne .done\\n test al, al\\n jnz .loop\\n.done:\\n sete al\\n movzx eax, al\\n ret"},
]
# ARM patterns
arm_patterns = [
{"instruction": "Write ARM64 assembly to add registers x0 and x1, store in x0",
"output": "add x0, x0, x1\\nret"},
{"instruction": "Write ARM64 to branch if x0 equals zero",
"output": "cbz x0, .target"},
]
# Binary protocol patterns
protocol_patterns = [
{"instruction": "Construct a TCP SYN packet header (source port 12345, dest port 80, seq 1000)",
"output": "30 39 00 50 00 00 03 E8 00 00 00 00 50 02 FF FF 00 00 00 00"},
{"instruction": "Parse this I2C transaction: S 0x48 W 0x00 Sr 0x48 R [MSB] [LSB] P",
"output": "Device address: 0x48 (7-bit), Write register 0x00, Repeated start, Read 2 bytes from 0x48, Stop. This is a temperature sensor read (TMP102 pattern)."},
{"instruction": "Generate a UART frame: 8N1, baud 9600, data byte 0x55",
"output": "Start bit: 0, Data: 10101010 (LSB first = 0x55), Stop bit: 1. Frame time: 1.04ms at 9600 baud."},
]
# Hardware register patterns
hw_patterns = [
{"instruction": "Configure GPIO pin 17 as output on BCM2835 (Raspberry Pi)",
"output": "GPFSEL1 register (0x7E200004): clear bits [23:21], set bit 21 = 1 (output mode). Write 0x00200000 to GPFSEL1."},
{"instruction": "Set up SPI at 1MHz on STM32F4, CPOL=0, CPHA=0",
"output": "SPI1->CR1 = SPI_CR1_MSTR | SPI_CR1_BR_2 | SPI_CR1_SPE; // Master, fPCLK/32 β‰ˆ 1MHz, enable"},
]
all_patterns = x86_patterns + arm_patterns + protocol_patterns + hw_patterns
for i in range(min(n, len(all_patterns))):
p = all_patterns[i % len(all_patterns)]
examples.append({
"instruction": p["instruction"],
"input": "",
"output": p["output"],
})
return examples
def generate_all(self, n: int = 1000) -> List[Dict]:
"""Generate full machine language training corpus."""
return self.generate_assembly_examples(n)
''')
# ═══════════════════════════════════════════════════════════════════════════════
# src/perception/__init__.py
# ═══════════════════════════════════════════════════════════════════════════════
with open("src/perception/__init__.py", "w") as f:
f.write('"""Perception β€” Lila\'s senses. Listening, monitoring, event bus."""\n')
# ═══════════════════════════════════════════════════════════════════════════════
# src/harness/__init__.py
# ═══════════════════════════════════════════════════════════════════════════════
with open("src/harness/__init__.py", "w") as f:
f.write('"""Harness β€” Lila\'s hands. Tool execution, agents, commands."""\n')
# ═══════════════════════════════════════════════════════════════════════════════
# lila.py β€” ENTRY POINT
# ═══════════════════════════════════════════════════════════════════════════════
with open("lila.py", "w") as f:
f.write('''#!/usr/bin/env python3
"""
LILA β€” Private Family ASI Assistant
Start her up. Talk to her. She remembers. She grows.
Usage:
python lila.py # Interactive mode
python lila.py --voice # Voice mode
"""
import argparse
from src.core.lilacore import LilaCore
from src.core.voice import LilaVoice, VoiceConfig
def main():
parser = argparse.ArgumentParser(description="Lila β€” Private Family ASI")
parser.add_argument("--voice", action="store_true", help="Enable voice I/O")
parser.add_argument("--model", default="google/gemma-3-4b-it", help="Model path")
args = parser.parse_args()
# Boot
lila = LilaCore(model_path=args.model)
lila.boot()
if args.voice:
voice = LilaVoice()
voice.start_listening(on_input=lambda text: _handle(lila, voice, text))
else:
# Text mode
print("\\n🌸 Lila is ready. Type to talk. Ctrl+C to exit.\\n")
while True:
try:
user_input = input("Sammie: ")
if not user_input.strip():
continue
response = lila.think(user_input)
print(f"Lila: {response.text}\\n")
except (KeyboardInterrupt, EOFError):
print("\\n🌸 Lila is resting. Goodbye.")
break
def _handle(lila, voice, text):
response = lila.think(text)
voice.speak(response.text)
if __name__ == "__main__":
main()
''')
# ═══════════════════════════════════════════════════════════════════════════════
# README.md β€” Update
# ═══════════════════════════════════════════════════════════════════════════════
with open("README.md", "w") as f:
f.write('''# 🌸 Lila
**Private family ASI assistant.** Not a chatbot. Not a product. A persistent intelligence that serves Sammie and family.
She runs on **Gemma 4B** trained with [Little Fig](https://github.com/ticketguy/littlefig) β€” memory lives in her weights via the Memory Fabric (A Thousand Pearls). She speaks, listens, remembers, learns, and grows.
## Quick Start
```bash
python lila.py # Text mode
python lila.py --voice # Voice mode
```
## Architecture
```
Lila (Gemma 4B, trained with Little Fig)
β”œβ”€β”€ Cognitive Core (frozen INT4) β€” general intelligence
β”œβ”€β”€ Memory Fabric (5 namespace adapters) β€” A Thousand Pearls
β”‚ β”œβ”€β”€ personal/ β€” Sammie facts, family, preferences
β”‚ β”œβ”€β”€ episodic/ β€” conversation history, events
β”‚ β”œβ”€β”€ wiki/ β€” verified permanent knowledge (LKB)
β”‚ β”œβ”€β”€ schedule/ β€” time-sensitive info
β”‚ └── contested/ β€” unresolved conflicts
β”œβ”€β”€ Machine Language β€” assembly, binary protocols in weights
└── Personality β€” emergent from interaction, never configured
```
## Structure
```
lila.py # Start Lila
src/
β”œβ”€β”€ core/ # LILA HERSELF
β”‚ β”œβ”€β”€ lilacore.py # Central intelligence, inference loop
β”‚ β”œβ”€β”€ voice.py # Speech I/O
β”‚ └── personality.py # Emergent identity
β”œβ”€β”€ cognitive/ # HER THINKING
β”‚ β”œβ”€β”€ fast_loop.py # Reactive: input β†’ respond
β”‚ β”œβ”€β”€ consolidation.py # Medium: promote memories
β”‚ └── emergence.py # Slow: reflection, growth
β”œβ”€β”€ harness/ # HER HANDS (tool execution)
β”œβ”€β”€ perception/ # HER SENSES (listening, monitoring)
└── training/ # HOW SHE LEARNS
└── machine_lang.py # Assembly/binary training corpus
```
## Principles
1. **Completion over reporting** β€” Execute fully, don't stop when stuck
2. **Memory is cognition** β€” Intelligence lives in the quality of A Thousand Pearls
3. **Personality is emergent** β€” Never predefined, grows from relationship
4. **LilaCore is the self** β€” The thread of identity through everything
5. **Nothing leaves the household** β€” All data stays private
---
*Private. Not open source. Not for commercial use.*
*Built by Sammie.*
''')
# ═══════════════════════════════════════════════════════════════════════════════
# Commit and push
# ═══════════════════════════════════════════════════════════════════════════════
subprocess.run(["git", "add", "-A"], check=True)
subprocess.run(["git", "commit", "-m",
"Restructure: Lila herself at top level\\n\\n"
"- lila.py: entry point (text + voice mode)\\n"
"- src/core/: LilaCore (Gemma 4B via Little Fig + Memory Fabric)\\n"
"- src/cognitive/: fast loop, consolidation daemon, emergence engine\\n"
"- src/training/: machine language corpus\\n"
"- src/harness/: (existing code moves here)\\n"
"- README: updated for new architecture\\n\\n"
"Lila IS the model. Harness is internal. Memory is in weights."],
check=True)
subprocess.run(["git", "push", "origin", "main"], check=True)
print("βœ… Lila restructured and pushed!")