Files
ai-utils/src/embedder.ts
ztimson 39537a4a8f
All checks were successful
Publish Library / Build NPM Project (push) Successful in 38s
Publish Library / Tag Version (push) Successful in 5s
Switching to processes and whisper.cpp to avoid transformers.js memory leaks
2026-02-20 21:50:01 -05:00

14 lines
502 B
TypeScript

import { pipeline } from '@xenova/transformers';
const [modelDir, model] = process.argv.slice(2);
let text = '';
process.stdin.on('data', chunk => text += chunk);
process.stdin.on('end', async () => {
const embedder = await pipeline('feature-extraction', 'Xenova/' + model, {quantized: true, cache_dir: modelDir});
const output = await embedder(text, { pooling: 'mean', normalize: true });
const embedding = Array.from(output.data);
console.log(JSON.stringify({embedding}));
process.exit();
});