| 1 | // --8<-- [start:import] |
| 2 | import * as lancedb from "vectordb"; |
| 3 | // --8<-- [end:import] |
| 4 | import * as fs from "fs"; |
| 5 | |
| 6 | async function setup() { |
| 7 | fs.rmSync("data/sample-lancedb", { recursive: true, force: true }); |
| 8 | const db = await lancedb.connect("data/sample-lancedb"); |
| 9 | |
| 10 | let data = []; |
| 11 | for (let i = 0; i < 10_000; i++) { |
| 12 | data.push({ |
| 13 | vector: Array(1536).fill(i), |
| 14 | id: `${i}`, |
| 15 | content: "", |
| 16 | longId: `${i}`, |
| 17 | }); |
| 18 | } |
| 19 | await db.createTable("my_vectors", data); |
| 20 | } |
| 21 | |
| 22 | async () => { |
| 23 | await setup(); |
| 24 | |
| 25 | // --8<-- [start:search1] |
| 26 | const db = await lancedb.connect("data/sample-lancedb"); |
| 27 | const tbl = await db.openTable("my_vectors"); |
| 28 | |
| 29 | const results_1 = await tbl.search(Array(1536).fill(1.2)).limit(10).execute(); |
| 30 | // --8<-- [end:search1] |
| 31 | |
| 32 | // --8<-- [start:search2] |
| 33 | const results_2 = await tbl |
| 34 | .search(Array(1536).fill(1.2)) |
| 35 | .metricType(lancedb.MetricType.Cosine) |
| 36 | .limit(10) |
| 37 | .execute(); |
| 38 | // --8<-- [end:search2] |
| 39 | |
| 40 | console.log("search: done"); |
| 41 | }; |