Elffuss BraidHub

kikocis/lancedb public

main · 16.9 KiBRenderedRawPermalink
1# Quick start
2
3!!! info "LanceDB can be run in a number of ways:"
4
5 * Embedded within an existing backend (like your Django, Flask, Node.js or FastAPI application)
6 * Directly from a client application like a Jupyter notebook for analytical workloads
7 * Deployed as a remote serverless database
8
9![](assets/lancedb_embedded_explanation.png)
10
11## Installation
12
13=== "Python"
14
15 ```shell
16 pip install lancedb
17 ```
18
19=== "Typescript[^1]"
20 === "@lancedb/lancedb"
21
22 ```shell
23 npm install @lancedb/lancedb
24 ```
25 !!! note "Bundling `@lancedb/lancedb` apps with Webpack"
26
27 Since LanceDB contains a prebuilt Node binary, you must configure `next.config.js` to exclude it from webpack. This is required for both using Next.js and deploying a LanceDB app on Vercel.
28
29 ```javascript
30 /** @type {import('next').NextConfig} */
31 module.exports = ({
32 webpack(config) {
33 config.externals.push({ '@lancedb/lancedb': '@lancedb/lancedb' })
34 return config;
35 }
36 })
37 ```
38
39 !!! note "Yarn users"
40
41 Unlike other package managers, Yarn does not automatically resolve peer dependencies. If you are using Yarn, you will need to manually install 'apache-arrow':
42
43 ```shell
44 yarn add apache-arrow
45 ```
46
47 === "vectordb (deprecated)"
48
49 ```shell
50 npm install vectordb
51 ```
52 !!! note "Bundling `vectordb` apps with Webpack"
53
54 Since LanceDB contains a prebuilt Node binary, you must configure `next.config.js` to exclude it from webpack. This is required for both using Next.js and deploying a LanceDB app on Vercel.
55
56 ```javascript
57 /** @type {import('next').NextConfig} */
58 module.exports = ({
59 webpack(config) {
60 config.externals.push({ vectordb: 'vectordb' })
61 return config;
62 }
63 })
64 ```
65
66 !!! note "Yarn users"
67
68 Unlike other package managers, Yarn does not automatically resolve peer dependencies. If you are using Yarn, you will need to manually install 'apache-arrow':
69
70 ```shell
71 yarn add apache-arrow
72 ```
73
74=== "Rust"
75
76 ```shell
77 cargo add lancedb
78 ```
79
80 !!! info "To use the lancedb create, you first need to install protobuf."
81
82 === "macOS"
83
84 ```shell
85 brew install protobuf
86 ```
87
88 === "Ubuntu/Debian"
89
90 ```shell
91 sudo apt install -y protobuf-compiler libssl-dev
92 ```
93
94 !!! info "Please also make sure you're using the same version of Arrow as in the [lancedb crate](https://github.com/lancedb/lancedb/blob/main/Cargo.toml)"
95
96### Preview releases
97
98Stable releases are created about every 2 weeks. For the latest features and bug
99fixes, you can install the preview release. These releases receive the same
100level of testing as stable releases, but are not guaranteed to be available for
101more than 6 months after they are released. Once your application is stable, we
102recommend switching to stable releases.
103
104=== "Python"
105
106 ```shell
107 pip install --pre --extra-index-url https://pypi.fury.io/lancedb/ lancedb
108 ```
109
110=== "Typescript[^1]"
111
112 === "@lancedb/lancedb"
113
114 ```shell
115 npm install @lancedb/lancedb@preview
116 ```
117 === "vectordb (deprecated)"
118
119 ```shell
120 npm install vectordb@preview
121 ```
122
123=== "Rust"
124
125 We don't push preview releases to crates.io, but you can referent the tag
126 in GitHub within your Cargo dependencies:
127
128 ```toml
129 [dependencies]
130 lancedb = { git = "https://github.com/lancedb/lancedb.git", tag = "vX.Y.Z-beta.N" }
131 ```
132
133## Connect to a database
134
135=== "Python"
136
137 ```python
138 --8<-- "python/python/tests/docs/test_basic.py:imports"
139 --8<-- "python/python/tests/docs/test_basic.py:connect"
140
141 --8<-- "python/python/tests/docs/test_basic.py:connect_async"
142 ```
143
144 !!! note "Asynchronous Python API"
145
146 The asynchronous Python API is new and has some slight differences compared
147 to the synchronous API. Feel free to start using the asynchronous version.
148 Once all features have migrated we will start to move the synchronous API to
149 use the same syntax as the asynchronous API. To help with this migration we
150 have created a [migration guide](migration.md) detailing the differences.
151
152=== "Typescript[^1]"
153
154 === "@lancedb/lancedb"
155
156 ```typescript
157 import * as lancedb from "@lancedb/lancedb";
158 import * as arrow from "apache-arrow";
159
160 --8<-- "nodejs/examples/basic.ts:connect"
161 ```
162
163 === "vectordb (deprecated)"
164
165 ```typescript
166 --8<-- "docs/src/basic_legacy.ts:open_db"
167 ```
168
169=== "Rust"
170
171 ```rust
172 #[tokio::main]
173 async fn main() -> Result<()> {
174 --8<-- "rust/lancedb/examples/simple.rs:connect"
175 }
176 ```
177
178 !!! info "See [examples/simple.rs](https://github.com/lancedb/lancedb/tree/main/rust/lancedb/examples/simple.rs) for a full working example."
179
180LanceDB will create the directory if it doesn't exist (including parent directories).
181
182If you need a reminder of the uri, you can call `db.uri()`.
183
184## Create a table
185
186### Create a table from initial data
187
188If you have data to insert into the table at creation time, you can simultaneously create a
189table and insert the data into it. The schema of the data will be used as the schema of the
190table.
191
192=== "Python"
193
194 ```python
195 --8<-- "python/python/tests/docs/test_basic.py:create_table"
196 --8<-- "python/python/tests/docs/test_basic.py:create_table_async"
197 ```
198
199 If the table already exists, LanceDB will raise an error by default.
200 If you want to overwrite the table, you can pass in `mode="overwrite"`
201 to the `create_table` method.
202
203 You can also pass in a pandas DataFrame directly:
204
205 ```python
206 --8<-- "python/python/tests/docs/test_basic.py:create_table_pandas"
207 --8<-- "python/python/tests/docs/test_basic.py:create_table_async_pandas"
208 ```
209
210=== "Typescript[^1]"
211
212 === "@lancedb/lancedb"
213
214 ```typescript
215 --8<-- "nodejs/examples/basic.ts:create_table"
216 ```
217
218 === "vectordb (deprecated)"
219
220 ```typescript
221 --8<-- "docs/src/basic_legacy.ts:create_table"
222 ```
223
224 If the table already exists, LanceDB will raise an error by default.
225 If you want to overwrite the table, you can pass in `mode:"overwrite"`
226 to the `createTable` function.
227
228=== "Rust"
229
230 ```rust
231 --8<-- "rust/lancedb/examples/simple.rs:create_table"
232 ```
233
234 If the table already exists, LanceDB will raise an error by default. See
235 [the mode option](https://docs.rs/lancedb/latest/lancedb/connection/struct.CreateTableBuilder.html#method.mode)
236 for details on how to overwrite (or open) existing tables instead.
237
238 !!! Providing table records in Rust
239
240 The Rust SDK currently expects data to be provided as an Arrow
241 [RecordBatchReader](https://docs.rs/arrow-array/latest/arrow_array/trait.RecordBatchReader.html)
242 Support for additional formats (such as serde or polars) is on the roadmap.
243
244!!! info "Under the hood, LanceDB reads in the Apache Arrow data and persists it to disk using the [Lance format](https://www.github.com/lancedb/lance)."
245
246!!! info "Automatic embedding generation with Embedding API"
247 When working with embedding models, it is recommended to use the LanceDB embedding API to automatically create vector representation of the data and queries in the background. See the [quickstart example](#using-the-embedding-api) or the embedding API [guide](./embeddings/)
248
249### Create an empty table
250
251Sometimes you may not have the data to insert into the table at creation time.
252In this case, you can create an empty table and specify the schema, so that you can add
253data to the table at a later time (as long as it conforms to the schema). This is
254similar to a `CREATE TABLE` statement in SQL.
255
256=== "Python"
257
258 ```python
259 --8<-- "python/python/tests/docs/test_basic.py:create_empty_table"
260 --8<-- "python/python/tests/docs/test_basic.py:create_empty_table_async"
261 ```
262
263 !!! note "You can define schema in Pydantic"
264 LanceDB comes with Pydantic support, which allows you to define the schema of your data using Pydantic models. This makes it easy to work with LanceDB tables and data. Learn more about all supported types in [tables guide](./guides/tables.md).
265
266=== "Typescript[^1]"
267
268 === "@lancedb/lancedb"
269
270 ```typescript
271 --8<-- "nodejs/examples/basic.ts:create_empty_table"
272 ```
273
274 === "vectordb (deprecated)"
275
276 ```typescript
277 --8<-- "docs/src/basic_legacy.ts:create_empty_table"
278 ```
279
280=== "Rust"
281
282 ```rust
283 --8<-- "rust/lancedb/examples/simple.rs:create_empty_table"
284 ```
285
286## Open an existing table
287
288Once created, you can open a table as follows:
289
290=== "Python"
291
292 ```python
293 --8<-- "python/python/tests/docs/test_basic.py:open_table"
294 --8<-- "python/python/tests/docs/test_basic.py:open_table_async"
295 ```
296
297=== "Typescript[^1]"
298 === "@lancedb/lancedb"
299
300 ```typescript
301 --8<-- "nodejs/examples/basic.ts:open_table"
302 ```
303
304 === "vectordb (deprecated)"
305
306 ```typescript
307 const tbl = await db.openTable("myTable");
308 ```
309
310
311=== "Rust"
312
313 ```rust
314 --8<-- "rust/lancedb/examples/simple.rs:open_existing_tbl"
315 ```
316
317If you forget the name of your table, you can always get a listing of all table names:
318
319=== "Python"
320
321 ```python
322 --8<-- "python/python/tests/docs/test_basic.py:table_names"
323 --8<-- "python/python/tests/docs/test_basic.py:table_names_async"
324 ```
325
326=== "Typescript[^1]"
327 === "@lancedb/lancedb"
328
329 ```typescript
330 --8<-- "nodejs/examples/basic.ts:table_names"
331 ```
332
333 === "vectordb (deprecated)"
334
335 ```typescript
336 console.log(await db.tableNames());
337 ```
338
339=== "Rust"
340
341 ```rust
342 --8<-- "rust/lancedb/examples/simple.rs:list_names"
343 ```
344
345## Add data to a table
346
347After a table has been created, you can always add more data to it as follows:
348
349=== "Python"
350
351 ```python
352 --8<-- "python/python/tests/docs/test_basic.py:add_data"
353 --8<-- "python/python/tests/docs/test_basic.py:add_data_async"
354 ```
355
356=== "Typescript[^1]"
357 === "@lancedb/lancedb"
358
359 ```typescript
360 --8<-- "nodejs/examples/basic.ts:add_data"
361 ```
362
363 === "vectordb (deprecated)"
364
365 ```typescript
366 --8<-- "docs/src/basic_legacy.ts:add"
367 ```
368
369=== "Rust"
370
371 ```rust
372 --8<-- "rust/lancedb/examples/simple.rs:add"
373 ```
374
375## Search for nearest neighbors
376
377Once you've embedded the query, you can find its nearest neighbors as follows:
378
379=== "Python"
380
381 ```python
382 --8<-- "python/python/tests/docs/test_basic.py:vector_search"
383 --8<-- "python/python/tests/docs/test_basic.py:vector_search_async"
384 ```
385
386 This returns a pandas DataFrame with the results.
387
388=== "Typescript[^1]"
389 === "@lancedb/lancedb"
390
391 ```typescript
392 --8<-- "nodejs/examples/basic.ts:vector_search"
393 ```
394
395 === "vectordb (deprecated)"
396
397 ```typescript
398 --8<-- "docs/src/basic_legacy.ts:search"
399 ```
400
401=== "Rust"
402
403 ```rust
404 use futures::TryStreamExt;
405
406 --8<-- "rust/lancedb/examples/simple.rs:search"
407 ```
408
409 !!! Query vectors in Rust
410 Rust does not yet support automatic execution of embedding functions. You will need to
411 calculate embeddings yourself. Support for this is on the roadmap and can be tracked at
412 https://github.com/lancedb/lancedb/issues/994
413
414 Query vectors can be provided as Arrow arrays or a Vec/slice of Rust floats.
415 Support for additional formats (e.g. `polars::series::Series`) is on the roadmap.
416
417By default, LanceDB runs a brute-force scan over dataset to find the K nearest neighbours (KNN).
418For tables with more than 50K vectors, creating an ANN index is recommended to speed up search performance.
419LanceDB allows you to create an ANN index on a table as follows:
420
421=== "Python"
422
423 ```py
424 --8<-- "python/python/tests/docs/test_basic.py:create_index"
425 --8<-- "python/python/tests/docs/test_basic.py:create_index_async"
426 ```
427
428=== "Typescript[^1]"
429 === "@lancedb/lancedb"
430
431 ```typescript
432 --8<-- "nodejs/examples/basic.ts:create_index"
433 ```
434
435 === "vectordb (deprecated)"
436
437 ```{.typescript .ignore}
438 --8<-- "docs/src/basic_legacy.ts:create_index"
439 ```
440
441=== "Rust"
442
443 ```rust
444 --8<-- "rust/lancedb/examples/simple.rs:create_index"
445 ```
446
447!!! note "Why do I need to create an index manually?"
448LanceDB does not automatically create the ANN index for two reasons. The first is that it's optimized
449for really fast retrievals via a disk-based index, and the second is that data and query workloads can
450be very diverse, so there's no one-size-fits-all index configuration. LanceDB provides many parameters
451to fine-tune index size, query latency and accuracy. See the section on
452[ANN indexes](ann_indexes.md) for more details.
453
454## Delete rows from a table
455
456Use the `delete()` method on tables to delete rows from a table. To choose
457which rows to delete, provide a filter that matches on the metadata columns.
458This can delete any number of rows that match the filter.
459
460=== "Python"
461
462 ```python
463 --8<-- "python/python/tests/docs/test_basic.py:delete_rows"
464 --8<-- "python/python/tests/docs/test_basic.py:delete_rows_async"
465 ```
466
467=== "Typescript[^1]"
468
469 === "@lancedb/lancedb"
470
471 ```typescript
472 --8<-- "nodejs/examples/basic.ts:delete_rows"
473 ```
474
475 === "vectordb (deprecated)"
476
477 ```typescript
478 --8<-- "docs/src/basic_legacy.ts:delete"
479 ```
480
481=== "Rust"
482
483 ```rust
484 --8<-- "rust/lancedb/examples/simple.rs:delete"
485 ```
486
487The deletion predicate is a SQL expression that supports the same expressions
488as the `where()` clause (`only_if()` in Rust) on a search. They can be as
489simple or complex as needed. To see what expressions are supported, see the
490[SQL filters](sql.md) section.
491
492=== "Python"
493
494 Read more: [lancedb.table.Table.delete][]
495
496=== "Typescript[^1]"
497
498 === "@lancedb/lancedb"
499
500 Read more: [lancedb.Table.delete](javascript/interfaces/Table.md#delete)
501
502 === "vectordb (deprecated)"
503
504 Read more: [vectordb.Table.delete](javascript/interfaces/Table.md#delete)
505
506=== "Rust"
507
508 Read more: [lancedb::Table::delete](https://docs.rs/lancedb/latest/lancedb/table/struct.Table.html#method.delete)
509
510## Drop a table
511
512Use the `drop_table()` method on the database to remove a table.
513
514=== "Python"
515
516 ```python
517 --8<-- "python/python/tests/docs/test_basic.py:drop_table"
518 --8<-- "python/python/tests/docs/test_basic.py:drop_table_async"
519 ```
520
521 This permanently removes the table and is not recoverable, unlike deleting rows.
522 By default, if the table does not exist an exception is raised. To suppress this,
523 you can pass in `ignore_missing=True`.
524
525=== "Typescript[^1]"
526
527 === "@lancedb/lancedb"
528
529 ```typescript
530 --8<-- "nodejs/examples/basic.ts:drop_table"
531 ```
532
533 === "vectordb (deprecated)"
534
535 ```typescript
536 --8<-- "docs/src/basic_legacy.ts:drop_table"
537 ```
538
539 This permanently removes the table and is not recoverable, unlike deleting rows.
540 If the table does not exist an exception is raised.
541
542=== "Rust"
543
544 ```rust
545 --8<-- "rust/lancedb/examples/simple.rs:drop_table"
546 ```
547
548
549## Using the Embedding API
550You can use the embedding API when working with embedding models. It automatically vectorizes the data at ingestion and query time and comes with built-in integrations with popular embedding models like Openai, Hugging Face, Sentence Transformers, CLIP and more.
551
552=== "Python"
553
554 ```python
555 --8<-- "python/python/tests/docs/test_embeddings_optional.py:imports"
556 --8<-- "python/python/tests/docs/test_embeddings_optional.py:openai_embeddings"
557 ```
558
559=== "Typescript[^1]"
560
561 === "@lancedb/lancedb"
562
563 ```typescript
564 --8<-- "nodejs/examples/embedding.ts:imports"
565 --8<-- "nodejs/examples/embedding.ts:openai_embeddings"
566 ```
567
568=== "Rust"
569
570 ```rust
571 --8<-- "rust/lancedb/examples/openai.rs:imports"
572 --8<-- "rust/lancedb/examples/openai.rs:openai_embeddings"
573 ```
574
575Learn about using the existing integrations and creating custom embedding functions in the [embedding API guide](./embeddings/index.md).
576
577
578## What's next
579
580This section covered the very basics of using LanceDB. If you're learning about vector databases for the first time, you may want to read the page on [indexing](concepts/index_ivfpq.md) to get familiar with the concepts.
581
582If you've already worked with other vector databases, you may want to read the [guides](guides/tables.md) to learn how to work with LanceDB in more detail.
583
584[^1]: The `vectordb` package is a legacy package that is deprecated in favor of `@lancedb/lancedb`. The `vectordb` package will continue to receive bug fixes and security updates until September 2024. We recommend all new projects use `@lancedb/lancedb`. See the [migration guide](migration.md) for more information.