Files
ai-utils/src/embedder.ts
ztimson cb60a0b0c5
All checks were successful
Publish Library / Build NPM Project (push) Successful in 41s
Publish Library / Tag Version (push) Successful in 7s
Moved embeddings to worker to prevent blocking
2026-01-28 22:17:39 -05:00

12 lines
415 B
TypeScript

import { pipeline } from '@xenova/transformers';
import { parentPort } from 'worker_threads';
let model: any;
parentPort?.on('message', async ({ id, text }) => {
if(!model) model = await pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2');
const output = await model(text, { pooling: 'mean', normalize: true });
const embedding = Array.from(output.data);
parentPort?.postMessage({ id, embedding });
});