-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlanguage_grounding.py
More file actions
273 lines (221 loc) · 8.19 KB
/
language_grounding.py
File metadata and controls
273 lines (221 loc) · 8.19 KB
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
#!/usr/bin/env python3
"""Language Grounding example for Living Memory Dynamics (LMD).
This example demonstrates:
1. Encoding text to embeddings
2. Building a text corpus for retrieval
3. Decoding embeddings back to human-readable text
4. Describing creative leaps in natural language
Requirements:
pip install living-memory-dynamics[language]
"""
import torch
from lmd import (
LanguageGrounding,
GroundingConfig,
EncoderType,
create_grounding,
LivingMemory,
ValenceTrajectory,
NarrativePhase,
CreativeLeapEngine,
CreativeLeapConfig,
SENTENCE_TRANSFORMERS_AVAILABLE,
)
def check_dependencies():
"""Check if required dependencies are installed."""
if not SENTENCE_TRANSFORMERS_AVAILABLE:
print("=" * 60)
print("sentence-transformers not installed!")
print("Install with: pip install sentence-transformers")
print("Or: pip install living-memory-dynamics[language]")
print("=" * 60)
return False
return True
def demo_basic_encoding():
"""Demonstrate basic text encoding."""
print("\n" + "=" * 60)
print("1. Basic Text Encoding")
print("=" * 60)
# Create grounding with MiniLM (fast, 384 dim)
grounding = create_grounding(encoder="minilm")
print(f" Encoder: MiniLM")
print(f" Embedding dimension: {grounding.embedding_dim}")
# Encode single text
text = "a fire-breathing dragon"
embedding = grounding.encode(text)
print(f"\n Text: '{text}'")
print(f" Embedding shape: {embedding.shape}")
print(f" Embedding norm: {embedding.norm().item():.4f}")
# Encode multiple texts
texts = [
"crystalline glass structure",
"prismatic light refraction",
"ancient mythical creature",
]
embeddings = grounding.encode(texts)
print(f"\n Encoded {len(texts)} texts")
print(f" Batch shape: {embeddings.shape}")
return grounding
def demo_corpus_building(grounding: LanguageGrounding):
"""Demonstrate building a text corpus."""
print("\n" + "=" * 60)
print("2. Building Text Corpus")
print("=" * 60)
# Add texts to corpus
corpus_texts = [
"a fire-breathing dragon",
"crystalline glass structure",
"prismatic light refraction",
"ancient mythical creature",
"rainbow spectrum of colors",
"molten lava flow",
"frozen ice sculpture",
"diamond crystal lattice",
"phoenix rising from flames",
"aurora borealis lights",
"volcanic eruption",
"glacier formation",
"sunlight through prism",
"stained glass window",
"dragon scales armor",
]
print(f" Adding {len(corpus_texts)} texts to corpus...")
for text in corpus_texts:
grounding.add_to_corpus(text)
print(f" Corpus size: {grounding.corpus_size}")
return corpus_texts
def demo_retrieval(grounding: LanguageGrounding):
"""Demonstrate retrieval-based decoding."""
print("\n" + "=" * 60)
print("3. Retrieval-Based Decoding")
print("=" * 60)
# Create a query embedding (average of two concepts)
dragon_emb = grounding.encode("a fire-breathing dragon")
glass_emb = grounding.encode("crystalline glass structure")
# Blend them (simulating a creative leap result)
blended = (dragon_emb + glass_emb) / 2
blended = blended / blended.norm()
print(" Query: blend of 'dragon' and 'glass'")
# Decode
result = grounding.decode(blended, top_k=5)
print(f"\n Top matches:")
for i, (text, similarity) in enumerate(result.matches, 1):
print(f" {i}. '{text}' (similarity: {similarity:.3f})")
print(f"\n Interpolated description: {result.interpolated_description}")
print(f" Confidence: {result.confidence:.3f}")
def demo_creative_leap_description(grounding: LanguageGrounding):
"""Demonstrate describing creative leaps."""
print("\n" + "=" * 60)
print("4. Creative Leap Descriptions")
print("=" * 60)
# Get source embeddings
sources = [
grounding.encode("a fire-breathing dragon"),
grounding.encode("crystalline glass structure"),
]
source_texts = ["fire-breathing dragon", "crystalline glass"]
# Create a "leap" result (perpendicular composition)
# This simulates what OrthogonalComposer might produce
result = sources[0] + sources[1]
result = result / result.norm()
# Add some novelty
noise = torch.randn_like(result) * 0.3
result = result + noise
result = result / result.norm()
# Describe the leap
description = grounding.describe_leap(
leap_type="ORTHOGONAL",
sources=sources,
result=result,
source_texts=source_texts
)
print(f" Leap type: {description.leap_type}")
print(f" Sources: {description.source_texts}")
print(f" Novelty score: {description.novelty_score:.3f}")
print(f" Grounding confidence: {description.grounding_confidence:.3f}")
print(f"\n Synthesized description:")
print(f" {description.synthesized_description}")
print(f"\n Nearest concepts in corpus:")
for text, sim in description.nearest_texts[:3]:
print(f" - '{text}' ({sim:.3f})")
def demo_memory_grounding(grounding: LanguageGrounding):
"""Demonstrate grounding LivingMemory objects."""
print("\n" + "=" * 60)
print("5. Grounding Living Memories")
print("=" * 60)
# Create memories with encoded content
memories = []
concepts = [
"ancient dragon guarding treasure",
"crystal cave with glowing gems",
"sunset over mountain peaks",
]
for i, concept in enumerate(concepts):
embedding = grounding.encode(concept)
memory = LivingMemory(
id=f"memory_{i}",
content=embedding,
energy=0.8,
valence=ValenceTrajectory(points=torch.tensor([0.5, 0.7, 0.6])),
phase=NarrativePhase.SETUP,
)
memories.append(memory)
# Also add to corpus
grounding.add_to_corpus(concept)
print(f" Created {len(memories)} memories")
# Ground memories back to text
print("\n Grounded memories:")
grounded = grounding.ground_memories(memories)
for g in grounded:
print(f" ID: {g.metadata['memory_id']}")
print(f" Text: '{g.text}'")
print(f" Confidence: {g.metadata['confidence']:.3f}")
print()
def demo_with_creative_leaps(grounding: LanguageGrounding):
"""Demonstrate integration with CreativeLeapEngine."""
print("\n" + "=" * 60)
print("6. Integration with Creative Leaps")
print("=" * 60)
# Create leap engine matching grounding dimension
config = CreativeLeapConfig(content_dim=grounding.embedding_dim)
leap_engine = CreativeLeapEngine(config)
# Encode source concepts (move to CPU for leap engine compatibility)
sources = [
grounding.encode("volcanic eruption").cpu(),
grounding.encode("frozen ice sculpture").cpu(),
grounding.encode("rainbow spectrum").cpu(),
]
print(" Sources: volcanic eruption, frozen ice, rainbow spectrum")
print(" Generating creative leaps...\n")
# Generate leaps with different operators
for dopamine in [0.3, 0.7, 0.95]:
leap = leap_engine.leap(sources, dopamine=dopamine)
# Describe the leap
description = grounding.describe_leap(
leap_type=leap.leap_type.name,
sources=sources,
result=leap.embedding,
)
print(f" Dopamine: {dopamine}")
print(f" Leap type: {leap.leap_type.name}")
print(f" Description: {description.synthesized_description}")
print(f" Novelty: {description.novelty_score:.2f}")
print()
def main():
print("Living Memory Dynamics - Language Grounding Example")
print("=" * 60)
# Check dependencies
if not check_dependencies():
return
# Run demos
grounding = demo_basic_encoding()
demo_corpus_building(grounding)
demo_retrieval(grounding)
demo_creative_leap_description(grounding)
demo_memory_grounding(grounding)
demo_with_creative_leaps(grounding)
print("\n" + "=" * 60)
print("Language Grounding Demo Complete!")
print("=" * 60)
if __name__ == "__main__":
main()