2026-01-06 12:52:34 -08:00
# Model Loading Guide
2025-08-26 12:32:21 -07:00
2026-01-06 12:52:34 -08:00
Brainy uses AI embedding models to understand and process your data. With the Candle WASM engine, the model is **embedded at compile time** - no downloads, no configuration, no external dependencies.
2025-08-26 12:32:21 -07:00
2026-01-06 12:52:34 -08:00
## Zero Configuration (Default)
2025-08-26 12:32:21 -07:00
2026-01-06 12:52:34 -08:00
**For all developers, no configuration is needed:**
2025-08-26 12:32:21 -07:00
```typescript
2025-09-30 17:09:15 -07:00
const brain = new Brainy()
2026-01-06 12:52:34 -08:00
await brain.init() // Model is already embedded - nothing to download!
2025-08-26 12:32:21 -07:00
```
**What happens automatically:**
2026-01-06 12:52:34 -08:00
1. Candle WASM module loads (~90MB, includes model weights)
2. Model initializes in ~200ms
3. Ready to use immediately
2025-08-26 12:32:21 -07:00
2026-01-06 12:52:34 -08:00
**No downloads. No CDN. No configuration. Just works.**
2025-08-26 12:32:21 -07:00
2026-01-06 12:52:34 -08:00
## How It Works
The all-MiniLM-L6-v2 model is embedded in the WASM binary using Rust's `include_bytes!` macro:
2025-08-26 12:32:21 -07:00
```
2026-01-06 12:52:34 -08:00
candle_embeddings_bg.wasm (~90MB)
├── Candle ML Runtime (~3MB)
├── Model Weights (safetensors format, ~87MB)
└── Tokenizer (HuggingFace tokenizers, ~450KB)
2025-08-26 12:32:21 -07:00
```
2026-01-06 12:52:34 -08:00
This single WASM file contains everything needed for sentence embeddings.
2025-08-26 12:32:21 -07:00
2026-01-06 12:52:34 -08:00
## Environments
2025-08-26 12:32:21 -07:00
2026-01-06 12:52:34 -08:00
### Bun (Recommended)
2025-08-26 12:32:21 -07:00
2026-07-01 09:16:46 -07:00
```bash
# Bun as a runtime — supported and recommended
bun add @soulcraft/brainy
2026-01-06 12:52:34 -08:00
bun run server.ts
2025-08-26 12:32:21 -07:00
```
2026-07-01 09:16:46 -07:00
Brainy is pure WebAssembly with no native binaries, so the module graph stays
bundler-friendly. Single-binary `bun build --compile` is **not a supported
target** at present: Bun 1.3.10 has a `--compile` codegen regression
(`__promiseAll is not defined` ) triggered by top-level `await` in the bundled
graph. Run Brainy under the Bun runtime (above) instead.
2026-01-06 12:52:34 -08:00
### Node.js
2025-08-26 12:32:21 -07:00
2026-01-06 12:52:34 -08:00
```typescript
// Standard Node.js
node dist/server.js
2025-08-26 12:32:21 -07:00
2026-01-06 12:52:34 -08:00
// Runs identically to Bun
2025-08-26 12:32:21 -07:00
```
2026-01-06 12:52:34 -08:00
### Browser
2025-08-26 12:32:21 -07:00
```typescript
2026-01-06 12:52:34 -08:00
// Model loads via WASM (single file, no additional assets)
const brain = new Brainy()
await brain.init()
2025-08-26 12:32:21 -07:00
```
2026-01-06 12:52:34 -08:00
### Docker/Kubernetes
2025-08-26 12:32:21 -07:00
```dockerfile
2026-01-06 12:52:34 -08:00
FROM oven/bun:1.1
2025-08-26 12:32:21 -07:00
WORKDIR /app
COPY package*.json ./
2026-01-06 12:52:34 -08:00
RUN bun install
2025-08-26 12:32:21 -07:00
COPY . .
EXPOSE 3000
2026-01-06 12:52:34 -08:00
CMD ["bun", "run", "server.ts"]
2025-08-26 12:32:21 -07:00
2026-01-06 12:52:34 -08:00
# That's it! No model download step needed.
# Model is embedded in the npm package.
2025-08-26 12:32:21 -07:00
```
2026-01-06 12:52:34 -08:00
## Model Information
2025-08-26 12:32:21 -07:00
2026-01-06 12:52:34 -08:00
### all-MiniLM-L6-v2 (Embedded)
- **Dimensions**: 384 (fixed)
- **Format**: Safetensors (FP32)
- **Size**: ~87MB (embedded in WASM)
- **Total WASM Size**: ~90MB
- **Language**: English-optimized, works with all languages
- **Inference**: ~2-10ms per embedding
- **Initialization**: ~200ms
2025-08-26 12:32:21 -07:00
2026-01-06 12:52:34 -08:00
### Memory Usage
- **Loaded WASM**: ~90MB
- **Inference peak**: ~140MB total
- **Steady state**: ~100MB
2025-08-26 12:32:21 -07:00
2026-01-06 12:52:34 -08:00
## Comparing to Previous Architecture
2025-08-26 12:32:21 -07:00
2026-01-06 12:52:34 -08:00
| Feature | Before (ONNX) | Now (Candle WASM) |
|---------|--------------|-------------------|
| Model downloads | Required on first use | None - embedded |
| External dependencies | onnxruntime-web | None |
| Model files | model.onnx, tokenizer.json | Embedded in WASM |
| Offline support | Required setup | Works by default |
| Bun compile | Broken | Works |
| Configuration | Environment variables | None needed |
2025-08-26 12:32:21 -07:00
2026-01-06 12:52:34 -08:00
## Troubleshooting
2025-08-26 12:32:21 -07:00
2026-01-06 12:52:34 -08:00
### "Failed to initialize Candle Embedding Engine"
2025-08-26 12:32:21 -07:00
2026-01-06 12:52:34 -08:00
**Cause**: WASM loading issue.
2025-08-26 12:32:21 -07:00
**Solutions**:
```bash
2026-01-06 12:52:34 -08:00
# Rebuild the WASM
npm run build:candle
2025-08-26 12:32:21 -07:00
2026-01-06 12:52:34 -08:00
# Verify WASM exists
ls dist/embeddings/wasm/pkg/candle_embeddings_bg.wasm
# Should be ~90MB
2025-08-26 12:32:21 -07:00
```
2026-01-06 12:52:34 -08:00
### Out of Memory
2025-08-26 12:32:21 -07:00
2026-01-06 12:52:34 -08:00
**Cause**: Container/environment has less than 256MB RAM.
2025-08-26 12:32:21 -07:00
**Solutions**:
```dockerfile
2026-01-06 12:52:34 -08:00
# Increase memory limit (recommended: 512MB+)
docker run -m 512m my-app
2025-08-26 12:32:21 -07:00
```
2026-01-06 12:52:34 -08:00
### Slow Initialization (>500ms)
2025-08-26 12:32:21 -07:00
2026-01-06 12:52:34 -08:00
**Cause**: Cold start, large WASM parsing.
2025-08-26 12:32:21 -07:00
**Solutions**:
2026-01-06 12:52:34 -08:00
```typescript
// Initialize once at startup, not per-request
await brain.init() // Do this once
2025-08-26 12:32:21 -07:00
2026-01-06 12:52:34 -08:00
// Then reuse for all requests
app.get('/api', async (req, res) => {
const results = await brain.find(req.query)
res.json(results)
2025-08-26 12:32:21 -07:00
})
```
2026-01-06 12:52:34 -08:00
## Migration from Previous Versions
### From v6.x (ONNX)
No changes needed for most users:
2025-08-26 12:32:21 -07:00
```typescript
2026-01-06 12:52:34 -08:00
// Same API - just upgrade
2025-09-30 17:09:15 -07:00
const brain = new Brainy()
2025-08-26 12:32:21 -07:00
await brain.init()
```
2026-01-06 12:52:34 -08:00
**What's removed:**
- `BRAINY_ALLOW_REMOTE_MODELS` - no downloads
- `BRAINY_MODELS_PATH` - no external model files
- `npm run download-models` - no longer needed
2025-08-26 12:32:21 -07:00
2026-01-06 12:52:34 -08:00
**What's new:**
- Faster initialization
2026-07-01 09:16:46 -07:00
- Bundler-friendly (pure WASM, no native binaries)
2026-01-06 12:52:34 -08:00
- No network requirements
2025-08-26 12:32:21 -07:00
2026-01-06 12:52:34 -08:00
### From Custom Embedding Functions
2025-08-26 12:32:21 -07:00
2026-01-06 12:52:34 -08:00
If you provided a custom embedding function, it still works:
2025-08-26 12:32:21 -07:00
```typescript
2025-09-30 17:09:15 -07:00
const brain = new Brainy({
2026-01-06 12:52:34 -08:00
embeddingFunction: myCustomEmbedder // Still supported
2025-08-26 12:32:21 -07:00
})
```
2026-01-06 12:52:34 -08:00
## Advanced: Building Custom WASM
2025-08-26 12:32:21 -07:00
2026-01-06 12:52:34 -08:00
For contributors who want to modify the embedding engine:
2025-08-26 12:32:21 -07:00
2026-01-06 12:52:34 -08:00
```bash
# Navigate to Candle WASM source
cd src/embeddings/candle-wasm
2025-08-26 12:32:21 -07:00
2026-01-06 12:52:34 -08:00
# Build with wasm-pack
wasm-pack build --target web --release
2025-08-26 12:32:21 -07:00
2026-01-06 12:52:34 -08:00
# Copy to pkg folder
cp pkg/* ../wasm/pkg/
2025-08-26 12:32:21 -07:00
2026-01-06 12:52:34 -08:00
# Build TypeScript
npm run build
2025-08-26 12:32:21 -07:00
```
2026-01-06 12:52:34 -08:00
## Best Practices
2025-08-26 12:32:21 -07:00
2026-01-06 12:52:34 -08:00
### Development
```typescript
// Just works - no setup
const brain = new Brainy()
2025-08-26 12:32:21 -07:00
await brain.init()
```
2026-01-06 12:52:34 -08:00
### Production
2025-08-26 12:32:21 -07:00
```typescript
2026-01-06 12:52:34 -08:00
// Initialize once at startup
const brain = new Brainy()
await brain.init()
2025-08-26 12:32:21 -07:00
2026-01-06 12:52:34 -08:00
// Singleton pattern recommended
export { brain }
```
2025-08-26 12:32:21 -07:00
2026-01-06 12:52:34 -08:00
### Deployment
```bash
2026-07-01 09:16:46 -07:00
# Option 1: Bun runtime
bun run server.ts
2025-08-26 12:32:21 -07:00
2026-01-06 12:52:34 -08:00
# Option 2: Docker
docker build -t my-app .
docker run -p 3000:3000 my-app
2025-08-26 12:32:21 -07:00
```
---
2026-01-06 12:52:34 -08:00
## Additional Resources
2025-08-26 12:32:21 -07:00
2026-01-06 12:52:34 -08:00
- [Production Service Architecture ](../PRODUCTION_SERVICE_ARCHITECTURE.md )
- [Zero Configuration Guide ](../architecture/zero-config.md )
2025-08-26 12:32:21 -07:00
- [Troubleshooting Guide ](../troubleshooting.md )
2026-01-06 12:52:34 -08:00
**Need help?** [Open an issue ](https://github.com/soulcraftlabs/brainy/issues )