RecapAnon commited on
Commit ·
dee4622
1
Parent(s): f0c3543
Update entity-db query to read in batches.
Browse files- entity-db.js +33 -9
entity-db.js
CHANGED
|
@@ -210,7 +210,7 @@ class EntityDB {
|
|
| 210 |
}
|
| 211 |
|
| 212 |
// Query vectors by cosine similarity (using a text input that will be converted into embeddings)
|
| 213 |
-
async query(queryText, { limit = 10 } = {}) {
|
| 214 |
try {
|
| 215 |
// Get embeddings for the query text
|
| 216 |
const queryVector = await getEmbeddingFromText(queryText, this.model);
|
|
@@ -218,16 +218,40 @@ class EntityDB {
|
|
| 218 |
const db = await this.dbPromise;
|
| 219 |
const transaction = db.transaction("vectors", "readonly");
|
| 220 |
const store = transaction.objectStore("vectors");
|
| 221 |
-
const vectors = await store.getAll(); // Retrieve all vectors
|
| 222 |
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
-
|
| 230 |
-
|
|
|
|
| 231 |
} catch (error) {
|
| 232 |
throw new Error(`Error querying vectors: ${error}`);
|
| 233 |
}
|
|
|
|
| 210 |
}
|
| 211 |
|
| 212 |
// Query vectors by cosine similarity (using a text input that will be converted into embeddings)
|
| 213 |
+
async query(queryText, { limit = 10, batchSize = 1000 } = {}) {
|
| 214 |
try {
|
| 215 |
// Get embeddings for the query text
|
| 216 |
const queryVector = await getEmbeddingFromText(queryText, this.model);
|
|
|
|
| 218 |
const db = await this.dbPromise;
|
| 219 |
const transaction = db.transaction("vectors", "readonly");
|
| 220 |
const store = transaction.objectStore("vectors");
|
|
|
|
| 221 |
|
| 222 |
+
let allSimilarities = [];
|
| 223 |
+
let range = null;
|
| 224 |
+
|
| 225 |
+
// Read vectors in batches and calculate similarities
|
| 226 |
+
while (true) {
|
| 227 |
+
const keys = await store.getAllKeys(range, batchSize);
|
| 228 |
+
const vectors = await store.getAll(range, batchSize);
|
| 229 |
+
|
| 230 |
+
if (vectors && vectors.length > 0) {
|
| 231 |
+
// Calculate cosine similarity for each vector in this batch
|
| 232 |
+
const batchSimilarities = vectors.map((entry, index) => ({
|
| 233 |
+
...entry,
|
| 234 |
+
similarity: cosineSimilarity(queryVector, entry.vector),
|
| 235 |
+
}));
|
| 236 |
+
|
| 237 |
+
allSimilarities.push(...batchSimilarities);
|
| 238 |
+
|
| 239 |
+
// Continue to next batch if we got a full batch
|
| 240 |
+
if (vectors.length === batchSize) {
|
| 241 |
+
range = IDBKeyRange.lowerBound(keys.at(-1), true);
|
| 242 |
+
} else {
|
| 243 |
+
// Last batch reached
|
| 244 |
+
break;
|
| 245 |
+
}
|
| 246 |
+
} else {
|
| 247 |
+
// No more vectors
|
| 248 |
+
break;
|
| 249 |
+
}
|
| 250 |
+
}
|
| 251 |
|
| 252 |
+
// Sort by similarity (descending) and return top N results
|
| 253 |
+
allSimilarities.sort((a, b) => b.similarity - a.similarity);
|
| 254 |
+
return allSimilarities.slice(0, limit);
|
| 255 |
} catch (error) {
|
| 256 |
throw new Error(`Error querying vectors: ${error}`);
|
| 257 |
}
|