-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.html
357 lines (307 loc) · 13.9 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<!DOCTYPE html>
<html lang="en">
<head><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.css">
<meta charset="UTF-8" />
<link rel="stylesheet" href="style.css" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="jason.css">
<meta property="og:title" content="Portable HNSW">
<meta property="og:description" content="Use natural language to search without a server">
<meta property="og:type" content="website">
<meta property="og:url" content="https://jasonjmcghee.github.io/portable-hnsw/">
<title>Portable HNSW</title>
<style>
.container {
padding: 16px;
display: flex;
justify-content: center;
}
.inner {
display: flex;
justify-content: center;
flex-direction: column;
max-width: 1000px;
width: 100%;
gap: 20px;
}
.query {
display: flex;
justify-content: center;
gap: 12px;
}
#text-input {
flex-grow: 1;
}
.path-input {
display: flex;
align-items: center;
gap: 12px;
}
#instant-mode {
cursor: pointer;
}
#path {
flex-grow: 1;
}
</style>
</head>
<body>
<main class="container">
<div class="inner">
<h1 class="title">Use natural language to search 50k blog posts without a server</h1>
Or something else if you build your own index and throw in it in a CDN.
<a href="https://github.com/jasonjmcghee/portable-hnsw">What is going on here? / Show me the code</a>
<i>Too slow for you? I'm probably doing things poorly. PRs welcome!</i>
<div class="path-input">
<a href="" id="frankenstein">Frankenstein (fastest)</a>
<a href="" id="shakespeare">Shakespeare</a>
<a href="" id="blogs">50k Blogs Dataset (slowest - ~15 seconds)</a>
</div>
<div class="path-input">
Path: <input type="text" id="path" placeholder="https://jasonjmcghee.github.io/portable-hnsw/frankenstein" value="frankenstein"></input>
</div>
<div class="path-input">Debounced auto-search<input id="instant-mode" type="checkbox"></input></div>
<div>
<div class="query">
<textarea id="text-input">falling in love</textarea>
<button disabled id="search">Search</button>
</div>
<p id="status"></p>
</div>
<div id="output"></div>
</div>
</main>
<script type="module">
import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@xenova/[email protected]';
// Since we will download the model from the Hugging Face Hub, we can skip the local model check
env.allowLocalModels = false;
// Reference the elements that we will need
const status = document.getElementById('status');
const textInput = document.getElementById('text-input');
const blogs = document.getElementById('blogs');
const shakespeare = document.getElementById('shakespeare');
const frankenstein = document.getElementById('frankenstein');
const search = document.getElementById('search');
const instantMode = document.getElementById('instant-mode');
instantMode.checked = true;
const output = document.getElementById('output');
const path = document.getElementById('path');
let cachedPath = "";
let conn = null;
// Create a new feature extraction pipeline
status.textContent = 'Loading embedding model...';
const extractor = await pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2');
status.textContent = 'Ready for user!';
import * as duckdb from 'https://cdn.jsdelivr.net/npm/@duckdb/[email protected]/+esm';
const JSDELIVR_BUNDLES = duckdb.getJsDelivrBundles();
// Select a bundle based on browser checks
const bundle = await duckdb.selectBundle(JSDELIVR_BUNDLES);
const worker_url = URL.createObjectURL(
new Blob([`importScripts("${bundle.mainWorker}");`], {type: 'text/javascript'})
);
// Instantiate the asynchronus version of DuckDB-wasm
const worker = new Worker(worker_url);
const logger = new duckdb.ConsoleLogger();
const db = new duckdb.AsyncDuckDB(logger, worker);
window.db = db;
await db.instantiate(bundle.mainModule, bundle.pthreadWorker);
URL.revokeObjectURL(worker_url);
function euclideanDistanceSquared(arr1, arr2) {
if (arr1.length !== arr2.length) {
throw new Error("Arrays must be of the same length");
}
let sum = 0;
for (let i = 0; i < arr1.length; i++) {
sum += (arr1[i] - arr2[i]) ** 2;
}
return sum;
}
async function searchWithSql(
conn,
queryData,
k,
path,
ef = 10
) {
path = path.startsWith("http") ? path : `${window.location.href}/${path}`;
status.textContent = 'Searching HNSW Index...';
await db.registerFileURL('docs.parquet', `${path}/docs.parquet`, duckdb.DuckDBDataProtocol.HTTP, false);
const cache = {};
const countQuery = `SELECT COUNT(node_id) FROM nodes.parquet`;
const countResult = await conn.query(countQuery);
const countArrayed = countResult.toArray().map(([count, _]) => count[1]);
const count = JSON.parse(countArrayed[0].toString());
const maxLayer = count > 0 ? Math.floor(Math.log2(count)) : 0;
const initNodeQuery = `
SELECT n.node_id, n.data as node_data
FROM nodes.parquet n
ORDER BY RANDOM() LIMIT 1
`;
let currentBest = await conn.query(initNodeQuery);
currentBest = currentBest.toArray().map(([nId, data]) => ([nId[1], euclideanDistanceSquared(data[1].data[0].values, queryData)]))
for (let layer = maxLayer; layer >= 0; layer--) {
status.textContent = `Searching HNSW Index: ${layer + 1} layers remaining...`;
let improved = true;
while (improved) {
improved = false;
const currentNodeIds = currentBest;
const candidates = new Set(currentNodeIds);
const newCandidates = new Set();
const filteredCandidates = Array.from(candidates).filter(nodeId => !cache[nodeId] || !cache[nodeId][layer]);
if (filteredCandidates.length === 0) {
continue;
}
const sqlSafeFilteredCandidates = filteredCandidates.join(',');
const sqlSafeCandidates = [...candidates, ...Object.keys(cache)].join(',');
// Create filtered_edges table
const createFilteredEdgesQuery = `
DROP TABLE IF EXISTS filtered_edges;
CREATE TEMP TABLE filtered_edges AS
SELECT * FROM edges.parquet
WHERE source_node_id IN (${sqlSafeFilteredCandidates})
AND layer = ${layer}
AND target_node_id NOT IN (${sqlSafeCandidates});
`;
await conn.query(createFilteredEdgesQuery);
// Perform the join and fetch neighbors
const fetchNeighborsQuery = `
SELECT
e.target_node_id as node_id,
n.data as node_data
FROM nodes.parquet n
INNER JOIN filtered_edges e ON n.node_id = e.target_node_id
`;
const neighbors = (await conn.query(fetchNeighborsQuery)).toArray();
if (neighbors.length === 0) {
continue;
}
for (const [neighborId_, node_data_] of neighbors) {
const neighborId = neighborId_[1];
if (newCandidates.has(neighborId)) {
continue;
}
const data = node_data_[1].data[0].values;
let dist;
if (cache[neighborId] && cache[neighborId].data) {
dist = cache[neighborId].distance;
} else {
dist = euclideanDistanceSquared(data, queryData);
cache[neighborId] = { data, distance: dist };
}
if (currentBest.length < ef || dist < currentBest[currentBest.length - 1][1]) {
currentBest.push([neighborId, dist]);
newCandidates.add(neighborId);
improved = true;
}
}
currentBest.sort((a, b) => a[1] - b[1]);
currentBest = currentBest.slice(0, ef);
}
}
const ids = currentBest.slice(0, k).map(([nodeId, _]) => nodeId);
console.log(ids);
status.textContent = `Found best candidates. Retrieving documents...`;
const out = await Promise.all(ids.map(async (id) => {
const result = await conn.query(`SELECT text FROM read_parquet('${path}/docs.parquet') OFFSET ${id} LIMIT 1`);
return result.toArray().map(([item]) => item[1])[0];
}));
return out;
}
async function embed(text) {
status.textContent = 'Analysing...';
const out = await extractor(text, { pooling: 'mean', normalize: true });
status.textContent = 'Ready for user!';
return out;
}
async function loadIndex() {
debugger;
if (path.value !== cachedPath) {
cachedPath = path.value;
}
status.textContent = 'Loading HNSW Index...';
conn = await db.connect();
const nodesRes = await fetch(`${cachedPath}/nodes.parquet`);
await db.registerFileBuffer('nodes.parquet', new Uint8Array(await nodesRes.arrayBuffer()));
const edgesRes = await fetch(`${cachedPath}/edges.parquet`);
await db.registerFileBuffer('edges.parquet', new Uint8Array(await edgesRes.arrayBuffer()));
status.textContent = 'HNSW Index Loaded.';
status.textContent = 'Ready for user!';
}
async function performSearch() {
const queryData = (await embed(textInput.value)).data;
status.textContent = 'Searching...';
if (path.value !== cachedPath) {
cachedPath = path.value;
if (conn != null) {
await conn.close();
}
await loadIndex();
}
if (conn == null) {
conn = await db.connect();
}
const out = await searchWithSql(
// DuckDB
conn,
// Embedding
queryData,
// K
5,
// Path to parquet indices
cachedPath,
// EF (the max number of candidate neighbors during search)
20
);
status.textContent = 'Ready for user!';
output.innerHTML = out.map((t) => `<p>${t}</p>`).join('')
}
const debounce = (callback, wait) => {
let timeoutId = null;
return () => {
window.clearTimeout(timeoutId);
timeoutId = window.setTimeout(async () => {
await callback();
}, wait);
};
}
const debouncedInstantSearch = debounce(async () => await performSearch(), 300);
textInput.addEventListener('input', async (e) => {
if (instantMode.checked && !search.disabled) {
debouncedInstantSearch();
}
});
search.addEventListener('click', async (e) => {
search.disabled = true;
await performSearch();
search.disabled = false;
});
blogs.addEventListener('click', async (e) => {
e.preventDefault();
e.stopPropagation();
path.value = "blogs"
instantMode.checked = false;
textInput.value = "falling in love";
await loadIndex();
status.textContent = 'This dataset is bigger - hit search when you\'re ready.'
});
frankenstein.addEventListener('click', async (e) => {
e.preventDefault();
e.stopPropagation();
instantMode.checked = true;
path.value = "frankenstein"
textInput.value = "in the ocean";
await performSearch();
search.disabled = false;
});
shakespeare.addEventListener('click', async (e) => {
e.preventDefault();
e.stopPropagation();
path.value = "shakespeare"
textInput.value = "got some blood on their hands"
await performSearch();
});
await performSearch();
search.disabled = false;
</script>
</body>
</html>