Blame Raw
· · · 27 lines (947 B)
0 contributors
1import { getPipeline } from '../transformers.js';
2const TASK = 'feature-extraction';
3
4/**
5 * Gets the vectorized text in form of an array of numbers.
6 * @param {string} text - The text to vectorize
7 * @returns {Promise<number[]>} - The vectorized text in form of an array of numbers
8 */
9export async function getTransformersVector(text) {
10 const pipe = await getPipeline(TASK);
11 const result = await pipe(text, { pooling: 'mean', normalize: true });
12 const vector = Array.from(result.data);
13 return vector;
14}
15
16/**
17 * Gets the vectorized texts in form of an array of arrays of numbers.
18 * @param {string[]} texts - The texts to vectorize
19 * @returns {Promise<number[][]>} - The vectorized texts in form of an array of arrays of numbers
20 */
21export async function getTransformersBatchVector(texts) {
22 const result = [];
23 for (const text of texts) {
24 result.push(await getTransformersVector(text));
25 }
26 return result;
27}