function internal_getEmbeddings(strings, { model = "Xenova/all-MiniLM-L6-v2" } = {}) {
const w = worker(
`
import * as transformers from 'https://unpkg.com/@xenova/transformers@2.5.4/dist/transformers.min.js'
function isIterable(obj) {
return (
typeof obj[Symbol.iterator] === "function" &&
typeof obj["next"] == "function"
);
}
const __run__ = async function (data) {
const extractor = await transformers.pipeline(
"feature-extraction",
"${model}"
)
const result = await extractor(data[0], {
pooling: "mean",
normalize: true
});
const embeddings = await Promise.all(
data.map((i) =>
extractor(i, {
pooling: "mean",
normalize: true
})
)
)
return embeddings.map((i) => i.data)
};
self.onmessage = async function(e) {
const t0 = performance.now();
let result = await __run__(e.data);
if (typeof result !== "undefined") {
if (!isIterable(result)) result = [result];
for (const p of result) {
postMessage(typeof p !== "object" ? p : Object.assign(p, {_time: performance.now() - t0}));
}
close();
}
}
`,
strings,
null,
{ type: "module" }
);
const p = new Promise((resolve, reject) => {
w((result) => {
resolve(result);
});
invalidation.then(() => reject());
});
return p;
}