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.
Node.js
Section titled “Node.js”Install the standard binding:
npm install hnswlib-nodeLoad 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);Python
Section titled “Python”Install the standard binding:
pip install hnswlib numpyLoad index from a local file
import numpy as npimport 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.requestimport numpy as npimport 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
dimensionsyou 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_queryare 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.