forked from ariya/pico-jarvis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector-sim.js
executable file
·35 lines (29 loc) · 999 Bytes
/
vector-sim.js
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
35
#!/usr/bin/env node
const EMBEDDING_MODEL = 'Xenova/paraphrase-MiniLM-L3-v2';
(async () => {
const args = process.argv.slice(2);
if (args.length < 1) {
console.log('Usage: vector-sim something another-thing');
process.exit(-1);
}
const transformers = await import('@xenova/transformers');
const { pipeline, cos_sim } = transformers;
const extractor = await pipeline('feature-extraction', EMBEDDING_MODEL, { quantized: 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);
}
const first = args[0];
const second = args[1];
console.log('Comparing');
console.log(' ', first);
console.log(' ', second);
console.log();
console.log(Math.abs(cos_sim(db[first], db[second])));
})();