brainy/docs/guides/model-loading.md
David Snelling 9c87982a7d 🧠 Brainy 2.0.0 - Zero-Configuration AI Database with Triple Intelligence™
MAJOR RELEASE: Complete evolution of Brainy with groundbreaking features and performance.

🎯 KEY FEATURES:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 Triple Intelligence™ Engine
  - Unified Vector + Metadata + Graph search
  - O(log n) performance on all operations
  - 3ms average search latency at any scale

 API Consolidation
  - 15+ search methods → 2 clean APIs
  - search() for vector similarity
  - find() for natural language queries

 Natural Language Processing
  - 220+ pre-computed NLP patterns
  - Instant context understanding
  - "Show me recent React components with tests"

 Zero Configuration
  - Works instantly, no setup required
  - Built-in embedding models (no API keys)
  - Smart defaults for everything
  - Automatic optimization

 Enterprise Features (Free for Everyone)
  - Scales to 10M+ items
  - Write-Ahead Logging (WAL) for durability
  - Distributed architecture with sharding
  - Read/write separation
  - Connection pooling & request deduplication
  - Built-in monitoring & health checks

 Universal Compatibility
  - Node.js, Browser, Edge Workers
  - 4 Storage Adapters (Memory, FileSystem, OPFS, S3)
  - TypeScript with full type safety
  - Worker-based embeddings

📦 WHAT'S INCLUDED:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
• Core AI Database with HNSW indexing
• 19 Production-ready augmentations
• Universal Memory Manager
• Complete CLI with all commands
• Brain Cloud integration (soulcraft.com)
• Comprehensive documentation
• 52 test files with 400+ tests
• Migration guide from 1.x

📊 PERFORMANCE:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
• Initialize: 450ms (24MB memory)
• Search: 3ms average (up to 10M items)
• Metadata Filter: 0.8ms (O(log n))
• Bulk Import: 2.3s per 1000 items
• Production Scale: 5.8ms at 10M items

🔧 TECHNICAL IMPROVEMENTS:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
• TypeScript compilation: 153 errors → 0
• Memory usage: 200MB → 24MB baseline
• Circular dependencies resolved
• Worker thread communication fixed
• Storage adapter consistency
• Request coalescing for 3x performance

🛠️ CLI FEATURES:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
• brainy add - Smart data ingestion
• brainy find - Natural language search
• brainy search - Vector similarity
• brainy chat - AI conversation mode
• brainy cloud - Brain Cloud integration
• brainy augment - Manage extensions
• 100% API compatibility

📚 DOCUMENTATION:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
• Professional README with examples
• Quick Start guide (5 minutes)
• Enterprise Features guide
• Migration guide from 1.x
• API reference
• Architecture documentation

🌟 USE CASES:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
• AI memory layer for chatbots
• Semantic document search
• Code intelligence platforms
• Knowledge management systems
• Real-time recommendation engines
• Customer support automation

MIT License - Enterprise features included free for everyone.
No premium tiers, no paywalls, no limits.

Built with ❤️ by the Brainy community.
Visit https://soulcraft.com for Brain Cloud integration.
2025-08-26 12:32:21 -07:00

7.9 KiB

🤖 Model Loading Guide

Brainy uses AI embedding models to understand and process your data. This guide explains how model loading works and how to handle different scenarios.

🚀 Zero Configuration (Default)

For most developers, no configuration is needed:

const brain = new BrainyData()
await brain.init() // Models load automatically

What happens automatically:

  1. Checks for local models in ./models/
  2. Downloads All-MiniLM-L6-v2 if needed (384 dimensions)
  3. Configures optimal settings for your environment
  4. Ready to use immediately

📦 Model Loading Cascade

Brainy tries multiple sources in this order:

1. LOCAL CACHE (./models/) 
   ↓ (if not found)
2. CDN DOWNLOAD (fast mirrors)
   ↓ (if fails)  
3. GITHUB RELEASES (github.com/xenova/transformers.js)
   ↓ (if fails)
4. HUGGINGFACE HUB (huggingface.co)
   ↓ (if fails)
5. FALLBACK STRATEGIES (different model variants)

🌍 Environment-Specific Behavior

Browser

// Automatically configured for browsers
const brain = new BrainyData() // Works in React, Vue, vanilla JS
await brain.init() // Downloads models via CDN

Node.js Development

// Zero config - downloads to ./models/
const brain = new BrainyData()
await brain.init() // Downloads once, cached forever

Production Server

// Preload models during build/deployment
const brain = new BrainyData()
await brain.init() // Uses cached local models

Docker/Kubernetes

# Dockerfile - preload models
RUN npm run download-models
ENV BRAINY_ALLOW_REMOTE_MODELS=false

🛠️ Manual Model Management

Pre-download Models

# Download models during build/deployment
npm run download-models

# Custom location
BRAINY_MODELS_PATH=./my-models npm run download-models

Verify Models

# Check if models exist
ls ./models/Xenova/all-MiniLM-L6-v2/

# Should see:
# - config.json
# - tokenizer.json  
# - onnx/model.onnx

Custom Model Path

const brain = new BrainyData({
  embedding: {
    cacheDir: './custom-models'
  }
})

🔒 Offline & Air-Gapped Environments

Complete Offline Setup

# 1. Download models on connected machine
npm run download-models

# 2. Copy models to offline machine
cp -r ./models /path/to/offline/project/

