| /** | |
| * preload.js — Downloads and caches the model during docker build. | |
| */ | |
| import { pipeline, env } from '@xenova/transformers'; | |
| import path from 'path'; | |
| import { fileURLToPath } from 'url'; | |
| const __dirname = path.dirname(fileURLToPath(import.meta.url)); | |
| // Disable multi-threading (not available in Alpine build context) | |
| import ort from 'onnxruntime-web'; | |
| ort.env.wasm.numThreads = 1; | |
| const MODEL = process.env.MODEL || 'Xenova/bge-small-en-v1.5'; | |
| env.cacheDir = path.join(__dirname, '.cache'); | |
| env.allowLocalModels = true; | |
| console.log(`Preloading model: ${MODEL}...`); | |
| const start = Date.now(); | |
| const pipe = await pipeline('feature-extraction', MODEL); | |
| const output = await pipe('test', { pooling: 'mean', normalize: true }); | |
| console.log(`Done in ${Date.now() - start}ms — dims: ${output.dims}`); | |
| process.exit(0); | |