Elffuss BraidHub

kikocis/lancedb public

main · 6.1 KiBRenderedRawPermalink
1# Rust-backed Client Migration Guide
2
3In an effort to ensure all clients have the same set of capabilities we have begun migrating the
4python and node clients onto a common Rust base library. In python, this new client is part of
5the same lancedb package, exposed as an asynchronous client. Once the asynchronous client has
6reached full functionality we will begin migrating the synchronous library to be a thin wrapper
7around the asynchronous client.
8
9This guide describes the differences between the two APIs and will hopefully assist users
10that would like to migrate to the new API.
11
12## Python
13### Closeable Connections
14
15The Connection now has a `close` method. You can call this when
16you are done with the connection to eagerly free resources. Currently
17this is limited to freeing/closing the HTTP connection for remote
18connections. In the future we may add caching or other resources to
19native connections so this is probably a good practice even if you
20aren't using remote connections.
21
22In addition, the connection can be used as a context manager which may
23be a more convenient way to ensure the connection is closed.
24
25```python
26import lancedb
27
28async def my_async_fn():
29 with await lancedb.connect_async("my_uri") as db:
30 print(await db.table_names())
31```
32
33It is not mandatory to call the `close` method. If you do not call it
34then the connection will be closed when the object is garbage collected.
35
36### Closeable Table
37
38The Table now also has a `close` method, similar to the connection. This
39can be used to eagerly free the cache used by a Table object. Similar to
40the connection, it can be used as a context manager and it is not mandatory
41to 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
51The `Table.create_index` method is now used for creating both vector indices
52and scalar indices. It currently requires a column name to be specified (the
53column to index). Vector index defaults are now smarter and scale better with
54the size of the data.
55
56To specify index configuration details you will need to specify which kind of
57index you are using.
58
59#### Querying
60
61The `Table.search` method has been renamed to `AsyncTable.vector_search` for
62clarity.
63
64### Features not yet supported
65
66The following features are not yet supported by the asynchronous API. However,
67we 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
81For JS/TS users, we offer a brand new SDK [@lancedb/lancedb](https://www.npmjs.com/package/@lancedb/lancedb)
82
83We 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
103Previously `Table.schema` was a property. Now it is an async method.
104
105#### Creating Indices
106
107The `Table.createIndex` method is now used for creating both vector indices
108and scalar indices. It currently requires a column name to be specified (the
109column to index). Vector index defaults are now smarter and scale better with
110the 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
136The 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
173You can also use a schema driven approach, which parallels the Pydantic integration in our Python SDK:
174
175```ts
176const func = getRegistry().get("openai").create({apiKey: API_KEY});
177
178const data = [
179 { id: 1, text: 'Black T-Shirt', price: 10 },
180 { id: 2, text: 'Leather Jacket', price: 50 }
181]
182const 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
189const table = await db.createTable('vectors', data, {schema})
190
191```