Skip to content

Loading your index

When a build completes you get a downloadUrl pointing to a serialized HNSW file. The file is compatible with the standard hnswlib libraries.

Install the standard binding:

Terminal window
npm install hnswlib-node

Load index from a local file

const { HierarchicalNSW } = require('hnswlib-node');
// set to 'l2' for default; for now only 'l2' is supported. future will add support for 'cosine'.
const index = new HierarchicalNSW('l2', 768);
index.readIndexSync('/tmp/index.hnsw');
const query = Array.from({ length: 768 }, () => Math.random());
const result = index.searchKnn(query, 5);
console.log(result.neighbors, result.distances);

Load index from a remote URL

const fs = require('fs');
const { HierarchicalNSW } = require('hnswlib-node');
const url = 'https://...hnsw';
const filePath = '/tmp/index.hnsw';
const res = await fetch(url);
const buf = Buffer.from(await res.arrayBuffer());
fs.writeFileSync(filePath, buf);
// set to 'l2' for default; for now only 'l2' is supported. future will add support for 'cosine'.
const index = new HierarchicalNSW('l2', 768);
index.readIndexSync(filePath);
const query = Array.from({ length: 768 }, () => Math.random());
const result = index.searchKnn(query, 5);
console.log(result.neighbors, result.distances);

Install the standard binding:

Terminal window
pip install hnswlib numpy

Load index from a local file

import numpy as np
import hnswlib
# set to 'l2' for default; for now only 'l2' is supported. future will add support for 'cosine'.
index = hnswlib.Index(space='l2', dim=768)
index.load_index('/tmp/index.hnsw')
query = np.random.rand(768).astype('float32')
labels, distances = index.knn_query(query, k=5)
print(labels, distances)

Load index from a remote URL

import urllib.request
import numpy as np
import hnswlib
url = 'https://...hnsw'
file_path = '/tmp/index.hnsw'
urllib.request.urlretrieve(url, file_path)
# set to 'l2' for default; for now only 'l2' is supported. future will add support for 'cosine'.
index = hnswlib.Index(space='l2', dim=768)
index.load_index(file_path)
query = np.random.rand(768).astype('float32')
labels, distances = index.knn_query(query, k=5)
print(labels, distances)
  • Use the same dimensions you used when submitting the build.
  • Pass 'l2' as the distance space (the first argument to the load call). Currently only 'l2' is supported; 'cosine' is on the roadmap.
  • The neighbor IDs returned by knn_query are the row indices from your original vector file. Map them back to your application’s record IDs in your own database.
  • For complete runnable examples (including end-to-end submit + poll + search), see TensorTensorCom/examples on GitHub.