Add alternative local vectors source.

x5 speed boost!!
This commit is contained in:
Cohee
2023-09-14 23:40:13 +03:00
parent 0cc048cb64
commit 6ad786f348
6 changed files with 42 additions and 11 deletions

17
src/embedding.js Normal file
View File

@@ -0,0 +1,17 @@
const TASK = 'feature-extraction';
/**
* @param {string} text - The text to vectorize
* @returns {Promise<number[]>} - The vectorized text in form of an array of numbers
*/
async function getTransformersVector(text) {
const module = await import('./transformers.mjs');
const pipe = await module.default.getPipeline(TASK);
const result = await pipe(text, { pooling: 'mean', normalize: true });
const vector = Array.from(result.data);
return vector;
}
module.exports = {
getTransformersVector,
}