# 3. Force local-only mode
export BRAINY_ALLOW_REMOTE_MODELS=false

Container/Server Deployment

FROM node:18
WORKDIR /app
COPY package*.json ./
RUN npm ci

# Download models during build
RUN npm run download-models

# Force local-only in production
ENV BRAINY_ALLOW_REMOTE_MODELS=false

COPY . .
EXPOSE 3000
CMD ["npm", "start"]

⚙️ Environment Variables

BRAINY_ALLOW_REMOTE_MODELS

Controls whether remote model downloads are allowed:

# Allow remote downloads (default in most environments)
export BRAINY_ALLOW_REMOTE_MODELS=true

# Force local-only (recommended for production)
export BRAINY_ALLOW_REMOTE_MODELS=false

BRAINY_MODELS_PATH

Custom model storage location:

# Custom model path
export BRAINY_MODELS_PATH=/opt/brainy/models

# Relative path
export BRAINY_MODELS_PATH=./my-custom-models

🚨 Troubleshooting

"Failed to load embedding model" Error

Cause: Models not found locally and remote download blocked/failed.

Solutions:

# Option 1: Allow remote downloads
export BRAINY_ALLOW_REMOTE_MODELS=true

# Option 2: Download models manually
npm run download-models

# Option 3: Check internet connectivity
ping huggingface.co

# Option 4: Use custom model path
export BRAINY_MODELS_PATH=/path/to/existing/models

Models Download Very Slowly

Cause: Network issues or regional restrictions.

Solutions:

# Pre-download during build/CI
npm run download-models

# Use faster mirrors (automatic in newer versions)
# No action needed - Brainy tries multiple CDNs

Container Out of Memory During Model Load

Cause: Limited container memory during model initialization.

Solutions:

# Increase memory limit
docker run -m 2g my-app

# Use quantized models (default)
ENV BRAINY_MODEL_DTYPE=q8

# Pre-load models at build time (recommended)
RUN npm run download-models

Permission Denied Creating Model Cache

Cause: Write permissions for model cache directory.

Solutions:

# Make directory writable
chmod 755 ./models

# Use custom writable path
export BRAINY_MODELS_PATH=/tmp/brainy-models

# Or use memory-only storage
const brain = new BrainyData({
  storage: { forceMemoryStorage: true }
})

🎯 Best Practices

Development

// ✅ Zero config - just works
const brain = new BrainyData()
await brain.init()

Production

# ✅ Pre-download models
RUN npm run download-models

# ✅ Force local-only
ENV BRAINY_ALLOW_REMOTE_MODELS=false

# ✅ Verify models exist
RUN test -f ./models/Xenova/all-MiniLM-L6-v2/onnx/model.onnx

CI/CD Pipeline

# .github/workflows/build.yml
- name: Download AI Models
  run: npm run download-models
  
- name: Verify Models
  run: |
    test -f ./models/Xenova/all-MiniLM-L6-v2/onnx/model.onnx
    echo "✅ Models verified"

- name: Test Offline Mode
  env:
    BRAINY_ALLOW_REMOTE_MODELS: false
  run: npm test

Lambda/Serverless

// ✅ Models in deployment package
const brain = new BrainyData({
  embedding: {
    localFilesOnly: true, // No downloads in lambda
    cacheDir: './models'  // Bundled with deployment
  }
})

📊 Model Information

All-MiniLM-L6-v2 (Default)

  • Dimensions: 384 (fixed)
  • Size: ~80MB compressed, ~330MB uncompressed
  • Language: English (optimized)
  • Speed: Very fast inference
  • Quality: High quality for most use cases

Model Files Structure

models/
└── Xenova/
    └── all-MiniLM-L6-v2/
        ├── config.json          # Model configuration
        ├── tokenizer.json       # Text tokenizer
        ├── tokenizer_config.json
        └── onnx/
            ├── model.onnx       # Main model file
            └── model_quantized.onnx  # Optimized version

🔄 Migration from Other Embedding Solutions

From OpenAI Embeddings

// Before: OpenAI API calls
const response = await openai.embeddings.create({
  model: "text-embedding-ada-002",
  input: "Your text"
})

// After: Local Brainy embeddings
const brain = new BrainyData()
await brain.init() // One-time setup
const id = await brain.add("Your text") // Embedded automatically

From Sentence Transformers

# Before: Python sentence-transformers
from sentence_transformers import SentenceTransformer
model = SentenceTransformer('all-MiniLM-L6-v2')

# After: JavaScript Brainy (same model!)
const brain = new BrainyData() // Uses same all-MiniLM-L6-v2
await brain.init()

🚀 Advanced Configuration

Custom Embedding Options

const brain = new BrainyData({
  embedding: {
    model: 'Xenova/all-MiniLM-L6-v2', // Default
    dtype: 'q8',                       // Quantized for speed
    device: 'cpu',                     // CPU inference
    localFilesOnly: false,             // Allow downloads
    verbose: true                      // Debug logging
  }
})

Multiple Model Support (Advanced)

// Use custom embedding function
import { createEmbeddingFunction } from 'brainy'

const customEmbedder = createEmbeddingFunction({
  model: 'Xenova/all-MiniLM-L12-v2', // Larger model
  dtype: 'fp32' // Higher precision
})

const brain = new BrainyData({
  embeddingFunction: customEmbedder
})

📚 Additional Resources

Need help? Check our troubleshooting guide or open an issue.