File size: 14,105 Bytes
3636a69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
test_aether_v3.py — Test suite for AETHER v3 (multi-modal GPT killer).

Covers all 6 new v3 capabilities:
  1. Semantic HD embeddings (pretrained.py) — categories, synonyms, antonyms
  2. HD n-gram LM with backoff + temperature sampling + beam search
  3. N-hop arbitrary depth with sub-goaling + path finding + reachability
  4. Tool composer (hybrid HD + pattern matching)
  5. HD RAG (web fetch + offline wiki fallback)
  6. Multi-modal HD encoding (images + audio + cross-modal)
"""

from __future__ import annotations
import sys
import os
import time
import numpy as np

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from aether import (
    AETHER,
    SemanticKB,
    HDLanguageModel,
    ToolComposer, ComposedExecutor,
    HDRAGStore, WebFetcher, get_global_rag, reset_global_rag,
    ImageHDEncoder, AudioHDEncoder, CrossModalSpace,
    integrate_into_agent,
)


def banner(title: str) -> None:
    print()
    print("=" * 76)
    print(f"  {title}")
    print("=" * 76)


# --------------------------------------------------------------------------- #
# 1. Semantic embeddings
# --------------------------------------------------------------------------- #
def test_semantic_embeddings():
    banner("1. Semantic HD Embeddings (categories + synonyms + antonyms)")

    skb = SemanticKB(dim=4096)
    skb.build()
    print(f"  Built semantic KB: {skb.stats()}")

    print("\n  Within-category similarity (HIGH):")
    pairs = [("dog", "cat"), ("paris", "london"), ("red", "blue"), ("three", "seven")]
    for w1, w2 in pairs:
        print(f"    sim({w1!r:10s}, {w2!r:10s}) = {skb.similarity(w1, w2):+.4f}")

    print("\n  Cross-category similarity (LOW):")
    pairs = [("dog", "paris"), ("red", "seven"), ("bread", "happy")]
    for w1, w2 in pairs:
        print(f"    sim({w1!r:10s}, {w2!r:10s}) = {skb.similarity(w1, w2):+.4f}")

    print("\n  Synonym similarity (~1.0):")
    for w1, w2 in [("happy", "joyful"), ("big", "large"), ("smart", "intelligent")]:
        print(f"    sim({w1!r:12s}, {w2!r:12s}) = {skb.similarity(w1, w2):+.4f}")

    print("\n  Antonym similarity (LOW):")
    for w1, w2 in [("hot", "cold"), ("big", "small"), ("day", "night")]:
        print(f"    sim({w1!r:12s}, {w2!r:12s}) = {skb.similarity(w1, w2):+.4f}")

    print(f"\n  Nearest neighbors of 'dog':")
    for w, s in skb.nearest_neighbors("dog", top_k=3):
        print(f"    {w!r:15s} : {s:+.4f}")

    # Test integration into agent
    agent = AETHER()
    before = len(agent.assoc.vocab)
    added = integrate_into_agent(agent)
    after = len(agent.assoc.vocab)
    print(f"\n  Integration into agent: {added} vectors added ({before} -> {after} vocab)")
    print("  -> Semantic embeddings OK")


# --------------------------------------------------------------------------- #
# 2. HD n-gram LM
# --------------------------------------------------------------------------- #
def test_hd_lm():
    banner("2. HD n-gram LM with backoff + sampling + beam search")

    agent = AETHER()
    lm = HDLanguageModel(agent.assoc, dim=agent.dim)

    sentences = [
        "the sun is a star",
        "the moon is a satellite",
        "water is a liquid",
        "gold is a metal",
        "python is a programming language",
        "the cat is an animal",
        "the dog is an animal",
        "tokyo is the capital of japan",
        "paris is the capital of france",
        "ottawa is the capital of canada",
    ]
    print(f"  Training LM on {len(sentences)} sentences...")
    for s in sentences:
        lm.learn_sequence(s.lower().split())
    print(f"  LM stats: {lm.stats()}")

    print("\n  Prediction with backoff:")
    contexts = [
        ["the", "sun"],
        ["the", "moon"],
        ["python", "is"],
        ["tokyo", "is", "the", "capital"],
    ]
    for ctx in contexts:
        pred = lm.predict_next(ctx)
        if pred:
            tok, sim, order = pred
            print(f"    ctx={ctx!r:45s} -> {tok!r:12s} (sim={sim:.3f}, order={order})")

    print("\n  Greedy generation (T=0):")
    rng = np.random.default_rng(42)
    for prompt in [["the", "sun"], ["python", "is"], ["paris", "is", "the"]]:
        gen = lm.generate(prompt, max_tokens=8, temperature=0.0, top_k=3, rng=rng)
        print(f"    {' '.join(prompt):20s} ... -> {' '.join(gen)}")

    print("\n  Sampled generation (T=1.0, 3 samples):")
    for prompt in [["the", "sun"]]:
        for i in range(3):
            rng_i = np.random.default_rng(i)
            gen = lm.generate(prompt, max_tokens=8, temperature=1.0, top_k=5, rng=rng_i)
            print(f"    sample {i+1}: {' '.join(prompt)} ... -> {' '.join(gen)}")

    print("\n  Beam search (beam_width=3):")
    for prompt in [["the", "sun"], ["python", "is"]]:
        beams = lm.beam_search(prompt, max_tokens=6, beam_width=3)
        print(f"    prompt: {' '.join(prompt)}")
        for seq, score in beams:
            print(f"      [{score:.3f}] {' '.join(seq)}")

    print("  -> HD LM OK")


# --------------------------------------------------------------------------- #
# 3. N-hop with sub-goaling
# --------------------------------------------------------------------------- #
def test_n_hop_subgoaling():
    banner("3. N-hop with sub-goaling + path finding + reachability")

    agent = AETHER()
    extra = [
        "Montreal is located in Canada",
        "Toronto is located in Canada",
        "Canada is located in America",
        "America is located in Earth",
        "Lyon is located in France",
        "France is located in Europe",
        "Europe is located in Earth",
        "Ottawa is the capital of Canada",
        "Paris is the capital of France",
        "Tokyo is the capital of Japan",
    ]
    for f in extra:
        agent.teach(f, silent=True)
    print(f"  KB: {len(agent.assoc.triples)} triples")

    print("\n  Direct lookup with explicit verification:")
    proof = agent.inference.n_hop_with_subgoaling("Montreal", "located_in", max_depth=4)
    print(f"    {agent.inference.explain(proof)}")

    print("\n  Path finding (Montreal -> Earth):")
    paths = agent.inference.find_paths("Montreal", "Earth", max_depth=5, max_paths=3)
    for i, p in enumerate(paths):
        print(f"    Path {i+1}:")
        for step in p.steps:
            s, pr, o = step.conclusion
            print(f"      ({s}, {pr}, {o}) [conf={step.confidence:.3f}]")

    print("\n  Reachability from Montreal (3 hops):")
    reachable = agent.inference.reachable("Montreal", max_hops=3)
    for entity, (path, conf) in sorted(reachable.items(), key=lambda x: -x[1][1])[:6]:
        print(f"    {entity:15s} via {' -> '.join(path):40s} conf={conf:.3f}")

    print("\n  Multi-hop query (Montreal -> located_in -> capital_of):")
    proof = agent.inference.multi_hop_query("Montreal", ["located_in", "capital_of"])
    print(f"    {agent.inference.explain(proof)}")

    print("  -> N-hop sub-goaling OK")


# --------------------------------------------------------------------------- #
# 4. Tool composer
# --------------------------------------------------------------------------- #
def test_tool_composer():
    banner("4. Tool Composer (hybrid HD + pattern matching)")

    agent = AETHER()
    composer = ToolComposer(agent.encoder, agent.tools, agent.inference)
    executor = ComposedExecutor(agent)

    for f in ["Tokyo is the capital of Japan", "Paris is the capital of France"]:
        agent.teach(f, silent=True)

    questions = [
        "compare Paris and Tokyo",
        "how many triples are in the KB",
        "calculate 5 * 8 + 3",
        "explain Python",
        "what is the capital of France",
        "where is Montreal located",
        "list all facts",
        "recall Paris",
        "time",
        "summarize 3",
    ]
    print(f"  Testing {len(questions)} questions:\n")
    for q in questions:
        nodes, rationale = composer.compose(q)
        print(f"  Q: {q}")
        print(f"    rationale: {rationale}")
        for i, n in enumerate(nodes):
            deps = f" depends_on={n.depends_on}" if n.depends_on else ""
            print(f"      node {i}: {n.tool_name}({n.args!r}){deps}")
        final, _ = executor.execute(nodes)
        print(f"    => {str(final)[:80]}")
        print()

    print("  -> Tool composer OK")


# --------------------------------------------------------------------------- #
# 5. HD RAG
# --------------------------------------------------------------------------- #
def test_hd_rag():
    banner("5. HD RAG (web fetch + offline wiki fallback)")

    reset_global_rag()
    agent = AETHER()
    rag = get_global_rag(agent.encoder)

    print(f"  RAG store stats: {rag.stats()}")
    print(f"  Has internet: {WebFetcher(rag).has_internet()}")

    print("\n  Retrieval tests (should find the right topic):")
    queries = ["tokyo", "paris", "python", "aether", "transformer",
               "kanerva", "water", "sun", "japan", "france"]
    correct = 0
    for q in queries:
        results = rag.retrieve(q, top_k=1)
        if results:
            doc, sim = results[0]
            ok = q in doc.topic.lower() or doc.topic.lower() in q
            if ok:
                correct += 1
            marker = "OK" if ok else "FAIL"
            print(f"    [{marker}] {q!r:15s} -> [{doc.topic}] (sim={sim:.3f})")
    print(f"\n  Retrieval accuracy: {correct}/{len(queries)}")

    print("\n  Tool-style fetch:")
    from aether.tools import ToolContext
    ctx = ToolContext(agent.encoder, agent.assoc)
    from aether.web import tool_web_search, tool_rag_query, tool_rag_stats
    print(f"  web_search Tokyo: {tool_web_search('Tokyo', ctx)[:80]}...")
    print(f"  rag_query paris:")
    for line in tool_rag_query('paris', ctx).split('\n'):
        print(f"    {line}")
    print(f"  {tool_rag_stats('', ctx)}")

    print("  -> HD RAG OK")


# --------------------------------------------------------------------------- #
# 6. Multi-modal
# --------------------------------------------------------------------------- #
def test_multimodal():
    banner("6. Multi-modal HD Encoding (images + audio + cross-modal)")

    print("  === Image HD Encoder ===")
    img_enc = ImageHDEncoder(dim=4096, grid_size=16, intensity_bins=8)

    # Create test images
    square = np.zeros((32, 32))
    square[8:24, 8:24] = 1.0
    circle = np.zeros((32, 32))
    for i in range(32):
        for j in range(32):
            if (i - 16) ** 2 + (j - 16) ** 2 < 100:
                circle[i, j] = 1.0
    noise = np.random.rand(32, 32)

    v_sq = img_enc.encode(square)
    v_ci = img_enc.encode(circle)
    v_no = img_enc.encode(noise)
    v_sq2 = img_enc.encode(square.copy())

    print(f"    sim(square, square_copy) = {v_sq.similarity(v_sq2):.3f}  (~1.0)")
    print(f"    sim(square, circle)      = {v_sq.similarity(v_ci):.3f}  (moderate)")
    print(f"    sim(square, noise)       = {v_sq.similarity(v_no):.3f}  (~0)")

    print("\n  === Audio HD Encoder ===")
    aud_enc = AudioHDEncoder(dim=4096, n_frames=16, n_bands=16, energy_bins=8)

    v_440 = aud_enc.encode_sine(440)
    v_440b = aud_enc.encode_sine(440)
    v_880 = aud_enc.encode_sine(880)
    v_220 = aud_enc.encode_sine(220)
    v_chord = aud_enc.encode_chord([440, 554, 659])

    print(f"    sim(440, 440_copy) = {v_440.similarity(v_440b):.3f}  (~1.0)")
    print(f"    sim(440, 880)      = {v_440.similarity(v_880):.3f}  (moderate)")
    print(f"    sim(440, 220)      = {v_440.similarity(v_220):.3f}  (moderate)")
    print(f"    sim(440, chord)    = {v_440.similarity(v_chord):.3f}  (lower)")

    print("\n  === Cross-modal Space ===")
    cms = CrossModalSpace(dim=4096)
    cms.add_image_pattern([
        "                                ",
        "         ###########            ",
        "       ###############          ",
        "      #################         ",
        "     ###################        ",
        "     ###################        ",
        "     ###################        ",
        "      #################         ",
        "       ###############          ",
        "         ###########            ",
    ], label="circle_pattern")
    cms.add_image_pattern([
        "                                ",
        "       ################         ",
        "       ################         ",
        "       ################         ",
        "                                ",
    ], label="rectangle_pattern")
    cms.add_sine(440, label="A4_note")
    cms.add_sine(880, label="A5_note")

    print(f"    CrossModal stats: {cms.stats()}")

    results = cms.find_similar_image_pattern([
        "                                ",
        "         ###########            ",
        "       ###############          ",
        "      #################         ",
        "     ###################        ",
        "     ###################        ",
        "     ###################        ",
        "      #################         ",
        "       ###############          ",
        "         ###########            ",
    ], top_k=3)
    print(f"\n    Find similar to circle_pattern:")
    for mod, label, sim in results:
        print(f"      [{mod}] {label!r:25s} sim={sim:.3f}")

    results = cms.find_similar_sine(440, top_k=3)
    print(f"\n    Find similar to A4 (440 Hz):")
    for mod, label, sim in results:
        print(f"      [{mod}] {label!r:25s} sim={sim:.3f}")

    print("  -> Multi-modal OK")


# --------------------------------------------------------------------------- #
# main
# --------------------------------------------------------------------------- #
def main():
    tests = [
        test_semantic_embeddings,
        test_hd_lm,
        test_n_hop_subgoaling,
        test_tool_composer,
        test_hd_rag,
        test_multimodal,
    ]
    for t in tests:
        try:
            t()
        except Exception as e:
            import traceback
            print(f"\n  [FAILED: {t.__name__}: {e}]")
            traceback.print_exc()
    print("\n" + "=" * 76)
    print("  All v3 tests complete.")
    print("=" * 76)


if __name__ == "__main__":
    main()