@lancedb/lancedb 0.5.1 → 0.7.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.
- package/Cargo.toml +3 -3
- package/biome.json +19 -3
- package/dist/arrow.d.ts +42 -7
- package/dist/arrow.js +6 -5
- package/dist/connection.d.ts +55 -29
- package/dist/connection.js +22 -74
- package/dist/embedding/embedding_function.d.ts +11 -3
- package/dist/embedding/embedding_function.js +36 -12
- package/dist/embedding/openai.d.ts +6 -5
- package/dist/embedding/openai.js +4 -2
- package/dist/embedding/registry.d.ts +10 -11
- package/dist/embedding/registry.js +4 -0
- package/dist/index.d.ts +51 -3
- package/dist/index.js +28 -4
- package/dist/merge.d.ts +54 -0
- package/dist/merge.js +64 -0
- package/dist/native.d.ts +34 -7
- package/dist/native.js +26 -9
- package/dist/query.d.ts +51 -16
- package/dist/query.js +122 -21
- package/dist/remote/client.d.ts +28 -0
- package/dist/remote/client.js +172 -0
- package/dist/remote/connection.d.ts +25 -0
- package/dist/remote/connection.js +110 -0
- package/dist/remote/index.d.ts +3 -0
- package/dist/remote/index.js +9 -0
- package/dist/remote/table.d.ts +42 -0
- package/dist/remote/table.js +179 -0
- package/dist/sanitize.d.ts +3 -2
- package/dist/sanitize.js +55 -1
- package/dist/table.d.ts +116 -25
- package/dist/table.js +117 -233
- package/dist/util.d.ts +14 -0
- package/dist/util.js +65 -0
- package/examples/ann_indexes.ts +49 -0
- package/examples/basic.ts +149 -0
- package/examples/embedding.ts +83 -0
- package/examples/filtering.ts +34 -0
- package/examples/jsconfig.json +27 -0
- package/examples/package-lock.json +79 -0
- package/examples/package.json +18 -0
- package/examples/search.ts +37 -0
- package/lancedb/arrow.ts +87 -24
- package/lancedb/connection.ts +115 -92
- package/lancedb/embedding/embedding_function.ts +48 -16
- package/lancedb/embedding/openai.ts +11 -6
- package/lancedb/embedding/registry.ts +38 -22
- package/lancedb/index.ts +101 -2
- package/lancedb/merge.ts +70 -0
- package/lancedb/query.ts +168 -39
- package/lancedb/remote/client.ts +221 -0
- package/lancedb/remote/connection.ts +201 -0
- package/lancedb/remote/index.ts +3 -0
- package/lancedb/remote/table.ts +226 -0
- package/lancedb/sanitize.ts +73 -1
- package/lancedb/table.ts +344 -101
- package/lancedb/util.ts +69 -0
- package/native.d.ts +208 -0
- package/nodejs-artifacts/arrow.d.ts +42 -7
- package/nodejs-artifacts/arrow.js +6 -5
- package/nodejs-artifacts/connection.d.ts +55 -29
- package/nodejs-artifacts/connection.js +22 -74
- package/nodejs-artifacts/embedding/embedding_function.d.ts +11 -3
- package/nodejs-artifacts/embedding/embedding_function.js +36 -12
- package/nodejs-artifacts/embedding/openai.d.ts +6 -5
- package/nodejs-artifacts/embedding/openai.js +4 -2
- package/nodejs-artifacts/embedding/registry.d.ts +10 -11
- package/nodejs-artifacts/embedding/registry.js +4 -0
- package/nodejs-artifacts/index.d.ts +51 -3
- package/nodejs-artifacts/index.js +28 -4
- package/nodejs-artifacts/merge.d.ts +54 -0
- package/nodejs-artifacts/merge.js +64 -0
- package/nodejs-artifacts/native.d.ts +34 -7
- package/nodejs-artifacts/native.js +26 -9
- package/nodejs-artifacts/query.d.ts +51 -16
- package/nodejs-artifacts/query.js +122 -21
- package/nodejs-artifacts/remote/client.d.ts +28 -0
- package/nodejs-artifacts/remote/client.js +172 -0
- package/nodejs-artifacts/remote/connection.d.ts +25 -0
- package/nodejs-artifacts/remote/connection.js +110 -0
- package/nodejs-artifacts/remote/index.d.ts +3 -0
- package/nodejs-artifacts/remote/index.js +9 -0
- package/nodejs-artifacts/remote/table.d.ts +42 -0
- package/nodejs-artifacts/remote/table.js +179 -0
- package/nodejs-artifacts/sanitize.d.ts +3 -2
- package/nodejs-artifacts/sanitize.js +55 -1
- package/nodejs-artifacts/table.d.ts +116 -25
- package/nodejs-artifacts/table.js +117 -233
- package/nodejs-artifacts/util.d.ts +14 -0
- package/nodejs-artifacts/util.js +65 -0
- package/package.json +25 -11
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { Table as ArrowTable, RecordBatch } from "./arrow";
|
|
1
|
+
import { Table as ArrowTable, type IntoVector, RecordBatch } from "./arrow";
|
|
2
|
+
import { type IvfPqOptions } from "./indices";
|
|
2
3
|
import { RecordBatchIterator as NativeBatchIterator, Query as NativeQuery, Table as NativeTable, VectorQuery as NativeVectorQuery } from "./native";
|
|
3
4
|
export declare class RecordBatchIterator implements AsyncIterator<RecordBatch> {
|
|
4
5
|
private promisedInner?;
|
|
@@ -6,10 +7,23 @@ export declare class RecordBatchIterator implements AsyncIterator<RecordBatch> {
|
|
|
6
7
|
constructor(promise?: Promise<NativeBatchIterator>);
|
|
7
8
|
next(): Promise<IteratorResult<RecordBatch<any>>>;
|
|
8
9
|
}
|
|
10
|
+
/**
|
|
11
|
+
* Options that control the behavior of a particular query execution
|
|
12
|
+
*/
|
|
13
|
+
export interface QueryExecutionOptions {
|
|
14
|
+
/**
|
|
15
|
+
* The maximum number of rows to return in a single batch
|
|
16
|
+
*
|
|
17
|
+
* Batches may have fewer rows if the underlying data is stored
|
|
18
|
+
* in smaller chunks.
|
|
19
|
+
*/
|
|
20
|
+
maxBatchLength?: number;
|
|
21
|
+
}
|
|
9
22
|
/** Common methods supported by all query types */
|
|
10
|
-
export declare class QueryBase<NativeQueryType extends NativeQuery | NativeVectorQuery
|
|
11
|
-
protected inner: NativeQueryType
|
|
12
|
-
protected constructor(inner: NativeQueryType);
|
|
23
|
+
export declare class QueryBase<NativeQueryType extends NativeQuery | NativeVectorQuery> implements AsyncIterable<RecordBatch> {
|
|
24
|
+
protected inner: NativeQueryType | Promise<NativeQueryType>;
|
|
25
|
+
protected constructor(inner: NativeQueryType | Promise<NativeQueryType>);
|
|
26
|
+
protected doCall(fn: (inner: NativeQueryType) => void): void;
|
|
13
27
|
/**
|
|
14
28
|
* A filter statement to be applied to this query.
|
|
15
29
|
*
|
|
@@ -22,7 +36,13 @@ export declare class QueryBase<NativeQueryType extends NativeQuery | NativeVecto
|
|
|
22
36
|
* Filtering performance can often be improved by creating a scalar index
|
|
23
37
|
* on the filter column(s).
|
|
24
38
|
*/
|
|
25
|
-
where(predicate: string):
|
|
39
|
+
where(predicate: string): this;
|
|
40
|
+
/**
|
|
41
|
+
* A filter statement to be applied to this query.
|
|
42
|
+
* @alias where
|
|
43
|
+
* @deprecated Use `where` instead
|
|
44
|
+
*/
|
|
45
|
+
filter(predicate: string): this;
|
|
26
46
|
/**
|
|
27
47
|
* Return only the specified columns.
|
|
28
48
|
*
|
|
@@ -53,15 +73,15 @@ export declare class QueryBase<NativeQueryType extends NativeQuery | NativeVecto
|
|
|
53
73
|
* uses `Object.entries` which should preserve the insertion order of the object. However,
|
|
54
74
|
* object insertion order is easy to get wrong and `Map` is more foolproof.
|
|
55
75
|
*/
|
|
56
|
-
select(columns: string[] | Map<string, string> | Record<string, string>):
|
|
76
|
+
select(columns: string[] | Map<string, string> | Record<string, string> | string): this;
|
|
57
77
|
/**
|
|
58
78
|
* Set the maximum number of results to return.
|
|
59
79
|
*
|
|
60
80
|
* By default, a plain search has no limit. If this method is not
|
|
61
81
|
* called then every valid row from the table will be returned.
|
|
62
82
|
*/
|
|
63
|
-
limit(limit: number):
|
|
64
|
-
protected nativeExecute(): Promise<NativeBatchIterator>;
|
|
83
|
+
limit(limit: number): this;
|
|
84
|
+
protected nativeExecute(options?: Partial<QueryExecutionOptions>): Promise<NativeBatchIterator>;
|
|
65
85
|
/**
|
|
66
86
|
* Execute the query and return the results as an @see {@link AsyncIterator}
|
|
67
87
|
* of @see {@link RecordBatch}.
|
|
@@ -73,12 +93,27 @@ export declare class QueryBase<NativeQueryType extends NativeQuery | NativeVecto
|
|
|
73
93
|
* single query)
|
|
74
94
|
*
|
|
75
95
|
*/
|
|
76
|
-
protected execute(): RecordBatchIterator;
|
|
96
|
+
protected execute(options?: Partial<QueryExecutionOptions>): RecordBatchIterator;
|
|
77
97
|
[Symbol.asyncIterator](): AsyncIterator<RecordBatch<any>>;
|
|
78
98
|
/** Collect the results as an Arrow @see {@link ArrowTable}. */
|
|
79
|
-
toArrow(): Promise<ArrowTable>;
|
|
99
|
+
toArrow(options?: Partial<QueryExecutionOptions>): Promise<ArrowTable>;
|
|
80
100
|
/** Collect the results as an array of objects. */
|
|
81
|
-
toArray(): Promise<
|
|
101
|
+
toArray(options?: Partial<QueryExecutionOptions>): Promise<any[]>;
|
|
102
|
+
/**
|
|
103
|
+
* Generates an explanation of the query execution plan.
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* import * as lancedb from "@lancedb/lancedb"
|
|
107
|
+
* const db = await lancedb.connect("./.lancedb");
|
|
108
|
+
* const table = await db.createTable("my_table", [
|
|
109
|
+
* { vector: [1.1, 0.9], id: "1" },
|
|
110
|
+
* ]);
|
|
111
|
+
* const plan = await table.query().nearestTo([0.5, 0.2]).explainPlan();
|
|
112
|
+
*
|
|
113
|
+
* @param verbose - If true, provides a more detailed explanation. Defaults to false.
|
|
114
|
+
* @returns A Promise that resolves to a string containing the query execution plan explanation.
|
|
115
|
+
*/
|
|
116
|
+
explainPlan(verbose?: boolean): Promise<string>;
|
|
82
117
|
}
|
|
83
118
|
/**
|
|
84
119
|
* An interface for a query that can be executed
|
|
@@ -92,8 +127,8 @@ export interface ExecutableQuery {
|
|
|
92
127
|
*
|
|
93
128
|
* This builder can be reused to execute the query many times.
|
|
94
129
|
*/
|
|
95
|
-
export declare class VectorQuery extends QueryBase<NativeVectorQuery
|
|
96
|
-
constructor(inner: NativeVectorQuery);
|
|
130
|
+
export declare class VectorQuery extends QueryBase<NativeVectorQuery> {
|
|
131
|
+
constructor(inner: NativeVectorQuery | Promise<NativeVectorQuery>);
|
|
97
132
|
/**
|
|
98
133
|
* Set the number of partitions to search (probe)
|
|
99
134
|
*
|
|
@@ -141,7 +176,7 @@ export declare class VectorQuery extends QueryBase<NativeVectorQuery, VectorQuer
|
|
|
141
176
|
*
|
|
142
177
|
* By default "l2" is used.
|
|
143
178
|
*/
|
|
144
|
-
distanceType(distanceType:
|
|
179
|
+
distanceType(distanceType: Required<IvfPqOptions>["distanceType"]): VectorQuery;
|
|
145
180
|
/**
|
|
146
181
|
* A multiplier to control how many additional rows are taken during the refine step
|
|
147
182
|
*
|
|
@@ -205,7 +240,7 @@ export declare class VectorQuery extends QueryBase<NativeVectorQuery, VectorQuer
|
|
|
205
240
|
bypassVectorIndex(): VectorQuery;
|
|
206
241
|
}
|
|
207
242
|
/** A builder for LanceDB queries. */
|
|
208
|
-
export declare class Query extends QueryBase<NativeQuery
|
|
243
|
+
export declare class Query extends QueryBase<NativeQuery> {
|
|
209
244
|
constructor(tbl: NativeTable);
|
|
210
245
|
/**
|
|
211
246
|
* Find the nearest vectors to the given query vector.
|
|
@@ -244,5 +279,5 @@ export declare class Query extends QueryBase<NativeQuery, Query> {
|
|
|
244
279
|
* Vector searches always have a `limit`. If `limit` has not been called then
|
|
245
280
|
* a default `limit` of 10 will be used. @see {@link Query#limit}
|
|
246
281
|
*/
|
|
247
|
-
nearestTo(vector:
|
|
282
|
+
nearestTo(vector: IntoVector): VectorQuery;
|
|
248
283
|
}
|
|
@@ -43,6 +43,18 @@ class RecordBatchIterator {
|
|
|
43
43
|
}
|
|
44
44
|
exports.RecordBatchIterator = RecordBatchIterator;
|
|
45
45
|
/* eslint-enable */
|
|
46
|
+
class RecordBatchIterable {
|
|
47
|
+
inner;
|
|
48
|
+
options;
|
|
49
|
+
constructor(inner, options) {
|
|
50
|
+
this.inner = inner;
|
|
51
|
+
this.options = options;
|
|
52
|
+
}
|
|
53
|
+
// biome-ignore lint/suspicious/noExplicitAny: skip
|
|
54
|
+
[Symbol.asyncIterator]() {
|
|
55
|
+
return new RecordBatchIterator(this.inner.execute(this.options?.maxBatchLength));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
46
58
|
/** Common methods supported by all query types */
|
|
47
59
|
class QueryBase {
|
|
48
60
|
inner;
|
|
@@ -50,6 +62,18 @@ class QueryBase {
|
|
|
50
62
|
this.inner = inner;
|
|
51
63
|
// intentionally empty
|
|
52
64
|
}
|
|
65
|
+
// call a function on the inner (either a promise or the actual object)
|
|
66
|
+
doCall(fn) {
|
|
67
|
+
if (this.inner instanceof Promise) {
|
|
68
|
+
this.inner = this.inner.then((inner) => {
|
|
69
|
+
fn(inner);
|
|
70
|
+
return inner;
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
fn(this.inner);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
53
77
|
/**
|
|
54
78
|
* A filter statement to be applied to this query.
|
|
55
79
|
*
|
|
@@ -63,9 +87,17 @@ class QueryBase {
|
|
|
63
87
|
* on the filter column(s).
|
|
64
88
|
*/
|
|
65
89
|
where(predicate) {
|
|
66
|
-
this.inner.onlyIf(predicate);
|
|
90
|
+
this.doCall((inner) => inner.onlyIf(predicate));
|
|
67
91
|
return this;
|
|
68
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* A filter statement to be applied to this query.
|
|
95
|
+
* @alias where
|
|
96
|
+
* @deprecated Use `where` instead
|
|
97
|
+
*/
|
|
98
|
+
filter(predicate) {
|
|
99
|
+
return this.where(predicate);
|
|
100
|
+
}
|
|
69
101
|
/**
|
|
70
102
|
* Return only the specified columns.
|
|
71
103
|
*
|
|
@@ -98,6 +130,9 @@ class QueryBase {
|
|
|
98
130
|
*/
|
|
99
131
|
select(columns) {
|
|
100
132
|
let columnTuples;
|
|
133
|
+
if (typeof columns === "string") {
|
|
134
|
+
columns = [columns];
|
|
135
|
+
}
|
|
101
136
|
if (Array.isArray(columns)) {
|
|
102
137
|
columnTuples = columns.map((c) => [c, c]);
|
|
103
138
|
}
|
|
@@ -107,7 +142,9 @@ class QueryBase {
|
|
|
107
142
|
else {
|
|
108
143
|
columnTuples = Object.entries(columns);
|
|
109
144
|
}
|
|
110
|
-
this.inner
|
|
145
|
+
this.doCall((inner) => {
|
|
146
|
+
inner.select(columnTuples);
|
|
147
|
+
});
|
|
111
148
|
return this;
|
|
112
149
|
}
|
|
113
150
|
/**
|
|
@@ -117,11 +154,16 @@ class QueryBase {
|
|
|
117
154
|
* called then every valid row from the table will be returned.
|
|
118
155
|
*/
|
|
119
156
|
limit(limit) {
|
|
120
|
-
this.inner.limit(limit);
|
|
157
|
+
this.doCall((inner) => inner.limit(limit));
|
|
121
158
|
return this;
|
|
122
159
|
}
|
|
123
|
-
nativeExecute() {
|
|
124
|
-
|
|
160
|
+
nativeExecute(options) {
|
|
161
|
+
if (this.inner instanceof Promise) {
|
|
162
|
+
return this.inner.then((inner) => inner.execute(options?.maxBatchLength));
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
return this.inner.execute(options?.maxBatchLength);
|
|
166
|
+
}
|
|
125
167
|
}
|
|
126
168
|
/**
|
|
127
169
|
* Execute the query and return the results as an @see {@link AsyncIterator}
|
|
@@ -134,8 +176,8 @@ class QueryBase {
|
|
|
134
176
|
* single query)
|
|
135
177
|
*
|
|
136
178
|
*/
|
|
137
|
-
execute() {
|
|
138
|
-
return new RecordBatchIterator(this.nativeExecute());
|
|
179
|
+
execute(options) {
|
|
180
|
+
return new RecordBatchIterator(this.nativeExecute(options));
|
|
139
181
|
}
|
|
140
182
|
// biome-ignore lint/suspicious/noExplicitAny: skip
|
|
141
183
|
[Symbol.asyncIterator]() {
|
|
@@ -143,19 +185,48 @@ class QueryBase {
|
|
|
143
185
|
return new RecordBatchIterator(promise);
|
|
144
186
|
}
|
|
145
187
|
/** Collect the results as an Arrow @see {@link ArrowTable}. */
|
|
146
|
-
async toArrow() {
|
|
188
|
+
async toArrow(options) {
|
|
147
189
|
const batches = [];
|
|
148
|
-
|
|
190
|
+
let inner;
|
|
191
|
+
if (this.inner instanceof Promise) {
|
|
192
|
+
inner = await this.inner;
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
inner = this.inner;
|
|
196
|
+
}
|
|
197
|
+
for await (const batch of new RecordBatchIterable(inner, options)) {
|
|
149
198
|
batches.push(batch);
|
|
150
199
|
}
|
|
151
200
|
return new arrow_1.Table(batches);
|
|
152
201
|
}
|
|
153
202
|
/** Collect the results as an array of objects. */
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
203
|
+
// biome-ignore lint/suspicious/noExplicitAny: arrow.toArrow() returns any[]
|
|
204
|
+
async toArray(options) {
|
|
205
|
+
const tbl = await this.toArrow(options);
|
|
157
206
|
return tbl.toArray();
|
|
158
207
|
}
|
|
208
|
+
/**
|
|
209
|
+
* Generates an explanation of the query execution plan.
|
|
210
|
+
*
|
|
211
|
+
* @example
|
|
212
|
+
* import * as lancedb from "@lancedb/lancedb"
|
|
213
|
+
* const db = await lancedb.connect("./.lancedb");
|
|
214
|
+
* const table = await db.createTable("my_table", [
|
|
215
|
+
* { vector: [1.1, 0.9], id: "1" },
|
|
216
|
+
* ]);
|
|
217
|
+
* const plan = await table.query().nearestTo([0.5, 0.2]).explainPlan();
|
|
218
|
+
*
|
|
219
|
+
* @param verbose - If true, provides a more detailed explanation. Defaults to false.
|
|
220
|
+
* @returns A Promise that resolves to a string containing the query execution plan explanation.
|
|
221
|
+
*/
|
|
222
|
+
async explainPlan(verbose = false) {
|
|
223
|
+
if (this.inner instanceof Promise) {
|
|
224
|
+
return this.inner.then((inner) => inner.explainPlan(verbose));
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
return this.inner.explainPlan(verbose);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
159
230
|
}
|
|
160
231
|
exports.QueryBase = QueryBase;
|
|
161
232
|
/**
|
|
@@ -190,7 +261,7 @@ class VectorQuery extends QueryBase {
|
|
|
190
261
|
* you the desired recall.
|
|
191
262
|
*/
|
|
192
263
|
nprobes(nprobes) {
|
|
193
|
-
|
|
264
|
+
super.doCall((inner) => inner.nprobes(nprobes));
|
|
194
265
|
return this;
|
|
195
266
|
}
|
|
196
267
|
/**
|
|
@@ -203,7 +274,7 @@ class VectorQuery extends QueryBase {
|
|
|
203
274
|
* whose data type is a fixed-size-list of floats.
|
|
204
275
|
*/
|
|
205
276
|
column(column) {
|
|
206
|
-
|
|
277
|
+
super.doCall((inner) => inner.column(column));
|
|
207
278
|
return this;
|
|
208
279
|
}
|
|
209
280
|
/**
|
|
@@ -221,7 +292,7 @@ class VectorQuery extends QueryBase {
|
|
|
221
292
|
* By default "l2" is used.
|
|
222
293
|
*/
|
|
223
294
|
distanceType(distanceType) {
|
|
224
|
-
|
|
295
|
+
super.doCall((inner) => inner.distanceType(distanceType));
|
|
225
296
|
return this;
|
|
226
297
|
}
|
|
227
298
|
/**
|
|
@@ -254,7 +325,7 @@ class VectorQuery extends QueryBase {
|
|
|
254
325
|
* distance between the query vector and the actual uncompressed vector.
|
|
255
326
|
*/
|
|
256
327
|
refineFactor(refineFactor) {
|
|
257
|
-
|
|
328
|
+
super.doCall((inner) => inner.refineFactor(refineFactor));
|
|
258
329
|
return this;
|
|
259
330
|
}
|
|
260
331
|
/**
|
|
@@ -278,7 +349,7 @@ class VectorQuery extends QueryBase {
|
|
|
278
349
|
* factor can often help restore some of the results lost by post filtering.
|
|
279
350
|
*/
|
|
280
351
|
postfilter() {
|
|
281
|
-
|
|
352
|
+
super.doCall((inner) => inner.postfilter());
|
|
282
353
|
return this;
|
|
283
354
|
}
|
|
284
355
|
/**
|
|
@@ -291,7 +362,7 @@ class VectorQuery extends QueryBase {
|
|
|
291
362
|
* calculate your recall to select an appropriate value for nprobes.
|
|
292
363
|
*/
|
|
293
364
|
bypassVectorIndex() {
|
|
294
|
-
|
|
365
|
+
super.doCall((inner) => inner.bypassVectorIndex());
|
|
295
366
|
return this;
|
|
296
367
|
}
|
|
297
368
|
}
|
|
@@ -339,9 +410,39 @@ class Query extends QueryBase {
|
|
|
339
410
|
* a default `limit` of 10 will be used. @see {@link Query#limit}
|
|
340
411
|
*/
|
|
341
412
|
nearestTo(vector) {
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
413
|
+
if (this.inner instanceof Promise) {
|
|
414
|
+
const nativeQuery = this.inner.then(async (inner) => {
|
|
415
|
+
if (vector instanceof Promise) {
|
|
416
|
+
const arr = await vector.then((v) => Float32Array.from(v));
|
|
417
|
+
return inner.nearestTo(arr);
|
|
418
|
+
}
|
|
419
|
+
else {
|
|
420
|
+
return inner.nearestTo(Float32Array.from(vector));
|
|
421
|
+
}
|
|
422
|
+
});
|
|
423
|
+
return new VectorQuery(nativeQuery);
|
|
424
|
+
}
|
|
425
|
+
if (vector instanceof Promise) {
|
|
426
|
+
const res = (async () => {
|
|
427
|
+
try {
|
|
428
|
+
const v = await vector;
|
|
429
|
+
const arr = Float32Array.from(v);
|
|
430
|
+
//
|
|
431
|
+
// biome-ignore lint/suspicious/noExplicitAny: we need to get the `inner`, but js has no package scoping
|
|
432
|
+
const value = this.nearestTo(arr);
|
|
433
|
+
const inner = value.inner;
|
|
434
|
+
return inner;
|
|
435
|
+
}
|
|
436
|
+
catch (e) {
|
|
437
|
+
return Promise.reject(e);
|
|
438
|
+
}
|
|
439
|
+
})();
|
|
440
|
+
return new VectorQuery(res);
|
|
441
|
+
}
|
|
442
|
+
else {
|
|
443
|
+
const vectorQuery = this.inner.nearestTo(Float32Array.from(vector));
|
|
444
|
+
return new VectorQuery(vectorQuery);
|
|
445
|
+
}
|
|
345
446
|
}
|
|
346
447
|
}
|
|
347
448
|
exports.Query = Query;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { type AxiosResponse } from "axios";
|
|
3
|
+
import { Table as ArrowTable } from "../arrow";
|
|
4
|
+
import { VectorQuery } from "../query";
|
|
5
|
+
export declare class RestfulLanceDBClient {
|
|
6
|
+
#private;
|
|
7
|
+
constructor(dbName: string, apiKey: string, region: string, hostOverride?: string, connectionTimeout?: number, readTimeout?: number);
|
|
8
|
+
get session(): import("axios").AxiosInstance;
|
|
9
|
+
get url(): string;
|
|
10
|
+
get headers(): {
|
|
11
|
+
[key: string]: string;
|
|
12
|
+
};
|
|
13
|
+
isOpen(): boolean;
|
|
14
|
+
private checkNotClosed;
|
|
15
|
+
close(): void;
|
|
16
|
+
get(uri: string, params?: Record<string, any>): Promise<any>;
|
|
17
|
+
post(uri: string, body?: any): Promise<any>;
|
|
18
|
+
post(uri: string, body: any, additional: {
|
|
19
|
+
config?: {
|
|
20
|
+
responseType: "arraybuffer";
|
|
21
|
+
};
|
|
22
|
+
headers?: Record<string, string>;
|
|
23
|
+
params?: Record<string, string>;
|
|
24
|
+
}): Promise<Buffer>;
|
|
25
|
+
listTables(limit?: number, pageToken?: string): Promise<string[]>;
|
|
26
|
+
query(tableName: string, query: VectorQuery): Promise<ArrowTable>;
|
|
27
|
+
static checkStatus(response: AxiosResponse): void;
|
|
28
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright 2023 LanceDB Developers.
|
|
3
|
+
//
|
|
4
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
// you may not use this file except in compliance with the License.
|
|
6
|
+
// You may obtain a copy of the License at
|
|
7
|
+
//
|
|
8
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
//
|
|
10
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
// See the License for the specific language governing permissions and
|
|
14
|
+
// limitations under the License.
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.RestfulLanceDBClient = void 0;
|
|
17
|
+
const axios_1 = require("axios");
|
|
18
|
+
const arrow_1 = require("../arrow");
|
|
19
|
+
class RestfulLanceDBClient {
|
|
20
|
+
#dbName;
|
|
21
|
+
#region;
|
|
22
|
+
#apiKey;
|
|
23
|
+
#hostOverride;
|
|
24
|
+
#closed = false;
|
|
25
|
+
#connectionTimeout = 12 * 1000; // 12 seconds;
|
|
26
|
+
#readTimeout = 30 * 1000; // 30 seconds;
|
|
27
|
+
#session;
|
|
28
|
+
constructor(dbName, apiKey, region, hostOverride, connectionTimeout, readTimeout) {
|
|
29
|
+
this.#dbName = dbName;
|
|
30
|
+
this.#apiKey = apiKey;
|
|
31
|
+
this.#region = region;
|
|
32
|
+
this.#hostOverride = hostOverride ?? this.#hostOverride;
|
|
33
|
+
this.#connectionTimeout = connectionTimeout ?? this.#connectionTimeout;
|
|
34
|
+
this.#readTimeout = readTimeout ?? this.#readTimeout;
|
|
35
|
+
}
|
|
36
|
+
// todo: cache the session.
|
|
37
|
+
get session() {
|
|
38
|
+
if (this.#session !== undefined) {
|
|
39
|
+
return this.#session;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
return axios_1.default.create({
|
|
43
|
+
baseURL: this.url,
|
|
44
|
+
headers: {
|
|
45
|
+
// biome-ignore lint: external API
|
|
46
|
+
Authorization: `Bearer ${this.#apiKey}`,
|
|
47
|
+
},
|
|
48
|
+
transformResponse: decodeErrorData,
|
|
49
|
+
timeout: this.#connectionTimeout,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
get url() {
|
|
54
|
+
return (this.#hostOverride ??
|
|
55
|
+
`https://${this.#dbName}.${this.#region}.api.lancedb.com`);
|
|
56
|
+
}
|
|
57
|
+
get headers() {
|
|
58
|
+
const headers = {
|
|
59
|
+
"x-api-key": this.#apiKey,
|
|
60
|
+
"x-request-id": "na",
|
|
61
|
+
};
|
|
62
|
+
if (this.#region == "local") {
|
|
63
|
+
headers["Host"] = `${this.#dbName}.${this.#region}.api.lancedb.com`;
|
|
64
|
+
}
|
|
65
|
+
if (this.#hostOverride) {
|
|
66
|
+
headers["x-lancedb-database"] = this.#dbName;
|
|
67
|
+
}
|
|
68
|
+
return headers;
|
|
69
|
+
}
|
|
70
|
+
isOpen() {
|
|
71
|
+
return !this.#closed;
|
|
72
|
+
}
|
|
73
|
+
checkNotClosed() {
|
|
74
|
+
if (this.#closed) {
|
|
75
|
+
throw new Error("Connection is closed");
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
close() {
|
|
79
|
+
this.#session = undefined;
|
|
80
|
+
this.#closed = true;
|
|
81
|
+
}
|
|
82
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
83
|
+
async get(uri, params) {
|
|
84
|
+
this.checkNotClosed();
|
|
85
|
+
uri = new URL(uri, this.url).toString();
|
|
86
|
+
let response;
|
|
87
|
+
try {
|
|
88
|
+
response = await this.session.get(uri, {
|
|
89
|
+
headers: this.headers,
|
|
90
|
+
params,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
catch (e) {
|
|
94
|
+
if (e instanceof axios_1.AxiosError) {
|
|
95
|
+
response = e.response;
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
throw e;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
RestfulLanceDBClient.checkStatus(response);
|
|
102
|
+
return response.data;
|
|
103
|
+
}
|
|
104
|
+
async post(uri,
|
|
105
|
+
// biome-ignore lint/suspicious/noExplicitAny: api request
|
|
106
|
+
body, additional) {
|
|
107
|
+
this.checkNotClosed();
|
|
108
|
+
uri = new URL(uri, this.url).toString();
|
|
109
|
+
additional = Object.assign({ config: { responseType: "json" } }, additional);
|
|
110
|
+
const headers = { ...this.headers, ...additional.headers };
|
|
111
|
+
if (!headers["Content-Type"]) {
|
|
112
|
+
headers["Content-Type"] = "application/json";
|
|
113
|
+
}
|
|
114
|
+
let response;
|
|
115
|
+
try {
|
|
116
|
+
response = await this.session.post(uri, body, {
|
|
117
|
+
headers,
|
|
118
|
+
responseType: additional.config.responseType,
|
|
119
|
+
params: new Map(Object.entries(additional.params ?? {})),
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
catch (e) {
|
|
123
|
+
if (e instanceof axios_1.AxiosError) {
|
|
124
|
+
response = e.response;
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
throw e;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
RestfulLanceDBClient.checkStatus(response);
|
|
131
|
+
if (additional.config.responseType === "arraybuffer") {
|
|
132
|
+
return response.data;
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
return JSON.parse(response.data);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
async listTables(limit = 10, pageToken = "") {
|
|
139
|
+
const json = await this.get("/v1/table", { limit, pageToken });
|
|
140
|
+
return json.tables;
|
|
141
|
+
}
|
|
142
|
+
async query(tableName, query) {
|
|
143
|
+
const tbl = await this.post(`/v1/table/${tableName}/query`, query, {
|
|
144
|
+
config: {
|
|
145
|
+
responseType: "arraybuffer",
|
|
146
|
+
},
|
|
147
|
+
});
|
|
148
|
+
return (0, arrow_1.tableFromIPC)(tbl);
|
|
149
|
+
}
|
|
150
|
+
static checkStatus(response) {
|
|
151
|
+
if (response.status === 404) {
|
|
152
|
+
throw new Error(`Not found: ${response.data}`);
|
|
153
|
+
}
|
|
154
|
+
else if (response.status >= 400 && response.status < 500) {
|
|
155
|
+
throw new Error(`Bad Request: ${response.status}, error: ${response.data}`);
|
|
156
|
+
}
|
|
157
|
+
else if (response.status >= 500 && response.status < 600) {
|
|
158
|
+
throw new Error(`Internal Server Error: ${response.status}, error: ${response.data}`);
|
|
159
|
+
}
|
|
160
|
+
else if (response.status !== 200) {
|
|
161
|
+
throw new Error(`Unknown Error: ${response.status}, error: ${response.data}`);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
exports.RestfulLanceDBClient = RestfulLanceDBClient;
|
|
166
|
+
function decodeErrorData(data) {
|
|
167
|
+
if (Buffer.isBuffer(data)) {
|
|
168
|
+
const decoded = data.toString("utf-8");
|
|
169
|
+
return decoded;
|
|
170
|
+
}
|
|
171
|
+
return data;
|
|
172
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Data, SchemaLike } from "../arrow";
|
|
2
|
+
import { Connection, CreateTableOptions, OpenTableOptions, TableNamesOptions } from "../connection";
|
|
3
|
+
import { Table } from "../table";
|
|
4
|
+
export interface RemoteConnectionOptions {
|
|
5
|
+
apiKey?: string;
|
|
6
|
+
region?: string;
|
|
7
|
+
hostOverride?: string;
|
|
8
|
+
connectionTimeout?: number;
|
|
9
|
+
readTimeout?: number;
|
|
10
|
+
}
|
|
11
|
+
export declare class RemoteConnection extends Connection {
|
|
12
|
+
#private;
|
|
13
|
+
constructor(url: string, { apiKey, region, hostOverride, connectionTimeout, readTimeout, }: RemoteConnectionOptions);
|
|
14
|
+
isOpen(): boolean;
|
|
15
|
+
close(): void;
|
|
16
|
+
display(): string;
|
|
17
|
+
tableNames(options?: Partial<TableNamesOptions>): Promise<string[]>;
|
|
18
|
+
openTable(name: string, _options?: Partial<OpenTableOptions> | undefined): Promise<Table>;
|
|
19
|
+
createTable(nameOrOptions: string | ({
|
|
20
|
+
name: string;
|
|
21
|
+
data: Data;
|
|
22
|
+
} & Partial<CreateTableOptions>), data?: Data, options?: Partial<CreateTableOptions> | undefined): Promise<Table>;
|
|
23
|
+
createEmptyTable(name: string, schema: SchemaLike, options?: Partial<CreateTableOptions> | undefined): Promise<Table>;
|
|
24
|
+
dropTable(name: string): Promise<void>;
|
|
25
|
+
}
|