2025-06-24 11:41:30 -07:00
|
|
|
/**
|
|
|
|
|
* Embedding functions for converting data to vectors
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { EmbeddingFunction, EmbeddingModel, Vector } from '../coreTypes.js'
|
|
|
|
|
import { executeInThread } from './workerUtils.js'
|
2025-07-28 10:04:45 -07:00
|
|
|
import { isBrowser } from './environment.js'
|
2025-08-01 18:31:37 -07:00
|
|
|
import {
|
|
|
|
|
RobustModelLoader,
|
|
|
|
|
ModelLoadOptions,
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
createRobustModelLoader,
|
2025-08-01 18:31:37 -07:00
|
|
|
getUniversalSentenceEncoderFallbacks
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
} from './robustModelLoader.js'
|
2025-06-24 11:41:30 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* TensorFlow Universal Sentence Encoder embedding model
|
|
|
|
|
* This model provides high-quality text embeddings using TensorFlow.js
|
|
|
|
|
* The required TensorFlow.js dependencies are automatically installed with this package
|
2025-06-26 19:04:37 -07:00
|
|
|
*
|
2025-06-27 14:06:59 -07:00
|
|
|
* This implementation attempts to use GPU processing when available for better performance,
|
|
|
|
|
* falling back to CPU processing for compatibility across all environments.
|
2025-06-24 11:41:30 -07:00
|
|
|
*/
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
export interface UniversalSentenceEncoderOptions extends ModelLoadOptions {
|
|
|
|
|
/** Whether to enable verbose logging */
|
|
|
|
|
verbose?: boolean
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-24 11:41:30 -07:00
|
|
|
export class UniversalSentenceEncoder implements EmbeddingModel {
|
|
|
|
|
private model: any = null
|
|
|
|
|
private initialized = false
|
|
|
|
|
private tf: any = null
|
|
|
|
|
private use: any = null
|
2025-06-27 14:06:59 -07:00
|
|
|
private backend: string = 'cpu' // Default to CPU
|
2025-07-18 10:40:37 -07:00
|
|
|
private verbose: boolean = true // Whether to log non-essential messages
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
private robustLoader: RobustModelLoader
|
|
|
|
|
|
2025-07-18 10:40:37 -07:00
|
|
|
/**
|
|
|
|
|
* Create a new UniversalSentenceEncoder instance
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
* @param options Configuration options including reliability settings
|
2025-07-18 10:40:37 -07:00
|
|
|
*/
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
constructor(options: UniversalSentenceEncoderOptions = {}) {
|
2025-07-18 10:40:37 -07:00
|
|
|
this.verbose = options.verbose !== undefined ? options.verbose : true
|
2025-08-01 18:31:37 -07:00
|
|
|
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
// Create robust model loader with enhanced reliability features
|
|
|
|
|
this.robustLoader = createRobustModelLoader({
|
|
|
|
|
maxRetries: options.maxRetries ?? 3,
|
|
|
|
|
initialRetryDelay: options.initialRetryDelay ?? 1000,
|
|
|
|
|
maxRetryDelay: options.maxRetryDelay ?? 30000,
|
|
|
|
|
timeout: options.timeout ?? 60000,
|
|
|
|
|
useExponentialBackoff: options.useExponentialBackoff ?? true,
|
2025-08-01 18:31:37 -07:00
|
|
|
fallbackUrls:
|
|
|
|
|
options.fallbackUrls ?? getUniversalSentenceEncoderFallbacks(),
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
verbose: this.verbose,
|
|
|
|
|
preferLocalModel: options.preferLocalModel ?? true
|
|
|
|
|
})
|
2025-07-18 10:40:37 -07:00
|
|
|
}
|
2025-06-24 11:41:30 -07:00
|
|
|
|
2025-07-02 16:16:19 -07:00
|
|
|
/**
|
|
|
|
|
* Add polyfills and patches for TensorFlow.js compatibility
|
2025-07-16 13:51:00 -07:00
|
|
|
* This addresses issues with TensorFlow.js across all server environments
|
|
|
|
|
* (Node.js, serverless, and other server environments)
|
|
|
|
|
*
|
2025-07-14 11:12:51 -07:00
|
|
|
* Note: The main TensorFlow.js patching is now centralized in textEncoding.ts
|
|
|
|
|
* and applied through setup.ts. This method only adds additional utility functions
|
|
|
|
|
* that might be needed by TensorFlow.js.
|
2025-07-02 16:16:19 -07:00
|
|
|
*/
|
2025-07-16 13:51:00 -07:00
|
|
|
private addServerCompatibilityPolyfills(): void {
|
|
|
|
|
// Apply in all non-browser environments (Node.js, serverless, server environments)
|
2025-07-28 10:04:45 -07:00
|
|
|
if (isBrowser()) {
|
2025-07-16 13:51:00 -07:00
|
|
|
return // Browser environments don't need these polyfills
|
2025-07-02 16:16:19 -07:00
|
|
|
}
|
|
|
|
|
|
2025-07-16 13:51:00 -07:00
|
|
|
// Get the appropriate global object for the current environment
|
|
|
|
|
const globalObj = (() => {
|
|
|
|
|
if (typeof globalThis !== 'undefined') return globalThis
|
|
|
|
|
if (typeof global !== 'undefined') return global
|
|
|
|
|
if (typeof self !== 'undefined') return self
|
|
|
|
|
return {} as any // Fallback for unknown environments
|
|
|
|
|
})()
|
|
|
|
|
|
|
|
|
|
// Add polyfill for utility functions across all server environments
|
|
|
|
|
// This fixes issues like "Cannot read properties of undefined (reading 'isFloat32Array')"
|
|
|
|
|
try {
|
|
|
|
|
// Ensure the util object exists
|
|
|
|
|
if (!globalObj.util) {
|
|
|
|
|
globalObj.util = {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add isFloat32Array method if it doesn't exist
|
|
|
|
|
if (!globalObj.util.isFloat32Array) {
|
|
|
|
|
globalObj.util.isFloat32Array = (obj: any) => {
|
|
|
|
|
return !!(
|
|
|
|
|
obj instanceof Float32Array ||
|
|
|
|
|
(obj &&
|
|
|
|
|
Object.prototype.toString.call(obj) === '[object Float32Array]')
|
|
|
|
|
)
|
2025-07-14 11:12:51 -07:00
|
|
|
}
|
2025-07-16 13:51:00 -07:00
|
|
|
}
|
2025-07-11 11:11:56 -07:00
|
|
|
|
2025-07-16 13:51:00 -07:00
|
|
|
// Add isTypedArray method if it doesn't exist
|
|
|
|
|
if (!globalObj.util.isTypedArray) {
|
|
|
|
|
globalObj.util.isTypedArray = (obj: any) => {
|
|
|
|
|
return !!(ArrayBuffer.isView(obj) && !(obj instanceof DataView))
|
2025-07-14 11:12:51 -07:00
|
|
|
}
|
2025-07-16 13:51:00 -07:00
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.warn('Failed to add utility polyfills:', error)
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-11 11:11:56 -07:00
|
|
|
|
2025-07-16 13:51:00 -07:00
|
|
|
/**
|
|
|
|
|
* Check if we're running in a test environment
|
|
|
|
|
*/
|
|
|
|
|
private isTestEnvironment(): boolean {
|
|
|
|
|
// Safely check for Node.js environment first
|
|
|
|
|
if (typeof process === 'undefined') {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2025-07-17 10:00:28 -07:00
|
|
|
|
2025-07-16 13:51:00 -07:00
|
|
|
return (
|
|
|
|
|
process.env.NODE_ENV === 'test' ||
|
|
|
|
|
process.env.VITEST === 'true' ||
|
|
|
|
|
(typeof global !== 'undefined' && global.__vitest__) ||
|
|
|
|
|
process.argv.some((arg) => arg.includes('vitest'))
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-07-18 10:40:37 -07:00
|
|
|
* Log message only if verbose mode is enabled or if it's an error
|
|
|
|
|
* This helps suppress non-essential log messages
|
2025-07-16 13:51:00 -07:00
|
|
|
*/
|
2025-07-17 10:00:28 -07:00
|
|
|
private logger(
|
2025-07-16 13:51:00 -07:00
|
|
|
level: 'log' | 'warn' | 'error',
|
|
|
|
|
message: string,
|
|
|
|
|
...args: any[]
|
|
|
|
|
): void {
|
2025-07-18 10:40:37 -07:00
|
|
|
// Always log errors, but only log other messages if verbose mode is enabled
|
|
|
|
|
if (level === 'error' || this.verbose) {
|
|
|
|
|
console[level](message, ...args)
|
|
|
|
|
}
|
2025-07-16 13:51:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
* Load the Universal Sentence Encoder model with robust retry and fallback mechanisms
|
|
|
|
|
* @param loadFunction The function to load the model from TensorFlow Hub
|
2025-07-16 13:51:00 -07:00
|
|
|
*/
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
private async loadModelFromLocal(
|
|
|
|
|
loadFunction: () => Promise<EmbeddingModel>
|
2025-07-16 13:51:00 -07:00
|
|
|
): Promise<EmbeddingModel> {
|
2025-08-01 18:31:37 -07:00
|
|
|
this.logger(
|
|
|
|
|
'log',
|
|
|
|
|
'Loading Universal Sentence Encoder model with robust loader...'
|
|
|
|
|
)
|
|
|
|
|
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
try {
|
|
|
|
|
// Use the robust model loader to handle all retry logic, timeouts, and fallbacks
|
|
|
|
|
const model = await this.robustLoader.loadModel(
|
|
|
|
|
loadFunction,
|
|
|
|
|
'universal-sentence-encoder'
|
|
|
|
|
)
|
2025-08-01 18:31:37 -07:00
|
|
|
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
this.logger('log', 'Successfully loaded Universal Sentence Encoder model')
|
|
|
|
|
return model
|
|
|
|
|
} catch (error) {
|
2025-08-01 18:31:37 -07:00
|
|
|
const errorMessage =
|
|
|
|
|
error instanceof Error ? error.message : String(error)
|
|
|
|
|
this.logger(
|
|
|
|
|
'error',
|
|
|
|
|
`Failed to load Universal Sentence Encoder model: ${errorMessage}`
|
|
|
|
|
)
|
|
|
|
|
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
// Log loading statistics for debugging
|
|
|
|
|
const stats = this.robustLoader.getLoadingStats()
|
|
|
|
|
if (Object.keys(stats).length > 0) {
|
|
|
|
|
this.logger('log', 'Loading attempt statistics:', stats)
|
2025-07-11 11:11:56 -07:00
|
|
|
}
|
2025-08-01 18:31:37 -07:00
|
|
|
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
throw error
|
2025-07-11 11:11:56 -07:00
|
|
|
}
|
2025-07-02 16:16:19 -07:00
|
|
|
}
|
|
|
|
|
|
2025-06-24 11:41:30 -07:00
|
|
|
/**
|
|
|
|
|
* Initialize the embedding model
|
|
|
|
|
*/
|
|
|
|
|
public async init(): Promise<void> {
|
2025-08-01 18:31:37 -07:00
|
|
|
// Use a mock implementation in test environments
|
|
|
|
|
if (this.isTestEnvironment()) {
|
|
|
|
|
this.logger('log', 'Using mock Universal Sentence Encoder for tests')
|
|
|
|
|
// Create a mock model that returns fixed embeddings
|
|
|
|
|
this.model = {
|
|
|
|
|
embed: async (sentences: string | string[]) => {
|
|
|
|
|
// Create a tensor-like object with a mock array method
|
|
|
|
|
return {
|
|
|
|
|
array: async () => {
|
|
|
|
|
// Return fixed embeddings for each input sentence
|
|
|
|
|
const inputArray = Array.isArray(sentences)
|
|
|
|
|
? sentences
|
|
|
|
|
: [sentences]
|
|
|
|
|
return inputArray.map(() =>
|
|
|
|
|
new Array(512).fill(0).map((_, i) => (i % 2 === 0 ? 0.1 : -0.1))
|
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
dispose: () => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this.initialized = true
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-24 11:41:30 -07:00
|
|
|
try {
|
|
|
|
|
// Save original console.warn
|
|
|
|
|
const originalWarn = console.warn
|
|
|
|
|
|
|
|
|
|
// Override console.warn to suppress TensorFlow.js Node.js backend message
|
|
|
|
|
console.warn = function (message?: any, ...optionalParams: any[]) {
|
|
|
|
|
if (
|
|
|
|
|
message &&
|
|
|
|
|
typeof message === 'string' &&
|
|
|
|
|
message.includes(
|
|
|
|
|
'Hi, looks like you are running TensorFlow.js in Node.js'
|
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
return // Suppress the specific warning
|
|
|
|
|
}
|
|
|
|
|
originalWarn(message, ...optionalParams)
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-02 16:16:19 -07:00
|
|
|
// Add polyfills for TensorFlow.js compatibility
|
2025-07-16 13:51:00 -07:00
|
|
|
this.addServerCompatibilityPolyfills()
|
2025-07-02 16:16:19 -07:00
|
|
|
|
2025-07-16 13:51:00 -07:00
|
|
|
// CRITICAL: Ensure TextEncoder/TextDecoder are available before TensorFlow.js loads
|
2025-06-26 19:04:37 -07:00
|
|
|
try {
|
2025-07-16 13:51:00 -07:00
|
|
|
// Get the appropriate global object for the current environment
|
|
|
|
|
const globalObj = (() => {
|
|
|
|
|
if (typeof globalThis !== 'undefined') return globalThis
|
|
|
|
|
if (typeof global !== 'undefined') return global
|
|
|
|
|
if (typeof self !== 'undefined') return self
|
|
|
|
|
return null
|
|
|
|
|
})()
|
|
|
|
|
|
|
|
|
|
// Ensure TextEncoder/TextDecoder are globally available in server environments
|
|
|
|
|
if (globalObj) {
|
|
|
|
|
// Try to use Node.js util module if available (Node.js environments)
|
|
|
|
|
try {
|
2025-07-17 10:00:28 -07:00
|
|
|
if (
|
|
|
|
|
typeof process !== 'undefined' &&
|
|
|
|
|
process.versions &&
|
|
|
|
|
process.versions.node
|
|
|
|
|
) {
|
2025-07-16 13:51:00 -07:00
|
|
|
const util = await import('util')
|
|
|
|
|
if (!globalObj.TextEncoder) {
|
2025-08-05 16:09:30 -07:00
|
|
|
globalObj.TextEncoder = util.TextEncoder as unknown as typeof TextEncoder
|
2025-07-16 13:51:00 -07:00
|
|
|
}
|
|
|
|
|
if (!globalObj.TextDecoder) {
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
globalObj.TextDecoder =
|
|
|
|
|
util.TextDecoder as unknown as typeof TextDecoder
|
2025-07-16 13:51:00 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (utilError) {
|
|
|
|
|
// Fallback to standard TextEncoder/TextDecoder for non-Node.js server environments
|
|
|
|
|
if (!globalObj.TextEncoder) {
|
|
|
|
|
globalObj.TextEncoder = TextEncoder
|
|
|
|
|
}
|
|
|
|
|
if (!globalObj.TextDecoder) {
|
|
|
|
|
globalObj.TextDecoder = TextDecoder
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Apply the TensorFlow.js patch
|
|
|
|
|
const { applyTensorFlowPatch } = await import('./textEncoding.js')
|
|
|
|
|
await applyTensorFlowPatch()
|
2025-07-14 11:12:51 -07:00
|
|
|
|
2025-07-16 13:51:00 -07:00
|
|
|
// Now load TensorFlow.js core module using dynamic imports
|
|
|
|
|
this.tf = await import('@tensorflow/tfjs-core')
|
2025-07-14 11:12:51 -07:00
|
|
|
|
2025-07-16 13:51:00 -07:00
|
|
|
// Import CPU backend (always needed as fallback)
|
|
|
|
|
await import('@tensorflow/tfjs-backend-cpu')
|
2025-07-14 11:12:51 -07:00
|
|
|
|
2025-07-16 13:51:00 -07:00
|
|
|
// Try to import WebGL backend for GPU acceleration in browser environments
|
|
|
|
|
try {
|
2025-07-28 10:04:45 -07:00
|
|
|
if (isBrowser()) {
|
2025-07-16 13:51:00 -07:00
|
|
|
await import('@tensorflow/tfjs-backend-webgl')
|
|
|
|
|
// Check if WebGL is available
|
2025-07-14 11:12:51 -07:00
|
|
|
try {
|
|
|
|
|
if (this.tf.setBackend) {
|
2025-07-16 13:51:00 -07:00
|
|
|
await this.tf.setBackend('webgl')
|
2025-07-14 11:12:51 -07:00
|
|
|
this.backend = 'webgl'
|
|
|
|
|
console.log('Using WebGL backend for TensorFlow.js')
|
|
|
|
|
} else {
|
|
|
|
|
console.warn(
|
|
|
|
|
'tf.setBackend is not available, falling back to CPU'
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
2025-06-27 14:06:59 -07:00
|
|
|
console.warn(
|
2025-07-14 11:12:51 -07:00
|
|
|
'WebGL backend not available, falling back to CPU:',
|
|
|
|
|
e
|
2025-06-27 14:06:59 -07:00
|
|
|
)
|
2025-07-14 11:12:51 -07:00
|
|
|
this.backend = 'cpu'
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-16 13:51:00 -07:00
|
|
|
} catch (error) {
|
|
|
|
|
console.warn(
|
|
|
|
|
'WebGL backend not available, falling back to CPU:',
|
|
|
|
|
error
|
2025-07-14 11:12:51 -07:00
|
|
|
)
|
2025-07-16 13:51:00 -07:00
|
|
|
this.backend = 'cpu'
|
2025-06-24 11:41:30 -07:00
|
|
|
}
|
2025-07-16 13:51:00 -07:00
|
|
|
|
2025-08-05 16:09:30 -07:00
|
|
|
// Note: @tensorflow-models/universal-sentence-encoder is no longer used
|
|
|
|
|
// Model loading is handled entirely by robustLoader
|
2025-06-27 14:06:59 -07:00
|
|
|
} catch (error) {
|
2025-07-17 10:00:28 -07:00
|
|
|
this.logger('error', 'Failed to initialize TensorFlow.js:', error)
|
2025-08-01 11:02:01 -07:00
|
|
|
// No fallback allowed - throw error
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
throw new Error(
|
|
|
|
|
`Universal Sentence Encoder initialization failed: ${error}`
|
|
|
|
|
)
|
2025-06-27 14:06:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set the backend
|
2025-07-28 16:00:05 -07:00
|
|
|
if (this.tf && this.tf.setBackend) {
|
2025-06-27 14:06:59 -07:00
|
|
|
await this.tf.setBackend(this.backend)
|
2025-06-24 11:41:30 -07:00
|
|
|
}
|
|
|
|
|
|
2025-08-05 16:09:30 -07:00
|
|
|
// Load model using robustLoader which handles all loading strategies:
|
|
|
|
|
// 1. @soulcraft/brainy-models package if available (offline mode)
|
|
|
|
|
// 2. Direct TensorFlow.js URL loading as fallback
|
2025-07-28 16:00:05 -07:00
|
|
|
try {
|
2025-08-05 16:09:30 -07:00
|
|
|
this.model = await this.robustLoader.loadModelWithFallbacks()
|
2025-07-28 16:00:05 -07:00
|
|
|
this.initialized = true
|
2025-08-05 16:09:30 -07:00
|
|
|
|
|
|
|
|
// If the model doesn't have an embed method but has embedToArrays, wrap it
|
|
|
|
|
if (!this.model.embed && this.model.embedToArrays) {
|
|
|
|
|
const originalModel = this.model
|
|
|
|
|
this.model = {
|
|
|
|
|
embed: async (sentences: string | string[]) => {
|
|
|
|
|
const input = Array.isArray(sentences) ? sentences : [sentences]
|
|
|
|
|
const embeddings = await originalModel.embedToArrays(input)
|
|
|
|
|
// Return TensorFlow tensor-like object
|
|
|
|
|
return {
|
|
|
|
|
array: async () => embeddings,
|
|
|
|
|
arraySync: () => embeddings
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
dispose: () => originalModel.dispose ? originalModel.dispose() : undefined
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-28 16:00:05 -07:00
|
|
|
} catch (modelError) {
|
|
|
|
|
this.logger(
|
2025-08-01 11:02:01 -07:00
|
|
|
'error',
|
|
|
|
|
'Failed to load Universal Sentence Encoder model:',
|
2025-07-28 16:00:05 -07:00
|
|
|
modelError
|
|
|
|
|
)
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
throw new Error(
|
|
|
|
|
`Universal Sentence Encoder model loading failed: ${modelError}`
|
|
|
|
|
)
|
2025-07-28 16:00:05 -07:00
|
|
|
}
|
2025-06-24 11:41:30 -07:00
|
|
|
|
|
|
|
|
// Restore original console.warn
|
|
|
|
|
console.warn = originalWarn
|
|
|
|
|
} catch (error) {
|
2025-07-17 10:00:28 -07:00
|
|
|
this.logger(
|
2025-07-16 13:51:00 -07:00
|
|
|
'error',
|
|
|
|
|
'Failed to initialize Universal Sentence Encoder:',
|
|
|
|
|
error
|
|
|
|
|
)
|
2025-08-01 11:02:01 -07:00
|
|
|
// No fallback allowed - throw error
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
throw new Error(
|
|
|
|
|
`Universal Sentence Encoder initialization failed: ${error}`
|
|
|
|
|
)
|
2025-06-24 11:41:30 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Embed text into a vector using Universal Sentence Encoder
|
|
|
|
|
* @param data Text to embed
|
|
|
|
|
*/
|
|
|
|
|
public async embed(data: string | string[]): Promise<Vector> {
|
|
|
|
|
if (!this.initialized) {
|
|
|
|
|
await this.init()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// Handle different input types
|
|
|
|
|
let textToEmbed: string[]
|
|
|
|
|
if (typeof data === 'string') {
|
|
|
|
|
// Handle empty string case
|
|
|
|
|
if (data.trim() === '') {
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
// Return a zero vector of 512 dimensions (standard for Universal Sentence Encoder)
|
2025-06-24 11:41:30 -07:00
|
|
|
return new Array(512).fill(0)
|
|
|
|
|
}
|
|
|
|
|
textToEmbed = [data]
|
|
|
|
|
} else if (
|
|
|
|
|
Array.isArray(data) &&
|
|
|
|
|
data.every((item) => typeof item === 'string')
|
|
|
|
|
) {
|
|
|
|
|
// Handle empty array or array with empty strings
|
|
|
|
|
if (data.length === 0 || data.every((item) => item.trim() === '')) {
|
|
|
|
|
return new Array(512).fill(0)
|
|
|
|
|
}
|
|
|
|
|
// Filter out empty strings
|
|
|
|
|
textToEmbed = data.filter((item) => item.trim() !== '')
|
|
|
|
|
if (textToEmbed.length === 0) {
|
|
|
|
|
return new Array(512).fill(0)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(
|
|
|
|
|
'UniversalSentenceEncoder only supports string or string[] data'
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
// Ensure the model is available
|
2025-07-28 16:00:05 -07:00
|
|
|
if (!this.model) {
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
throw new Error('Universal Sentence Encoder model is not available')
|
2025-07-28 16:00:05 -07:00
|
|
|
}
|
|
|
|
|
|
2025-06-24 11:41:30 -07:00
|
|
|
// Get embeddings
|
|
|
|
|
const embeddings = await this.model.embed(textToEmbed)
|
|
|
|
|
|
|
|
|
|
// Convert to array and return the first embedding
|
|
|
|
|
const embeddingArray = await embeddings.array()
|
2025-06-27 14:06:59 -07:00
|
|
|
|
|
|
|
|
// Dispose of the tensor to free memory
|
|
|
|
|
embeddings.dispose()
|
|
|
|
|
|
2025-07-28 16:00:05 -07:00
|
|
|
// Get the first embedding
|
|
|
|
|
let embedding = embeddingArray[0]
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
|
|
|
|
|
// Always ensure the embedding is exactly 512 dimensions
|
2025-07-28 16:00:05 -07:00
|
|
|
if (embedding.length !== 512) {
|
|
|
|
|
this.logger(
|
|
|
|
|
'warn',
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
`Embedding dimension mismatch: expected 512, got ${embedding.length}. Standardizing to 512 dimensions.`
|
2025-07-28 16:00:05 -07:00
|
|
|
)
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
|
2025-07-28 16:00:05 -07:00
|
|
|
// If the embedding is too short, pad with zeros
|
|
|
|
|
if (embedding.length < 512) {
|
|
|
|
|
const paddedEmbedding = new Array(512).fill(0)
|
|
|
|
|
for (let i = 0; i < embedding.length; i++) {
|
|
|
|
|
paddedEmbedding[i] = embedding[i]
|
|
|
|
|
}
|
|
|
|
|
embedding = paddedEmbedding
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
}
|
2025-07-28 16:00:05 -07:00
|
|
|
// If the embedding is too long, truncate
|
|
|
|
|
else if (embedding.length > 512) {
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
embedding = embedding.slice(0, 512)
|
2025-07-28 16:00:05 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return embedding
|
2025-06-24 11:41:30 -07:00
|
|
|
} catch (error) {
|
2025-07-17 10:00:28 -07:00
|
|
|
this.logger(
|
2025-08-01 11:02:01 -07:00
|
|
|
'error',
|
|
|
|
|
'Failed to embed text with Universal Sentence Encoder:',
|
2025-06-24 11:41:30 -07:00
|
|
|
error
|
|
|
|
|
)
|
2025-08-01 11:02:01 -07:00
|
|
|
throw new Error(`Universal Sentence Encoder embedding failed: ${error}`)
|
2025-06-24 11:41:30 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-27 14:06:59 -07:00
|
|
|
/**
|
|
|
|
|
* Embed multiple texts into vectors using Universal Sentence Encoder
|
|
|
|
|
* This is more efficient than calling embed() multiple times
|
|
|
|
|
* @param dataArray Array of texts to embed
|
|
|
|
|
* @returns Array of embedding vectors
|
|
|
|
|
*/
|
|
|
|
|
public async embedBatch(dataArray: string[]): Promise<Vector[]> {
|
|
|
|
|
if (!this.initialized) {
|
|
|
|
|
await this.init()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// Handle empty array case
|
|
|
|
|
if (dataArray.length === 0) {
|
|
|
|
|
return []
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Filter out empty strings and handle edge cases
|
|
|
|
|
const textToEmbed = dataArray.filter(
|
|
|
|
|
(text: string) => typeof text === 'string' && text.trim() !== ''
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// If all strings were empty, return appropriate zero vectors
|
|
|
|
|
if (textToEmbed.length === 0) {
|
|
|
|
|
return dataArray.map(() => new Array(512).fill(0))
|
|
|
|
|
}
|
|
|
|
|
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
// Ensure the model is available
|
2025-07-28 16:00:05 -07:00
|
|
|
if (!this.model) {
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
throw new Error('Universal Sentence Encoder model is not available')
|
2025-07-28 16:00:05 -07:00
|
|
|
}
|
|
|
|
|
|
2025-06-27 14:06:59 -07:00
|
|
|
// Get embeddings for all texts in a single batch operation
|
|
|
|
|
const embeddings = await this.model.embed(textToEmbed)
|
|
|
|
|
|
|
|
|
|
// Convert to array
|
|
|
|
|
const embeddingArray = await embeddings.array()
|
|
|
|
|
|
|
|
|
|
// Dispose of the tensor to free memory
|
|
|
|
|
embeddings.dispose()
|
|
|
|
|
|
2025-07-28 16:00:05 -07:00
|
|
|
// Standardize embeddings to ensure they're all 512 dimensions
|
|
|
|
|
const standardizedEmbeddings = embeddingArray.map((embedding: Vector) => {
|
|
|
|
|
if (embedding.length !== 512) {
|
|
|
|
|
this.logger(
|
|
|
|
|
'warn',
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
`Batch embedding dimension mismatch: expected 512, got ${embedding.length}. Standardizing to 512 dimensions.`
|
2025-07-28 16:00:05 -07:00
|
|
|
)
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
|
2025-07-28 16:00:05 -07:00
|
|
|
// If the embedding is too short, pad with zeros
|
|
|
|
|
if (embedding.length < 512) {
|
|
|
|
|
const paddedEmbedding = new Array(512).fill(0)
|
|
|
|
|
for (let i = 0; i < embedding.length; i++) {
|
|
|
|
|
paddedEmbedding[i] = embedding[i]
|
|
|
|
|
}
|
|
|
|
|
return paddedEmbedding
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
}
|
2025-07-28 16:00:05 -07:00
|
|
|
// If the embedding is too long, truncate
|
|
|
|
|
else if (embedding.length > 512) {
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
return embedding.slice(0, 512)
|
2025-07-28 16:00:05 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return embedding
|
|
|
|
|
})
|
|
|
|
|
|
2025-06-27 14:06:59 -07:00
|
|
|
// Map the results back to the original array order
|
|
|
|
|
const results: Vector[] = []
|
|
|
|
|
let embeddingIndex = 0
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < dataArray.length; i++) {
|
|
|
|
|
const text = dataArray[i]
|
|
|
|
|
if (typeof text === 'string' && text.trim() !== '') {
|
2025-07-28 16:00:05 -07:00
|
|
|
// Use the standardized embedding for non-empty strings
|
|
|
|
|
results.push(standardizedEmbeddings[embeddingIndex])
|
2025-06-27 14:06:59 -07:00
|
|
|
embeddingIndex++
|
|
|
|
|
} else {
|
|
|
|
|
// Use a zero vector for empty strings
|
|
|
|
|
results.push(new Array(512).fill(0))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return results
|
|
|
|
|
} catch (error) {
|
2025-07-17 10:00:28 -07:00
|
|
|
this.logger(
|
2025-08-01 11:02:01 -07:00
|
|
|
'error',
|
|
|
|
|
'Failed to batch embed text with Universal Sentence Encoder:',
|
2025-06-27 14:06:59 -07:00
|
|
|
error
|
|
|
|
|
)
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
throw new Error(
|
|
|
|
|
`Universal Sentence Encoder batch embedding failed: ${error}`
|
|
|
|
|
)
|
2025-06-27 14:06:59 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-24 11:41:30 -07:00
|
|
|
/**
|
|
|
|
|
* Dispose of the model resources
|
|
|
|
|
*/
|
|
|
|
|
public async dispose(): Promise<void> {
|
|
|
|
|
if (this.model && this.tf) {
|
|
|
|
|
try {
|
|
|
|
|
// Dispose of the model and tensors
|
|
|
|
|
this.model.dispose()
|
|
|
|
|
this.tf.disposeVariables()
|
|
|
|
|
this.initialized = false
|
|
|
|
|
} catch (error) {
|
2025-07-17 10:00:28 -07:00
|
|
|
this.logger(
|
2025-07-16 13:51:00 -07:00
|
|
|
'error',
|
|
|
|
|
'Failed to dispose Universal Sentence Encoder:',
|
|
|
|
|
error
|
|
|
|
|
)
|
2025-06-24 11:41:30 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Promise.resolve()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-27 14:06:59 -07:00
|
|
|
/**
|
2025-08-05 16:09:30 -07:00
|
|
|
* Helper function - NO LONGER USED
|
|
|
|
|
* Kept for compatibility but will be removed in next major version
|
|
|
|
|
* @deprecated Since we removed @tensorflow-models/universal-sentence-encoder dependency
|
2025-06-27 14:06:59 -07:00
|
|
|
*/
|
2025-06-30 11:00:36 -07:00
|
|
|
function findUSELoadFunction(
|
|
|
|
|
sentenceEncoderModule: any
|
|
|
|
|
): (() => Promise<EmbeddingModel>) | null {
|
2025-07-17 10:12:15 -07:00
|
|
|
// Module structure available for debugging if needed
|
2025-06-27 14:06:59 -07:00
|
|
|
|
2025-07-28 16:00:05 -07:00
|
|
|
// Find the appropriate load function from the module
|
2025-06-27 14:06:59 -07:00
|
|
|
let loadFunction = null
|
|
|
|
|
|
|
|
|
|
// Try sentenceEncoderModule.load first (direct export)
|
|
|
|
|
if (
|
|
|
|
|
sentenceEncoderModule.load &&
|
|
|
|
|
typeof sentenceEncoderModule.load === 'function'
|
|
|
|
|
) {
|
|
|
|
|
loadFunction = sentenceEncoderModule.load
|
|
|
|
|
}
|
|
|
|
|
// Then try sentenceEncoderModule.default.load (default export)
|
|
|
|
|
else if (
|
|
|
|
|
sentenceEncoderModule.default &&
|
|
|
|
|
sentenceEncoderModule.default.load &&
|
|
|
|
|
typeof sentenceEncoderModule.default.load === 'function'
|
|
|
|
|
) {
|
|
|
|
|
loadFunction = sentenceEncoderModule.default.load
|
|
|
|
|
}
|
|
|
|
|
// Try sentenceEncoderModule.default directly if it's a function
|
|
|
|
|
else if (
|
|
|
|
|
sentenceEncoderModule.default &&
|
|
|
|
|
typeof sentenceEncoderModule.default === 'function'
|
|
|
|
|
) {
|
|
|
|
|
loadFunction = sentenceEncoderModule.default
|
|
|
|
|
}
|
|
|
|
|
// Try sentenceEncoderModule directly if it's a function
|
|
|
|
|
else if (typeof sentenceEncoderModule === 'function') {
|
|
|
|
|
loadFunction = sentenceEncoderModule
|
|
|
|
|
}
|
|
|
|
|
// Try additional common patterns
|
|
|
|
|
else if (
|
|
|
|
|
sentenceEncoderModule.UniversalSentenceEncoder &&
|
|
|
|
|
typeof sentenceEncoderModule.UniversalSentenceEncoder.load === 'function'
|
|
|
|
|
) {
|
|
|
|
|
loadFunction = sentenceEncoderModule.UniversalSentenceEncoder.load
|
|
|
|
|
} else if (
|
|
|
|
|
sentenceEncoderModule.default &&
|
|
|
|
|
sentenceEncoderModule.default.UniversalSentenceEncoder &&
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
typeof sentenceEncoderModule.default.UniversalSentenceEncoder.load ===
|
|
|
|
|
'function'
|
2025-06-27 14:06:59 -07:00
|
|
|
) {
|
|
|
|
|
loadFunction = sentenceEncoderModule.default.UniversalSentenceEncoder.load
|
|
|
|
|
}
|
|
|
|
|
// Try to find the load function in the module's properties
|
|
|
|
|
else {
|
|
|
|
|
// Look for any property that might be a load function
|
|
|
|
|
for (const key in sentenceEncoderModule) {
|
|
|
|
|
if (typeof sentenceEncoderModule[key] === 'function') {
|
|
|
|
|
// Check if the function name or key contains 'load'
|
2025-06-30 09:39:16 -07:00
|
|
|
const fnName = sentenceEncoderModule[key].name || key
|
2025-06-27 14:06:59 -07:00
|
|
|
if (fnName.toLowerCase().includes('load')) {
|
2025-06-30 09:39:16 -07:00
|
|
|
loadFunction = sentenceEncoderModule[key]
|
|
|
|
|
break
|
2025-06-27 14:06:59 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Also check nested objects
|
2025-06-30 09:39:16 -07:00
|
|
|
else if (
|
|
|
|
|
typeof sentenceEncoderModule[key] === 'object' &&
|
|
|
|
|
sentenceEncoderModule[key] !== null
|
|
|
|
|
) {
|
2025-06-27 14:06:59 -07:00
|
|
|
for (const nestedKey in sentenceEncoderModule[key]) {
|
|
|
|
|
if (typeof sentenceEncoderModule[key][nestedKey] === 'function') {
|
2025-06-30 09:39:16 -07:00
|
|
|
const fnName =
|
|
|
|
|
sentenceEncoderModule[key][nestedKey].name || nestedKey
|
2025-06-27 14:06:59 -07:00
|
|
|
if (fnName.toLowerCase().includes('load')) {
|
2025-06-30 09:39:16 -07:00
|
|
|
loadFunction = sentenceEncoderModule[key][nestedKey]
|
|
|
|
|
break
|
2025-06-27 14:06:59 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-30 09:39:16 -07:00
|
|
|
if (loadFunction) break
|
2025-06-27 14:06:59 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-28 16:00:05 -07:00
|
|
|
// Return a function that calls the load function without arguments
|
|
|
|
|
// This will use the bundled model from the package
|
|
|
|
|
if (loadFunction) {
|
|
|
|
|
return async () => await loadFunction()
|
|
|
|
|
}
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
|
2025-07-28 16:00:05 -07:00
|
|
|
return null
|
2025-06-27 14:06:59 -07:00
|
|
|
}
|
|
|
|
|
|
2025-07-16 13:51:00 -07:00
|
|
|
/**
|
|
|
|
|
* Check if we're running in a test environment (standalone version)
|
2025-07-28 10:04:45 -07:00
|
|
|
* Uses the same logic as the class method to avoid duplication
|
2025-07-16 13:51:00 -07:00
|
|
|
*/
|
|
|
|
|
function isTestEnvironment(): boolean {
|
2025-07-28 10:04:45 -07:00
|
|
|
// Use the same implementation as the class method
|
2025-07-16 13:51:00 -07:00
|
|
|
// Safely check for Node.js environment first
|
|
|
|
|
if (typeof process === 'undefined') {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2025-07-17 10:00:28 -07:00
|
|
|
|
2025-07-16 13:51:00 -07:00
|
|
|
return (
|
|
|
|
|
process.env.NODE_ENV === 'test' ||
|
|
|
|
|
process.env.VITEST === 'true' ||
|
|
|
|
|
(typeof global !== 'undefined' && global.__vitest__) ||
|
|
|
|
|
process.argv.some((arg) => arg.includes('vitest'))
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-07-18 10:40:37 -07:00
|
|
|
* Log message only if not in test environment and verbose mode is enabled (standalone version)
|
|
|
|
|
* @param level Log level ('log', 'warn', 'error')
|
|
|
|
|
* @param message Message to log
|
|
|
|
|
* @param args Additional arguments to log
|
|
|
|
|
* @param verbose Whether to log non-essential messages (default: true)
|
2025-07-16 13:51:00 -07:00
|
|
|
*/
|
|
|
|
|
function logIfNotTest(
|
|
|
|
|
level: 'log' | 'warn' | 'error',
|
|
|
|
|
message: string,
|
2025-07-18 10:40:37 -07:00
|
|
|
args: any[] = [],
|
|
|
|
|
verbose: boolean = true
|
2025-07-16 13:51:00 -07:00
|
|
|
): void {
|
2025-07-18 10:40:37 -07:00
|
|
|
// Always log errors, but only log other messages if verbose mode is enabled
|
|
|
|
|
if ((level === 'error' || verbose) && !isTestEnvironment()) {
|
2025-07-16 13:51:00 -07:00
|
|
|
console[level](message, ...args)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-24 11:41:30 -07:00
|
|
|
/**
|
|
|
|
|
* Create an embedding function from an embedding model
|
2025-07-16 13:51:00 -07:00
|
|
|
* @param model Embedding model to use (optional, defaults to UniversalSentenceEncoder)
|
2025-06-24 11:41:30 -07:00
|
|
|
*/
|
|
|
|
|
export function createEmbeddingFunction(
|
2025-07-16 13:51:00 -07:00
|
|
|
model?: EmbeddingModel
|
2025-06-24 11:41:30 -07:00
|
|
|
): EmbeddingFunction {
|
2025-07-16 13:51:00 -07:00
|
|
|
// If no model is provided, use the default TensorFlow embedding function
|
|
|
|
|
if (!model) {
|
|
|
|
|
return createTensorFlowEmbeddingFunction()
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-24 11:41:30 -07:00
|
|
|
return async (data: any): Promise<Vector> => {
|
|
|
|
|
return await model.embed(data)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a TensorFlow-based Universal Sentence Encoder embedding function
|
|
|
|
|
* This is the required embedding function for all text embeddings
|
2025-06-27 14:06:59 -07:00
|
|
|
* Uses a shared model instance for better performance across multiple calls
|
2025-07-18 10:40:37 -07:00
|
|
|
* @param options Configuration options
|
|
|
|
|
* @param options.verbose Whether to log non-essential messages (default: true)
|
2025-06-24 11:41:30 -07:00
|
|
|
*/
|
2025-06-27 14:06:59 -07:00
|
|
|
// Create a single shared instance of the model that persists across all embedding calls
|
2025-07-18 10:40:37 -07:00
|
|
|
let sharedModel: UniversalSentenceEncoder | null = null
|
2025-06-27 14:06:59 -07:00
|
|
|
let sharedModelInitialized = false
|
2025-07-18 10:40:37 -07:00
|
|
|
let sharedModelVerbose = true
|
2025-06-24 11:41:30 -07:00
|
|
|
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
export function createTensorFlowEmbeddingFunction(
|
|
|
|
|
options: { verbose?: boolean } = {}
|
|
|
|
|
): EmbeddingFunction {
|
2025-07-18 10:40:37 -07:00
|
|
|
// Update verbose setting if provided
|
|
|
|
|
if (options.verbose !== undefined) {
|
|
|
|
|
sharedModelVerbose = options.verbose
|
|
|
|
|
}
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
|
2025-07-18 10:40:37 -07:00
|
|
|
// Create the shared model if it doesn't exist yet
|
|
|
|
|
if (!sharedModel) {
|
|
|
|
|
sharedModel = new UniversalSentenceEncoder({ verbose: sharedModelVerbose })
|
|
|
|
|
}
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
|
2025-06-24 11:41:30 -07:00
|
|
|
return async (data: any): Promise<Vector> => {
|
|
|
|
|
try {
|
|
|
|
|
// Initialize the model if it hasn't been initialized yet
|
2025-06-27 14:06:59 -07:00
|
|
|
if (!sharedModelInitialized) {
|
2025-07-16 13:51:00 -07:00
|
|
|
try {
|
2025-07-18 10:40:37 -07:00
|
|
|
await sharedModel!.init()
|
2025-07-16 13:51:00 -07:00
|
|
|
sharedModelInitialized = true
|
|
|
|
|
} catch (initError) {
|
|
|
|
|
// Reset the flag so we can retry initialization on the next call
|
|
|
|
|
sharedModelInitialized = false
|
|
|
|
|
throw initError
|
|
|
|
|
}
|
2025-06-24 11:41:30 -07:00
|
|
|
}
|
|
|
|
|
|
2025-07-18 10:40:37 -07:00
|
|
|
return await sharedModel!.embed(data)
|
2025-06-24 11:41:30 -07:00
|
|
|
} catch (error) {
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
logIfNotTest(
|
|
|
|
|
'error',
|
|
|
|
|
'Failed to use Universal Sentence Encoder:',
|
|
|
|
|
[error],
|
|
|
|
|
sharedModelVerbose
|
|
|
|
|
)
|
2025-08-01 11:02:01 -07:00
|
|
|
// No fallback - Universal Sentence Encoder is required
|
2025-06-24 11:41:30 -07:00
|
|
|
throw new Error(
|
2025-08-01 11:02:01 -07:00
|
|
|
`Universal Sentence Encoder is required and no fallbacks are allowed: ${error}`
|
2025-06-24 11:41:30 -07:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-06-27 14:06:59 -07:00
|
|
|
* Default embedding function
|
|
|
|
|
* Uses UniversalSentenceEncoder for all text embeddings
|
|
|
|
|
* TensorFlow.js is required for this to work
|
|
|
|
|
* Uses CPU for compatibility
|
2025-07-18 10:40:37 -07:00
|
|
|
* @param options Configuration options
|
|
|
|
|
* @param options.verbose Whether to log non-essential messages (default: true)
|
2025-06-24 11:41:30 -07:00
|
|
|
*/
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
export function getDefaultEmbeddingFunction(
|
|
|
|
|
options: { verbose?: boolean } = {}
|
|
|
|
|
): EmbeddingFunction {
|
2025-07-18 10:40:37 -07:00
|
|
|
return createTensorFlowEmbeddingFunction(options)
|
|
|
|
|
}
|
2025-06-24 11:41:30 -07:00
|
|
|
|
2025-06-27 14:06:59 -07:00
|
|
|
/**
|
2025-07-18 10:40:37 -07:00
|
|
|
* Default embedding function with default options
|
2025-06-27 14:06:59 -07:00
|
|
|
* Uses UniversalSentenceEncoder for all text embeddings
|
|
|
|
|
* TensorFlow.js is required for this to work
|
2025-07-18 10:40:37 -07:00
|
|
|
* Uses CPU for compatibility
|
|
|
|
|
*/
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
export const defaultEmbeddingFunction: EmbeddingFunction =
|
|
|
|
|
getDefaultEmbeddingFunction()
|
2025-07-18 10:40:37 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a batch embedding function that uses UniversalSentenceEncoder
|
|
|
|
|
* TensorFlow.js is required for this to work
|
2025-06-27 14:06:59 -07:00
|
|
|
* Processes all items in a single batch operation
|
|
|
|
|
* Uses a shared model instance for better performance across multiple calls
|
2025-07-18 10:40:37 -07:00
|
|
|
* @param options Configuration options
|
|
|
|
|
* @param options.verbose Whether to log non-essential messages (default: true)
|
2025-06-27 14:06:59 -07:00
|
|
|
*/
|
|
|
|
|
// Create a single shared instance of the model that persists across function calls
|
2025-07-18 10:40:37 -07:00
|
|
|
let sharedBatchModel: UniversalSentenceEncoder | null = null
|
2025-06-27 14:06:59 -07:00
|
|
|
let sharedBatchModelInitialized = false
|
2025-07-18 10:40:37 -07:00
|
|
|
let sharedBatchModelVerbose = true
|
2025-06-27 14:06:59 -07:00
|
|
|
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
export function createBatchEmbeddingFunction(
|
|
|
|
|
options: { verbose?: boolean } = {}
|
|
|
|
|
): (dataArray: string[]) => Promise<Vector[]> {
|
2025-07-18 10:40:37 -07:00
|
|
|
// Update verbose setting if provided
|
|
|
|
|
if (options.verbose !== undefined) {
|
|
|
|
|
sharedBatchModelVerbose = options.verbose
|
|
|
|
|
}
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
|
2025-07-18 10:40:37 -07:00
|
|
|
// Create the shared model if it doesn't exist yet
|
|
|
|
|
if (!sharedBatchModel) {
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
sharedBatchModel = new UniversalSentenceEncoder({
|
|
|
|
|
verbose: sharedBatchModelVerbose
|
|
|
|
|
})
|
2025-07-18 10:40:37 -07:00
|
|
|
}
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
|
2025-07-18 10:40:37 -07:00
|
|
|
return async (dataArray: string[]): Promise<Vector[]> => {
|
|
|
|
|
try {
|
|
|
|
|
// Initialize the model if it hasn't been initialized yet
|
|
|
|
|
if (!sharedBatchModelInitialized) {
|
2025-08-01 11:02:01 -07:00
|
|
|
try {
|
|
|
|
|
await sharedBatchModel!.init()
|
|
|
|
|
sharedBatchModelInitialized = true
|
|
|
|
|
} catch (initError) {
|
|
|
|
|
// Reset the flag so we can retry initialization on the next call
|
|
|
|
|
sharedBatchModelInitialized = false
|
|
|
|
|
throw initError
|
|
|
|
|
}
|
2025-07-18 10:40:37 -07:00
|
|
|
}
|
2025-06-24 11:41:30 -07:00
|
|
|
|
2025-07-18 10:40:37 -07:00
|
|
|
return await sharedBatchModel!.embedBatch(dataArray)
|
|
|
|
|
} catch (error) {
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
logIfNotTest(
|
|
|
|
|
'error',
|
|
|
|
|
'Failed to use Universal Sentence Encoder batch embedding:',
|
|
|
|
|
[error],
|
|
|
|
|
sharedBatchModelVerbose
|
|
|
|
|
)
|
2025-08-01 11:02:01 -07:00
|
|
|
// No fallback - Universal Sentence Encoder is required
|
2025-07-18 10:40:37 -07:00
|
|
|
throw new Error(
|
2025-08-01 11:02:01 -07:00
|
|
|
`Universal Sentence Encoder is required for batch embedding and no fallbacks are allowed: ${error}`
|
2025-07-18 10:40:37 -07:00
|
|
|
)
|
|
|
|
|
}
|
2025-06-24 11:41:30 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-18 10:40:37 -07:00
|
|
|
/**
|
|
|
|
|
* Get a batch embedding function with custom options
|
|
|
|
|
* Uses UniversalSentenceEncoder for all text embeddings
|
|
|
|
|
* TensorFlow.js is required for this to work
|
|
|
|
|
* Processes all items in a single batch operation
|
|
|
|
|
* @param options Configuration options
|
|
|
|
|
* @param options.verbose Whether to log non-essential messages (default: true)
|
|
|
|
|
*/
|
**feat(docs): add comprehensive documentation for model bundling and robust loading**
- Introduced new documentation files under `docs/`:
- `model-bundling-analysis.md`: Provides detailed analysis of current, bundled, hybrid, and dynamic model loading approaches, including pros, cons, and recommendations.
- `model-management.md`: Explains how Brainy manages Universal Sentence Encoder models, including setup, usage, and troubleshooting.
- `optional-model-bundling.md`: Details the `@soulcraft/brainy-models` package for offline reliability with pre-bundled models.
- Added `src/utils/robustModelLoader.ts`:
- Implements enhanced model loading with retry mechanisms, timeout handling, fallback URLs, and optional local model bundling.
- Supports Node.js and browser environments with exponential backoff logic.
- Key Updates:
- **Hybrid Loading Strategy**: Recommended for balancing reliability and flexibility via hybrid online/offline mechanisms.
- **Enhanced Fallback Scenarios**: Robust loader improves network-dependent reliability for embedding workflows.
- **Offline Reliability Support**: Optional model bundling eliminates dependency on external services, supporting air-gapped and edge environments.
**Purpose**: Introduce a hybrid model loading approach with robust options for
2025-08-01 15:35:29 -07:00
|
|
|
export function getDefaultBatchEmbeddingFunction(
|
|
|
|
|
options: { verbose?: boolean } = {}
|
|
|
|
|
): (dataArray: string[]) => Promise<Vector[]> {
|
2025-07-18 10:40:37 -07:00
|
|
|
return createBatchEmbeddingFunction(options)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Default batch embedding function with default options
|
|
|
|
|
* Uses UniversalSentenceEncoder for all text embeddings
|
|
|
|
|
* TensorFlow.js is required for this to work
|
|
|
|
|
* Processes all items in a single batch operation
|
|
|
|
|
*/
|
|
|
|
|
export const defaultBatchEmbeddingFunction = getDefaultBatchEmbeddingFunction()
|
|
|
|
|
|
2025-06-24 11:41:30 -07:00
|
|
|
/**
|
2025-06-27 14:06:59 -07:00
|
|
|
* Creates an embedding function that runs in a separate thread
|
|
|
|
|
* This is a wrapper around createEmbeddingFunction that uses executeInThread
|
|
|
|
|
* @param model Embedding model to use
|
2025-06-24 11:41:30 -07:00
|
|
|
*/
|
2025-06-27 14:06:59 -07:00
|
|
|
export function createThreadedEmbeddingFunction(
|
|
|
|
|
model: EmbeddingModel
|
|
|
|
|
): EmbeddingFunction {
|
|
|
|
|
const embeddingFunction = createEmbeddingFunction(model)
|
|
|
|
|
|
|
|
|
|
return async (data: any): Promise<Vector> => {
|
|
|
|
|
// Convert the embedding function to a string
|
|
|
|
|
const fnString = embeddingFunction.toString()
|
|
|
|
|
|
|
|
|
|
// Execute the embedding function in a "thread" (main thread in this implementation)
|
|
|
|
|
return await executeInThread<Vector>(fnString, data)
|
|
|
|
|
}
|
|
|
|
|
}
|