hyperspace-sdk-ts 3.0.0 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/dist/client.d.ts +5 -0
- package/dist/client.js +39 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -39,6 +39,9 @@ async function main() {
|
|
|
39
39
|
await client.insert(1, [0.1, 0.2, 0.3], { source: "demo" }, collection);
|
|
40
40
|
await client.insert(2, [0.2, 0.1, 0.4], { source: "demo" }, collection);
|
|
41
41
|
|
|
42
|
+
// Delete vector by ID
|
|
43
|
+
await client.delete(1);
|
|
44
|
+
|
|
42
45
|
const results = await client.search([0.1, 0.2, 0.3], 5, collection);
|
|
43
46
|
console.log(results);
|
|
44
47
|
|
package/dist/client.d.ts
CHANGED
|
@@ -55,6 +55,10 @@ export declare const HyperbolicMath: {
|
|
|
55
55
|
riemannianGradient(x: number[], euclideanGrad: number[], c?: number): number[];
|
|
56
56
|
parallelTransport(x: number[], y: number[], v: number[], c?: number): number[];
|
|
57
57
|
frechetMean(points: number[][], c?: number, maxIter?: number, tol?: number): number[];
|
|
58
|
+
analyzeDeltaHyperbolicity(vectors: number[][], numSamples?: number): {
|
|
59
|
+
delta: number;
|
|
60
|
+
recommendation: string;
|
|
61
|
+
};
|
|
58
62
|
};
|
|
59
63
|
export declare class HyperspaceClient {
|
|
60
64
|
private client;
|
|
@@ -65,6 +69,7 @@ export declare class HyperspaceClient {
|
|
|
65
69
|
constructor(host?: string, apiKey?: string, userId?: string);
|
|
66
70
|
createCollection(name: string, dimension: number, metric: string): Promise<boolean>;
|
|
67
71
|
deleteCollection(name: string): Promise<boolean>;
|
|
72
|
+
delete(id: number, collection?: string): Promise<boolean>;
|
|
68
73
|
insert(id: number, vector: number[] | Float32Array | Float64Array, meta?: {
|
|
69
74
|
[key: string]: string;
|
|
70
75
|
}, collection?: string, durability?: DurabilityLevel, typedMetadata?: {
|
package/dist/client.js
CHANGED
|
@@ -152,6 +152,33 @@ exports.HyperbolicMath = {
|
|
|
152
152
|
mu = exports.HyperbolicMath.projectToBall(mu, c);
|
|
153
153
|
}
|
|
154
154
|
return mu;
|
|
155
|
+
},
|
|
156
|
+
analyzeDeltaHyperbolicity(vectors, numSamples = 1000) {
|
|
157
|
+
if (vectors.length < 4)
|
|
158
|
+
return { delta: 0, recommendation: 'euclidean' };
|
|
159
|
+
const l2Dist = (a, b) => Math.sqrt(a.reduce((s, x, i) => s + (x - b[i]) ** 2, 0));
|
|
160
|
+
let maxDelta = 0;
|
|
161
|
+
for (let s = 0; s < numSamples; s++) {
|
|
162
|
+
const indices = new Set();
|
|
163
|
+
while (indices.size < 4)
|
|
164
|
+
indices.add(Math.floor(Math.random() * vectors.length));
|
|
165
|
+
const [i, j, k, l] = Array.from(indices);
|
|
166
|
+
const d_ij = l2Dist(vectors[i], vectors[j]);
|
|
167
|
+
const d_kl = l2Dist(vectors[k], vectors[l]);
|
|
168
|
+
const d_ik = l2Dist(vectors[i], vectors[k]);
|
|
169
|
+
const d_jl = l2Dist(vectors[j], vectors[l]);
|
|
170
|
+
const d_il = l2Dist(vectors[i], vectors[l]);
|
|
171
|
+
const d_jk = l2Dist(vectors[j], vectors[k]);
|
|
172
|
+
const s1 = d_ij + d_kl;
|
|
173
|
+
const s2 = d_ik + d_jl;
|
|
174
|
+
const s3 = d_il + d_jk;
|
|
175
|
+
const sums = [s1, s2, s3].sort((a, b) => b - a);
|
|
176
|
+
const delta = (sums[0] - sums[1]) / 2.0;
|
|
177
|
+
if (delta > maxDelta)
|
|
178
|
+
maxDelta = delta;
|
|
179
|
+
}
|
|
180
|
+
const recommendation = maxDelta < 0.15 ? 'lorentz' : (maxDelta < 0.30 ? 'poincare' : 'l2');
|
|
181
|
+
return { delta: maxDelta, recommendation };
|
|
155
182
|
}
|
|
156
183
|
};
|
|
157
184
|
class HyperspaceClient {
|
|
@@ -241,6 +268,18 @@ class HyperspaceClient {
|
|
|
241
268
|
});
|
|
242
269
|
});
|
|
243
270
|
}
|
|
271
|
+
delete(id, collection = '') {
|
|
272
|
+
return new Promise((resolve, reject) => {
|
|
273
|
+
const req = new hyperspace_pb.DeleteRequest();
|
|
274
|
+
req.setCollection(collection);
|
|
275
|
+
req.setId(id);
|
|
276
|
+
this.client.delete(req, this.metadata, (err, resp) => {
|
|
277
|
+
if (err)
|
|
278
|
+
return reject(err);
|
|
279
|
+
resolve(resp.getSuccess());
|
|
280
|
+
});
|
|
281
|
+
});
|
|
282
|
+
}
|
|
244
283
|
insert(id, vector, meta, collection = '', durability = hyperspace_pb_1.DurabilityLevel.DEFAULT_LEVEL, typedMetadata) {
|
|
245
284
|
return new Promise((resolve, reject) => {
|
|
246
285
|
const req = new hyperspace_pb_1.InsertRequest();
|