-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05_knowledge_graphs.py
More file actions
431 lines (357 loc) · 13.4 KB
/
05_knowledge_graphs.py
File metadata and controls
431 lines (357 loc) · 13.4 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
"""
Example 5: Knowledge Graphs and Ologs
Demonstrates:
- Extracting entity-relationship graphs (ologs) from text/images
- Generating structured language from graphs
- Tracking information loss through the pipeline
- Round-trip fidelity analysis
- LLM integration with loss warnings
An olog (ontology log) is a categorical representation of knowledge:
- Objects are types ("a person", "a city")
- Morphisms are aspects ("lives in", "was born in")
- Composition gives transitive relationships
"""
from modalsheaf import (
# Core
ModalityGraph,
Transformation,
TransformationType,
# Knowledge graph
Entity,
Relationship,
Olog,
KnowledgeExtractor,
StructuredGenerator,
LLMPipelineConfig,
# Analysis
estimate_info_loss,
analyze_round_trip,
InfoLossReport,
)
# ==================== Basic Olog Construction ====================
def example_manual_olog():
"""Manually construct an olog from known facts."""
print("=" * 60)
print("Example 1: Manual Olog Construction")
print("=" * 60)
# Create an olog about Einstein
olog = Olog()
# Add entities (types in categorical terms)
olog.add_entity(Entity(
id="einstein",
label="a physicist named Albert Einstein",
entity_type="person",
attributes={"birth_year": 1879, "death_year": 1955}
))
olog.add_entity(Entity(
id="ulm",
label="a city named Ulm",
entity_type="location",
attributes={"country": "Germany"}
))
olog.add_entity(Entity(
id="relativity",
label="the theory of relativity",
entity_type="theory",
attributes={"year": 1905}
))
olog.add_entity(Entity(
id="nobel",
label="the Nobel Prize in Physics",
entity_type="award",
attributes={"year": 1921}
))
# Add relationships (aspects/morphisms)
olog.add_relationship(Relationship(
id="r1",
source="einstein",
target="ulm",
label="was born in",
relationship_type="birthplace"
))
olog.add_relationship(Relationship(
id="r2",
source="einstein",
target="relativity",
label="developed",
relationship_type="created"
))
olog.add_relationship(Relationship(
id="r3",
source="einstein",
target="nobel",
label="received",
relationship_type="awarded"
))
print(f"Created: {olog}")
print(f"\nTriples:")
for subj, pred, obj in olog.to_triples():
print(f" ({subj}, {pred}, {obj})")
return olog
# ==================== Information Loss Tracking ====================
def example_info_loss_tracking():
"""Track information loss through a transformation pipeline."""
print("\n" + "=" * 60)
print("Example 2: Information Loss Tracking")
print("=" * 60)
# Define a typical text -> olog -> text pipeline
transformations = [
Transformation(
source="text",
target="embedding",
forward=lambda x: x, # Placeholder
info_loss_estimate=0.7, # High loss - semantic compression
transform_type=TransformationType.LOSSY,
name="text_to_embedding"
),
Transformation(
source="embedding",
target="entities",
forward=lambda x: x,
info_loss_estimate=0.4, # Medium loss - structure extraction
transform_type=TransformationType.LOSSY,
name="embedding_to_entities"
),
Transformation(
source="entities",
target="olog",
forward=lambda x: x,
info_loss_estimate=0.1, # Low loss - just organizing
transform_type=TransformationType.LOSSY,
name="entities_to_olog"
),
]
# Estimate loss (will emit warning if significant)
report = estimate_info_loss(transformations, warn=True)
print(f"\nPipeline Analysis:")
print(f" Total information loss: {report.total_loss:.1%}")
print(f" Preservation rate: {report.preservation_rate:.1%}")
print(f" Warning level: {report.warning_level.name}")
print(f" Round-trip success probability: {report.round_trip_success_probability:.1%}")
print(f"\nPer-step breakdown:")
for step in report.steps:
print(f" {step['name']}: {step['loss']:.0%} loss "
f"(cumulative preservation: {step['cumulative_preservation']:.1%})")
if report.recommendations:
print(f"\nRecommendations:")
for rec in report.recommendations:
print(f" - {rec}")
return report
# ==================== Round-Trip Analysis ====================
def example_round_trip():
"""Analyze round-trip fidelity: text -> olog -> text."""
print("\n" + "=" * 60)
print("Example 3: Round-Trip Analysis")
print("=" * 60)
# Forward: text -> olog
forward_transforms = [
Transformation(
source="text",
target="embedding",
forward=lambda x: x,
info_loss_estimate=0.7,
name="encode_text"
),
Transformation(
source="embedding",
target="olog",
forward=lambda x: x,
info_loss_estimate=0.4,
name="extract_olog"
),
]
# Backward: olog -> text
backward_transforms = [
Transformation(
source="olog",
target="text",
forward=lambda x: x,
info_loss_estimate=0.3, # Generation is creative
name="generate_text"
),
]
analysis = analyze_round_trip(
source_modality="text",
target_modality="olog",
transformations=forward_transforms,
inverse_transformations=backward_transforms
)
print(f"\nRound-trip: text -> olog -> text")
print(f" Forward loss: {analysis['forward_loss']:.1%}")
print(f" Backward loss: {analysis['backward_loss']:.1%}")
print(f" Round-trip preservation: {analysis['round_trip_preservation']:.1%}")
print(f" Success probability: {analysis['success_probability']:.1%}")
# Compare different modalities
print("\n--- Comparison of different source modalities ---")
modality_losses = {
"text": 0.7,
"image": 0.85,
"audio": 0.75,
"structured_data": 0.2,
}
for modality, embed_loss in modality_losses.items():
forward = [
Transformation(
source=modality, target="embedding",
forward=lambda x: x, info_loss_estimate=embed_loss,
name=f"{modality}_to_embedding"
),
Transformation(
source="embedding", target="olog",
forward=lambda x: x, info_loss_estimate=0.4,
name="embedding_to_olog"
),
]
backward = [
Transformation(
source="olog", target=modality,
forward=lambda x: x, info_loss_estimate=0.3,
name=f"olog_to_{modality}"
),
]
result = analyze_round_trip(modality, "olog", forward, backward)
print(f" {modality:20s} -> olog -> {modality:20s}: "
f"{result['success_probability']:.1%} success")
# ==================== LLM Pipeline with Warnings ====================
def example_llm_pipeline():
"""Demonstrate LLM pipeline with information loss warnings."""
print("\n" + "=" * 60)
print("Example 4: LLM Pipeline with Loss Warnings")
print("=" * 60)
# Configure the pipeline
config = LLMPipelineConfig(
extraction_model="gpt-4",
generation_model="gpt-4",
text_to_embedding_loss=0.7,
image_to_embedding_loss=0.85,
embedding_to_entities_loss=0.4,
entities_to_text_loss=0.3,
warn_on_loss=True,
loss_threshold_warn=0.3,
)
print(f"Pipeline configuration:")
print(f" Text -> Embedding loss: {config.text_to_embedding_loss:.0%}")
print(f" Image -> Embedding loss: {config.image_to_embedding_loss:.0%}")
print(f" Embedding -> Entities loss: {config.embedding_to_entities_loss:.0%}")
print(f" Entities -> Text loss: {config.entities_to_text_loss:.0%}")
# Create extractor and generator
extractor = KnowledgeExtractor(config)
generator = StructuredGenerator(config)
# Simulate extraction from text
print("\n--- Extracting from text ---")
sample_text = "Albert Einstein was born in Ulm, Germany in 1879."
# This will trigger a warning due to high loss
olog, extract_report = extractor.extract(text=sample_text)
print(f"Extraction report:")
print(f" Total loss: {extract_report.total_loss:.1%}")
print(f" Warning level: {extract_report.warning_level.name}")
# Simulate extraction from image (even higher loss)
print("\n--- Extracting from image ---")
import numpy as np
sample_image = np.zeros((224, 224, 3)) # Placeholder
olog_img, img_report = extractor.extract(image=sample_image)
print(f"Image extraction report:")
print(f" Total loss: {img_report.total_loss:.1%}")
print(f" Warning level: {img_report.warning_level.name}")
# Generate text from olog
print("\n--- Generating from olog ---")
manual_olog = example_manual_olog()
generated_text, gen_report = generator.generate(
manual_olog,
style="factual",
include_confidence=True
)
print(f"\nGenerated text: {generated_text}")
print(f"Generation loss: {gen_report.total_loss:.1%}")
# ==================== Integration with ModalityGraph ====================
def example_modality_graph_integration():
"""Show how ologs integrate with the ModalityGraph system."""
print("\n" + "=" * 60)
print("Example 5: ModalityGraph Integration")
print("=" * 60)
# Create a graph with olog support
graph = ModalityGraph("knowledge_extraction")
# Add modalities
graph.add_modality("text", dtype="str", description="Raw text")
graph.add_modality("image", shape=(224, 224, 3), description="Image array")
graph.add_modality("embedding", shape=(768,), description="Dense vector")
graph.add_modality("olog", dtype="object", description="Knowledge graph")
graph.add_modality("triples", dtype="object", description="RDF-style triples")
# Add transformations with loss estimates
graph.add_transformation(
"text", "embedding",
forward=lambda x: x, # Placeholder for real encoder
info_loss="high",
name="text_encoder"
)
graph.add_transformation(
"image", "embedding",
forward=lambda x: x,
info_loss=0.85, # Even higher for images
name="image_encoder"
)
graph.add_transformation(
"embedding", "olog",
forward=lambda x: Olog(), # Placeholder
info_loss="medium",
name="entity_extractor"
)
graph.add_transformation(
"olog", "triples",
forward=lambda o: o.to_triples() if isinstance(o, Olog) else [],
inverse=lambda t: Olog(), # Simplified inverse
info_loss="low",
name="olog_to_triples"
)
graph.add_transformation(
"olog", "text",
forward=lambda o: " ".join(f"{s} {p} {obj}." for s, p, obj in o.to_triples()) if isinstance(o, Olog) else "",
info_loss="medium",
name="text_generator"
)
print(f"Graph: {graph}")
print(f"Modalities: {graph.modalities}")
# Find paths
print(f"\nPath from text to triples: {graph.find_path('text', 'triples')}")
print(f"Path from image to text: {graph.find_path('image', 'text')}")
# Estimate path loss
text_to_triples_loss = graph.estimate_path_info_loss("text", "triples")
image_to_text_loss = graph.estimate_path_info_loss("image", "text")
print(f"\nEstimated losses:")
print(f" text -> triples: {text_to_triples_loss:.1%}")
print(f" image -> text: {image_to_text_loss:.1%}")
# ==================== Main ====================
if __name__ == "__main__":
# Run all examples
example_manual_olog()
example_info_loss_tracking()
example_round_trip()
example_llm_pipeline()
example_modality_graph_integration()
print("\n" + "=" * 60)
print("Summary")
print("=" * 60)
print("""
Key takeaways:
1. OLOGS provide categorical structure for knowledge representation
- Entities are types (objects in a category)
- Relationships are aspects (morphisms)
- Composition gives transitive relationships
2. INFORMATION LOSS is tracked through the pipeline
- Text -> Embedding: ~70% loss (semantic compression)
- Image -> Embedding: ~85% loss (visual detail lost)
- Embedding -> Olog: ~40% loss (structure extraction)
- Olog -> Text: ~30% loss (generation is creative)
3. ROUND-TRIP SUCCESS depends on cumulative preservation
- text -> olog -> text: ~13% success probability
- image -> olog -> text: ~6% success probability
- structured_data -> olog -> text: ~34% success probability
4. WARNINGS alert users when feeding lossy data to LLMs
- Helps users understand what detail is lost
- Suggests preserving original alongside embeddings
- Recommends including structured summaries
5. INTEGRATION with ModalityGraph enables:
- Automatic path finding through modality space
- Cumulative loss estimation along paths
- Consistency checking across modalities
""")