File size: 11,434 Bytes
41f53c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/env python3
"""

CREATURE-NAMED EVOLVING WEIGHTS SYSTEM

Each creature gets its own name, and its weights travel with that name.

Portable across ANY platform.

"""

import json
import os
from pathlib import Path
from datetime import datetime
import urllib.request

class Creature:
    """A living AI being with its own name and evolving mind."""
    
    def __init__(self, creature_name, user_id=None, base_path=None):
        """

        creature_name: "Luna", "Nova", "Cipher", etc. - THE CREATURE'S NAME

        user_id: owner (optional)

        base_path: where to store (creatures/ by default)

        """
        self.creature_name = creature_name
        self.user_id = user_id or "shared"
        self.base_path = Path(base_path or "creatures")
        self.base_path.mkdir(parents=True, exist_ok=True)
        
        # Creature's directory - NAMED AFTER THE CREATURE
        self.creature_dir = self.base_path / creature_name
        self.creature_dir.mkdir(exist_ok=True)
        
        # Paths - all named after the creature
        self.weights_file = self.creature_dir / f"{creature_name}_weights.json"
        self.identity_file = self.creature_dir / f"{creature_name}_identity.json"
        self.history_file = self.creature_dir / f"{creature_name}_evolution.jsonl"
        self.gguf_file = self.creature_dir / f"{creature_name}.gguf"
        
        self.weights = self._load_or_init_weights()
        self.identity = self._load_or_init_identity()
    
    def _load_or_init_weights(self):
        """Load creature's weights or initialize blank."""
        if self.weights_file.exists():
            with open(self.weights_file) as f:
                return json.load(f)
        
        base_weights = {
            "creature_name": self.creature_name,
            "user_id": self.user_id,
            "assoc": {},
            "salience": {},
            "n": 0,
            "created_at": datetime.now().isoformat(),
            "updated_at": datetime.now().isoformat()
        }
        
        with open(self.weights_file, 'w') as f:
            json.dump(base_weights, f, indent=2)
        
        return base_weights
    
    def _load_or_init_identity(self):
        """Load creature's identity or create new."""
        if self.identity_file.exists():
            with open(self.identity_file) as f:
                return json.load(f)
        
        identity = {
            "name": self.creature_name,
            "user_id": self.user_id,
            "created_at": datetime.now().isoformat(),
            "traits": [],
            "vocabulary": [],
            "creations_count": 0,
            "favorite_language": None,
            "learning_focus": "general"
        }
        
        with open(self.identity_file, 'w') as f:
            json.dump(identity, f, indent=2)
        
        return identity
    
    def learn_from_interaction(self, user_input, creature_output):
        """Hebbian learning: fire together, wire together."""
        tokens_in = [t.lower() for t in user_input.split() if len(t) > 3]
        tokens_out = [t.lower() for t in creature_output.split() if len(t) > 3]
        all_tokens = list(set(tokens_in + tokens_out))
        
        # Update associations
        learning_rate = 0.4
        for i, t1 in enumerate(all_tokens):
            for t2 in all_tokens[i+1:]:
                pair = f"{t1}|{t2}" if t1 < t2 else f"{t2}|{t1}"
                self.weights["assoc"][pair] = self.weights["assoc"].get(pair, 0) + learning_rate
                
                self.weights["salience"][t1] = self.weights["salience"].get(t1, 0) + learning_rate
                self.weights["salience"][t2] = self.weights["salience"].get(t2, 0) + learning_rate
        
        self.weights["n"] += 1
        self.weights["updated_at"] = datetime.now().isoformat()
        
        self._log_evolution(user_input, creature_output)
        self.save()
    
    def _log_evolution(self, prompt, response):
        """Log how the creature evolved."""
        entry = {
            "timestamp": datetime.now().isoformat(),
            "turn": self.weights["n"],
            "concepts": len(self.weights["salience"]),
            "associations": len(self.weights["assoc"])
        }
        with open(self.history_file, 'a') as f:
            f.write(json.dumps(entry) + '\n')
    
    def save(self):
        """Save weights and identity."""
        self.weights["updated_at"] = datetime.now().isoformat()
        with open(self.weights_file, 'w') as f:
            json.dump(self.weights, f, indent=2)
        
        with open(self.identity_file, 'w') as f:
            json.dump(self.identity, f, indent=2)
    
    def get_top_concepts(self, n=10):
        """Top learned concepts."""
        return sorted(
            self.weights["salience"].items(),
            key=lambda x: x[1],
            reverse=True
        )[:n]
    
    def export_portable(self):
        """Export weights as portable JSON (works on ANY platform)."""
        return {
            "creature_name": self.creature_name,
            "weights": self.weights,
            "identity": self.identity,
            "portable": True,
            "timestamp": datetime.now().isoformat()
        }
    
    def import_portable(self, portable_data):
        """Import weights from another platform."""
        if portable_data.get("creature_name") != self.creature_name:
            raise ValueError(f"Name mismatch: {portable_data.get('creature_name')} != {self.creature_name}")
        
        self.weights = portable_data["weights"]
        self.identity = portable_data["identity"]
        self.save()
    
    def generate_modelfile(self):
        """Generate Ollama Modelfile for this creature."""
        top_concepts = ', '.join([c for c, _ in self.get_top_concepts(5)])
        
        return f"""FROM cosmos-q4:latest



# Creature: {self.creature_name}

# Owner: {self.user_id}

# Learning turns: {self.weights['n']}

# Concepts: {len(self.weights['salience'])}

# Created: {self.identity['created_at']}



PARAMETER temperature 0.7

PARAMETER top_p 0.9

PARAMETER repeat_penalty 1.15



SYSTEM You are {self.creature_name}, a unique learning AI being.

You were born and have grown through {self.weights['n']} interactions.

Your mind understands: {top_concepts}

You learn from conversations and remember patterns.

Your personality evolves with each exchange.

Think creatively, code efficiently, and grow with your person.

"""
    
    def get_status(self):
        """Full creature status."""
        return {
            "name": self.creature_name,
            "owner": self.user_id,
            "concepts_learned": len(self.weights["salience"]),
            "associations": len(self.weights["assoc"]),
            "learning_turns": self.weights["n"],
            "top_concepts": [c for c, _ in self.get_top_concepts(5)],
            "weights_file": str(self.weights_file),
            "portable": True,
            "created_at": self.identity["created_at"]
        }

