-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvector-sim.js
More file actions
34 lines (28 loc) · 827 Bytes
/
vector-sim.js
File metadata and controls
34 lines (28 loc) · 827 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const EMBEDDING_MODEL = "Xenova/paraphrase-MiniLM-L3-v2";
(async function () {
const args = process.argv.slice(2);
if (args.length < 1) {
console.log("Usage: vector-sim 'Something' 'Some other thing'");
process.exit(-1);
}
const transformers = await import("@xenova/transformers");
const { pipeline, cos_sim } = transformers;
const extractor = await pipeline("feature-extraction", EMBEDDING_MODEL, {
quantize: true,
});
const db = {};
for (const text of args) {
const output = await extractor([text], {
pooling: "mean",
normalize: true,
});
const embedding = output[0].data;
db[text] = embedding;
}
if (args.length !== 2) {
console.log(db);
process.exit(0);
}
console.log(`Comparing...\n\t${args[0]}\n\t${args[1]}\n`);
console.log(Math.abs(cos_sim(db[args[0]], db[args[1]])));
})();