- 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
5.3 KiB
Model Management Guide
This guide explains how to manage the TensorFlow Universal Sentence Encoder model used by Brainy for text embeddings.
Overview
Brainy uses the Universal Sentence Encoder model from TensorFlow.js to convert text into vector embeddings. These embeddings are essential for semantic search and other features.
The model is referenced through local configuration files that point to the TensorFlow Hub URL. This approach ensures that:
- The model is automatically downloaded when first needed
- Subsequent uses leverage the cached model
- The application works consistently across all environments (browser, Node.js, serverless, workers)
Model Files
The model files are stored in the models/sentence-encoder/ directory and include:
model.json(~1KB) - The main model configuration file that references the TensorFlow Hub URLgroup1-shard1of1.bin(~2KB) - A sample embedding file (not the full model weights)metadata.json(~0.4KB) - Additional information about the model
Important Note: These are small reference files (total ~3KB), not the full model (~25MB). The full model is downloaded automatically when first needed and then cached locally. This approach keeps the repository size small while ensuring the model is available when needed.
These files should be checked into version control to ensure they're available in all environments.
Setting Up Model Reference Files
The model reference files are generated using the download-model.js script:
node scripts/download-model.js
Important: This script does NOT download the full model (~25MB) locally. Instead, it creates small reference files (~3KB total) that point to the TensorFlow Hub URL. The full model will be downloaded automatically when your application first uses it, and then cached for future use.
When to Run the Script
You should run this script in the following situations:
- Initial Setup: When first setting up the project
- Model Updates: When updating to a new version of the Universal Sentence Encoder
- Missing Files: If the model reference files are missing or corrupted
The script will:
- Load the model from TensorFlow Hub to verify it works (temporarily downloading it to memory)
- Create a model.json file that references the TensorFlow Hub URL
- Generate a sample embedding and save it as a small binary file
- Create a metadata file with information about the model
What to Expect
After running the script:
- You'll see small files in the
models/sentence-encoder/directory (total ~3KB) - When your application first uses the model, it will download the full model (~25MB) from TensorFlow Hub
- The downloaded model will be cached locally for subsequent use
- No further downloads will be needed unless the cache is cleared
Using the Model
The model is automatically loaded by the UniversalSentenceEncoder class in src/utils/embedding.ts. The loading
process follows these steps:
- Check if local model reference files exist
- If they exist, load the model using the TensorFlow Hub URL referenced in the model.json file
- The first time this happens, the full model will be downloaded and cached
- Subsequent uses will use the cached model
Environments
The model loading works across all environments:
- Node.js: Uses file system paths to load the model
- Browser: Uses relative URLs to load the model
- Serverless/Workers: Uses the embedded model files
Best Practices
- Always check in model files: The model files should be committed to version control
- Run tests after model updates: Use
node test-model-loading.jsto verify the model works - Update model during build: If you prefer not to check in model files, run the download script as part of your build process
Troubleshooting
Common Issues
"The model files are too small (only a few KB)"
This is expected behavior. The script creates small reference files (~3KB total), not the full model (~25MB). The full model will be downloaded automatically when your application first uses it.
"Model not found or loading errors"
- Verify the model reference files exist in
models/sentence-encoder/ - Run
node scripts/download-model.jsto generate fresh reference files - Check for errors in the console related to model loading
- Ensure the model reference files are properly included in your deployment package
- Make sure your application has internet access the first time it runs to download the full model
"Model download is slow"
The first time your application uses the model, it will download the full model (~25MB) from TensorFlow Hub. This may take a few minutes depending on your internet connection. Subsequent uses will use the cached model and will be much faster.
Technical Details
The Universal Sentence Encoder model:
- Produces 512-dimensional embeddings
- Works with text in any language
- Is optimized for semantic similarity tasks
- Has a size of approximately 25MB when fully downloaded
- Is downloaded from TensorFlow Hub on first use and then cached
Our approach of referencing the model via TensorFlow Hub URL provides several benefits:
- Small package size (only reference files are included, not the full model)
- Automatic caching for improved performance after first use
- Consistent behavior across all environments
- Always uses the correct model weights