-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Buffers, IP, Factories & more * scope & instance data * export MetricType enum * fix d.ts * fix JSDoc * fix type of sub class * feedback changes * fix factory metric * remove handlescope * inline constructor causes stability issues * fix inline variables warning * update JSDoc * remove unreached code and its test case * remove fromFactory from IndexFlatL2 and IndexFlatIP * update docs and example --------- Co-authored-by: ewfian <[email protected]>
- Loading branch information
Showing
8 changed files
with
492 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,60 @@ | ||
const { IndexFlatL2 } = require('../'); | ||
const { IndexFlatL2, Index, IndexFlatIP, MetricType } = require('../'); | ||
|
||
const dimension = 2; | ||
const index = new IndexFlatL2(dimension); | ||
|
||
console.log(index.getDimension()); | ||
console.log(index.isTrained()); | ||
console.log(index.ntotal()); | ||
console.log(index.getDimension()); // 2 | ||
console.log(index.isTrained()); // true | ||
console.log(index.ntotal()); // 0 | ||
|
||
// inserting data into index. | ||
index.add([1, 0]); | ||
index.add([1, 2]); | ||
index.add([1, 3]); | ||
index.add([1, 1]); | ||
console.log(index.ntotal()); | ||
|
||
console.log(index.ntotal()); // 4 | ||
|
||
const k = 4; | ||
const results = index.search([1, 0], k); | ||
console.log(results.labels); | ||
console.log(results.distances); | ||
console.log(results.labels); // [ 0, 3, 1, 2 ] | ||
console.log(results.distances); // [ 0, 1, 4, 9 ] | ||
|
||
// Save index | ||
const fname = 'faiss.index'; | ||
index.write(fname); | ||
|
||
// Load saved index | ||
const index_loaded = IndexFlatL2.read(fname); | ||
console.log(index_loaded.getDimension()); | ||
console.log(index_loaded.ntotal()); | ||
console.log(index_loaded.getDimension()); //2 | ||
console.log(index_loaded.ntotal()); //4 | ||
const results1 = index_loaded.search([1, 1], 4); | ||
console.log(results1.labels); | ||
console.log(results1.distances); | ||
console.log(results1.labels); // [ 3, 0, 1, 2 ] | ||
console.log(results1.distances); // [ 0, 1, 1, 4 ] | ||
|
||
// Merge index | ||
const newIndex = new IndexFlatL2(dimension); | ||
newIndex.mergeFrom(index); | ||
console.log(newIndex.ntotal()); | ||
console.log(newIndex.ntotal()); // 4 | ||
|
||
console.log(newIndex.search([1, 2], 1)); | ||
// Remove items | ||
console.log(newIndex.search([1, 2], 1)); // { distances: [ 0 ], labels: [ 1 ] } | ||
const removedCount = newIndex.removeIds([0]); | ||
console.log(removedCount); | ||
console.log(newIndex.ntotal()); | ||
console.log(newIndex.search([1, 2], 1)); | ||
console.log(removedCount); // 1 | ||
console.log(newIndex.ntotal()); // 3 | ||
console.log(newIndex.search([1, 2], 1)); // { distances: [ 0 ], labels: [ 0 ] } | ||
|
||
// IndexFlatIP | ||
const ipIndex = new IndexFlatIP(2); | ||
ipIndex.add([1, 0]); | ||
|
||
// Serialize an index | ||
const index_buf = newIndex.toBuffer(); | ||
const deserializedIndex = Index.fromBuffer(index_buf); | ||
console.log(deserializedIndex.ntotal()); // 3 | ||
|
||
// Factory index | ||
const hnswIndex = Index.fromFactory(2, 'HNSW,Flat', MetricType.METRIC_INNER_PRODUCT); | ||
const x = [1, 0, 0, 1]; | ||
hnswIndex.train(x); | ||
hnswIndex.add(x); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,17 @@ | ||
const faiss = require('bindings')('faiss-node'); | ||
|
||
faiss.MetricType = void 0; | ||
var MetricType; | ||
(function (MetricType) { | ||
MetricType[MetricType["METRIC_INNER_PRODUCT"] = 0] = "METRIC_INNER_PRODUCT"; | ||
MetricType[MetricType["METRIC_L2"] = 1] = "METRIC_L2"; | ||
MetricType[MetricType["METRIC_L1"] = 2] = "METRIC_L1"; | ||
MetricType[MetricType["METRIC_Linf"] = 3] = "METRIC_Linf"; | ||
MetricType[MetricType["METRIC_Lp"] = 4] = "METRIC_Lp"; | ||
MetricType[MetricType["METRIC_Canberra"] = 20] = "METRIC_Canberra"; | ||
MetricType[MetricType["METRIC_BrayCurtis"] = 21] = "METRIC_BrayCurtis"; | ||
MetricType[MetricType["METRIC_JensenShannon"] = 22] = "METRIC_JensenShannon"; | ||
MetricType[MetricType["METRIC_Jaccard"] = 23] = "METRIC_Jaccard"; | ||
})(MetricType || (faiss.MetricType = MetricType = {})); | ||
|
||
module.exports = faiss; |
Oops, something went wrong.