@lancedb/lancedb 0.7.1 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/dist/arrow.d.ts +2 -3
  2. package/dist/arrow.js +2 -27
  3. package/dist/connection.d.ts +9 -0
  4. package/dist/connection.js +16 -2
  5. package/dist/embedding/embedding_function.d.ts +11 -0
  6. package/dist/embedding/embedding_function.js +28 -7
  7. package/dist/embedding/index.d.ts +2 -1
  8. package/dist/embedding/index.js +7 -6
  9. package/dist/embedding/openai.d.ts +1 -1
  10. package/dist/embedding/registry.d.ts +9 -7
  11. package/dist/embedding/registry.js +24 -6
  12. package/dist/embedding/transformers.d.ts +37 -0
  13. package/dist/embedding/transformers.js +147 -0
  14. package/dist/index.d.ts +1 -1
  15. package/dist/query.js +15 -9
  16. package/dist/remote/client.d.ts +1 -1
  17. package/dist/remote/client.js +6 -8
  18. package/dist/remote/connection.d.ts +2 -3
  19. package/dist/remote/connection.js +2 -2
  20. package/dist/sanitize.js +4 -2
  21. package/dist/table.d.ts +3 -0
  22. package/dist/table.js +1 -1
  23. package/package.json +17 -13
  24. package/Cargo.toml +0 -28
  25. package/biome.json +0 -158
  26. package/build.rs +0 -5
  27. package/dist/native.d.ts +0 -208
  28. package/examples/ann_indexes.ts +0 -49
  29. package/examples/basic.ts +0 -149
  30. package/examples/embedding.ts +0 -83
  31. package/examples/filtering.ts +0 -34
  32. package/examples/jsconfig.json +0 -27
  33. package/examples/package-lock.json +0 -79
  34. package/examples/package.json +0 -18
  35. package/examples/search.ts +0 -37
  36. package/jest.config.js +0 -7
  37. package/lancedb/arrow.ts +0 -947
  38. package/lancedb/connection.ts +0 -333
  39. package/lancedb/embedding/embedding_function.ts +0 -194
  40. package/lancedb/embedding/index.ts +0 -113
  41. package/lancedb/embedding/openai.ts +0 -113
  42. package/lancedb/embedding/registry.ts +0 -188
  43. package/lancedb/index.ts +0 -142
  44. package/lancedb/indices.ts +0 -203
  45. package/lancedb/merge.ts +0 -70
  46. package/lancedb/query.ts +0 -507
  47. package/lancedb/remote/client.ts +0 -221
  48. package/lancedb/remote/connection.ts +0 -201
  49. package/lancedb/remote/index.ts +0 -3
  50. package/lancedb/remote/table.ts +0 -226
  51. package/lancedb/sanitize.ts +0 -588
  52. package/lancedb/table.ts +0 -669
  53. package/lancedb/util.ts +0 -69
  54. package/native.d.ts +0 -208
  55. package/nodejs-artifacts/arrow.d.ts +0 -250
  56. package/nodejs-artifacts/arrow.js +0 -768
  57. package/nodejs-artifacts/connection.d.ts +0 -171
  58. package/nodejs-artifacts/connection.js +0 -135
  59. package/nodejs-artifacts/embedding/embedding_function.d.ts +0 -79
  60. package/nodejs-artifacts/embedding/embedding_function.js +0 -112
  61. package/nodejs-artifacts/embedding/index.d.ts +0 -28
  62. package/nodejs-artifacts/embedding/index.js +0 -114
  63. package/nodejs-artifacts/embedding/openai.d.ts +0 -18
  64. package/nodejs-artifacts/embedding/openai.js +0 -105
  65. package/nodejs-artifacts/embedding/registry.d.ts +0 -53
  66. package/nodejs-artifacts/embedding/registry.js +0 -127
  67. package/nodejs-artifacts/index.d.ts +0 -55
  68. package/nodejs-artifacts/index.js +0 -57
  69. package/nodejs-artifacts/indices.d.ts +0 -165
  70. package/nodejs-artifacts/indices.js +0 -71
  71. package/nodejs-artifacts/merge.d.ts +0 -54
  72. package/nodejs-artifacts/merge.js +0 -64
  73. package/nodejs-artifacts/native.d.ts +0 -208
  74. package/nodejs-artifacts/native.js +0 -330
  75. package/nodejs-artifacts/query.d.ts +0 -283
  76. package/nodejs-artifacts/query.js +0 -448
  77. package/nodejs-artifacts/remote/client.d.ts +0 -28
  78. package/nodejs-artifacts/remote/client.js +0 -172
  79. package/nodejs-artifacts/remote/connection.d.ts +0 -25
  80. package/nodejs-artifacts/remote/connection.js +0 -110
  81. package/nodejs-artifacts/remote/index.d.ts +0 -3
  82. package/nodejs-artifacts/remote/index.js +0 -9
  83. package/nodejs-artifacts/remote/table.d.ts +0 -42
  84. package/nodejs-artifacts/remote/table.js +0 -179
  85. package/nodejs-artifacts/sanitize.d.ts +0 -31
  86. package/nodejs-artifacts/sanitize.js +0 -436
  87. package/nodejs-artifacts/table.d.ts +0 -395
  88. package/nodejs-artifacts/table.js +0 -230
  89. package/nodejs-artifacts/util.d.ts +0 -14
  90. package/nodejs-artifacts/util.js +0 -65
  91. package/tsconfig.json +0 -25
  92. package/typedoc.json +0 -10
