-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.html
110 lines (96 loc) · 3.46 KB
/
index.html
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>wllama.cpp demo</title>
<style>
body {
background-color: rgb(55, 55, 55);
color: rgb(222, 222, 222);
font-family: 'Courier New', Courier, monospace;
padding: 1em;
}
</style>
</head>
<body>
<div id="output"></div>
<script type="module">
import { Wllama } from '../../esm/index.js';
const CONFIG_PATHS = {
'single-thread/wllama.js' : '../../esm/single-thread/wllama.js',
'single-thread/wllama.wasm' : '../../esm/single-thread/wllama.wasm',
'multi-thread/wllama.js' : '../../esm/multi-thread/wllama.js',
'multi-thread/wllama.wasm' : '../../esm/multi-thread/wllama.wasm',
'multi-thread/wllama.worker.mjs': '../../esm/multi-thread/wllama.worker.mjs',
};
const MODEL = 'https://huggingface.co/CompendiumLabs/bge-base-en-v1.5-gguf/resolve/main/bge-base-en-v1.5-q4_k_m.gguf';
const TEXTS = [
'Taking advantage of the perfect weather, I went out for a leisurely walk through the park.',
'A stunning sunny day inspired me to get outside and enjoy the beauty of nature while going for a walk.',
'French is one of her strong points when it comes to languages. Speaking French comes naturally to her.',
'Represent this sentence for searching relevant passages: Does anyone here speak French?',
];
async function main() {
let res, tokens, elapsed, buffer;
const wllama = new Wllama(CONFIG_PATHS);
print(`DEMO EMBEDDINGS`);
print(`Loading model ${MODEL}`);
timeStart();
await wllama.loadModelFromUrl(MODEL, {
embeddings: true,
n_ctx: 1024,
n_batch: 1024,
n_ubatch: 1024,
pooling_type: 'LLAMA_POOLING_TYPE_MEAN',
});
print(`Loaded, take ${timeEnd()} ms`);
print(`BOS token = ${wllama.getBOS()}`);
print(`EOS token = ${wllama.getEOS()}`);
print(`\n--------------\n`);
const embeddings = [];
let i = 0;
for (const sentence of TEXTS) {
i++;
print(`Calculating embedding for sentence #${i}: "${sentence}"`);
timeStart();
const vector = await wllama.createEmbedding(sentence);
print(`OK, take ${timeEnd()} ms`);
embeddings.push(vector);
}
print(`\n--------------\n`);
const vecDot = (a, b) => a.reduce((acc, _, i) => acc + a[i]*b[i], 0);
for (let i = 0; i < TEXTS.length - 1; i++) {
print(`Cosine similarity of sentence #${i+1} and #${i+2} = ${vecDot(embeddings[i], embeddings[i+1])}`);
}
}
/////////////////////////////////////////////////////////////////////
const elemOutput = document.getElementById('output');
function print(message, bold) {
const elem = document.createElement('div');
if (bold) {
const b = document.createElement('b');
b.innerText = message;
elem.appendChild(b);
} else {
elem.innerText = message;
}
elemOutput.appendChild(elem);
// scroll to bottom
setTimeout(() => window.scrollTo({
top: document.documentElement.scrollHeight - window.innerHeight,
left: 0,
behavior: 'smooth',
}), 10);
}
let __startTime = 0;
function timeStart() {
__startTime = Date.now();
}
function timeEnd() {
return Date.now() - __startTime;
}
main();
</script>
</body>
</html>