| 1 | # Rust-backed Client Migration Guide |
| 2 | |
| 3 | In an effort to ensure all clients have the same set of capabilities we have begun migrating the |
| 4 | python and node clients onto a common Rust base library. In python, this new client is part of |
| 5 | the same lancedb package, exposed as an asynchronous client. Once the asynchronous client has |
| 6 | reached full functionality we will begin migrating the synchronous library to be a thin wrapper |
| 7 | around the asynchronous client. |
| 8 | |
| 9 | This guide describes the differences between the two APIs and will hopefully assist users |
| 10 | that would like to migrate to the new API. |
| 11 | |
| 12 | ## Python |
| 13 | ### Closeable Connections |
| 14 | |
| 15 | The Connection now has a `close` method. You can call this when |
| 16 | you are done with the connection to eagerly free resources. Currently |
| 17 | this is limited to freeing/closing the HTTP connection for remote |
| 18 | connections. In the future we may add caching or other resources to |
| 19 | native connections so this is probably a good practice even if you |
| 20 | aren't using remote connections. |
| 21 | |
| 22 | In addition, the connection can be used as a context manager which may |
| 23 | be a more convenient way to ensure the connection is closed. |
| 24 | |
| 25 | ```python |
| 26 | import lancedb |
| 27 | |
| 28 | async def my_async_fn(): |
| 29 | with await lancedb.connect_async("my_uri") as db: |
| 30 | print(await db.table_names()) |
| 31 | ``` |
| 32 | |
| 33 | It is not mandatory to call the `close` method. If you do not call it |
| 34 | then the connection will be closed when the object is garbage collected. |
| 35 | |
| 36 | ### Closeable Table |
| 37 | |
| 38 | The Table now also has a `close` method, similar to the connection. This |
| 39 | can be used to eagerly free the cache used by a Table object. Similar to |
| 40 | the connection, it can be used as a context manager and it is not mandatory |
| 41 | to call the `close` method. |
| 42 | |
| 43 | #### Changes to Table APIs |
| 44 | |
| 45 | - Previously `Table.schema` was a property. Now it is an async method. |
| 46 | - The method `Table.__len__` was removed and `len(table)` will no longer |
| 47 | work. Use `Table.count_rows` instead. |
| 48 | |
| 49 | #### Creating Indices |
| 50 | |
| 51 | The `Table.create_index` method is now used for creating both vector indices |
| 52 | and scalar indices. It currently requires a column name to be specified (the |
| 53 | column to index). Vector index defaults are now smarter and scale better with |
| 54 | the size of the data. |
| 55 | |
| 56 | To specify index configuration details you will need to specify which kind of |
| 57 | index you are using. |
| 58 | |
| 59 | #### Querying |
| 60 | |
| 61 | The `Table.search` method has been renamed to `AsyncTable.vector_search` for |
| 62 | clarity. |
| 63 | |
| 64 | ### Features not yet supported |
| 65 | |
| 66 | The following features are not yet supported by the asynchronous API. However, |
| 67 | we plan to support them soon. |
| 68 | |
| 69 | - You cannot specify an embedding function when creating or opening a table. |
| 70 | You must calculate embeddings yourself if using the asynchronous API |
| 71 | - The merge insert operation is not supported in the asynchronous API |
| 72 | - Cleanup / compact / optimize indices are not supported in the asynchronous API |
| 73 | - add / alter columns is not supported in the asynchronous API |
| 74 | - The asynchronous API does not yet support any full text search or reranking |
| 75 | search |
| 76 | - Remote connections to LanceDb Cloud are not yet supported. |
| 77 | - The method Table.head is not yet supported. |
| 78 | |
| 79 | ## TypeScript/JavaScript |
| 80 | |
| 81 | For JS/TS users, we offer a brand new SDK [@lancedb/lancedb](https://www.npmjs.com/package/@lancedb/lancedb) |
| 82 | |
| 83 | We tried to keep the API as similar as possible to the previous version, but there are a few small changes. Here are the most important ones: |
| 84 | |
| 85 | ### Creating Tables |
| 86 | |
| 87 | [CreateTableOptions.writeOptions.writeMode](./javascript/interfaces/WriteOptions.md#writemode) has been replaced with [CreateTableOptions.mode](./js/interfaces/CreateTableOptions.md#mode) |
| 88 | |
| 89 | === "vectordb (deprecated)" |
| 90 | |
| 91 | ```ts |
| 92 | db.createTable(tableName, data, { writeMode: lancedb.WriteMode.Overwrite }); |
| 93 | ``` |
| 94 | |
| 95 | === "@lancedb/lancedb" |
| 96 | |
| 97 | ```ts |
| 98 | db.createTable(tableName, data, { mode: "overwrite" }) |
| 99 | ``` |
| 100 | |
| 101 | ### Changes to Table APIs |
| 102 | |
| 103 | Previously `Table.schema` was a property. Now it is an async method. |
| 104 | |
| 105 | #### Creating Indices |
| 106 | |
| 107 | The `Table.createIndex` method is now used for creating both vector indices |
| 108 | and scalar indices. It currently requires a column name to be specified (the |
| 109 | column to index). Vector index defaults are now smarter and scale better with |
| 110 | the size of the data. |
| 111 | |
| 112 | === "vectordb (deprecated)" |
| 113 | |
| 114 | ```ts |
| 115 | await tbl.createIndex({ |
| 116 | column: "vector", // default |
| 117 | type: "ivf_pq", |
| 118 | num_partitions: 2, |
| 119 | num_sub_vectors: 2, |
| 120 | }); |
| 121 | ``` |
| 122 | |
| 123 | === "@lancedb/lancedb" |
| 124 | |
| 125 | ```ts |
| 126 | await table.createIndex("vector", { |
| 127 | config: lancedb.Index.ivfPq({ |
| 128 | numPartitions: 2, |
| 129 | numSubVectors: 2, |
| 130 | }), |
| 131 | }); |
| 132 | ``` |
| 133 | |
| 134 | ### Embedding Functions |
| 135 | |
| 136 | The embedding API has been completely reworked, and it now more closely resembles the Python API, including the new [embedding registry](./js/classes/embedding.EmbeddingFunctionRegistry.md) |
| 137 | |
| 138 | === "vectordb (deprecated)" |
| 139 | |
| 140 | ```ts |
| 141 | |
| 142 | const embeddingFunction = new lancedb.OpenAIEmbeddingFunction('text', API_KEY) |
| 143 | const data = [ |
| 144 | { id: 1, text: 'Black T-Shirt', price: 10 }, |
| 145 | { id: 2, text: 'Leather Jacket', price: 50 } |
| 146 | ] |
| 147 | const table = await db.createTable('vectors', data, embeddingFunction) |
| 148 | ``` |
| 149 | |
| 150 | === "@lancedb/lancedb" |
| 151 | |
| 152 | ```ts |
| 153 | import * as lancedb from "@lancedb/lancedb"; |
| 154 | import * as arrow from "apache-arrow"; |
| 155 | import { LanceSchema, getRegistry } from "@lancedb/lancedb/embedding"; |
| 156 | |
| 157 | const func = getRegistry().get("openai").create({apiKey: API_KEY}); |
| 158 | |
| 159 | const data = [ |
| 160 | { id: 1, text: 'Black T-Shirt', price: 10 }, |
| 161 | { id: 2, text: 'Leather Jacket', price: 50 } |
| 162 | ] |
| 163 | |
| 164 | const table = await db.createTable('vectors', data, { |
| 165 | embeddingFunction: { |
| 166 | sourceColumn: "text", |
| 167 | function: func, |
| 168 | } |
| 169 | }) |
| 170 | |
| 171 | ``` |
| 172 | |
| 173 | You can also use a schema driven approach, which parallels the Pydantic integration in our Python SDK: |
| 174 | |
| 175 | ```ts |
| 176 | const func = getRegistry().get("openai").create({apiKey: API_KEY}); |
| 177 | |
| 178 | const data = [ |
| 179 | { id: 1, text: 'Black T-Shirt', price: 10 }, |
| 180 | { id: 2, text: 'Leather Jacket', price: 50 } |
| 181 | ] |
| 182 | const schema = LanceSchema({ |
| 183 | id: new arrow.Int32(), |
| 184 | text: func.sourceField(new arrow.Utf8()), |
| 185 | price: new arrow.Float64(), |
| 186 | vector: func.vectorField() |
| 187 | }) |
| 188 | |
| 189 | const table = await db.createTable('vectors', data, {schema}) |
| 190 | |
| 191 | ``` |