package/lancedb/merge.ts DELETED
@@ -1,70 +0,0 @@
1
- import { Data, fromDataToBuffer } from "./arrow";
2
- import { NativeMergeInsertBuilder } from "./native";
3
-
4
- /** A builder used to create and run a merge insert operation */
5
- export class MergeInsertBuilder {
6
- #native: NativeMergeInsertBuilder;
7
-
8
- /** Construct a MergeInsertBuilder. __Internal use only.__ */
9
- constructor(native: NativeMergeInsertBuilder) {
10
- this.#native = native;
11
- }
12
-
13
- /**
14
- * Rows that exist in both the source table (new data) and
15
- * the target table (old data) will be updated, replacing
16
- * the old row with the corresponding matching row.
17
- *
18
- * If there are multiple matches then the behavior is undefined.
19
- * Currently this causes multiple copies of the row to be created
20
- * but that behavior is subject to change.
21
- *
22
- * An optional condition may be specified. If it is, then only
23
- * matched rows that satisfy the condtion will be updated. Any
24
- * rows that do not satisfy the condition will be left as they
25
- * are. Failing to satisfy the condition does not cause a
26
- * "matched row" to become a "not matched" row.
27
- *
28
- * The condition should be an SQL string. Use the prefix
29
- * target. to refer to rows in the target table (old data)
30
- * and the prefix source. to refer to rows in the source
31
- * table (new data).
32
- *
33
- * For example, "target.last_update < source.last_update"
34
- */
35
- whenMatchedUpdateAll(options?: { where: string }): MergeInsertBuilder {
36
- return new MergeInsertBuilder(
37
- this.#native.whenMatchedUpdateAll(options?.where),
38
- );
39
- }
40
- /**
41
- * Rows that exist only in the source table (new data) should
42
- * be inserted into the target table.
43
- */
44
- whenNotMatchedInsertAll(): MergeInsertBuilder {
45
- return new MergeInsertBuilder(this.#native.whenNotMatchedInsertAll());
46
- }
47
- /**
48
- * Rows that exist only in the target table (old data) will be
49
- * deleted. An optional condition can be provided to limit what
50
- * data is deleted.
51
- *
52
- * @param options.where - An optional condition to limit what data is deleted
53
- */
54
- whenNotMatchedBySourceDelete(options?: {
55
- where: string;
56
- }): MergeInsertBuilder {
57
- return new MergeInsertBuilder(
58
- this.#native.whenNotMatchedBySourceDelete(options?.where),
59
- );
60
- }
61
- /**
62
- * Executes the merge insert operation
63
- *
64
- * Nothing is returned but the `Table` is updated
65
- */
66
- async execute(data: Data): Promise<void> {
67
- const buffer = await fromDataToBuffer(data);
68
- await this.#native.execute(buffer);
69
- }
70
- }
package/lancedb/query.ts DELETED
@@ -1,507 +0,0 @@
1
- // Copyright 2024 Lance Developers.
2
- //
3
- // Licensed under the Apache License, Version 2.0 (the "License");
4
- // you may not use this file except in compliance with the License.
5
- // You may obtain a copy of the License at
6
- //
7
- // http://www.apache.org/licenses/LICENSE-2.0
8
- //
9
- // Unless required by applicable law or agreed to in writing, software
10
- // distributed under the License is distributed on an "AS IS" BASIS,
11
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- // See the License for the specific language governing permissions and
13
- // limitations under the License.
14
-
15
- import {
16
- Table as ArrowTable,
17
- type IntoVector,
18
- RecordBatch,
19
- tableFromIPC,
20
- } from "./arrow";
21
- import { type IvfPqOptions } from "./indices";
22
- import {
23
- RecordBatchIterator as NativeBatchIterator,
24
- Query as NativeQuery,
25
- Table as NativeTable,
26
- VectorQuery as NativeVectorQuery,
27
- } from "./native";
28
- export class RecordBatchIterator implements AsyncIterator<RecordBatch> {
29
- private promisedInner?: Promise<NativeBatchIterator>;
30
- private inner?: NativeBatchIterator;
31
-
32
- constructor(promise?: Promise<NativeBatchIterator>) {
33
- // TODO: check promise reliably so we dont need to pass two arguments.
34
- this.promisedInner = promise;
35
- }
36
-
37
- // biome-ignore lint/suspicious/noExplicitAny: skip
38
- async next(): Promise<IteratorResult<RecordBatch<any>>> {
39
- if (this.inner === undefined) {
40
- this.inner = await this.promisedInner;
41
- }
42
- if (this.inner === undefined) {
43
- throw new Error("Invalid iterator state state");
44
- }
45
- const n = await this.inner.next();
46
- if (n == null) {
47
- return Promise.resolve({ done: true, value: null });
48
- }
49
- const tbl = tableFromIPC(n);
50
- if (tbl.batches.length != 1) {
51
- throw new Error("Expected only one batch");
52
- }
53
- return Promise.resolve({ done: false, value: tbl.batches[0] });
54
- }
55
- }
56
- /* eslint-enable */
57
-
58
- class RecordBatchIterable<
59
- NativeQueryType extends NativeQuery | NativeVectorQuery,
60
- > implements AsyncIterable<RecordBatch>
61
- {
62
- private inner: NativeQueryType;
63
- private options?: QueryExecutionOptions;
64
-
65
- constructor(inner: NativeQueryType, options?: QueryExecutionOptions) {
66
- this.inner = inner;
67
- this.options = options;
68
- }
69
-
70
- // biome-ignore lint/suspicious/noExplicitAny: skip
71
- [Symbol.asyncIterator](): AsyncIterator<RecordBatch<any>, any, undefined> {
72
- return new RecordBatchIterator(
73
- this.inner.execute(this.options?.maxBatchLength),
74
- );
75
- }
76
- }
77
-
78
- /**
79
- * Options that control the behavior of a particular query execution
80
- */
81
- export interface QueryExecutionOptions {
82
- /**
83
- * The maximum number of rows to return in a single batch
84
- *
85
- * Batches may have fewer rows if the underlying data is stored
86
- * in smaller chunks.
87
- */
88
- maxBatchLength?: number;
89
- }
90
-
91
- /** Common methods supported by all query types */
92
- export class QueryBase<NativeQueryType extends NativeQuery | NativeVectorQuery>
93
- implements AsyncIterable<RecordBatch>
94
- {
95
- protected constructor(
96
- protected inner: NativeQueryType | Promise<NativeQueryType>,
97
- ) {
98
- // intentionally empty
99
- }
100
-
101
- // call a function on the inner (either a promise or the actual object)
102
- protected doCall(fn: (inner: NativeQueryType) => void) {
103
- if (this.inner instanceof Promise) {
104
- this.inner = this.inner.then((inner) => {
105
- fn(inner);
106
- return inner;
107
- });
108
- } else {
109
- fn(this.inner);
110
- }
111
- }
112
- /**
113
- * A filter statement to be applied to this query.
114
- *
115
- * The filter should be supplied as an SQL query string. For example:
116
- * @example
117
- * x > 10
118
- * y > 0 AND y < 100
119
- * x > 5 OR y = 'test'
120
- *
121
- * Filtering performance can often be improved by creating a scalar index
122
- * on the filter column(s).
123
- */
124
- where(predicate: string): this {
125
- this.doCall((inner: NativeQueryType) => inner.onlyIf(predicate));
126
- return this;
127
- }
128
- /**
129
- * A filter statement to be applied to this query.
130
- * @alias where
131
- * @deprecated Use `where` instead
132
- */
133
- filter(predicate: string): this {
134
- return this.where(predicate);
135
- }
136
-
137
- /**
138
- * Return only the specified columns.
139
- *
140
- * By default a query will return all columns from the table. However, this can have
141
- * a very significant impact on latency. LanceDb stores data in a columnar fashion. This
142
- * means we can finely tune our I/O to select exactly the columns we need.
143
- *
144
- * As a best practice you should always limit queries to the columns that you need. If you
145
- * pass in an array of column names then only those columns will be returned.
146
- *
147
- * You can also use this method to create new "dynamic" columns based on your existing columns.
148
- * For example, you may not care about "a" or "b" but instead simply want "a + b". This is often
149
- * seen in the SELECT clause of an SQL query (e.g. `SELECT a+b FROM my_table`).
150
- *
151
- * To create dynamic columns you can pass in a Map<string, string>. A column will be returned
152
- * for each entry in the map. The key provides the name of the column. The value is
153
- * an SQL string used to specify how the column is calculated.
154
- *
155
- * For example, an SQL query might state `SELECT a + b AS combined, c`. The equivalent
156
- * input to this method would be:
157
- * @example
158
- * new Map([["combined", "a + b"], ["c", "c"]])
159
- *
160
- * Columns will always be returned in the order given, even if that order is different than
161
- * the order used when adding the data.
162
- *
163
- * Note that you can pass in a `Record<string, string>` (e.g. an object literal). This method
164
- * uses `Object.entries` which should preserve the insertion order of the object. However,
165
- * object insertion order is easy to get wrong and `Map` is more foolproof.
166
- */
167
- select(
168
- columns: string[] | Map<string, string> | Record<string, string> | string,
169
- ): this {
170
- let columnTuples: [string, string][];
171
- if (typeof columns === "string") {
172
- columns = [columns];
173
- }
174
- if (Array.isArray(columns)) {
175
- columnTuples = columns.map((c) => [c, c]);
176
- } else if (columns instanceof Map) {
177
- columnTuples = Array.from(columns.entries());
178
- } else {
179
- columnTuples = Object.entries(columns);
180
- }
181
- this.doCall((inner: NativeQueryType) => {
182
- inner.select(columnTuples);
183
- });
184
- return this;
185
- }
186
-
187
- /**
188
- * Set the maximum number of results to return.
189
- *
190
- * By default, a plain search has no limit. If this method is not
191
- * called then every valid row from the table will be returned.
192
- */
193
- limit(limit: number): this {
194
- this.doCall((inner: NativeQueryType) => inner.limit(limit));
195
- return this;
196
- }
197
-
198
- protected nativeExecute(
199
- options?: Partial<QueryExecutionOptions>,
200
- ): Promise<NativeBatchIterator> {
201
- if (this.inner instanceof Promise) {
202
- return this.inner.then((inner) => inner.execute(options?.maxBatchLength));
203
- } else {
204
- return this.inner.execute(options?.maxBatchLength);
205
- }
206
- }
207
-
208
- /**
209
- * Execute the query and return the results as an @see {@link AsyncIterator}
210
- * of @see {@link RecordBatch}.
211
- *
212
- * By default, LanceDb will use many threads to calculate results and, when
213
- * the result set is large, multiple batches will be processed at one time.
214
- * This readahead is limited however and backpressure will be applied if this
215
- * stream is consumed slowly (this constrains the maximum memory used by a
216
- * single query)
217
- *
218
- */
219
- protected execute(
220
- options?: Partial<QueryExecutionOptions>,
221
- ): RecordBatchIterator {
222
- return new RecordBatchIterator(this.nativeExecute(options));
223
- }
224
-
225
- // biome-ignore lint/suspicious/noExplicitAny: skip
226
- [Symbol.asyncIterator](): AsyncIterator<RecordBatch<any>> {
227
- const promise = this.nativeExecute();
228
- return new RecordBatchIterator(promise);
229
- }
230
-
231
- /** Collect the results as an Arrow @see {@link ArrowTable}. */
232
- async toArrow(options?: Partial<QueryExecutionOptions>): Promise<ArrowTable> {
233
- const batches = [];
234
- let inner;
235
- if (this.inner instanceof Promise) {
236
- inner = await this.inner;
237
- } else {
238
- inner = this.inner;
239
- }
240
- for await (const batch of new RecordBatchIterable(inner, options)) {
241
- batches.push(batch);
242
- }
243
- return new ArrowTable(batches);
244
- }
245
-
246
- /** Collect the results as an array of objects. */
247
- // biome-ignore lint/suspicious/noExplicitAny: arrow.toArrow() returns any[]
248
- async toArray(options?: Partial<QueryExecutionOptions>): Promise<any[]> {
249
- const tbl = await this.toArrow(options);
250
- return tbl.toArray();
251
- }
252
-
253
- /**
254
- * Generates an explanation of the query execution plan.
255
- *
256
- * @example
257
- * import * as lancedb from "@lancedb/lancedb"
258
- * const db = await lancedb.connect("./.lancedb");
259
- * const table = await db.createTable("my_table", [
260
- * { vector: [1.1, 0.9], id: "1" },
261
- * ]);
262
- * const plan = await table.query().nearestTo([0.5, 0.2]).explainPlan();
263
- *
264
- * @param verbose - If true, provides a more detailed explanation. Defaults to false.
265
- * @returns A Promise that resolves to a string containing the query execution plan explanation.
266
- */
267
- async explainPlan(verbose = false): Promise<string> {
268
- if (this.inner instanceof Promise) {
269
- return this.inner.then((inner) => inner.explainPlan(verbose));
270
- } else {
271
- return this.inner.explainPlan(verbose);
272
- }
273
- }
274
- }
275
-
276
- /**
277
- * An interface for a query that can be executed
278
- *
279
- * Supported by all query types
280
- */
281
- export interface ExecutableQuery {}
282
-
283
- /**
284
- * A builder used to construct a vector search
285
- *
286
- * This builder can be reused to execute the query many times.
287
- */
288
- export class VectorQuery extends QueryBase<NativeVectorQuery> {
289
- constructor(inner: NativeVectorQuery | Promise<NativeVectorQuery>) {
290
- super(inner);
291
- }
292
-
293
- /**
294
- * Set the number of partitions to search (probe)
295
- *
296
- * This argument is only used when the vector column has an IVF PQ index.
297
- * If there is no index then this value is ignored.
298
- *
299
- * The IVF stage of IVF PQ divides the input into partitions (clusters) of
300
- * related values.
301
- *
302
- * The partition whose centroids are closest to the query vector will be
303
- * exhaustiely searched to find matches. This parameter controls how many
304
- * partitions should be searched.
305
- *
306
- * Increasing this value will increase the recall of your query but will
307
- * also increase the latency of your query. The default value is 20. This
308
- * default is good for many cases but the best value to use will depend on
309
- * your data and the recall that you need to achieve.
310
- *
311
- * For best results we recommend tuning this parameter with a benchmark against
312
- * your actual data to find the smallest possible value that will still give
313
- * you the desired recall.
314
- */
315
- nprobes(nprobes: number): VectorQuery {
316
- super.doCall((inner) => inner.nprobes(nprobes));
317
-
318
- return this;
319
- }
320
-
321
- /**
322
- * Set the vector column to query
323
- *
324
- * This controls which column is compared to the query vector supplied in
325
- * the call to @see {@link Query#nearestTo}
326
- *
327
- * This parameter must be specified if the table has more than one column
328
- * whose data type is a fixed-size-list of floats.
329
- */
330
- column(column: string): VectorQuery {
331
- super.doCall((inner) => inner.column(column));
332
- return this;
333
- }
334
-
335
- /**
336
- * Set the distance metric to use
337
- *
338
- * When performing a vector search we try and find the "nearest" vectors according
339
- * to some kind of distance metric. This parameter controls which distance metric to
340
- * use. See @see {@link IvfPqOptions.distanceType} for more details on the different
341
- * distance metrics available.
342
- *
343
- * Note: if there is a vector index then the distance type used MUST match the distance
344
- * type used to train the vector index. If this is not done then the results will be
345
- * invalid.
346
- *
347
- * By default "l2" is used.
348
- */
349
- distanceType(
350
- distanceType: Required<IvfPqOptions>["distanceType"],
351
- ): VectorQuery {
352
- super.doCall((inner) => inner.distanceType(distanceType));
353
- return this;
354
- }
355
-
356
- /**
357
- * A multiplier to control how many additional rows are taken during the refine step
358
- *
359
- * This argument is only used when the vector column has an IVF PQ index.
360
- * If there is no index then this value is ignored.
361
- *
362
- * An IVF PQ index stores compressed (quantized) values. They query vector is compared
363
- * against these values and, since they are compressed, the comparison is inaccurate.
364
- *
365
- * This parameter can be used to refine the results. It can improve both improve recall
366
- * and correct the ordering of the nearest results.
367
- *
368
- * To refine results LanceDb will first perform an ANN search to find the nearest
369
- * `limit` * `refine_factor` results. In other words, if `refine_factor` is 3 and
370
- * `limit` is the default (10) then the first 30 results will be selected. LanceDb
371
- * then fetches the full, uncompressed, values for these 30 results. The results are
372
- * then reordered by the true distance and only the nearest 10 are kept.
373
- *
374
- * Note: there is a difference between calling this method with a value of 1 and never
375
- * calling this method at all. Calling this method with any value will have an impact
376
- * on your search latency. When you call this method with a `refine_factor` of 1 then
377
- * LanceDb still needs to fetch the full, uncompressed, values so that it can potentially
378
- * reorder the results.
379
- *
380
- * Note: if this method is NOT called then the distances returned in the _distance column
381
- * will be approximate distances based on the comparison of the quantized query vector
382
- * and the quantized result vectors. This can be considerably different than the true
383
- * distance between the query vector and the actual uncompressed vector.
384
- */
385
- refineFactor(refineFactor: number): VectorQuery {
386
- super.doCall((inner) => inner.refineFactor(refineFactor));
387
- return this;
388
- }
389
-
390
- /**
391
- * If this is called then filtering will happen after the vector search instead of
392
- * before.
393
- *
394
- * By default filtering will be performed before the vector search. This is how
395
- * filtering is typically understood to work. This prefilter step does add some
396
- * additional latency. Creating a scalar index on the filter column(s) can
397
- * often improve this latency. However, sometimes a filter is too complex or scalar
398
- * indices cannot be applied to the column. In these cases postfiltering can be
399
- * used instead of prefiltering to improve latency.
400
- *
401
- * Post filtering applies the filter to the results of the vector search. This means
402
- * we only run the filter on a much smaller set of data. However, it can cause the
403
- * query to return fewer than `limit` results (or even no results) if none of the nearest
404
- * results match the filter.
405
- *
406
- * Post filtering happens during the "refine stage" (described in more detail in
407
- * @see {@link VectorQuery#refineFactor}). This means that setting a higher refine
408
- * factor can often help restore some of the results lost by post filtering.
409
- */
410
- postfilter(): VectorQuery {
411
- super.doCall((inner) => inner.postfilter());
412
- return this;
413
- }
414
-
415
- /**
416
- * If this is called then any vector index is skipped
417
- *
418
- * An exhaustive (flat) search will be performed. The query vector will
419
- * be compared to every vector in the table. At high scales this can be
420
- * expensive. However, this is often still useful. For example, skipping
421
- * the vector index can give you ground truth results which you can use to
422
- * calculate your recall to select an appropriate value for nprobes.
423
- */
424
- bypassVectorIndex(): VectorQuery {
425
- super.doCall((inner) => inner.bypassVectorIndex());
426
- return this;
427
- }
428
- }
429
-
430
- /** A builder for LanceDB queries. */
431
- export class Query extends QueryBase<NativeQuery> {
432
- constructor(tbl: NativeTable) {
433
- super(tbl.query());
434
- }
435
-
436
- /**
437
- * Find the nearest vectors to the given query vector.
438
- *
439
- * This converts the query from a plain query to a vector query.
440
- *
441
- * This method will attempt to convert the input to the query vector
442
- * expected by the embedding model. If the input cannot be converted
443
- * then an error will be thrown.
444
- *
445
- * By default, there is no embedding model, and the input should be
446
- * an array-like object of numbers (something that can be used as input
447
- * to Float32Array.from)
448
- *
449
- * If there is only one vector column (a column whose data type is a
450
- * fixed size list of floats) then the column does not need to be specified.
451
- * If there is more than one vector column you must use
452
- * @see {@link VectorQuery#column} to specify which column you would like
453
- * to compare with.
454
- *
455
- * If no index has been created on the vector column then a vector query
456
- * will perform a distance comparison between the query vector and every
457
- * vector in the database and then sort the results. This is sometimes
458
- * called a "flat search"
459
- *
460
- * For small databases, with a few hundred thousand vectors or less, this can
461
- * be reasonably fast. In larger databases you should create a vector index
462
- * on the column. If there is a vector index then an "approximate" nearest
463
- * neighbor search (frequently called an ANN search) will be performed. This
464
- * search is much faster, but the results will be approximate.
465
- *
466
- * The query can be further parameterized using the returned builder. There
467
- * are various ANN search parameters that will let you fine tune your recall
468
- * accuracy vs search latency.
469
- *
470
- * Vector searches always have a `limit`. If `limit` has not been called then
471
- * a default `limit` of 10 will be used. @see {@link Query#limit}
472
- */
473
- nearestTo(vector: IntoVector): VectorQuery {
474
- if (this.inner instanceof Promise) {
475
- const nativeQuery = this.inner.then(async (inner) => {
476
- if (vector instanceof Promise) {
477
- const arr = await vector.then((v) => Float32Array.from(v));
478
- return inner.nearestTo(arr);
479
- } else {
480
- return inner.nearestTo(Float32Array.from(vector));
481
- }
482
- });
483
- return new VectorQuery(nativeQuery);
484
- }
485
- if (vector instanceof Promise) {
486
- const res = (async () => {
487
- try {
488
- const v = await vector;
489
- const arr = Float32Array.from(v);
490
- //
491
- // biome-ignore lint/suspicious/noExplicitAny: we need to get the `inner`, but js has no package scoping
492
- const value: any = this.nearestTo(arr);
493
- const inner = value.inner as
494
- | NativeVectorQuery
495
- | Promise<NativeVectorQuery>;
496
- return inner;
497
- } catch (e) {
498
- return Promise.reject(e);
499
- }
500
- })();
501
- return new VectorQuery(res);
502
- } else {
503
- const vectorQuery = this.inner.nearestTo(Float32Array.from(vector));
504
- return new VectorQuery(vectorQuery);
505
- }
506
- }
507
- }