@lancedb/lancedb 0.5.0 → 0.5.1
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/biome.json +8 -2
- package/dist/arrow.d.ts +34 -9
- package/dist/arrow.js +220 -23
- package/dist/connection.d.ts +4 -1
- package/dist/connection.js +11 -5
- package/dist/embedding/embedding_function.d.ts +54 -28
- package/dist/embedding/embedding_function.js +71 -10
- package/dist/embedding/index.d.ts +28 -2
- package/dist/embedding/index.js +111 -4
- package/dist/embedding/openai.d.ts +16 -7
- package/dist/embedding/openai.js +62 -12
- package/dist/embedding/registry.d.ts +54 -0
- package/dist/embedding/registry.js +123 -0
- package/dist/query.d.ts +1 -1
- package/dist/query.js +3 -3
- package/dist/sanitize.d.ts +22 -1
- package/dist/sanitize.js +123 -110
- package/dist/table.d.ts +1 -2
- package/dist/table.js +6 -3
- package/lancedb/arrow.ts +234 -38
- package/lancedb/connection.ts +27 -6
- package/lancedb/embedding/embedding_function.ts +126 -42
- package/lancedb/embedding/index.ts +113 -2
- package/lancedb/embedding/openai.ts +62 -16
- package/lancedb/embedding/registry.ts +172 -0
- package/lancedb/query.ts +2 -1
- package/lancedb/sanitize.ts +22 -22
- package/lancedb/table.ts +10 -3
- package/nodejs-artifacts/arrow.d.ts +34 -9
- package/nodejs-artifacts/arrow.js +220 -23
- package/nodejs-artifacts/connection.d.ts +4 -1
- package/nodejs-artifacts/connection.js +11 -5
- package/nodejs-artifacts/embedding/embedding_function.d.ts +54 -28
- package/nodejs-artifacts/embedding/embedding_function.js +71 -10
- package/nodejs-artifacts/embedding/index.d.ts +28 -2
- package/nodejs-artifacts/embedding/index.js +111 -4
- package/nodejs-artifacts/embedding/openai.d.ts +16 -7
- package/nodejs-artifacts/embedding/openai.js +62 -12
- package/nodejs-artifacts/embedding/registry.d.ts +54 -0
- package/nodejs-artifacts/embedding/registry.js +123 -0
- package/nodejs-artifacts/query.d.ts +1 -1
- package/nodejs-artifacts/query.js +3 -3
- package/nodejs-artifacts/sanitize.d.ts +22 -1
- package/nodejs-artifacts/sanitize.js +123 -110
- package/nodejs-artifacts/table.d.ts +1 -2
- package/nodejs-artifacts/table.js +6 -3
- package/package.json +14 -9
- package/tsconfig.json +3 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
// Copyright
|
|
2
|
+
// Copyright 2024 Lance Developers.
|
|
3
3
|
//
|
|
4
4
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
5
|
// you may not use this file except in compliance with the License.
|
|
@@ -13,15 +13,76 @@
|
|
|
13
13
|
// See the License for the specific language governing permissions and
|
|
14
14
|
// limitations under the License.
|
|
15
15
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
-
exports.
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
exports.EmbeddingFunction = void 0;
|
|
17
|
+
require("reflect-metadata");
|
|
18
|
+
const arrow_1 = require("../arrow");
|
|
19
|
+
const sanitize_1 = require("../sanitize");
|
|
20
|
+
/**
|
|
21
|
+
* An embedding function that automatically creates vector representation for a given column.
|
|
22
|
+
*/
|
|
23
|
+
class EmbeddingFunction {
|
|
24
|
+
/**
|
|
25
|
+
* sourceField is used in combination with `LanceSchema` to provide a declarative data model
|
|
26
|
+
*
|
|
27
|
+
* @param optionsOrDatatype - The options for the field or the datatype
|
|
28
|
+
*
|
|
29
|
+
* @see {@link lancedb.LanceSchema}
|
|
30
|
+
*/
|
|
31
|
+
sourceField(optionsOrDatatype) {
|
|
32
|
+
let datatype = (0, arrow_1.isDataType)(optionsOrDatatype)
|
|
33
|
+
? optionsOrDatatype
|
|
34
|
+
: optionsOrDatatype?.datatype;
|
|
35
|
+
if (!datatype) {
|
|
36
|
+
throw new Error("Datatype is required");
|
|
37
|
+
}
|
|
38
|
+
datatype = (0, sanitize_1.sanitizeType)(datatype);
|
|
39
|
+
const metadata = new Map();
|
|
40
|
+
metadata.set("source_column_for", this);
|
|
41
|
+
return [datatype, metadata];
|
|
21
42
|
}
|
|
22
|
-
|
|
23
|
-
|
|
43
|
+
/**
|
|
44
|
+
* vectorField is used in combination with `LanceSchema` to provide a declarative data model
|
|
45
|
+
*
|
|
46
|
+
* @param options - The options for the field
|
|
47
|
+
*
|
|
48
|
+
* @see {@link lancedb.LanceSchema}
|
|
49
|
+
*/
|
|
50
|
+
vectorField(options) {
|
|
51
|
+
let dtype;
|
|
52
|
+
const dims = this.ndims() ?? options?.dims;
|
|
53
|
+
if (!options?.datatype) {
|
|
54
|
+
if (dims === undefined) {
|
|
55
|
+
throw new Error("ndims is required for vector field");
|
|
56
|
+
}
|
|
57
|
+
dtype = new arrow_1.FixedSizeList(dims, new arrow_1.Field("item", new arrow_1.Float32(), true));
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
if ((0, arrow_1.isFixedSizeList)(options.datatype)) {
|
|
61
|
+
dtype = options.datatype;
|
|
62
|
+
}
|
|
63
|
+
else if ((0, arrow_1.isFloat)(options.datatype)) {
|
|
64
|
+
if (dims === undefined) {
|
|
65
|
+
throw new Error("ndims is required for vector field");
|
|
66
|
+
}
|
|
67
|
+
dtype = (0, arrow_1.newVectorType)(dims, options.datatype);
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
throw new Error("Expected FixedSizeList or Float as datatype for vector field");
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
const metadata = new Map();
|
|
74
|
+
metadata.set("vector_column_for", this);
|
|
75
|
+
return [dtype, metadata];
|
|
76
|
+
}
|
|
77
|
+
/** The number of dimensions of the embeddings */
|
|
78
|
+
ndims() {
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
Compute the embeddings for a single query
|
|
83
|
+
*/
|
|
84
|
+
async computeQueryEmbeddings(data) {
|
|
85
|
+
return this.computeSourceEmbeddings([data]).then((embeddings) => embeddings[0]);
|
|
24
86
|
}
|
|
25
|
-
return (typeof value.sourceColumn === "string" && typeof value.embed === "function");
|
|
26
87
|
}
|
|
27
|
-
exports.
|
|
88
|
+
exports.EmbeddingFunction = EmbeddingFunction;
|
|
@@ -1,2 +1,28 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { Schema } from "../arrow";
|
|
2
|
+
import { EmbeddingFunction } from "./embedding_function";
|
|
3
|
+
export { EmbeddingFunction } from "./embedding_function";
|
|
4
|
+
export * from "./openai";
|
|
5
|
+
export * from "./registry";
|
|
6
|
+
/**
|
|
7
|
+
* Create a schema with embedding functions.
|
|
8
|
+
*
|
|
9
|
+
* @param fields
|
|
10
|
+
* @returns Schema
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* class MyEmbeddingFunction extends EmbeddingFunction {
|
|
14
|
+
* // ...
|
|
15
|
+
* }
|
|
16
|
+
* const func = new MyEmbeddingFunction();
|
|
17
|
+
* const schema = LanceSchema({
|
|
18
|
+
* id: new Int32(),
|
|
19
|
+
* text: func.sourceField(new Utf8()),
|
|
20
|
+
* vector: func.vectorField(),
|
|
21
|
+
* // optional: specify the datatype and/or dimensions
|
|
22
|
+
* vector2: func.vectorField({ datatype: new Float32(), dims: 3}),
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* const table = await db.createTable("my_table", data, { schema });
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare function LanceSchema(fields: Record<string, [object, Map<string, EmbeddingFunction>] | object>): Schema;
|
|
@@ -1,7 +1,114 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
// Copyright 2023 Lance 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
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
16
|
+
if (k2 === undefined) k2 = k;
|
|
17
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
18
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
19
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
20
|
+
}
|
|
21
|
+
Object.defineProperty(o, k2, desc);
|
|
22
|
+
}) : (function(o, m, k, k2) {
|
|
23
|
+
if (k2 === undefined) k2 = k;
|
|
24
|
+
o[k2] = m[k];
|
|
25
|
+
}));
|
|
26
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
27
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
28
|
+
};
|
|
2
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
30
|
+
exports.LanceSchema = exports.EmbeddingFunction = void 0;
|
|
31
|
+
const arrow_1 = require("../arrow");
|
|
32
|
+
const arrow_2 = require("../arrow");
|
|
33
|
+
const sanitize_1 = require("../sanitize");
|
|
34
|
+
const registry_1 = require("./registry");
|
|
4
35
|
var embedding_function_1 = require("./embedding_function");
|
|
5
|
-
Object.defineProperty(exports, "
|
|
6
|
-
|
|
7
|
-
|
|
36
|
+
Object.defineProperty(exports, "EmbeddingFunction", { enumerable: true, get: function () { return embedding_function_1.EmbeddingFunction; } });
|
|
37
|
+
// We need to explicitly export '*' so that the `register` decorator actually registers the class.
|
|
38
|
+
__exportStar(require("./openai"), exports);
|
|
39
|
+
__exportStar(require("./registry"), exports);
|
|
40
|
+
/**
|
|
41
|
+
* Create a schema with embedding functions.
|
|
42
|
+
*
|
|
43
|
+
* @param fields
|
|
44
|
+
* @returns Schema
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* class MyEmbeddingFunction extends EmbeddingFunction {
|
|
48
|
+
* // ...
|
|
49
|
+
* }
|
|
50
|
+
* const func = new MyEmbeddingFunction();
|
|
51
|
+
* const schema = LanceSchema({
|
|
52
|
+
* id: new Int32(),
|
|
53
|
+
* text: func.sourceField(new Utf8()),
|
|
54
|
+
* vector: func.vectorField(),
|
|
55
|
+
* // optional: specify the datatype and/or dimensions
|
|
56
|
+
* vector2: func.vectorField({ datatype: new Float32(), dims: 3}),
|
|
57
|
+
* });
|
|
58
|
+
*
|
|
59
|
+
* const table = await db.createTable("my_table", data, { schema });
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
function LanceSchema(fields) {
|
|
63
|
+
const arrowFields = [];
|
|
64
|
+
const embeddingFunctions = new Map();
|
|
65
|
+
Object.entries(fields).forEach(([key, value]) => {
|
|
66
|
+
if ((0, arrow_2.isDataType)(value)) {
|
|
67
|
+
arrowFields.push(new arrow_1.Field(key, (0, sanitize_1.sanitizeType)(value), true));
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
const [dtype, metadata] = value;
|
|
71
|
+
arrowFields.push(new arrow_1.Field(key, (0, sanitize_1.sanitizeType)(dtype), true));
|
|
72
|
+
parseEmbeddingFunctions(embeddingFunctions, key, metadata);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
const registry = (0, registry_1.getRegistry)();
|
|
76
|
+
const metadata = registry.getTableMetadata(Array.from(embeddingFunctions.values()));
|
|
77
|
+
const schema = new arrow_1.Schema(arrowFields, metadata);
|
|
78
|
+
return schema;
|
|
79
|
+
}
|
|
80
|
+
exports.LanceSchema = LanceSchema;
|
|
81
|
+
function parseEmbeddingFunctions(embeddingFunctions, key, metadata) {
|
|
82
|
+
if (metadata.has("source_column_for")) {
|
|
83
|
+
const embedFunction = metadata.get("source_column_for");
|
|
84
|
+
const current = embeddingFunctions.get(embedFunction);
|
|
85
|
+
if (current !== undefined) {
|
|
86
|
+
embeddingFunctions.set(embedFunction, {
|
|
87
|
+
...current,
|
|
88
|
+
sourceColumn: key,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
embeddingFunctions.set(embedFunction, {
|
|
93
|
+
sourceColumn: key,
|
|
94
|
+
function: embedFunction,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
else if (metadata.has("vector_column_for")) {
|
|
99
|
+
const embedFunction = metadata.get("vector_column_for");
|
|
100
|
+
const current = embeddingFunctions.get(embedFunction);
|
|
101
|
+
if (current !== undefined) {
|
|
102
|
+
embeddingFunctions.set(embedFunction, {
|
|
103
|
+
...current,
|
|
104
|
+
vectorColumn: key,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
embeddingFunctions.set(embedFunction, {
|
|
109
|
+
vectorColumn: key,
|
|
110
|
+
function: embedFunction,
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
@@ -1,8 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import { Float } from "../arrow";
|
|
2
|
+
import { EmbeddingFunction } from "./embedding_function";
|
|
3
|
+
export type OpenAIOptions = {
|
|
4
|
+
apiKey?: string;
|
|
5
|
+
model?: string;
|
|
6
|
+
};
|
|
7
|
+
export declare class OpenAIEmbeddingFunction extends EmbeddingFunction<string, OpenAIOptions> {
|
|
8
|
+
#private;
|
|
9
|
+
constructor(options?: OpenAIOptions);
|
|
10
|
+
toJSON(): {
|
|
11
|
+
model: string;
|
|
12
|
+
};
|
|
13
|
+
ndims(): number;
|
|
14
|
+
embeddingDataType(): Float;
|
|
15
|
+
computeSourceEmbeddings(data: string[]): Promise<number[][]>;
|
|
16
|
+
computeQueryEmbeddings(data: string): Promise<number[]>;
|
|
8
17
|
}
|
|
@@ -12,12 +12,30 @@
|
|
|
12
12
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
13
|
// See the License for the specific language governing permissions and
|
|
14
14
|
// limitations under the License.
|
|
15
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
16
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
17
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
18
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
19
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
20
|
+
};
|
|
21
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
22
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
23
|
+
};
|
|
15
24
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
25
|
exports.OpenAIEmbeddingFunction = void 0;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
26
|
+
const arrow_1 = require("../arrow");
|
|
27
|
+
const embedding_function_1 = require("./embedding_function");
|
|
28
|
+
const registry_1 = require("./registry");
|
|
29
|
+
let OpenAIEmbeddingFunction = class OpenAIEmbeddingFunction extends embedding_function_1.EmbeddingFunction {
|
|
30
|
+
#openai;
|
|
31
|
+
#modelName;
|
|
32
|
+
constructor(options = { model: "text-embedding-ada-002" }) {
|
|
33
|
+
super();
|
|
34
|
+
const openAIKey = options?.apiKey ?? process.env.OPENAI_API_KEY;
|
|
35
|
+
if (!openAIKey) {
|
|
36
|
+
throw new Error("OpenAI API key is required");
|
|
37
|
+
}
|
|
38
|
+
const modelName = options?.model ?? "text-embedding-ada-002";
|
|
21
39
|
/**
|
|
22
40
|
* @type {import("openai").default}
|
|
23
41
|
*/
|
|
@@ -30,16 +48,35 @@ class OpenAIEmbeddingFunction {
|
|
|
30
48
|
catch {
|
|
31
49
|
throw new Error("please install openai@^4.24.1 using npm install openai");
|
|
32
50
|
}
|
|
33
|
-
this.sourceColumn = sourceColumn;
|
|
34
51
|
const configuration = {
|
|
35
52
|
apiKey: openAIKey,
|
|
36
53
|
};
|
|
37
|
-
this
|
|
38
|
-
this
|
|
54
|
+
this.#openai = new Openai(configuration);
|
|
55
|
+
this.#modelName = modelName;
|
|
56
|
+
}
|
|
57
|
+
toJSON() {
|
|
58
|
+
return {
|
|
59
|
+
model: this.#modelName,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
ndims() {
|
|
63
|
+
switch (this.#modelName) {
|
|
64
|
+
case "text-embedding-ada-002":
|
|
65
|
+
return 1536;
|
|
66
|
+
case "text-embedding-3-large":
|
|
67
|
+
return 3072;
|
|
68
|
+
case "text-embedding-3-small":
|
|
69
|
+
return 1536;
|
|
70
|
+
default:
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
39
73
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
74
|
+
embeddingDataType() {
|
|
75
|
+
return new arrow_1.Float32();
|
|
76
|
+
}
|
|
77
|
+
async computeSourceEmbeddings(data) {
|
|
78
|
+
const response = await this.#openai.embeddings.create({
|
|
79
|
+
model: this.#modelName,
|
|
43
80
|
input: data,
|
|
44
81
|
});
|
|
45
82
|
const embeddings = [];
|
|
@@ -48,6 +85,19 @@ class OpenAIEmbeddingFunction {
|
|
|
48
85
|
}
|
|
49
86
|
return embeddings;
|
|
50
87
|
}
|
|
51
|
-
|
|
52
|
-
|
|
88
|
+
async computeQueryEmbeddings(data) {
|
|
89
|
+
if (typeof data !== "string") {
|
|
90
|
+
throw new Error("Data must be a string");
|
|
91
|
+
}
|
|
92
|
+
const response = await this.#openai.embeddings.create({
|
|
93
|
+
model: this.#modelName,
|
|
94
|
+
input: data,
|
|
95
|
+
});
|
|
96
|
+
return response.data[0].embedding;
|
|
97
|
+
}
|
|
98
|
+
};
|
|
53
99
|
exports.OpenAIEmbeddingFunction = OpenAIEmbeddingFunction;
|
|
100
|
+
exports.OpenAIEmbeddingFunction = OpenAIEmbeddingFunction = __decorate([
|
|
101
|
+
(0, registry_1.register)("openai"),
|
|
102
|
+
__metadata("design:paramtypes", [Object])
|
|
103
|
+
], OpenAIEmbeddingFunction);
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { EmbeddingFunction } from "./embedding_function";
|
|
2
|
+
import "reflect-metadata";
|
|
3
|
+
export interface EmbeddingFunctionOptions {
|
|
4
|
+
[key: string]: unknown;
|
|
5
|
+
}
|
|
6
|
+
export interface EmbeddingFunctionFactory<T extends EmbeddingFunction = EmbeddingFunction> {
|
|
7
|
+
new (modelOptions?: EmbeddingFunctionOptions): T;
|
|
8
|
+
}
|
|
9
|
+
interface EmbeddingFunctionCreate<T extends EmbeddingFunction> {
|
|
10
|
+
create(options?: EmbeddingFunctionOptions): T;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* This is a singleton class used to register embedding functions
|
|
14
|
+
* and fetch them by name. It also handles serializing and deserializing.
|
|
15
|
+
* You can implement your own embedding function by subclassing EmbeddingFunction
|
|
16
|
+
* or TextEmbeddingFunction and registering it with the registry
|
|
17
|
+
*/
|
|
18
|
+
export declare class EmbeddingFunctionRegistry {
|
|
19
|
+
#private;
|
|
20
|
+
/**
|
|
21
|
+
* Register an embedding function
|
|
22
|
+
* @param name The name of the function
|
|
23
|
+
* @param func The function to register
|
|
24
|
+
*/
|
|
25
|
+
register<T extends EmbeddingFunctionFactory = EmbeddingFunctionFactory>(this: EmbeddingFunctionRegistry, alias?: string): (ctor: T) => any;
|
|
26
|
+
/**
|
|
27
|
+
* Fetch an embedding function by name
|
|
28
|
+
* @param name The name of the function
|
|
29
|
+
*/
|
|
30
|
+
get<T extends EmbeddingFunction<unknown> = EmbeddingFunction>(name: string): EmbeddingFunctionCreate<T> | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* reset the registry to the initial state
|
|
33
|
+
*/
|
|
34
|
+
reset(this: EmbeddingFunctionRegistry): void;
|
|
35
|
+
parseFunctions(this: EmbeddingFunctionRegistry, metadata: Map<string, string>): Map<string, EmbeddingFunctionConfig>;
|
|
36
|
+
functionToMetadata(conf: EmbeddingFunctionConfig): Record<string, any>;
|
|
37
|
+
getTableMetadata(functions: EmbeddingFunctionConfig[]): Map<string, string>;
|
|
38
|
+
}
|
|
39
|
+
export declare function register(name?: string): (ctor: EmbeddingFunctionFactory<EmbeddingFunction<any, import("./embedding_function").FunctionOptions>>) => any;
|
|
40
|
+
/**
|
|
41
|
+
* Utility function to get the global instance of the registry
|
|
42
|
+
* @returns `EmbeddingFunctionRegistry` The global instance of the registry
|
|
43
|
+
* @example
|
|
44
|
+
* ```ts
|
|
45
|
+
* const registry = getRegistry();
|
|
46
|
+
* const openai = registry.get("openai").create();
|
|
47
|
+
*/
|
|
48
|
+
export declare function getRegistry(): EmbeddingFunctionRegistry;
|
|
49
|
+
export interface EmbeddingFunctionConfig {
|
|
50
|
+
sourceColumn: string;
|
|
51
|
+
vectorColumn?: string;
|
|
52
|
+
function: EmbeddingFunction;
|
|
53
|
+
}
|
|
54
|
+
export {};
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright 2024 Lance 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.getRegistry = exports.register = exports.EmbeddingFunctionRegistry = void 0;
|
|
17
|
+
require("reflect-metadata");
|
|
18
|
+
/**
|
|
19
|
+
* This is a singleton class used to register embedding functions
|
|
20
|
+
* and fetch them by name. It also handles serializing and deserializing.
|
|
21
|
+
* You can implement your own embedding function by subclassing EmbeddingFunction
|
|
22
|
+
* or TextEmbeddingFunction and registering it with the registry
|
|
23
|
+
*/
|
|
24
|
+
class EmbeddingFunctionRegistry {
|
|
25
|
+
#functions = new Map();
|
|
26
|
+
/**
|
|
27
|
+
* Register an embedding function
|
|
28
|
+
* @param name The name of the function
|
|
29
|
+
* @param func The function to register
|
|
30
|
+
*/
|
|
31
|
+
register(alias) {
|
|
32
|
+
const self = this;
|
|
33
|
+
return function (ctor) {
|
|
34
|
+
if (!alias) {
|
|
35
|
+
alias = ctor.name;
|
|
36
|
+
}
|
|
37
|
+
if (self.#functions.has(alias)) {
|
|
38
|
+
throw new Error(`Embedding function with alias "${alias}" already exists`);
|
|
39
|
+
}
|
|
40
|
+
self.#functions.set(alias, ctor);
|
|
41
|
+
Reflect.defineMetadata("lancedb::embedding::name", alias, ctor);
|
|
42
|
+
return ctor;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Fetch an embedding function by name
|
|
47
|
+
* @param name The name of the function
|
|
48
|
+
*/
|
|
49
|
+
get(name) {
|
|
50
|
+
const factory = this.#functions.get(name);
|
|
51
|
+
if (!factory) {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
create: function (options) {
|
|
56
|
+
return new factory(options);
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* reset the registry to the initial state
|
|
62
|
+
*/
|
|
63
|
+
reset() {
|
|
64
|
+
this.#functions.clear();
|
|
65
|
+
}
|
|
66
|
+
parseFunctions(metadata) {
|
|
67
|
+
if (!metadata.has("embedding_functions")) {
|
|
68
|
+
return new Map();
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
const functions = (JSON.parse(metadata.get("embedding_functions")));
|
|
72
|
+
return new Map(functions.map((f) => {
|
|
73
|
+
const fn = this.get(f.name);
|
|
74
|
+
if (!fn) {
|
|
75
|
+
throw new Error(`Function "${f.name}" not found in registry`);
|
|
76
|
+
}
|
|
77
|
+
return [
|
|
78
|
+
f.name,
|
|
79
|
+
{
|
|
80
|
+
sourceColumn: f.sourceColumn,
|
|
81
|
+
vectorColumn: f.vectorColumn,
|
|
82
|
+
function: this.get(f.name).create(f.model),
|
|
83
|
+
},
|
|
84
|
+
];
|
|
85
|
+
}));
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
89
|
+
functionToMetadata(conf) {
|
|
90
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
91
|
+
const metadata = {};
|
|
92
|
+
const name = Reflect.getMetadata("lancedb::embedding::name", conf.function.constructor);
|
|
93
|
+
metadata["sourceColumn"] = conf.sourceColumn;
|
|
94
|
+
metadata["vectorColumn"] = conf.vectorColumn ?? "vector";
|
|
95
|
+
metadata["name"] = name ?? conf.function.constructor.name;
|
|
96
|
+
metadata["model"] = conf.function.toJSON();
|
|
97
|
+
return metadata;
|
|
98
|
+
}
|
|
99
|
+
getTableMetadata(functions) {
|
|
100
|
+
const metadata = new Map();
|
|
101
|
+
const jsonData = functions.map((conf) => this.functionToMetadata(conf));
|
|
102
|
+
metadata.set("embedding_functions", JSON.stringify(jsonData));
|
|
103
|
+
return metadata;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
exports.EmbeddingFunctionRegistry = EmbeddingFunctionRegistry;
|
|
107
|
+
const _REGISTRY = new EmbeddingFunctionRegistry();
|
|
108
|
+
function register(name) {
|
|
109
|
+
return _REGISTRY.register(name);
|
|
110
|
+
}
|
|
111
|
+
exports.register = register;
|
|
112
|
+
/**
|
|
113
|
+
* Utility function to get the global instance of the registry
|
|
114
|
+
* @returns `EmbeddingFunctionRegistry` The global instance of the registry
|
|
115
|
+
* @example
|
|
116
|
+
* ```ts
|
|
117
|
+
* const registry = getRegistry();
|
|
118
|
+
* const openai = registry.get("openai").create();
|
|
119
|
+
*/
|
|
120
|
+
function getRegistry() {
|
|
121
|
+
return _REGISTRY;
|
|
122
|
+
}
|
|
123
|
+
exports.getRegistry = getRegistry;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Table as ArrowTable, RecordBatch } from "
|
|
1
|
+
import { Table as ArrowTable, RecordBatch } from "./arrow";
|
|
2
2
|
import { RecordBatchIterator as NativeBatchIterator, Query as NativeQuery, Table as NativeTable, VectorQuery as NativeVectorQuery } from "./native";
|
|
3
3
|
export declare class RecordBatchIterator implements AsyncIterator<RecordBatch> {
|
|
4
4
|
private promisedInner?;
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
// limitations under the License.
|
|
15
15
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
16
|
exports.Query = exports.VectorQuery = exports.QueryBase = exports.RecordBatchIterator = void 0;
|
|
17
|
-
const
|
|
17
|
+
const arrow_1 = require("./arrow");
|
|
18
18
|
class RecordBatchIterator {
|
|
19
19
|
promisedInner;
|
|
20
20
|
inner;
|
|
@@ -34,7 +34,7 @@ class RecordBatchIterator {
|
|
|
34
34
|
if (n == null) {
|
|
35
35
|
return Promise.resolve({ done: true, value: null });
|
|
36
36
|
}
|
|
37
|
-
const tbl = (0,
|
|
37
|
+
const tbl = (0, arrow_1.tableFromIPC)(n);
|
|
38
38
|
if (tbl.batches.length != 1) {
|
|
39
39
|
throw new Error("Expected only one batch");
|
|
40
40
|
}
|
|
@@ -148,7 +148,7 @@ class QueryBase {
|
|
|
148
148
|
for await (const batch of this) {
|
|
149
149
|
batches.push(batch);
|
|
150
150
|
}
|
|
151
|
-
return new
|
|
151
|
+
return new arrow_1.Table(batches);
|
|
152
152
|
}
|
|
153
153
|
/** Collect the results as an array of objects. */
|
|
154
154
|
async toArray() {
|
|
@@ -1,4 +1,25 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { TKeys } from "apache-arrow/type";
|
|
2
|
+
import { DataType, Date_, Decimal, DenseUnion, Dictionary, Duration, Field, FixedSizeBinary, FixedSizeList, Float, Int, Interval, List, Map_, Schema, SparseUnion, Struct, Time, Timestamp, TimestampMicrosecond, TimestampMillisecond, TimestampNanosecond, TimestampSecond, Type, Union } from "./arrow";
|
|
3
|
+
export declare function sanitizeMetadata(metadataLike?: unknown): Map<string, string> | undefined;
|
|
4
|
+
export declare function sanitizeInt(typeLike: object): Int<Type.Int | Type.Int8 | Type.Int16 | Type.Int32 | Type.Int64 | Type.Uint8 | Type.Uint16 | Type.Uint32 | Type.Uint64>;
|
|
5
|
+
export declare function sanitizeFloat(typeLike: object): Float<Type.Float | Type.Float16 | Type.Float32 | Type.Float64>;
|
|
6
|
+
export declare function sanitizeDecimal(typeLike: object): Decimal;
|
|
7
|
+
export declare function sanitizeDate(typeLike: object): Date_<import("apache-arrow/type").Dates>;
|
|
8
|
+
export declare function sanitizeTime(typeLike: object): Time<Type.Time | Type.TimeSecond | Type.TimeMillisecond | Type.TimeMicrosecond | Type.TimeNanosecond>;
|
|
9
|
+
export declare function sanitizeTimestamp(typeLike: object): Timestamp<Type.Timestamp | Type.TimestampSecond | Type.TimestampMillisecond | Type.TimestampMicrosecond | Type.TimestampNanosecond>;
|
|
10
|
+
export declare function sanitizeTypedTimestamp(typeLike: object, Datatype: typeof TimestampNanosecond | typeof TimestampMicrosecond | typeof TimestampMillisecond | typeof TimestampSecond): TimestampSecond | TimestampMillisecond | TimestampMicrosecond | TimestampNanosecond;
|
|
11
|
+
export declare function sanitizeInterval(typeLike: object): Interval<Type.Interval | Type.IntervalDayTime | Type.IntervalYearMonth>;
|
|
12
|
+
export declare function sanitizeList(typeLike: object): List<any>;
|
|
13
|
+
export declare function sanitizeStruct(typeLike: object): Struct<any>;
|
|
14
|
+
export declare function sanitizeUnion(typeLike: object): Union<Type.Union | Type.DenseUnion | Type.SparseUnion>;
|
|
15
|
+
export declare function sanitizeTypedUnion(typeLike: object, UnionType: typeof DenseUnion | typeof SparseUnion): SparseUnion | DenseUnion;
|
|
16
|
+
export declare function sanitizeFixedSizeBinary(typeLike: object): FixedSizeBinary;
|
|
17
|
+
export declare function sanitizeFixedSizeList(typeLike: object): FixedSizeList<any>;
|
|
18
|
+
export declare function sanitizeMap(typeLike: object): Map_<any, any>;
|
|
19
|
+
export declare function sanitizeDuration(typeLike: object): Duration<Type.Duration | Type.DurationSecond | Type.DurationMillisecond | Type.DurationMicrosecond | Type.DurationNanosecond>;
|
|
20
|
+
export declare function sanitizeDictionary(typeLike: object): Dictionary<DataType<any, any>, TKeys>;
|
|
21
|
+
export declare function sanitizeType(typeLike: unknown): DataType<any>;
|
|
22
|
+
export declare function sanitizeField(fieldLike: unknown): Field;
|
|
2
23
|
/**
|
|
3
24
|
* Convert something schemaLike into a Schema instance
|
|
4
25
|
*
|