UNPKG

multimind-sdk

Version:

This SDK gives JavaScript/TypeScript developers full access to advanced AI features like agent orchestration, RAG, and fine-tuning — without needing to manage backend code.

274 lines 9.35 kB
import { py } from './bridge/multimind-bridge.js'; export class LSTMModelClient { constructor(config) { this.config = config; } async initialize() { try { this.client = await py `LSTMModelClient( model_path=${this.config.modelPath || ''}, model_name=${this.config.modelName || 'lstm'}, tokenizer=${this.config.tokenizer || null}, device=${this.config.device || 'auto'}, max_length=${this.config.maxLength || 512}, temperature=${this.config.temperature || 0.7} )`; return { success: true, message: 'LSTM model client initialized' }; } catch (error) { console.error('Error initializing LSTM model client:', error); throw error; } } async generate(input, config) { try { const result = await py `self.client.generate( input=${input}, max_length=${config?.maxLength || this.config.maxLength || 512}, temperature=${config?.temperature || this.config.temperature || 0.7} )`; return result; } catch (error) { console.error('Error generating with LSTM:', error); throw error; } } async encode(input) { try { const embeddings = await py `self.client.encode(${input})`; return embeddings; } catch (error) { console.error('Error encoding with LSTM:', error); throw error; } } } export class MoEModelClient { constructor(config) { this.experts = new Map(); this.config = config; } async initialize() { try { // Initialize expert models for (const [name, expertConfig] of Object.entries(this.config.experts)) { const expert = new LSTMModelClient(expertConfig); await expert.initialize(); this.experts.set(name, expert); } this.client = await py `MoEModelClient( experts=${Object.keys(this.config.experts)}, router_fn=${this.config.router.toString()}, load_balancing=${this.config.loadBalancing || false}, fallback_expert=${this.config.fallbackExpert || Object.keys(this.config.experts)[0]} )`; return { success: true, message: 'MoE model client initialized' }; } catch (error) { console.error('Error initializing MoE model client:', error); throw error; } } async generate(input) { try { const expertName = this.config.router(input); const expert = this.experts.get(expertName); if (!expert) { throw new Error(`Expert ${expertName} not found`); } return await expert.generate(input); } catch (error) { console.error('Error generating with MoE:', error); throw error; } } async getExpertUsage() { try { const usage = await py `self.client.get_expert_usage()`; return usage; } catch (error) { console.error('Error getting expert usage:', error); throw error; } } async addExpert(name, config) { try { const expert = new LSTMModelClient(config); await expert.initialize(); this.experts.set(name, expert); await py `self.client.add_expert(${name})`; return { success: true, message: `Expert ${name} added` }; } catch (error) { console.error('Error adding expert:', error); throw error; } } } export class MultiModalClient { constructor(config) { this.clients = new Map(); this.config = config; } async initialize() { try { // Initialize modality-specific clients if (this.config.textClient) { this.clients.set('text', this.config.textClient); } if (this.config.imageClient) { this.clients.set('image', this.config.imageClient); } if (this.config.audioClient) { this.clients.set('audio', this.config.audioClient); } if (this.config.videoClient) { this.clients.set('video', this.config.videoClient); } if (this.config.codeClient) { this.clients.set('code', this.config.codeClient); } this.client = await py `MultiModalClient( text_client=${this.config.textClient || null}, image_client=${this.config.imageClient || null}, audio_client=${this.config.audioClient || null}, video_client=${this.config.videoClient || null}, code_client=${this.config.codeClient || null}, fusion_strategy=${this.config.fusionStrategy || 'concat'} )`; return { success: true, message: 'MultiModal client initialized' }; } catch (error) { console.error('Error initializing MultiModal client:', error); throw error; } } async generate(input, inputType) { try { const result = await py `self.client.generate( input=${input}, input_type=${inputType} )`; return result; } catch (error) { console.error('Error generating with MultiModal:', error); throw error; } } async processMultimodal(inputs) { try { const result = await py `self.client.process_multimodal(${inputs})`; return result; } catch (error) { console.error('Error processing multimodal input:', error); throw error; } } async getSupportedModalities() { try { const modalities = await py `self.client.get_supported_modalities()`; return modalities; } catch (error) { console.error('Error getting supported modalities:', error); throw error; } } } export class FederatedRouter { constructor(config) { this.config = config; } async initialize() { try { this.router = await py `FederatedRouter( local_client=${this.config.localClient}, cloud_client=${this.config.cloudClient}, routing_strategy=${this.config.routingStrategy}, fallback_strategy=${this.config.fallbackStrategy || 'local'}, cost_threshold=${this.config.costThreshold || 0.1} )`; return { success: true, message: 'Federated router initialized' }; } catch (error) { console.error('Error initializing federated router:', error); throw error; } } async route(input) { try { const result = await py `self.router.route(${input})`; return result; } catch (error) { console.error('Error routing input:', error); throw error; } } async generate(input) { try { const result = await py `self.router.generate(${input})`; return result; } catch (error) { console.error('Error generating with federated router:', error); throw error; } } async getRoutingStats() { try { const stats = await py `self.router.get_routing_stats()`; return stats; } catch (error) { console.error('Error getting routing stats:', error); throw error; } } async updateRoutingStrategy(strategy) { try { await py `self.router.update_routing_strategy(${strategy})`; return { success: true, message: 'Routing strategy updated' }; } catch (error) { console.error('Error updating routing strategy:', error); throw error; } } } // Utility functions for dynamic routing export function createLoadBalancedRouter(clients, weights) { const clientWeights = weights || new Array(clients.length).fill(1 / clients.length); let currentIndex = 0; return (input) => { // Simple round-robin with weights const selectedClient = clients[currentIndex]; currentIndex = (currentIndex + 1) % clients.length; return selectedClient; }; } export function createIntelligentRouter(clients, routingRules) { return (input) => { // Implement intelligent routing based on input characteristics const inputLength = input.length; const hasCode = input.includes('```') || input.includes('def ') || input.includes('function'); const hasMath = input.includes('=') || input.includes('+') || input.includes('*'); if (hasCode && routingRules.codeClient) { return routingRules.codeClient; } if (hasMath && routingRules.mathClient) { return routingRules.mathClient; } if (inputLength > 1000 && routingRules.longTextClient) { return routingRules.longTextClient; } return routingRules.defaultClient || clients[0]; }; } //# sourceMappingURL=modelClientSystem.js.map