Vector database in browser based on IndexedDB.
- CRUD operations for vectors
- Similarity search using multiple distance metrics
- Optional LSH based index with approximate nearest neighbour (ANN) search
npm install vecal
# or
yarn add vecalimport { VectorDB } from 'vecal';
const db = new VectorDB({ dbName: 'example', dimension: 3 });
const id = await db.add(new Float32Array([0.9, 0.1, 0.1]), { label: 'Apple' });
const results = await db.search(new Float32Array([0.85, 0.2, 0.15]), 2);
console.log(results);await db.buildIndex(8);
const ann = await db.annSearch(new Float32Array([0.85, 0.2, 0.15]), 2);await db.close();Creates a database instance. config fields:
dbName– name of the IndexedDB database.dimension– length of the stored vectors.storeName– optional object store name (defaults to"vectors").distanceType– optional default distance metric.minkowskiP– power parameter when using Minkowski distance (default3).
Add a vector with optional metadata. Returns the generated id.
Retrieve a stored entry.
Partially update an entry.
Remove an entry from the database.
Build an LSH index from all entries. numHashes controls the number of hyperplanes (default 10).
Exact similarity search. distanceType can be "cosine", "l2", "l1", "dot", "hamming", or "minkowski".
Approximate nearest neighbour search using the LSH index. The index is built lazily when first needed. distanceType uses the same options as search.
Close the underlying IndexedDB connection.
VectorDBConfigVectorEntrySearchResultDistanceType
The src/main.ts file in this repository demonstrates how to build a small Hacker News search tool. The high level steps are:
- obtain an OpenAI API key;
- fetch items to index;
- convert each title to an embedding using the API;
- create a
VectorDBwith the embedding dimension and store each vector; - run searches with
db.searchordb.annSearch.
Refer to the code for a full example.