- Added batch embedding support with `defaultBatchEmbeddingFunction`, leveraging shared model instances for optimized performance. - Integrated `isInitializing` flag to prevent recursive initialization and ensure smooth concurrent operation handling during `BrainyData` initialization. - Pre-loaded Universal Sentence Encoder in `BrainyData` to prevent delays during embedding. - Introduced fallback mechanisms in embedding initialization for better error resiliency and model reusability. - Updated `addBatch` with support for batchSize and refactored text/vector processing logic for clearer separation and memory management. - Improved GPU and CPU backend selection in Universal Sentence Encoder for compatibility across environments. - Enhanced memory management by cleaning tensors after embedding operations. - Updated README with instructions for batch embedding, threading updates, and GPU/CPU optimizations.
58 lines
1.4 KiB
TypeScript
58 lines
1.4 KiB
TypeScript
/**
|
|
* Utility functions for environment detection
|
|
*/
|
|
|
|
/**
|
|
* Check if code is running in a browser environment
|
|
*/
|
|
export function isBrowser(): boolean {
|
|
return typeof window !== 'undefined' && typeof document !== 'undefined';
|
|
}
|
|
|
|
/**
|
|
* Check if code is running in a Node.js environment
|
|
*/
|
|
export function isNode(): boolean {
|
|
return typeof process !== 'undefined' &&
|
|
process.versions != null &&
|
|
process.versions.node != null;
|
|
}
|
|
|
|
/**
|
|
* Check if code is running in a Web Worker environment
|
|
*/
|
|
export function isWebWorker(): boolean {
|
|
return typeof self === 'object' &&
|
|
self.constructor &&
|
|
self.constructor.name === 'DedicatedWorkerGlobalScope';
|
|
}
|
|
|
|
/**
|
|
* Check if Web Workers are available in the current environment
|
|
*/
|
|
export function areWebWorkersAvailable(): boolean {
|
|
return isBrowser() && typeof Worker !== 'undefined';
|
|
}
|
|
|
|
/**
|
|
* Check if Worker Threads are available in the current environment (Node.js)
|
|
*/
|
|
export function areWorkerThreadsAvailable(): boolean {
|
|
if (!isNode()) return false;
|
|
|
|
try {
|
|
// Dynamic import to avoid errors in browser environments
|
|
require('worker_threads');
|
|
return true;
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Determine if threading is available in the current environment
|
|
* Always returns false since multithreading has been removed
|
|
*/
|
|
export function isThreadingAvailable(): boolean {
|
|
return false;
|
|
}
|