Moved embeddings to worker to prevent blocking
This commit is contained in:
11
src/embedder.ts
Normal file
11
src/embedder.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
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 });
|
||||
});
|
||||
Reference in New Issue
Block a user