BREAKING CHANGE: Complete migration from TensorFlow.js to Transformers.js for embedding generation This is a major architectural change that replaces TensorFlow.js (USE model) with Transformers.js (all-MiniLM-L6-v2) for significantly improved performance and reduced complexity. Key Changes: - Replace TensorFlow.js Universal Sentence Encoder with Transformers.js all-MiniLM-L6-v2 - Reduce model size from 525MB to 87MB (83% reduction) - Reduce embedding dimensions from 512 to 384 (faster distance calculations) - Remove TensorFlow.js Float32Array patching (caused ONNX conflicts) - Implement smart bundled model detection for offline operation - Add explicit model download script for Docker deployments - Remove complex environment variables in favor of simple configuration - Update all distance functions to use optimized pure JavaScript - Remove TensorFlow-specific utilities and type definitions Performance Improvements: - Model loading: 5x faster (87MB vs 525MB) - Memory usage: 75% reduction (~200-400MB vs ~1.5GB) - Distance calculations: Faster pure JS vs GPU overhead for small vectors - Cold start performance: Significantly improved Files Changed: - Updated package.json: New dependencies, simplified scripts - Rewrote src/utils/embedding.ts: Complete Transformers.js implementation - Updated src/utils/distance.ts: Optimized JavaScript distance functions - Simplified src/setup.ts: Removed TensorFlow-specific patching - Simplified src/utils/textEncoding.ts: Only Node.js TextEncoder/Decoder patches - Deleted src/utils/robustModelLoader.ts: TensorFlow-specific loader - Deleted src/types/tensorflowTypes.ts: TensorFlow type definitions - Added scripts/download-models.cjs: Docker-compatible model downloader - Added comprehensive documentation: README.md, OFFLINE_MODELS.md, analysis docs Testing: - All 19 tests passing - Removed test mocking in favor of real implementation testing - Updated test environment for Transformers.js compatibility - Performance tests validate improved efficiency This migration resolves production issues with Docker egress limitations and provides a more robust, performant foundation for vector operations.
177 lines
No EOL
5.2 KiB
TypeScript
177 lines
No EOL
5.2 KiB
TypeScript
/**
|
|
* Custom Models Path Test
|
|
*
|
|
* Tests the custom models directory functionality for Docker deployments
|
|
*/
|
|
|
|
import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest'
|
|
import { RobustModelLoader } from '../src/utils/robustModelLoader.js'
|
|
import { writeFile, mkdir, rm } from 'fs/promises'
|
|
import { join } from 'path'
|
|
import { tmpdir } from 'os'
|
|
|
|
describe('Custom Models Path', () => {
|
|
let tempDir: string
|
|
let originalEnv: string | undefined
|
|
|
|
beforeAll(() => {
|
|
// Save original environment variable
|
|
originalEnv = process.env.BRAINY_MODELS_PATH
|
|
})
|
|
|
|
afterAll(() => {
|
|
// Restore original environment variable
|
|
if (originalEnv !== undefined) {
|
|
process.env.BRAINY_MODELS_PATH = originalEnv
|
|
} else {
|
|
delete process.env.BRAINY_MODELS_PATH
|
|
}
|
|
})
|
|
|
|
beforeEach(async () => {
|
|
// Create temporary directory for each test
|
|
tempDir = join(tmpdir(), `brainy-test-${Date.now()}`)
|
|
await mkdir(tempDir, { recursive: true })
|
|
})
|
|
|
|
afterEach(async () => {
|
|
// Clean up temporary directory
|
|
try {
|
|
await rm(tempDir, { recursive: true, force: true })
|
|
} catch (error) {
|
|
console.error('Cleanup error:', error)
|
|
}
|
|
})
|
|
|
|
it('should use BRAINY_MODELS_PATH environment variable', async () => {
|
|
// Set environment variable
|
|
process.env.BRAINY_MODELS_PATH = tempDir
|
|
|
|
const loader = new RobustModelLoader({ verbose: true })
|
|
expect((loader as any).options.customModelsPath).toBe(tempDir)
|
|
})
|
|
|
|
it('should use MODELS_PATH environment variable as fallback', async () => {
|
|
// Clear BRAINY_MODELS_PATH and set MODELS_PATH
|
|
delete process.env.BRAINY_MODELS_PATH
|
|
process.env.MODELS_PATH = tempDir
|
|
|
|
const loader = new RobustModelLoader({ verbose: true })
|
|
expect((loader as any).options.customModelsPath).toBe(tempDir)
|
|
|
|
// Clean up
|
|
delete process.env.MODELS_PATH
|
|
})
|
|
|
|
it('should prioritize customModelsPath option over environment variables', async () => {
|
|
process.env.BRAINY_MODELS_PATH = '/env/path'
|
|
const customPath = '/custom/path'
|
|
|
|
const loader = new RobustModelLoader({
|
|
customModelsPath: customPath,
|
|
verbose: true
|
|
})
|
|
|
|
expect((loader as any).options.customModelsPath).toBe(customPath)
|
|
})
|
|
|
|
it('should check multiple subdirectories for models', async () => {
|
|
const loader = new RobustModelLoader({
|
|
customModelsPath: tempDir,
|
|
verbose: true
|
|
})
|
|
|
|
// Create a mock model.json in one of the expected subdirectories
|
|
const modelDir = join(tempDir, 'universal-sentence-encoder')
|
|
await mkdir(modelDir, { recursive: true })
|
|
|
|
const mockModelJson = {
|
|
format: 'tfjs-graph-model',
|
|
modelTopology: {},
|
|
weightsManifest: []
|
|
}
|
|
|
|
await writeFile(
|
|
join(modelDir, 'model.json'),
|
|
JSON.stringify(mockModelJson, null, 2)
|
|
)
|
|
|
|
// Mock the tryLoadFromCustomPath method to avoid actual TensorFlow loading
|
|
const tryLoadSpy = vi.spyOn(loader as any, 'tryLoadFromCustomPath')
|
|
tryLoadSpy.mockResolvedValue(null) // Return null to avoid complex mocking
|
|
|
|
await (loader as any).tryLoadLocalBundledModel()
|
|
|
|
// Verify the method was called with the correct path
|
|
expect(tryLoadSpy).toHaveBeenCalledWith(tempDir)
|
|
})
|
|
|
|
it('should log helpful messages when checking custom path', async () => {
|
|
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
|
|
|
|
const loader = new RobustModelLoader({
|
|
customModelsPath: tempDir,
|
|
verbose: true
|
|
})
|
|
|
|
try {
|
|
await (loader as any).tryLoadLocalBundledModel()
|
|
} catch (error) {
|
|
// Expected in test environment
|
|
}
|
|
|
|
// Check that it logged the custom path check
|
|
expect(consoleSpy).toHaveBeenCalledWith(
|
|
expect.stringContaining(`Checking custom models directory: ${tempDir}`)
|
|
)
|
|
|
|
consoleSpy.mockRestore()
|
|
})
|
|
|
|
it('should handle non-existent custom paths gracefully', async () => {
|
|
const nonExistentPath = '/this/path/does/not/exist'
|
|
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
|
|
|
|
const loader = new RobustModelLoader({
|
|
customModelsPath: nonExistentPath,
|
|
verbose: true
|
|
})
|
|
|
|
const result = await (loader as any).tryLoadFromCustomPath(nonExistentPath)
|
|
expect(result).toBeNull()
|
|
|
|
// Should log that no model was found
|
|
expect(consoleSpy).toHaveBeenCalledWith(
|
|
expect.stringContaining(`No model found in custom path: ${nonExistentPath}`)
|
|
)
|
|
|
|
consoleSpy.mockRestore()
|
|
})
|
|
|
|
it('should provide helpful warning messages about custom paths', async () => {
|
|
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
|
|
|
|
// Test without any custom path set
|
|
const loader = new RobustModelLoader({ verbose: false })
|
|
|
|
try {
|
|
await loader.loadModelWithFallbacks()
|
|
} catch (error) {
|
|
// Expected in test environment without actual models
|
|
}
|
|
|
|
// Check that the warning mentions the custom path option or brainy-models
|
|
const warnCalls = consoleSpy.mock.calls.flat()
|
|
const hasCustomPathMention = warnCalls.some(call =>
|
|
typeof call === 'string' && (
|
|
call.includes('BRAINY_MODELS_PATH') ||
|
|
call.includes('customModelsPath') ||
|
|
call.includes('@soulcraft/brainy-models')
|
|
)
|
|
)
|
|
|
|
expect(hasCustomPathMention).toBe(true)
|
|
|
|
consoleSpy.mockRestore()
|
|
})
|
|
}) |