| """Test recursive link graph: context storage, linking, traversal.""" |
|
|
| import sys |
| import os |
| sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) |
|
|
| from singularity_llm.Singularity.recursive_link import RecursiveLinkGraph |
| from singularity_llm.Singularity.universal_link import UniversalLinkManager |
| from singularity_llm.Singularity.Singularity_tokens import SingularityTokenizer, SingularityTokenConfig |
|
|
|
|
| def test_recursive_link_basic(): |
| """Test basic context storage and retrieval.""" |
| graph = RecursiveLinkGraph() |
| ctx_id = graph.add_context("Hello world", "Hi there!") |
| assert ctx_id in graph._contexts |
| print(f" Context stored: {ctx_id[:8]}") |
|
|
|
|
| def test_recursive_link_related(): |
| """Test finding related contexts.""" |
| graph = RecursiveLinkGraph() |
| graph.add_context("Hello world programming", "Programming is fun!") |
| graph.add_context("Hello world greeting", "Hi there!") |
| graph.add_context("Python code example", "Here is some code.") |
|
|
| related = graph.find_related("Hello world") |
| assert len(related) > 0, "No related contexts found" |
| print(f" Found {len(related)} related contexts") |
|
|
|
|
| def test_recursive_link_injection(): |
| """Test context injection for inference.""" |
| graph = RecursiveLinkGraph() |
| graph.add_context("What is Python?", "Python is a programming language.") |
| graph.add_context("How to code in Python?", "You can write Python code in any editor.") |
|
|
| injection = graph.get_injection_context("Tell me about Python") |
| assert "Python" in injection, f"Injection missing context: {injection}" |
| print(f" Injection: {injection[:80]}") |
|
|
|
|
| def test_universal_link(): |
| """Test universal link manager.""" |
| mgr = UniversalLinkManager(instance_id="test-instance") |
| learning_id = mgr.share_learning("conversation", {"message": "test"}) |
| assert learning_id is not None |
| stats = mgr.get_stats() |
| assert stats["learnings_shared"] == 1 |
| print(f" Shared learning: {learning_id[:8]}") |
|
|
|
|
| def test_Singularity_tokens(): |
| """Test Singularity token encoding/decoding.""" |
| tok = SingularityTokenizer(SingularityTokenConfig(format="q4_k_m")) |
| token_ids = [1, 2, 3, 4, 5, 10, 20, 30, 40, 50] |
| encoded = tok.encode(token_ids) |
| decoded = tok.decode(encoded, len(token_ids)) |
|
|
| assert len(encoded) < len(token_ids) * 4, "No compression achieved" |
| print(f" Encoded {len(token_ids)} tokens to {len(encoded)} bytes") |
| print(f" Compression: {len(token_ids) * 4 / len(encoded):.1f}x") |
|
|
|
|
| if __name__ == "__main__": |
| print("Running Singularity tests...") |
| test_recursive_link_basic() |
| print(" β test_recursive_link_basic") |
| test_recursive_link_related() |
| print(" β test_recursive_link_related") |
| test_recursive_link_injection() |
| print(" β test_recursive_link_injection") |
| test_universal_link() |
| print(" β test_universal_link") |
| test_Singularity_tokens() |
| print(" β test_Singularity_tokens") |
| print("\nAll Singularity tests passed!") |
|
|