universal-ai-brain
Version:
🧠 UNIVERSAL AI BRAIN 3.3 - The world's most advanced cognitive architecture with 24 specialized systems, MongoDB 8.1 $rankFusion hybrid search, latest Voyage 3.5 embeddings, and framework-agnostic design. Works with Mastra, Vercel AI, LangChain, OpenAI A
612 lines (556 loc) • 20 kB
text/typescript
/**
* @file AnalogicalMappingSystem - Advanced analogical reasoning using MongoDB Atlas Vector Search
*
* This system demonstrates MongoDB Atlas EXCLUSIVE Vector Search capabilities for analogical reasoning.
* Based on official MongoDB Atlas documentation: https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-stage/
*
* CRITICAL: This uses MongoDB Atlas EXCLUSIVE features:
* - $vectorSearch aggregation stage (Atlas ONLY)
* - Atlas Vector Search indexes (Atlas ONLY)
* - Vector similarity search with embeddings (Atlas ONLY)
* - Semantic search capabilities (Atlas ONLY)
*
* Features:
* - $vectorSearch for semantic analogical similarity
* - Vector embeddings for analogical reasoning
* - Structural and semantic analogy detection
* - Multi-dimensional analogical mapping
* - Analogical inference and projection
*/
import { Db } from 'mongodb';
import { AnalogicalMappingCollection, AnalogicalMapping } from '../collections/AnalogicalMappingCollection';
export interface AnalogicalReasoningRequest {
agentId: string;
scenario: {
description: string;
context: Record<string, any>;
domain: string;
complexity?: number;
};
// Source domain (base for analogy)
source: {
id: string;
name: string;
description: string;
domain: string;
type: 'concept' | 'situation' | 'process' | 'structure' | 'relationship';
embedding?: number[]; // Vector embedding for Atlas Vector Search
structure?: Record<string, any>;
semantics?: Record<string, any>;
};
// Target domain (to find analogies for)
target?: {
id: string;
name: string;
description: string;
domain: string;
type: 'concept' | 'situation' | 'process' | 'structure' | 'relationship';
embedding?: number[]; // Vector embedding for Atlas Vector Search
structure?: Record<string, any>;
semantics?: Record<string, any>;
};
// Search parameters
parameters: {
searchType: 'similarity' | 'structure' | 'function' | 'causal' | 'hybrid';
maxResults: number;
minSimilarity: number;
domains?: string[]; // Restrict search to specific domains
vectorSearchIndex: string; // Atlas Vector Search index name
};
}
export interface AnalogicalReasoningResult {
request: AnalogicalReasoningRequest;
// Found analogical mappings
analogies: Array<{
mapping: AnalogicalMapping;
similarity: number;
confidence: number;
type: 'structural' | 'semantic' | 'functional' | 'causal' | 'surface';
// Correspondence details
correspondences: Array<{
sourceElement: string;
targetElement: string;
type: 'object' | 'relation' | 'attribute' | 'function';
strength: number;
justification: string;
}>;
// Quality assessment
quality: {
systematicity: number; // Coherent system of relations
oneToOne: number; // One-to-one correspondence
semantic: number; // Semantic similarity
pragmatic: number; // Practical utility
overall: number; // Overall quality
};
}>;
// Analogical inferences
inferences: Array<{
type: 'prediction' | 'explanation' | 'hypothesis' | 'generalization';
content: string;
confidence: number;
basedOn: string[]; // Which analogies support this inference
testable: boolean;
}>;
// Novel insights
insights: Array<{
insight: string;
novelty: number; // 0-1 how novel this insight is
plausibility: number; // 0-1 how plausible
implications: string[];
evidence: string[];
}>;
// Metadata
metadata: {
searchTime: number;
analogiesExplored: number;
vectorSearchUsed: boolean;
embeddingModel?: string;
qualityThreshold: number;
};
}
export interface AnalogicalLearningRequest {
agentId: string;
examples: Array<{
source: Record<string, any>;
target: Record<string, any>;
mapping: Record<string, any>;
quality: number; // 0-1
feedback?: string;
}>;
// Learning parameters
parameters: {
method: 'similarity_learning' | 'structure_mapping' | 'case_based' | 'neural_analogy';
iterations: number;
learningRate: number;
generalizationLevel: number; // 0-1
};
}
/**
* AnalogicalMappingSystem - Advanced analogical reasoning using MongoDB Atlas Vector Search
*
* This system demonstrates MongoDB Atlas EXCLUSIVE capabilities:
* - $vectorSearch aggregation stage for semantic similarity
* - Atlas Vector Search indexes for vector embeddings
* - Similarity scoring and ranking with Atlas
* - Multi-dimensional analogical reasoning with vectors
*
* CRITICAL: Requires MongoDB Atlas (not local MongoDB)
*/
export class AnalogicalMappingSystem {
private analogicalCollection: AnalogicalMappingCollection;
private isInitialized = false;
constructor(private db: Db) {
this.analogicalCollection = new AnalogicalMappingCollection(db);
}
/**
* Initialize the analogical mapping system
*/
async initialize(): Promise<void> {
try {
await this.analogicalCollection.createIndexes();
this.isInitialized = true;
console.log('AnalogicalMappingSystem initialized successfully');
console.log('📝 Note: Atlas Vector Search indexes must be created separately in Atlas UI or API');
} catch (error) {
console.error('Failed to initialize AnalogicalMappingSystem:', error);
throw error;
}
}
/**
* Perform analogical reasoning using Atlas Vector Search
*/
async performAnalogicalReasoning(request: AnalogicalReasoningRequest): Promise<AnalogicalReasoningResult> {
if (!this.isInitialized) {
throw new Error('AnalogicalMappingSystem not initialized');
}
const startTime = Date.now();
try {
let analogies: any[] = [];
let vectorSearchUsed = false;
// Use Atlas Vector Search if embedding is provided
if (request.source.embedding && request.source.embedding.length > 0) {
try {
const vectorResults = await this.analogicalCollection.findSimilarAnalogies(
request.source.embedding,
{
indexName: request.parameters.vectorSearchIndex,
limit: request.parameters.maxResults,
numCandidates: request.parameters.maxResults * 10,
minScore: request.parameters.minSimilarity,
filter: {
agentId: request.agentId,
...(request.parameters.domains && {
'mapping.target.domain': { $in: request.parameters.domains }
})
}
}
);
analogies = vectorResults;
vectorSearchUsed = true;
console.log(`🔍 Atlas Vector Search found ${analogies.length} similar analogies`);
} catch (error) {
console.warn('Atlas Vector Search failed, falling back to traditional search:', error);
vectorSearchUsed = false;
}
}
// Fallback to traditional search if vector search not available
if (!vectorSearchUsed) {
const traditionalResults = await this.analogicalCollection.getAgentAnalogicalMappings(
request.agentId,
{
...(request.parameters.domains && {
'mapping.target.domain': { $in: request.parameters.domains }
})
}
);
analogies = traditionalResults.slice(0, request.parameters.maxResults).map(mapping => ({
mapping,
similarity: 0.5, // Default similarity
score: 0.5
}));
}
// Process analogies into structured format
const processedAnalogies = analogies.map(analogy => ({
mapping: analogy.mapping,
similarity: analogy.similarity,
confidence: analogy.mapping.mapping.confidence,
type: analogy.mapping.mapping.type,
correspondences: this.extractCorrespondences(analogy.mapping),
quality: analogy.mapping.mapping.quality
}));
// Generate analogical inferences
const inferences = this.generateInferences(processedAnalogies, request);
// Generate novel insights
const insights = this.generateInsights(processedAnalogies, request);
const searchTime = Date.now() - startTime;
return {
request,
analogies: processedAnalogies,
inferences,
insights,
metadata: {
searchTime,
analogiesExplored: analogies.length,
vectorSearchUsed,
embeddingModel: vectorSearchUsed ? 'atlas-vector-search' : undefined,
qualityThreshold: request.parameters.minSimilarity
}
};
} catch (error) {
console.error('Analogical reasoning failed:', error);
throw error;
}
}
/**
* Learn analogical patterns from examples
*/
async learnAnalogicalPatterns(request: AnalogicalLearningRequest): Promise<{
learnedPatterns: Array<{
pattern: string;
strength: number;
generality: number;
examples: number;
}>;
performance: {
accuracy: number;
generalization: number;
efficiency: number;
};
insights: string[];
}> {
if (!this.isInitialized) {
throw new Error('AnalogicalMappingSystem not initialized');
}
try {
// For demonstration, we'll implement a simple pattern learning algorithm
const patterns = [];
const insights = [];
// Analyze examples to find common patterns
for (let i = 0; i < request.examples.length; i++) {
const example = request.examples[i];
// Extract structural patterns
const structuralPattern = this.extractStructuralPattern(example.source, example.target);
if (structuralPattern) {
patterns.push({
pattern: `Structural: ${structuralPattern}`,
strength: example.quality,
generality: 0.7,
examples: 1
});
}
// Extract semantic patterns
const semanticPattern = this.extractSemanticPattern(example.source, example.target);
if (semanticPattern) {
patterns.push({
pattern: `Semantic: ${semanticPattern}`,
strength: example.quality,
generality: 0.6,
examples: 1
});
}
}
// Generate insights from learned patterns
if (patterns.length > 0) {
insights.push(`Learned ${patterns.length} analogical patterns from ${request.examples.length} examples`);
const avgStrength = patterns.reduce((sum, p) => sum + p.strength, 0) / patterns.length;
insights.push(`Average pattern strength: ${avgStrength.toFixed(2)}`);
if (avgStrength > 0.8) {
insights.push('High-quality analogical patterns detected - strong systematic correspondences');
}
}
// Store learned patterns as analogical mappings
for (const pattern of patterns) {
await this.storeLearnedPattern(request.agentId, pattern, request.examples);
}
return {
learnedPatterns: patterns,
performance: {
accuracy: Math.min(patterns.reduce((sum, p) => sum + p.strength, 0) / patterns.length || 0, 1),
generalization: Math.min(patterns.reduce((sum, p) => sum + p.generality, 0) / patterns.length || 0, 1),
efficiency: Math.min(patterns.length / request.examples.length, 1)
},
insights
};
} catch (error) {
console.error('Analogical learning failed:', error);
throw error;
}
}
/**
* Get analogical patterns for an agent
*/
async getAnalogicalPatterns(agentId: string): Promise<{
commonMappings: Array<{ type: string; frequency: number; averageStrength: number }>;
domainPairs: Array<{ source: string; target: string; frequency: number; averageQuality: number }>;
reasoningMethods: Array<{ method: string; frequency: number; averageAccuracy: number }>;
qualityMetrics: Array<{ metric: string; average: number; distribution: Record<string, number> }>;
}> {
if (!this.isInitialized) {
throw new Error('AnalogicalMappingSystem not initialized');
}
return await this.analogicalCollection.findAnalogicalPatterns(agentId);
}
/**
* Extract correspondences from an analogical mapping
*/
private extractCorrespondences(mapping: AnalogicalMapping): Array<{
sourceElement: string;
targetElement: string;
type: 'object' | 'relation' | 'attribute' | 'function';
strength: number;
justification: string;
}> {
return mapping.mapping.correspondences.map(corr => ({
sourceElement: corr.sourceEntity,
targetElement: corr.targetEntity,
type: corr.type as 'object' | 'relation' | 'attribute' | 'function',
strength: corr.strength,
justification: corr.justification
}));
}
/**
* Generate analogical inferences
*/
private generateInferences(analogies: any[], request: AnalogicalReasoningRequest): Array<{
type: 'prediction' | 'explanation' | 'hypothesis' | 'generalization';
content: string;
confidence: number;
basedOn: string[];
testable: boolean;
}> {
const inferences = [];
for (const analogy of analogies) {
// Generate predictions based on analogical mapping
if (analogy.similarity > 0.7) {
inferences.push({
type: 'prediction',
content: `Based on analogy with ${analogy.mapping.mapping.source.name}, we predict similar behavior in ${request.source.name}`,
confidence: analogy.similarity,
basedOn: [analogy.mapping.mapping.id],
testable: true
});
}
// Generate explanations
if (analogy.quality.systematicity > 0.8) {
inferences.push({
type: 'explanation',
content: `The systematic correspondence between ${analogy.mapping.mapping.source.domain} and ${analogy.mapping.mapping.target.domain} explains the observed patterns`,
confidence: analogy.quality.systematicity,
basedOn: [analogy.mapping.mapping.id],
testable: false
});
}
}
return inferences;
}
/**
* Generate novel insights from analogical mappings
*/
private generateInsights(analogies: any[], request: AnalogicalReasoningRequest): Array<{
insight: string;
novelty: number;
plausibility: number;
implications: string[];
evidence: string[];
}> {
const insights = [];
// Cross-domain insights
const domains = new Set(analogies.map(a => a.mapping.mapping.target.domain));
if (domains.size > 1) {
insights.push({
insight: `Cross-domain patterns detected across ${domains.size} different domains`,
novelty: 0.8,
plausibility: 0.9,
implications: ['Universal principles may apply', 'Transfer learning opportunities'],
evidence: Array.from(domains)
});
}
// High-quality mapping insights
const highQualityMappings = analogies.filter(a => a.quality.overall > 0.8);
if (highQualityMappings.length > 0) {
insights.push({
insight: `${highQualityMappings.length} high-quality analogical mappings suggest strong structural similarities`,
novelty: 0.6,
plausibility: 0.95,
implications: ['Reliable analogical reasoning possible', 'Strong predictive power'],
evidence: highQualityMappings.map(m => m.mapping.mapping.id)
});
}
return insights;
}
/**
* Extract structural pattern from source-target pair
*/
private extractStructuralPattern(source: any, target: any): string | null {
// Simple pattern extraction - in real implementation, this would be more sophisticated
if (source.type && target.type && source.type === target.type) {
return `Same type: ${source.type}`;
}
return null;
}
/**
* Extract semantic pattern from source-target pair
*/
private extractSemanticPattern(source: any, target: any): string | null {
// Simple pattern extraction - in real implementation, this would use semantic analysis
if (source.domain && target.domain) {
return `Domain mapping: ${source.domain} -> ${target.domain}`;
}
return null;
}
/**
* Store a learned pattern as an analogical mapping
*/
private async storeLearnedPattern(agentId: string, pattern: any, examples: any[]): Promise<void> {
// Create a synthetic analogical mapping from the learned pattern
const mapping: Omit<AnalogicalMapping, '_id' | 'createdAt' | 'updatedAt'> = {
agentId,
timestamp: new Date(),
mapping: {
id: `learned_pattern_${Date.now()}`,
type: 'structural',
category: 'conceptual',
strength: pattern.strength,
confidence: pattern.generality,
source: {
id: 'learned_source',
name: 'Learned Source Pattern',
description: pattern.pattern,
domain: 'learned',
type: 'structure',
embedding: [], // Would be populated with actual embeddings
structure: { entities: [], relationships: [], patterns: [] },
semantics: { concepts: [], attributes: {}, functions: [], goals: [], constraints: [] },
context: { domain: 'learned', subdomain: 'pattern', complexity: 0.5, abstractness: 0.7, familiarity: 0.6, cultural: {}, temporal: {} }
},
target: {
id: 'learned_target',
name: 'Learned Target Pattern',
description: pattern.pattern,
domain: 'learned',
type: 'structure',
embedding: [], // Would be populated with actual embeddings
structure: { entities: [], relationships: [], patterns: [] },
semantics: { concepts: [], attributes: {}, functions: [], goals: [], constraints: [] },
context: { domain: 'learned', subdomain: 'pattern', complexity: 0.5, abstractness: 0.7, familiarity: 0.6, cultural: {}, temporal: {} }
},
correspondences: [],
quality: {
systematicity: pattern.generality,
oneToOne: 0.8,
semantic: 0.7,
pragmatic: 0.9,
overall: pattern.strength
}
},
reasoning: {
discovery: {
method: 'similarity',
trigger: 'learning',
searchSpace: [],
candidates: [],
selection: { criteria: [], winner: '', justification: '' }
},
alignment: {
strategy: 'incremental',
iterations: 1,
convergence: 1.0,
conflicts: []
},
evaluation: {
criteria: ['quality', 'generality'],
scores: { quality: pattern.strength, generality: pattern.generality },
strengths: ['learned from examples'],
weaknesses: [],
alternatives: []
},
projection: {
predictions: [],
inferences: [],
hypotheses: []
}
},
learning: {
usage: [],
generalization: {
abstractions: [],
schemas: []
},
performance: {
accuracy: pattern.strength,
utility: 0.8,
efficiency: 0.7,
robustness: 0.6,
transferability: pattern.generality
}
},
metadata: {
framework: 'analogical-mapping-system',
version: '1.0.0',
source: 'learned',
reliability: pattern.strength,
lastValidated: new Date(),
quality: {
completeness: 0.8,
consistency: 0.9,
coherence: 0.8,
novelty: 0.7
},
vectorSearch: {
indexName: 'analogical_mappings_vector_index',
embeddingModel: 'atlas-vector-search',
dimensions: 384,
similarity: 'cosine',
lastIndexed: new Date()
}
}
};
await this.analogicalCollection.storeAnalogicalMapping(mapping);
}
/**
* Cleanup resources
*/
async cleanup(): Promise<void> {
// Cleanup any resources if needed
}
}