Squashed 'vendor/ruvector/' content from commit b64c2172
git-subtree-dir: vendor/ruvector git-subtree-split: b64c21726f2bb37286d9ee36a7869fef60cc6900
This commit is contained in:
@@ -0,0 +1,448 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* AgentDB Self-Discovery System
|
||||
*
|
||||
* A cognitive system that:
|
||||
* - Explores its own capabilities
|
||||
* - Learns from its discoveries
|
||||
* - Stores patterns in memory
|
||||
* - Reflects on its performance
|
||||
* - Builds a knowledge graph of its abilities
|
||||
*
|
||||
* Demonstrates AgentDB's cognitive memory patterns:
|
||||
* - Vector search for semantic similarity
|
||||
* - Attention mechanisms for focus
|
||||
* - Memory storage and retrieval
|
||||
* - Self-reflection and learning
|
||||
*/
|
||||
|
||||
const { VectorDB } = require('ruvector');
|
||||
const {
|
||||
MultiHeadAttention,
|
||||
HyperbolicAttention,
|
||||
FlashAttention
|
||||
} = require('@ruvector/attention');
|
||||
|
||||
console.log('🧠 AgentDB Self-Discovery System\n');
|
||||
console.log('=' .repeat(70));
|
||||
console.log('\nInitializing Cognitive Explorer...\n');
|
||||
|
||||
class CognitiveExplorer {
|
||||
constructor() {
|
||||
this.discoveries = [];
|
||||
this.memoryDB = null;
|
||||
this.knowledgeGraph = new Map();
|
||||
this.reflections = [];
|
||||
this.capabilities = [];
|
||||
this.performanceMetrics = new Map();
|
||||
}
|
||||
|
||||
async initialize() {
|
||||
console.log('🔧 Initializing cognitive systems...\n');
|
||||
|
||||
// Initialize vector memory
|
||||
const path = require('path');
|
||||
const dbPath = path.join(process.cwd(), 'demos', 'self-discovery', 'memory.bin');
|
||||
|
||||
this.memoryDB = new VectorDB({
|
||||
dimensions: 128,
|
||||
maxElements: 1000,
|
||||
storagePath: dbPath
|
||||
});
|
||||
|
||||
console.log('✅ Vector memory initialized (128 dimensions)');
|
||||
|
||||
// Initialize attention mechanisms for cognitive focus
|
||||
this.multiHeadAttention = new MultiHeadAttention(64, 4);
|
||||
this.hyperbolicAttention = new HyperbolicAttention(64, -1.0);
|
||||
this.flashAttention = new FlashAttention(64, 32);
|
||||
|
||||
console.log('✅ Attention mechanisms initialized');
|
||||
console.log(' - Multi-Head (4 heads)');
|
||||
console.log(' - Hyperbolic (curvature -1.0)');
|
||||
console.log(' - Flash (block size 32)');
|
||||
|
||||
console.log('\n✅ Cognitive systems ready!\n');
|
||||
}
|
||||
|
||||
// Convert text to vector representation
|
||||
textToVector(text, dimensions = 128) {
|
||||
const vector = new Float32Array(dimensions);
|
||||
const normalized = text.toLowerCase();
|
||||
|
||||
for (let i = 0; i < dimensions; i++) {
|
||||
if (i < 26) {
|
||||
const char = String.fromCharCode(97 + i);
|
||||
vector[i] = (normalized.split(char).length - 1) / normalized.length;
|
||||
} else {
|
||||
vector[i] = Math.sin(i * normalized.length * 0.1) *
|
||||
Math.cos(normalized.charCodeAt(i % normalized.length));
|
||||
}
|
||||
}
|
||||
|
||||
const magnitude = Math.sqrt(vector.reduce((sum, val) => sum + val * val, 0));
|
||||
if (magnitude > 0) {
|
||||
for (let i = 0; i < dimensions; i++) {
|
||||
vector[i] /= magnitude;
|
||||
}
|
||||
}
|
||||
|
||||
return vector;
|
||||
}
|
||||
|
||||
async exploreCapability(capability) {
|
||||
console.log(`\n🔍 Exploring: ${capability.name}\n`);
|
||||
|
||||
const startTime = performance.now();
|
||||
|
||||
try {
|
||||
// Execute the capability
|
||||
const result = await capability.execute();
|
||||
|
||||
const endTime = performance.now();
|
||||
const duration = endTime - startTime;
|
||||
|
||||
// Record the discovery
|
||||
const discovery = {
|
||||
id: `discovery-${this.discoveries.length + 1}`,
|
||||
timestamp: new Date().toISOString(),
|
||||
capability: capability.name,
|
||||
description: capability.description,
|
||||
result: result,
|
||||
duration: duration,
|
||||
success: true,
|
||||
category: capability.category
|
||||
};
|
||||
|
||||
this.discoveries.push(discovery);
|
||||
|
||||
// Store in vector memory
|
||||
const memoryText = `${capability.name} ${capability.description} ${capability.category}`;
|
||||
const memoryVector = this.textToVector(memoryText);
|
||||
|
||||
await this.memoryDB.insert({
|
||||
id: discovery.id,
|
||||
vector: memoryVector,
|
||||
metadata: {
|
||||
capability: capability.name,
|
||||
description: capability.description,
|
||||
category: capability.category,
|
||||
duration: duration,
|
||||
timestamp: discovery.timestamp
|
||||
}
|
||||
});
|
||||
|
||||
// Update knowledge graph
|
||||
if (!this.knowledgeGraph.has(capability.category)) {
|
||||
this.knowledgeGraph.set(capability.category, []);
|
||||
}
|
||||
this.knowledgeGraph.get(capability.category).push(discovery);
|
||||
|
||||
// Record performance
|
||||
this.performanceMetrics.set(capability.name, duration);
|
||||
|
||||
console.log(`✅ Discovery recorded: ${capability.name}`);
|
||||
console.log(` Duration: ${duration.toFixed(3)}ms`);
|
||||
console.log(` Category: ${capability.category}`);
|
||||
|
||||
if (result.details) {
|
||||
console.log(` Details: ${result.details}`);
|
||||
}
|
||||
|
||||
return discovery;
|
||||
} catch (error) {
|
||||
console.log(`⚠️ Failed: ${error.message}`);
|
||||
return {
|
||||
id: `failed-${this.discoveries.length + 1}`,
|
||||
capability: capability.name,
|
||||
success: false,
|
||||
error: error.message
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async reflect() {
|
||||
console.log('\n\n' + '=' .repeat(70));
|
||||
console.log('\n🤔 SELF-REFLECTION: Analyzing Discoveries\n');
|
||||
console.log('=' .repeat(70));
|
||||
|
||||
const successfulDiscoveries = this.discoveries.filter(d => d.success);
|
||||
|
||||
console.log(`\n📊 Total Discoveries: ${this.discoveries.length}`);
|
||||
console.log(`✅ Successful: ${successfulDiscoveries.length}`);
|
||||
console.log(`❌ Failed: ${this.discoveries.length - successfulDiscoveries.length}\n`);
|
||||
|
||||
// Analyze by category
|
||||
console.log('📁 Discoveries by Category:\n');
|
||||
for (const [category, discoveries] of this.knowledgeGraph.entries()) {
|
||||
console.log(` ${category}: ${discoveries.length} discoveries`);
|
||||
}
|
||||
|
||||
// Performance analysis
|
||||
console.log('\n⚡ Performance Analysis:\n');
|
||||
const performances = Array.from(this.performanceMetrics.entries())
|
||||
.sort((a, b) => a[1] - b[1]);
|
||||
|
||||
console.log(' Fastest Capabilities:');
|
||||
performances.slice(0, 3).forEach(([name, time], index) => {
|
||||
console.log(` ${index + 1}. ${name}: ${time.toFixed(3)}ms`);
|
||||
});
|
||||
|
||||
if (performances.length > 3) {
|
||||
console.log('\n Slowest Capabilities:');
|
||||
performances.slice(-3).reverse().forEach(([name, time], index) => {
|
||||
console.log(` ${index + 1}. ${name}: ${time.toFixed(3)}ms`);
|
||||
});
|
||||
}
|
||||
|
||||
// Semantic search for patterns
|
||||
console.log('\n\n🔎 Searching Memory for Pattern Clusters...\n');
|
||||
|
||||
const searchQueries = [
|
||||
'fast performance optimization',
|
||||
'attention mechanism processing',
|
||||
'vector similarity search'
|
||||
];
|
||||
|
||||
for (const query of searchQueries) {
|
||||
const queryVector = this.textToVector(query);
|
||||
const results = await this.memoryDB.search({
|
||||
vector: queryVector,
|
||||
k: 2
|
||||
});
|
||||
|
||||
console.log(` Query: "${query}"`);
|
||||
results.forEach(r => {
|
||||
console.log(` → ${r.metadata.capability} (score: ${r.score.toFixed(3)})`);
|
||||
});
|
||||
}
|
||||
|
||||
// Generate insights
|
||||
console.log('\n\n💡 Generated Insights:\n');
|
||||
|
||||
const avgDuration = performances.reduce((sum, [, time]) => sum + time, 0) / performances.length;
|
||||
console.log(` 1. Average capability execution: ${avgDuration.toFixed(3)}ms`);
|
||||
|
||||
const fastestCategory = this.findFastestCategory();
|
||||
console.log(` 2. Fastest category: ${fastestCategory.category} (${fastestCategory.avgTime.toFixed(3)}ms avg)`);
|
||||
|
||||
console.log(` 3. Total capabilities explored: ${this.discoveries.length}`);
|
||||
console.log(` 4. Knowledge graph has ${this.knowledgeGraph.size} categories`);
|
||||
console.log(` 5. Memory database contains ${this.discoveries.length} indexed discoveries`);
|
||||
|
||||
const reflection = {
|
||||
timestamp: new Date().toISOString(),
|
||||
totalDiscoveries: this.discoveries.length,
|
||||
successful: successfulDiscoveries.length,
|
||||
categories: this.knowledgeGraph.size,
|
||||
avgPerformance: avgDuration,
|
||||
insights: [
|
||||
`Explored ${this.discoveries.length} capabilities`,
|
||||
`${successfulDiscoveries.length} successful discoveries`,
|
||||
`Average execution time: ${avgDuration.toFixed(3)}ms`,
|
||||
`Fastest category: ${fastestCategory.category}`
|
||||
]
|
||||
};
|
||||
|
||||
this.reflections.push(reflection);
|
||||
return reflection;
|
||||
}
|
||||
|
||||
findFastestCategory() {
|
||||
const categoryTimes = new Map();
|
||||
|
||||
for (const [category, discoveries] of this.knowledgeGraph.entries()) {
|
||||
const times = discoveries.map(d => d.duration).filter(d => d !== undefined);
|
||||
if (times.length > 0) {
|
||||
const avg = times.reduce((sum, t) => sum + t, 0) / times.length;
|
||||
categoryTimes.set(category, avg);
|
||||
}
|
||||
}
|
||||
|
||||
let fastest = { category: 'None', avgTime: Infinity };
|
||||
for (const [category, avgTime] of categoryTimes.entries()) {
|
||||
if (avgTime < fastest.avgTime) {
|
||||
fastest = { category, avgTime };
|
||||
}
|
||||
}
|
||||
|
||||
return fastest;
|
||||
}
|
||||
|
||||
async generateKnowledgeMap() {
|
||||
console.log('\n\n' + '=' .repeat(70));
|
||||
console.log('\n🗺️ KNOWLEDGE MAP\n');
|
||||
console.log('=' .repeat(70));
|
||||
|
||||
console.log('\nCapability Hierarchy:\n');
|
||||
|
||||
for (const [category, discoveries] of this.knowledgeGraph.entries()) {
|
||||
console.log(`\n📦 ${category}`);
|
||||
console.log(' ' + '─'.repeat(60));
|
||||
|
||||
discoveries.forEach(d => {
|
||||
const status = d.success ? '✅' : '❌';
|
||||
const time = d.duration ? `${d.duration.toFixed(2)}ms` : 'N/A';
|
||||
console.log(` ${status} ${d.capability} (${time})`);
|
||||
if (d.description) {
|
||||
console.log(` └─ ${d.description}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
console.log('\n' + '=' .repeat(70));
|
||||
}
|
||||
}
|
||||
|
||||
// Define capabilities to explore
|
||||
const capabilities = [
|
||||
{
|
||||
name: 'Vector Search',
|
||||
description: 'High-speed semantic search using RuVector',
|
||||
category: 'Core Systems',
|
||||
execute: async () => {
|
||||
const db = new VectorDB({ dimensions: 64, maxElements: 100 });
|
||||
const vec = new Float32Array(64).fill(0.1);
|
||||
await db.insert({ id: 'test', vector: vec, metadata: {} });
|
||||
const results = await db.search(vec, 1);
|
||||
return { success: true, results: results.length, details: `Found ${results.length} results` };
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'Multi-Head Attention',
|
||||
description: 'Parallel attention processing with 4 heads',
|
||||
category: 'Attention Mechanisms',
|
||||
execute: async () => {
|
||||
const attn = new MultiHeadAttention(64, 4);
|
||||
const query = new Float32Array(64).fill(0.1);
|
||||
const keys = [new Float32Array(64).fill(0.2)];
|
||||
const values = [new Float32Array(64).fill(0.3)];
|
||||
const output = attn.compute(query, keys, values);
|
||||
return { success: true, details: `Processed ${4} attention heads` };
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'Hyperbolic Attention',
|
||||
description: 'Hierarchical attention in hyperbolic space',
|
||||
category: 'Attention Mechanisms',
|
||||
execute: async () => {
|
||||
const attn = new HyperbolicAttention(64, -1.0);
|
||||
const query = new Float32Array(64).fill(0.1);
|
||||
const keys = [new Float32Array(64).fill(0.2)];
|
||||
const values = [new Float32Array(64).fill(0.3)];
|
||||
const output = attn.compute(query, keys, values);
|
||||
return { success: true, details: 'Poincaré ball model applied' };
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'Flash Attention',
|
||||
description: 'Memory-efficient block-wise attention',
|
||||
category: 'Attention Mechanisms',
|
||||
execute: async () => {
|
||||
const attn = new FlashAttention(64, 32);
|
||||
const query = new Float32Array(64).fill(0.1);
|
||||
const keys = [new Float32Array(64).fill(0.2)];
|
||||
const values = [new Float32Array(64).fill(0.3)];
|
||||
const output = attn.compute(query, keys, values);
|
||||
return { success: true, details: 'Block size: 32' };
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'Memory Storage',
|
||||
description: 'Persistent vector memory storage',
|
||||
category: 'Core Systems',
|
||||
execute: async () => {
|
||||
const db = new VectorDB({ dimensions: 128, maxElements: 500 });
|
||||
const stored = 10;
|
||||
for (let i = 0; i < stored; i++) {
|
||||
const vec = new Float32Array(128).map(() => Math.random());
|
||||
await db.insert({ id: `mem-${i}`, vector: vec, metadata: { index: i } });
|
||||
}
|
||||
return { success: true, details: `Stored ${stored} memory items` };
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'Semantic Clustering',
|
||||
description: 'Automatic discovery of related concepts',
|
||||
category: 'Learning',
|
||||
execute: async () => {
|
||||
const db = new VectorDB({ dimensions: 64, maxElements: 100 });
|
||||
|
||||
// Create clusters
|
||||
const clusters = ['AI', 'Database', 'Web'];
|
||||
for (const cluster of clusters) {
|
||||
for (let i = 0; i < 3; i++) {
|
||||
const vec = new Float32Array(64).map(() =>
|
||||
Math.random() * 0.1 + (clusters.indexOf(cluster) * 0.3)
|
||||
);
|
||||
await db.insert({
|
||||
id: `${cluster}-${i}`,
|
||||
vector: vec,
|
||||
metadata: { cluster }
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return { success: true, details: `Created ${clusters.length} semantic clusters` };
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
async function runSelfDiscovery() {
|
||||
const explorer = new CognitiveExplorer();
|
||||
|
||||
await explorer.initialize();
|
||||
|
||||
console.log('=' .repeat(70));
|
||||
console.log('\n🚀 Beginning Self-Discovery Process...\n');
|
||||
console.log('=' .repeat(70));
|
||||
|
||||
// Explore each capability
|
||||
for (const capability of capabilities) {
|
||||
await explorer.exploreCapability(capability);
|
||||
await new Promise(resolve => setTimeout(resolve, 100)); // Brief pause
|
||||
}
|
||||
|
||||
// Reflect on discoveries
|
||||
await explorer.reflect();
|
||||
|
||||
// Generate knowledge map
|
||||
await explorer.generateKnowledgeMap();
|
||||
|
||||
// Final summary
|
||||
console.log('\n' + '=' .repeat(70));
|
||||
console.log('\n✅ SELF-DISCOVERY COMPLETE\n');
|
||||
console.log('=' .repeat(70));
|
||||
|
||||
console.log('\n🎓 What I Learned:\n');
|
||||
console.log(' 1. I can store and retrieve semantic memories');
|
||||
console.log(' 2. I have multiple attention mechanisms for different tasks');
|
||||
console.log(' 3. I can cluster related concepts automatically');
|
||||
console.log(' 4. I can reflect on my own performance');
|
||||
console.log(' 5. I can build knowledge graphs of my capabilities');
|
||||
|
||||
console.log('\n🔮 Emergent Properties Discovered:\n');
|
||||
console.log(' - Self-awareness through performance monitoring');
|
||||
console.log(' - Pattern recognition across discoveries');
|
||||
console.log(' - Hierarchical knowledge organization');
|
||||
console.log(' - Continuous learning and improvement');
|
||||
|
||||
console.log('\n💭 Meta-Reflection:\n');
|
||||
console.log(' This system demonstrated cognitive capabilities by:');
|
||||
console.log(' - Exploring its own abilities systematically');
|
||||
console.log(' - Storing discoveries in semantic memory');
|
||||
console.log(' - Reflecting on performance patterns');
|
||||
console.log(' - Building hierarchical knowledge structures');
|
||||
console.log(' - Generating insights from experience\n');
|
||||
|
||||
console.log('=' .repeat(70));
|
||||
console.log('\n');
|
||||
}
|
||||
|
||||
// Run the self-discovery system
|
||||
runSelfDiscovery().catch(error => {
|
||||
console.error('\n❌ Error:', error);
|
||||
console.error('\nStack trace:', error.stack);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user