| 344 | } |
| 345 | |
| 346 | async function query(searchQuery: string, k = 3): Promise<SearchResult[]> { |
| 347 | if (searchQuery && searchQuery.length > 0) { |
| 348 | const res = await knnQuery(searchQuery, k); |
| 349 | const docs: SearchResult[] = []; |
| 350 | if (res) { |
| 351 | for (let i = 0; i < (res?.neighbors.length || 0); i++) { |
| 352 | const docId = res.neighbors[i]; |
| 353 | const distance = res.distances[i]; |
| 354 | if (documentStore?.idb) { |
| 355 | const doc = await documentStore.idb.documents.get(docId); |
| 356 | if (doc) { |
| 357 | docs.push({ |
| 358 | distance, |
| 359 | document: doc, |
| 360 | }); |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | return docs; |
| 366 | } else { |
| 367 | return []; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | // TODO: move this into the useVectorStore function |
| 372 | console.log(`load ${statename}`); |