| 1 | // --8<-- [start:import] |
| 2 | import * as lancedb from "vectordb"; |
| 3 | import { |
| 4 | Schema, |
| 5 | Field, |
| 6 | Float32, |
| 7 | FixedSizeList, |
| 8 | Int32, |
| 9 | Float16, |
| 10 | } from "apache-arrow"; |
| 11 | import * as arrow from "apache-arrow"; |
| 12 | // --8<-- [end:import] |
| 13 | import * as fs from "fs"; |
| 14 | import { Table as ArrowTable, Utf8 } from "apache-arrow"; |
| 15 | |
| 16 | const example = async () => { |
| 17 | fs.rmSync("data/sample-lancedb", { recursive: true, force: true }); |
| 18 | // --8<-- [start:open_db] |
| 19 | const lancedb = require("vectordb"); |
| 20 | const uri = "data/sample-lancedb"; |
| 21 | const db = await lancedb.connect(uri); |
| 22 | // --8<-- [end:open_db] |
| 23 | |
| 24 | // --8<-- [start:create_table] |
| 25 | const tbl = await db.createTable( |
| 26 | "myTable", |
| 27 | [ |
| 28 | { vector: [3.1, 4.1], item: "foo", price: 10.0 }, |
| 29 | { vector: [5.9, 26.5], item: "bar", price: 20.0 }, |
| 30 | ], |
| 31 | { writeMode: lancedb.WriteMode.Overwrite }, |
| 32 | ); |
| 33 | // --8<-- [end:create_table] |
| 34 | { |
| 35 | // --8<-- [start:create_table_with_schema] |
| 36 | const schema = new arrow.Schema([ |
| 37 | new arrow.Field( |
| 38 | "vector", |
| 39 | new arrow.FixedSizeList( |
| 40 | 2, |
| 41 | new arrow.Field("item", new arrow.Float32(), true), |
| 42 | ), |
| 43 | ), |
| 44 | new arrow.Field("item", new arrow.Utf8(), true), |
| 45 | new arrow.Field("price", new arrow.Float32(), true), |
| 46 | ]); |
| 47 | const data = [ |
| 48 | { vector: [3.1, 4.1], item: "foo", price: 10.0 }, |
| 49 | { vector: [5.9, 26.5], item: "bar", price: 20.0 }, |
| 50 | ]; |
| 51 | const tbl = await db.createTable({ |
| 52 | name: "myTableWithSchema", |
| 53 | data, |
| 54 | schema, |
| 55 | }); |
| 56 | // --8<-- [end:create_table_with_schema] |
| 57 | } |
| 58 | |
| 59 | // --8<-- [start:add] |
| 60 | const newData = Array.from({ length: 500 }, (_, i) => ({ |
| 61 | vector: [i, i + 1], |
| 62 | item: "fizz", |
| 63 | price: i * 0.1, |
| 64 | })); |
| 65 | await tbl.add(newData); |
| 66 | // --8<-- [end:add] |
| 67 | |
| 68 | // --8<-- [start:create_index] |
| 69 | await tbl.createIndex({ |
| 70 | type: "ivf_pq", |
| 71 | num_partitions: 2, |
| 72 | num_sub_vectors: 2, |
| 73 | }); |
| 74 | // --8<-- [end:create_index] |
| 75 | |
| 76 | // --8<-- [start:create_empty_table] |
| 77 | const schema = new arrow.Schema([ |
| 78 | new arrow.Field("id", new arrow.Int32()), |
| 79 | new arrow.Field("name", new arrow.Utf8()), |
| 80 | ]); |
| 81 | |
| 82 | const empty_tbl = await db.createTable({ name: "empty_table", schema }); |
| 83 | // --8<-- [end:create_empty_table] |
| 84 | { |
| 85 | // --8<-- [start:create_f16_table] |
| 86 | const dim = 16; |
| 87 | const total = 10; |
| 88 | const schema = new Schema([ |
| 89 | new Field("id", new Int32()), |
| 90 | new Field( |
| 91 | "vector", |
| 92 | new FixedSizeList(dim, new Field("item", new Float16(), true)), |
| 93 | false, |
| 94 | ), |
| 95 | ]); |
| 96 | const data = lancedb.makeArrowTable( |
| 97 | Array.from(Array(total), (_, i) => ({ |
| 98 | id: i, |
| 99 | vector: Array.from(Array(dim), Math.random), |
| 100 | })), |
| 101 | { schema }, |
| 102 | ); |
| 103 | const table = await db.createTable("f16_tbl", data); |
| 104 | // --8<-- [end:create_f16_table] |
| 105 | } |
| 106 | |
| 107 | // --8<-- [start:search] |
| 108 | const query = await tbl.search([100, 100]).limit(2).execute(); |
| 109 | // --8<-- [end:search] |
| 110 | console.log(query); |
| 111 | |
| 112 | // --8<-- [start:delete] |
| 113 | await tbl.delete('item = "fizz"'); |
| 114 | // --8<-- [end:delete] |
| 115 | |
| 116 | // --8<-- [start:drop_table] |
| 117 | await db.dropTable("myTable"); |
| 118 | // --8<-- [end:drop_table] |
| 119 | }; |
| 120 | |
| 121 | async function main() { |
| 122 | await example(); |
| 123 | console.log("Basic example: done"); |
| 124 | } |
| 125 | |
| 126 | main(); |