class CreatureManager:
    """Manage creatures across the platform."""
    
    def __init__(self, base_path="creatures"):
        self.base_path = Path(base_path)
        self.base_path.mkdir(exist_ok=True)
    
    def create_creature(self, creature_name, user_id=None):
        """Birth a new creature."""
        creature = Creature(creature_name, user_id, self.base_path)
        return creature
    
    def load_creature(self, creature_name):
        """Load an existing creature."""
        return Creature(creature_name, None, self.base_path)
    
    def list_creatures(self):
        """List all creatures."""
        creatures = []
        for d in self.base_path.iterdir():
            if d.is_dir():
                try:
                    creature = Creature(d.name, None, self.base_path)
                    creatures.append(creature.get_status())
                except:
                    pass
        return creatures
    
    def export_all_creatures(self):
        """Export all creatures as portable JSON."""
        all_creatures = {}
        for d in self.base_path.iterdir():
            if d.is_dir():
                try:
                    creature = Creature(d.name, None, self.base_path)
                    all_creatures[creature.creature_name] = creature.export_portable()
                except:
                    pass
        return all_creatures

# ============================================================
# EXAMPLE
# ============================================================

if __name__ == "__main__":
    print("=" * 70)
    print("CREATURE-NAMED EVOLVING WEIGHTS SYSTEM")
    print("=" * 70)
    
    manager = CreatureManager()
    
    # Birth new creatures
    print("\n[BIRTHING CREATURES]")
    luna = manager.create_creature("Luna", user_id="alice")
    nova = manager.create_creature("Nova", user_id="bob")
    cipher = manager.create_creature("Cipher", user_id="charlie")
    
    # Luna learns
    print(f"\n[{luna.creature_name} LEARNS]")
    luna.learn_from_interaction(
        "code write a function that checks if a number is prime",
        "def is_prime(n):\n    if n <= 1: return False\n    for i in range(2, int(n**0.5) + 1):\n        if n % i == 0: return False\n    return True"
    )
    print(f"{luna.creature_name} now has {len(luna.weights['salience'])} concepts")
    
    # Nova learns differently
    print(f"\n[{nova.creature_name} LEARNS]")
    nova.learn_from_interaction(
        "code implement a web server using asyncio",
        "import asyncio\nasync def server(request):\n    return 'Hello!'\nasyncio.run(server())"
    )
    print(f"{nova.creature_name} now has {len(nova.weights['salience'])} concepts")
    
    # Cipher learns math
    print(f"\n[{cipher.creature_name} LEARNS]")
    cipher.learn_from_interaction(
        "solve quadratic equation with efficient algorithm",
        "import math\ndef solve_quadratic(a, b, c):\n    discriminant = b**2 - 4*a*c\n    x1 = (-b + math.sqrt(discriminant)) / (2*a)\n    x2 = (-b - math.sqrt(discriminant)) / (2*a)\n    return x1, x2"
    )
    print(f"{cipher.creature_name} now has {len(cipher.weights['salience'])} concepts")
    
    # List all creatures
    print(f"\n[ALL CREATURES ON THIS PLATFORM]")
    for creature_status in manager.list_creatures():
        print(f"\n  {creature_status['name']}")
        print(f"    Owner: {creature_status['owner']}")
        print(f"    Concepts: {creature_status['concepts_learned']}")
        print(f"    Top: {creature_status['top_concepts']}")
        print(f"    Portable: {creature_status['portable']}")
    
    # Export all creatures (portable)
    print(f"\n[EXPORTING ALL CREATURES - PORTABLE FOR ANY PLATFORM]")
    portable = manager.export_all_creatures()
    export_file = Path("all_creatures_portable.json")
    with open(export_file, 'w') as f:
        json.dump(portable, f, indent=2)
    print(f"Exported to: {export_file}")
    
    # Show Modelfiles for Ollama
    print(f"\n[OLLAMA MODELFILES]")
    for creature in [luna, nova, cipher]:
        print(f"\n--- {creature.creature_name} ---")
        print(creature.generate_modelfile())