presubmit 0.1.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/LICENSE +202 -0
- package/README.md +51 -0
- package/THIRD_PARTY_NOTICES.md +24 -0
- package/apps/web/dist/assets/FileCode-CZZH52o7.js +5 -0
- package/apps/web/dist/assets/FileCode-DovDO7Jh.css +1 -0
- package/apps/web/dist/assets/SettingsPage-CRvOwdjt.js +4 -0
- package/apps/web/dist/assets/SettingsPage-UbYSCwJj.css +1 -0
- package/apps/web/dist/assets/TerminalPanel-0sw-J6YS.css +1 -0
- package/apps/web/dist/assets/TerminalPanel-BqUjPWav.js +16 -0
- package/apps/web/dist/assets/index-BhqNNFja.js +54 -0
- package/apps/web/dist/assets/index-DD8VXy8H.css +1 -0
- package/apps/web/dist/assets/presubmit-mark-BBQ19Fmr.svg +7 -0
- package/apps/web/dist/assets/presubmit-mark-busy-CwBC-7on.svg +8 -0
- package/apps/web/dist/assets/react-vendor-wiHys0m2.js +9 -0
- package/apps/web/dist/assets/rolldown-runtime-hePW80VL.js +1 -0
- package/apps/web/dist/index.html +6 -0
- package/apps/web/dist/service-worker.js +11 -0
- package/dist/apps/server/src/agent-builder.js +204 -0
- package/dist/apps/server/src/attach.js +163 -0
- package/dist/apps/server/src/attachments.js +86 -0
- package/dist/apps/server/src/browser-access.js +131 -0
- package/dist/apps/server/src/check-provenance.js +95 -0
- package/dist/apps/server/src/check-supervisor.js +112 -0
- package/dist/apps/server/src/checks.js +402 -0
- package/dist/apps/server/src/child-collaboration-guidance.js +2 -0
- package/dist/apps/server/src/child-sessions.js +191 -0
- package/dist/apps/server/src/cli.js +162 -0
- package/dist/apps/server/src/conversation-search.js +12 -0
- package/dist/apps/server/src/coordinator-messages.js +72 -0
- package/dist/apps/server/src/dev-auth.js +54 -0
- package/dist/apps/server/src/dev-server.js +201 -0
- package/dist/apps/server/src/dev-worker.js +48 -0
- package/dist/apps/server/src/diagnostics.js +105 -0
- package/dist/apps/server/src/fast-mode.js +8 -0
- package/dist/apps/server/src/folder-picker.js +54 -0
- package/dist/apps/server/src/git-blobs.js +62 -0
- package/dist/apps/server/src/git-handoff.js +127 -0
- package/dist/apps/server/src/history-sources.js +59 -0
- package/dist/apps/server/src/hub.js +159 -0
- package/dist/apps/server/src/internal-transport.js +38 -0
- package/dist/apps/server/src/local-embeddings.js +70 -0
- package/dist/apps/server/src/ollama-setup.js +56 -0
- package/dist/apps/server/src/planning.js +30 -0
- package/dist/apps/server/src/project-agents.js +276 -0
- package/dist/apps/server/src/project-memory.js +94 -0
- package/dist/apps/server/src/projects.js +106 -0
- package/dist/apps/server/src/queued-launch.js +13 -0
- package/dist/apps/server/src/reply-search.js +172 -0
- package/dist/apps/server/src/review-context.js +60 -0
- package/dist/apps/server/src/review-refresh.js +14 -0
- package/dist/apps/server/src/reviewers.js +75 -0
- package/dist/apps/server/src/search-settings.js +22 -0
- package/dist/apps/server/src/semantic-history.js +186 -0
- package/dist/apps/server/src/server.js +4338 -0
- package/dist/apps/server/src/session-changes.js +181 -0
- package/dist/apps/server/src/session-router-settings.js +198 -0
- package/dist/apps/server/src/session-workspaces.js +135 -0
- package/dist/apps/server/src/sessions.js +579 -0
- package/dist/apps/server/src/shared-auth.js +70 -0
- package/dist/apps/server/src/shell-evaluator-settings.js +53 -0
- package/dist/apps/server/src/store.js +203 -0
- package/dist/apps/server/src/tailscale-process.js +115 -0
- package/dist/apps/server/src/tailscale-serve-worker.js +33 -0
- package/dist/apps/server/src/tailscale-serve.js +276 -0
- package/dist/apps/server/src/task-capacity.js +92 -0
- package/dist/apps/server/src/task-graph.js +190 -0
- package/dist/apps/server/src/task-logs.js +74 -0
- package/dist/apps/server/src/task-numbers.js +25 -0
- package/dist/apps/server/src/task-orchestrator.js +1088 -0
- package/dist/apps/server/src/terminal.js +182 -0
- package/dist/apps/server/src/terminals.js +80 -0
- package/dist/apps/server/src/tool-safety.js +2 -0
- package/dist/apps/server/src/workspace-lock.js +140 -0
- package/dist/apps/server/src/workspace-watcher.js +65 -0
- package/dist/apps/server/src/workspace.js +517 -0
- package/dist/packages/agent-pi/src/agent-builder-tool.js +32 -0
- package/dist/packages/agent-pi/src/agent-capabilities.js +44 -0
- package/dist/packages/agent-pi/src/ask-user.js +86 -0
- package/dist/packages/agent-pi/src/auth.js +510 -0
- package/dist/packages/agent-pi/src/check-tools.js +72 -0
- package/dist/packages/agent-pi/src/command-policy.js +246 -0
- package/dist/packages/agent-pi/src/coordinator-tools.js +122 -0
- package/dist/packages/agent-pi/src/fast-mode.js +67 -0
- package/dist/packages/agent-pi/src/file-tools.js +163 -0
- package/dist/packages/agent-pi/src/history-tool.js +40 -0
- package/dist/packages/agent-pi/src/history.js +34 -0
- package/dist/packages/agent-pi/src/index.js +177 -0
- package/dist/packages/agent-pi/src/ipc.js +314 -0
- package/dist/packages/agent-pi/src/jev-session-router.js +408 -0
- package/dist/packages/agent-pi/src/jev-tools.js +71 -0
- package/dist/packages/agent-pi/src/local-model.js +277 -0
- package/dist/packages/agent-pi/src/memory-tools.js +119 -0
- package/dist/packages/agent-pi/src/model.js +25 -0
- package/dist/packages/agent-pi/src/planning.js +206 -0
- package/dist/packages/agent-pi/src/resource-loader.js +23 -0
- package/dist/packages/agent-pi/src/review-tools.js +158 -0
- package/dist/packages/agent-pi/src/review-transition.js +73 -0
- package/dist/packages/agent-pi/src/review.js +40 -0
- package/dist/packages/agent-pi/src/routing.js +27 -0
- package/dist/packages/agent-pi/src/session-setup.js +118 -0
- package/dist/packages/agent-pi/src/shell-context.js +51 -0
- package/dist/packages/agent-pi/src/shell-policy.js +281 -0
- package/dist/packages/agent-pi/src/shell-safety.js +205 -0
- package/dist/packages/agent-pi/src/shell.js +121 -0
- package/dist/packages/agent-pi/src/snapshot-permissions.js +12 -0
- package/dist/packages/agent-pi/src/spike.js +41 -0
- package/dist/packages/agent-pi/src/task-tools.js +53 -0
- package/dist/packages/agent-pi/src/types.js +1 -0
- package/dist/packages/agent-pi/src/usage.js +30 -0
- package/dist/packages/agent-pi/src/worker.js +602 -0
- package/dist/packages/protocol/src/activity-summary.js +77 -0
- package/dist/packages/protocol/src/agents.js +301 -0
- package/dist/packages/protocol/src/browser-access.js +1 -0
- package/dist/packages/protocol/src/checks.js +40 -0
- package/dist/packages/protocol/src/child-sessions.js +28 -0
- package/dist/packages/protocol/src/command-policy.js +3 -0
- package/dist/packages/protocol/src/coordinator.js +135 -0
- package/dist/packages/protocol/src/delegation.js +40 -0
- package/dist/packages/protocol/src/git-diff.js +71 -0
- package/dist/packages/protocol/src/git-handoff.js +1 -0
- package/dist/packages/protocol/src/index.js +1 -0
- package/dist/packages/protocol/src/memory.js +46 -0
- package/dist/packages/protocol/src/metrics.js +70 -0
- package/dist/packages/protocol/src/planning.js +138 -0
- package/dist/packages/protocol/src/retrieval.js +36 -0
- package/dist/packages/protocol/src/reviewers.js +17 -0
- package/dist/packages/protocol/src/search.js +16 -0
- package/dist/packages/protocol/src/session-changes.js +29 -0
- package/dist/packages/protocol/src/session-router.js +123 -0
- package/dist/packages/protocol/src/shell-evaluator.js +43 -0
- package/dist/packages/protocol/src/task-graph.js +1 -0
- package/dist/packages/protocol/src/token-usage.js +17 -0
- package/dist/packages/protocol/src/user-questions.js +106 -0
- package/package.json +71 -0
- package/vendor/lancedb/NODEJS_THIRD_PARTY_LICENSES.md +668 -0
- package/vendor/lancedb/RUST_THIRD_PARTY_LICENSES.html +14607 -0
- package/vendor/lancedb/dist/arrow.d.ts +296 -0
- package/vendor/lancedb/dist/arrow.js +1145 -0
- package/vendor/lancedb/dist/arrow_type.d.ts +9 -0
- package/vendor/lancedb/dist/arrow_type.js +29 -0
- package/vendor/lancedb/dist/connection.d.ts +508 -0
- package/vendor/lancedb/dist/connection.js +298 -0
- package/vendor/lancedb/dist/embedding/embedding_function.d.ts +103 -0
- package/vendor/lancedb/dist/embedding/embedding_function.js +192 -0
- package/vendor/lancedb/dist/embedding/index.d.ts +37 -0
- package/vendor/lancedb/dist/embedding/index.js +108 -0
- package/vendor/lancedb/dist/embedding/openai.d.ts +16 -0
- package/vendor/lancedb/dist/embedding/openai.js +81 -0
- package/vendor/lancedb/dist/embedding/registry.d.ts +94 -0
- package/vendor/lancedb/dist/embedding/registry.js +221 -0
- package/vendor/lancedb/dist/embedding/transformers.d.ts +36 -0
- package/vendor/lancedb/dist/embedding/transformers.js +110 -0
- package/vendor/lancedb/dist/header.d.ts +162 -0
- package/vendor/lancedb/dist/header.js +217 -0
- package/vendor/lancedb/dist/index.d.ts +246 -0
- package/vendor/lancedb/dist/index.js +176 -0
- package/vendor/lancedb/dist/indices.d.ts +721 -0
- package/vendor/lancedb/dist/indices.js +166 -0
- package/vendor/lancedb/dist/materialized_view.d.ts +69 -0
- package/vendor/lancedb/dist/materialized_view.js +112 -0
- package/vendor/lancedb/dist/merge.d.ts +104 -0
- package/vendor/lancedb/dist/merge.js +120 -0
- package/vendor/lancedb/dist/native.d.ts +1193 -0
- package/vendor/lancedb/dist/native.js +688 -0
- package/vendor/lancedb/dist/oauth.d.ts +66 -0
- package/vendor/lancedb/dist/oauth.js +15 -0
- package/vendor/lancedb/dist/otel.d.ts +26 -0
- package/vendor/lancedb/dist/otel.js +114 -0
- package/vendor/lancedb/dist/permutation.d.ts +143 -0
- package/vendor/lancedb/dist/permutation.js +184 -0
- package/vendor/lancedb/dist/query.d.ts +663 -0
- package/vendor/lancedb/dist/query.js +966 -0
- package/vendor/lancedb/dist/rerankers/index.d.ts +5 -0
- package/vendor/lancedb/dist/rerankers/index.js +19 -0
- package/vendor/lancedb/dist/rerankers/rrf.d.ts +14 -0
- package/vendor/lancedb/dist/rerankers/rrf.js +28 -0
- package/vendor/lancedb/dist/sanitize.d.ts +32 -0
- package/vendor/lancedb/dist/sanitize.js +544 -0
- package/vendor/lancedb/dist/scannable.d.ts +92 -0
- package/vendor/lancedb/dist/scannable.js +200 -0
- package/vendor/lancedb/dist/schema.d.ts +16 -0
- package/vendor/lancedb/dist/schema.js +387 -0
- package/vendor/lancedb/dist/table.d.ts +1069 -0
- package/vendor/lancedb/dist/table.js +498 -0
- package/vendor/lancedb/dist/util.d.ts +14 -0
- package/vendor/lancedb/dist/util.js +77 -0
- package/vendor/lancedb/license_header.txt +2 -0
- package/vendor/lancedb/package.json +56 -0
- package/vendor/transformers/LICENSE +202 -0
- package/vendor/transformers/dist/transformers.cjs +30844 -0
- package/vendor/transformers/dist/transformers.mjs +31404 -0
- package/vendor/transformers/package.json +55 -0
- package/vendor/transformers/types/backends/onnx.d.ts +30 -0
- package/vendor/transformers/types/configs.d.ts +85 -0
- package/vendor/transformers/types/env.d.ts +110 -0
- package/vendor/transformers/types/generation/configuration_utils.d.ts +320 -0
- package/vendor/transformers/types/generation/logits_process.d.ts +354 -0
- package/vendor/transformers/types/generation/logits_sampler.d.ts +51 -0
- package/vendor/transformers/types/generation/parameters.d.ts +47 -0
- package/vendor/transformers/types/generation/stopping_criteria.d.ts +81 -0
- package/vendor/transformers/types/generation/streamers.d.ts +81 -0
- package/vendor/transformers/types/models/whisper/common_whisper.d.ts +8 -0
- package/vendor/transformers/types/models/whisper/generation_whisper.d.ts +76 -0
- package/vendor/transformers/types/models.d.ts +3684 -0
- package/vendor/transformers/types/ops/registry.d.ts +11 -0
- package/vendor/transformers/types/pipelines.d.ts +2402 -0
- package/vendor/transformers/types/processors.d.ts +924 -0
- package/vendor/transformers/types/tokenizers.d.ts +990 -0
- package/vendor/transformers/types/transformers.d.ts +13 -0
- package/vendor/transformers/types/utils/audio.d.ts +130 -0
- package/vendor/transformers/types/utils/constants.d.ts +2 -0
- package/vendor/transformers/types/utils/core.d.ts +98 -0
- package/vendor/transformers/types/utils/data-structures.d.ts +236 -0
- package/vendor/transformers/types/utils/devices.d.ts +18 -0
- package/vendor/transformers/types/utils/dtypes.d.ts +19 -0
- package/vendor/transformers/types/utils/generic.d.ts +11 -0
- package/vendor/transformers/types/utils/hub.d.ts +152 -0
- package/vendor/transformers/types/utils/image.d.ts +119 -0
- package/vendor/transformers/types/utils/maths.d.ts +280 -0
- package/vendor/transformers/types/utils/tensor.d.ts +418 -0
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright The LanceDB Authors
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.register = exports.parseEmbeddingMetadata = exports.EmbeddingFunctionRegistry = exports.TextEmbeddingFunction = exports.EmbeddingFunction = void 0;
|
|
6
|
+
exports.getRegistry = getRegistry;
|
|
7
|
+
exports.LanceSchema = LanceSchema;
|
|
8
|
+
const arrow_1 = require("../arrow");
|
|
9
|
+
const sanitize_1 = require("../sanitize");
|
|
10
|
+
const registry_1 = require("./registry");
|
|
11
|
+
var embedding_function_1 = require("./embedding_function");
|
|
12
|
+
Object.defineProperty(exports, "EmbeddingFunction", { enumerable: true, get: function () { return embedding_function_1.EmbeddingFunction; } });
|
|
13
|
+
Object.defineProperty(exports, "TextEmbeddingFunction", { enumerable: true, get: function () { return embedding_function_1.TextEmbeddingFunction; } });
|
|
14
|
+
var registry_2 = require("./registry");
|
|
15
|
+
Object.defineProperty(exports, "EmbeddingFunctionRegistry", { enumerable: true, get: function () { return registry_2.EmbeddingFunctionRegistry; } });
|
|
16
|
+
Object.defineProperty(exports, "parseEmbeddingMetadata", { enumerable: true, get: function () { return registry_2.parseEmbeddingMetadata; } });
|
|
17
|
+
Object.defineProperty(exports, "register", { enumerable: true, get: function () { return registry_2.register; } });
|
|
18
|
+
function initializeBuiltInProviders() {
|
|
19
|
+
const { OpenAIEmbeddingFunction } = require("./openai");
|
|
20
|
+
const { TransformersEmbeddingFunction } = require("./transformers");
|
|
21
|
+
(0, registry_1.registerBuiltIn)("openai", OpenAIEmbeddingFunction);
|
|
22
|
+
(0, registry_1.registerBuiltIn)("huggingface", TransformersEmbeddingFunction);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Get the global embedding function registry.
|
|
26
|
+
*
|
|
27
|
+
* LanceDB built-in providers are initialized when this public API is first
|
|
28
|
+
* used, so importing the root package does not change automatic search
|
|
29
|
+
* selection for tables without embedding metadata.
|
|
30
|
+
*/
|
|
31
|
+
function getRegistry() {
|
|
32
|
+
initializeBuiltInProviders();
|
|
33
|
+
return (0, registry_1.getRegistry)();
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Create a schema with embedding functions.
|
|
37
|
+
*
|
|
38
|
+
* @param fields
|
|
39
|
+
* @returns Schema
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* class MyEmbeddingFunction extends EmbeddingFunction {
|
|
43
|
+
* // ...
|
|
44
|
+
* }
|
|
45
|
+
* const func = new MyEmbeddingFunction();
|
|
46
|
+
* const schema = LanceSchema({
|
|
47
|
+
* id: new Int32(),
|
|
48
|
+
* text: func.sourceField(new Utf8()),
|
|
49
|
+
* vector: func.vectorField(),
|
|
50
|
+
* // optional: specify the datatype and/or dimensions
|
|
51
|
+
* vector2: func.vectorField({ datatype: new Float32(), dims: 3}),
|
|
52
|
+
* });
|
|
53
|
+
*
|
|
54
|
+
* const table = await db.createTable("my_table", data, { schema });
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
function LanceSchema(fields) {
|
|
58
|
+
const arrowFields = [];
|
|
59
|
+
const embeddingFunctions = new Map();
|
|
60
|
+
Object.entries(fields).forEach(([key, value]) => {
|
|
61
|
+
if (Array.isArray(value)) {
|
|
62
|
+
const [dtype, metadata] = value;
|
|
63
|
+
arrowFields.push(new arrow_1.Field(key, (0, sanitize_1.sanitizeType)(dtype), true));
|
|
64
|
+
parseEmbeddingFunctions(embeddingFunctions, key, metadata);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
arrowFields.push(new arrow_1.Field(key, (0, sanitize_1.sanitizeType)(value), true));
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
const registry = getRegistry();
|
|
71
|
+
const metadata = registry.getTableMetadata(Array.from(embeddingFunctions.values()));
|
|
72
|
+
const schema = new arrow_1.Schema(arrowFields, metadata);
|
|
73
|
+
return schema;
|
|
74
|
+
}
|
|
75
|
+
function parseEmbeddingFunctions(embeddingFunctions, key, metadata) {
|
|
76
|
+
if (metadata.has("source_column_for")) {
|
|
77
|
+
const embedFunction = metadata.get("source_column_for");
|
|
78
|
+
const current = embeddingFunctions.get(embedFunction);
|
|
79
|
+
if (current !== undefined) {
|
|
80
|
+
embeddingFunctions.set(embedFunction, {
|
|
81
|
+
...current,
|
|
82
|
+
sourceColumn: key,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
embeddingFunctions.set(embedFunction, {
|
|
87
|
+
sourceColumn: key,
|
|
88
|
+
function: embedFunction,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
else if (metadata.has("vector_column_for")) {
|
|
93
|
+
const embedFunction = metadata.get("vector_column_for");
|
|
94
|
+
const current = embeddingFunctions.get(embedFunction);
|
|
95
|
+
if (current !== undefined) {
|
|
96
|
+
embeddingFunctions.set(embedFunction, {
|
|
97
|
+
...current,
|
|
98
|
+
vectorColumn: key,
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
embeddingFunctions.set(embedFunction, {
|
|
103
|
+
vectorColumn: key,
|
|
104
|
+
function: embedFunction,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { EmbeddingCreateParams } from "openai/resources/index";
|
|
2
|
+
import { Float } from "../arrow";
|
|
3
|
+
import { EmbeddingFunction } from "./embedding_function";
|
|
4
|
+
export type OpenAIOptions = {
|
|
5
|
+
apiKey: string;
|
|
6
|
+
model: EmbeddingCreateParams["model"];
|
|
7
|
+
};
|
|
8
|
+
export declare class OpenAIEmbeddingFunction extends EmbeddingFunction<string, Partial<OpenAIOptions>> {
|
|
9
|
+
#private;
|
|
10
|
+
constructor(optionsRaw?: Partial<OpenAIOptions>);
|
|
11
|
+
protected getSensitiveKeys(): string[];
|
|
12
|
+
ndims(): number;
|
|
13
|
+
embeddingDataType(): Float;
|
|
14
|
+
computeSourceEmbeddings(data: string[]): Promise<number[][]>;
|
|
15
|
+
computeQueryEmbeddings(data: string): Promise<number[]>;
|
|
16
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright The LanceDB Authors
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.OpenAIEmbeddingFunction = void 0;
|
|
6
|
+
const arrow_1 = require("../arrow");
|
|
7
|
+
const embedding_function_1 = require("./embedding_function");
|
|
8
|
+
const registry_1 = require("./registry");
|
|
9
|
+
class OpenAIEmbeddingFunction extends embedding_function_1.EmbeddingFunction {
|
|
10
|
+
#openai;
|
|
11
|
+
#modelName;
|
|
12
|
+
constructor(optionsRaw = {
|
|
13
|
+
model: "text-embedding-ada-002",
|
|
14
|
+
}) {
|
|
15
|
+
super();
|
|
16
|
+
const options = this.resolveVariables(optionsRaw);
|
|
17
|
+
const openAIKey = options?.apiKey ?? process.env.OPENAI_API_KEY;
|
|
18
|
+
if (!openAIKey) {
|
|
19
|
+
throw new Error("OpenAI API key is required");
|
|
20
|
+
}
|
|
21
|
+
const modelName = options?.model ?? "text-embedding-ada-002";
|
|
22
|
+
/**
|
|
23
|
+
* @type {import("openai").default}
|
|
24
|
+
*/
|
|
25
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
26
|
+
let Openai;
|
|
27
|
+
try {
|
|
28
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
29
|
+
Openai = require("openai");
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
throw new Error("please install openai@^4.24.1 using npm install openai");
|
|
33
|
+
}
|
|
34
|
+
const configuration = {
|
|
35
|
+
apiKey: openAIKey,
|
|
36
|
+
};
|
|
37
|
+
this.#openai = new Openai(configuration);
|
|
38
|
+
this.#modelName = modelName;
|
|
39
|
+
}
|
|
40
|
+
getSensitiveKeys() {
|
|
41
|
+
return ["apiKey"];
|
|
42
|
+
}
|
|
43
|
+
ndims() {
|
|
44
|
+
switch (this.#modelName) {
|
|
45
|
+
case "text-embedding-ada-002":
|
|
46
|
+
return 1536;
|
|
47
|
+
case "text-embedding-3-large":
|
|
48
|
+
return 3072;
|
|
49
|
+
case "text-embedding-3-small":
|
|
50
|
+
return 1536;
|
|
51
|
+
default:
|
|
52
|
+
throw new Error(`Unknown model: ${this.#modelName}`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
embeddingDataType() {
|
|
56
|
+
return new arrow_1.Float32();
|
|
57
|
+
}
|
|
58
|
+
async computeSourceEmbeddings(data) {
|
|
59
|
+
const response = await this.#openai.embeddings.create({
|
|
60
|
+
model: this.#modelName,
|
|
61
|
+
input: data,
|
|
62
|
+
});
|
|
63
|
+
const embeddings = [];
|
|
64
|
+
for (let i = 0; i < response.data.length; i++) {
|
|
65
|
+
embeddings.push(response.data[i].embedding);
|
|
66
|
+
}
|
|
67
|
+
return embeddings;
|
|
68
|
+
}
|
|
69
|
+
async computeQueryEmbeddings(data) {
|
|
70
|
+
if (typeof data !== "string") {
|
|
71
|
+
throw new Error("Data must be a string");
|
|
72
|
+
}
|
|
73
|
+
const response = await this.#openai.embeddings.create({
|
|
74
|
+
model: this.#modelName,
|
|
75
|
+
input: data,
|
|
76
|
+
});
|
|
77
|
+
return response.data[0].embedding;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
exports.OpenAIEmbeddingFunction = OpenAIEmbeddingFunction;
|
|
81
|
+
(0, registry_1.registerBuiltIn)("openai", OpenAIEmbeddingFunction);
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { type EmbeddingFunction, type EmbeddingFunctionConstructor } from "./embedding_function";
|
|
2
|
+
import "reflect-metadata";
|
|
3
|
+
export type CreateReturnType<T> = T extends {
|
|
4
|
+
init: () => Promise<void>;
|
|
5
|
+
} ? Promise<T> : T;
|
|
6
|
+
export interface EmbeddingFunctionCreate<T extends EmbeddingFunction> {
|
|
7
|
+
create(options?: T["TOptions"]): CreateReturnType<T>;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* This is a singleton class used to register embedding functions
|
|
11
|
+
* and fetch them by name. It also handles serializing and deserializing.
|
|
12
|
+
* You can implement your own embedding function by subclassing EmbeddingFunction
|
|
13
|
+
* or TextEmbeddingFunction and registering it with the registry
|
|
14
|
+
*/
|
|
15
|
+
export declare class EmbeddingFunctionRegistry {
|
|
16
|
+
#private;
|
|
17
|
+
/**
|
|
18
|
+
* Get the number of registered functions
|
|
19
|
+
*/
|
|
20
|
+
length(): number;
|
|
21
|
+
/**
|
|
22
|
+
* Register an embedding function
|
|
23
|
+
* @throws Error if the function is already registered
|
|
24
|
+
*/
|
|
25
|
+
register<T extends EmbeddingFunctionConstructor = EmbeddingFunctionConstructor>(this: EmbeddingFunctionRegistry, alias?: string): (ctor: T) => any;
|
|
26
|
+
/** @ignore */
|
|
27
|
+
setBuiltIn<T extends EmbeddingFunctionConstructor = EmbeddingFunctionConstructor>(name: string, ctor: T): T;
|
|
28
|
+
get<T extends EmbeddingFunction<unknown>>(name: string): EmbeddingFunctionCreate<T> | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* reset the registry to the initial state
|
|
31
|
+
*/
|
|
32
|
+
reset(this: EmbeddingFunctionRegistry): void;
|
|
33
|
+
/**
|
|
34
|
+
* @ignore
|
|
35
|
+
*/
|
|
36
|
+
parseFunctions(this: EmbeddingFunctionRegistry, metadata: Map<string, string>): Promise<Map<string, ResolvedEmbeddingFunctionConfig>>;
|
|
37
|
+
functionToMetadata(conf: EmbeddingFunctionConfig): Record<string, any>;
|
|
38
|
+
getTableMetadata(functions: EmbeddingFunctionConfig[]): Map<string, string>;
|
|
39
|
+
/**
|
|
40
|
+
* Set a variable. These can be accessed in the embedding function
|
|
41
|
+
* configuration using the syntax `$var:variable_name`. If they are not
|
|
42
|
+
* set, an error will be thrown letting you know which key is unset. If you
|
|
43
|
+
* want to supply a default value, you can add an additional part in the
|
|
44
|
+
* configuration like so: `$var:variable_name:default_value`. Default values
|
|
45
|
+
* can be used for runtime configurations that are not sensitive, such as
|
|
46
|
+
* whether to use a GPU for inference.
|
|
47
|
+
*
|
|
48
|
+
* The name must not contain colons. The default value can contain colons.
|
|
49
|
+
*
|
|
50
|
+
* @param name
|
|
51
|
+
* @param value
|
|
52
|
+
*/
|
|
53
|
+
setVar(name: string, value: string): void;
|
|
54
|
+
/**
|
|
55
|
+
* Get a variable.
|
|
56
|
+
* @param name
|
|
57
|
+
* @returns
|
|
58
|
+
* @see {@link setVar}
|
|
59
|
+
*/
|
|
60
|
+
getVar(name: string): string | undefined;
|
|
61
|
+
}
|
|
62
|
+
export declare function register(name?: string): (ctor: EmbeddingFunctionConstructor<EmbeddingFunction<any, import("./embedding_function").FunctionOptions>>) => any;
|
|
63
|
+
/** @ignore */
|
|
64
|
+
export declare function registerBuiltIn<T extends EmbeddingFunctionConstructor = EmbeddingFunctionConstructor>(name: string, ctor: T): T;
|
|
65
|
+
/**
|
|
66
|
+
* Utility function to get the global instance of the registry
|
|
67
|
+
* @returns `EmbeddingFunctionRegistry` The global instance of the registry
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* const registry = getRegistry();
|
|
71
|
+
* const openai = registry.get("openai").create();
|
|
72
|
+
*/
|
|
73
|
+
export declare function getRegistry(): EmbeddingFunctionRegistry;
|
|
74
|
+
export interface EmbeddingFunctionConfig {
|
|
75
|
+
sourceColumn: string;
|
|
76
|
+
vectorColumn?: string;
|
|
77
|
+
function: EmbeddingFunction;
|
|
78
|
+
}
|
|
79
|
+
/** An [EmbeddingFunctionConfig] read back from table metadata, where the
|
|
80
|
+
* vector column is always recorded. */
|
|
81
|
+
export type ResolvedEmbeddingFunctionConfig = EmbeddingFunctionConfig & {
|
|
82
|
+
vectorColumn: string;
|
|
83
|
+
};
|
|
84
|
+
/** One entry of the `embedding_functions` schema metadata, with the column
|
|
85
|
+
* keys normalized across the bindings' spellings. */
|
|
86
|
+
export type EmbeddingMetadataEntry = {
|
|
87
|
+
name: string;
|
|
88
|
+
sourceColumn: string;
|
|
89
|
+
vectorColumn: string;
|
|
90
|
+
model: EmbeddingFunction["TOptions"];
|
|
91
|
+
};
|
|
92
|
+
/** The single parser for `embedding_functions` schema metadata: every reader
|
|
93
|
+
* goes through here, so the wire contract cannot fork between them. */
|
|
94
|
+
export declare function parseEmbeddingMetadata(json: string): EmbeddingMetadataEntry[];
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright The LanceDB Authors
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.EmbeddingFunctionRegistry = void 0;
|
|
6
|
+
exports.register = register;
|
|
7
|
+
exports.registerBuiltIn = registerBuiltIn;
|
|
8
|
+
exports.getRegistry = getRegistry;
|
|
9
|
+
exports.parseEmbeddingMetadata = parseEmbeddingMetadata;
|
|
10
|
+
require("reflect-metadata");
|
|
11
|
+
const builtInFunctionsKey = Symbol.for("@lancedb/lancedb::embedding-built-in-functions::v1");
|
|
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
|
+
class EmbeddingFunctionRegistry {
|
|
19
|
+
#functions = new Map();
|
|
20
|
+
#variables = new Map();
|
|
21
|
+
/**
|
|
22
|
+
* Get the number of registered functions
|
|
23
|
+
*/
|
|
24
|
+
length() {
|
|
25
|
+
return this.#functions.size;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Register an embedding function
|
|
29
|
+
* @throws Error if the function is already registered
|
|
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
|
+
/** @ignore */
|
|
46
|
+
setBuiltIn(name, ctor) {
|
|
47
|
+
this.#functions.set(name, ctor);
|
|
48
|
+
Reflect.defineMetadata("lancedb::embedding::name", name, ctor);
|
|
49
|
+
return ctor;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Fetch an embedding function by name
|
|
53
|
+
* @param name The name of the function
|
|
54
|
+
*/
|
|
55
|
+
get(name) {
|
|
56
|
+
const factory = this.#functions.get(name);
|
|
57
|
+
if (!factory) {
|
|
58
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
62
|
+
let create;
|
|
63
|
+
if (factory.prototype.init) {
|
|
64
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
65
|
+
create = async function (options) {
|
|
66
|
+
const instance = new factory(options);
|
|
67
|
+
await instance.init();
|
|
68
|
+
return instance;
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
73
|
+
create = (options) => new factory(options);
|
|
74
|
+
}
|
|
75
|
+
return {
|
|
76
|
+
create,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* reset the registry to the initial state
|
|
81
|
+
*/
|
|
82
|
+
reset() {
|
|
83
|
+
this.#functions.clear();
|
|
84
|
+
getBuiltInFunctions(this).clear();
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* @ignore
|
|
88
|
+
*/
|
|
89
|
+
async parseFunctions(metadata) {
|
|
90
|
+
if (!metadata.has("embedding_functions")) {
|
|
91
|
+
return new Map();
|
|
92
|
+
}
|
|
93
|
+
const entries = parseEmbeddingMetadata(metadata.get("embedding_functions"));
|
|
94
|
+
const items = await Promise.all(entries.map(async (f) => {
|
|
95
|
+
const fn = this.get(f.name);
|
|
96
|
+
if (!fn) {
|
|
97
|
+
throw new Error(`Function "${f.name}" not found in registry`);
|
|
98
|
+
}
|
|
99
|
+
const func = await fn.create(f.model);
|
|
100
|
+
return {
|
|
101
|
+
sourceColumn: f.sourceColumn,
|
|
102
|
+
vectorColumn: f.vectorColumn,
|
|
103
|
+
function: func,
|
|
104
|
+
};
|
|
105
|
+
}));
|
|
106
|
+
// Keyed by output column: one function may serve several columns.
|
|
107
|
+
return new Map(items.map((config) => [config.vectorColumn, config]));
|
|
108
|
+
}
|
|
109
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
110
|
+
functionToMetadata(conf) {
|
|
111
|
+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
112
|
+
const metadata = {};
|
|
113
|
+
const name = Reflect.getMetadata("lancedb::embedding::name", conf.function.constructor);
|
|
114
|
+
metadata["sourceColumn"] = conf.sourceColumn;
|
|
115
|
+
metadata["vectorColumn"] = conf.vectorColumn ?? "vector";
|
|
116
|
+
metadata["name"] = name ?? conf.function.constructor.name;
|
|
117
|
+
metadata["model"] = conf.function.toJSON();
|
|
118
|
+
return metadata;
|
|
119
|
+
}
|
|
120
|
+
getTableMetadata(functions) {
|
|
121
|
+
const metadata = new Map();
|
|
122
|
+
const jsonData = functions.map((conf) => this.functionToMetadata(conf));
|
|
123
|
+
metadata.set("embedding_functions", JSON.stringify(jsonData));
|
|
124
|
+
return metadata;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Set a variable. These can be accessed in the embedding function
|
|
128
|
+
* configuration using the syntax `$var:variable_name`. If they are not
|
|
129
|
+
* set, an error will be thrown letting you know which key is unset. If you
|
|
130
|
+
* want to supply a default value, you can add an additional part in the
|
|
131
|
+
* configuration like so: `$var:variable_name:default_value`. Default values
|
|
132
|
+
* can be used for runtime configurations that are not sensitive, such as
|
|
133
|
+
* whether to use a GPU for inference.
|
|
134
|
+
*
|
|
135
|
+
* The name must not contain colons. The default value can contain colons.
|
|
136
|
+
*
|
|
137
|
+
* @param name
|
|
138
|
+
* @param value
|
|
139
|
+
*/
|
|
140
|
+
setVar(name, value) {
|
|
141
|
+
if (name.includes(":")) {
|
|
142
|
+
throw new Error("Variable names cannot contain colons");
|
|
143
|
+
}
|
|
144
|
+
this.#variables.set(name, value);
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Get a variable.
|
|
148
|
+
* @param name
|
|
149
|
+
* @returns
|
|
150
|
+
* @see {@link setVar}
|
|
151
|
+
*/
|
|
152
|
+
getVar(name) {
|
|
153
|
+
return this.#variables.get(name);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
exports.EmbeddingFunctionRegistry = EmbeddingFunctionRegistry;
|
|
157
|
+
function getBuiltInFunctions(registry) {
|
|
158
|
+
const registryWithBuiltIns = registry;
|
|
159
|
+
let builtInFunctions = registryWithBuiltIns[builtInFunctionsKey];
|
|
160
|
+
if (builtInFunctions === undefined) {
|
|
161
|
+
builtInFunctions = new Set();
|
|
162
|
+
registryWithBuiltIns[builtInFunctionsKey] = builtInFunctions;
|
|
163
|
+
}
|
|
164
|
+
return builtInFunctions;
|
|
165
|
+
}
|
|
166
|
+
// Server bundlers can load the side-effect embedding entry points and the public
|
|
167
|
+
// embedding API from separate module graphs. Keep their registry shared.
|
|
168
|
+
const registryKey = Symbol.for("@lancedb/lancedb::embedding-function-registry::v1");
|
|
169
|
+
const registryGlobal = globalThis;
|
|
170
|
+
function getGlobalRegistry() {
|
|
171
|
+
const existingRegistry = registryGlobal[registryKey];
|
|
172
|
+
if (existingRegistry !== undefined) {
|
|
173
|
+
return existingRegistry;
|
|
174
|
+
}
|
|
175
|
+
const registry = new EmbeddingFunctionRegistry();
|
|
176
|
+
registryGlobal[registryKey] = registry;
|
|
177
|
+
return registry;
|
|
178
|
+
}
|
|
179
|
+
const _REGISTRY = getGlobalRegistry();
|
|
180
|
+
function register(name) {
|
|
181
|
+
return _REGISTRY.register(name);
|
|
182
|
+
}
|
|
183
|
+
/** @ignore */
|
|
184
|
+
function registerBuiltIn(name, ctor) {
|
|
185
|
+
const builtInFunctions = getBuiltInFunctions(_REGISTRY);
|
|
186
|
+
if (builtInFunctions.has(name)) {
|
|
187
|
+
return _REGISTRY.setBuiltIn(name, ctor);
|
|
188
|
+
}
|
|
189
|
+
_REGISTRY.register(name)(ctor);
|
|
190
|
+
builtInFunctions.add(name);
|
|
191
|
+
return ctor;
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Utility function to get the global instance of the registry
|
|
195
|
+
* @returns `EmbeddingFunctionRegistry` The global instance of the registry
|
|
196
|
+
* @example
|
|
197
|
+
* ```ts
|
|
198
|
+
* const registry = getRegistry();
|
|
199
|
+
* const openai = registry.get("openai").create();
|
|
200
|
+
*/
|
|
201
|
+
function getRegistry() {
|
|
202
|
+
return _REGISTRY;
|
|
203
|
+
}
|
|
204
|
+
/** The single parser for `embedding_functions` schema metadata: every reader
|
|
205
|
+
* goes through here, so the wire contract cannot fork between them. */
|
|
206
|
+
function parseEmbeddingMetadata(json) {
|
|
207
|
+
const entries = JSON.parse(json);
|
|
208
|
+
const seen = new Set();
|
|
209
|
+
return entries.map((f) => {
|
|
210
|
+
const sourceColumn = f.sourceColumn ?? f.source_column;
|
|
211
|
+
const vectorColumn = f.vectorColumn ?? f.vector_column;
|
|
212
|
+
if (sourceColumn === undefined || vectorColumn === undefined) {
|
|
213
|
+
throw new Error(`Embedding function "${f.name}" metadata names no source or vector column`);
|
|
214
|
+
}
|
|
215
|
+
if (seen.has(vectorColumn)) {
|
|
216
|
+
throw new Error(`Multiple embedding configs claim vector column "${vectorColumn}"`);
|
|
217
|
+
}
|
|
218
|
+
seen.add(vectorColumn);
|
|
219
|
+
return { name: f.name, sourceColumn, vectorColumn, model: f.model };
|
|
220
|
+
});
|
|
221
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Float } from "../arrow";
|
|
2
|
+
import { EmbeddingFunction } from "./embedding_function";
|
|
3
|
+
export type XenovaTransformerOptions = {
|
|
4
|
+
/** The wasm compatible model to use */
|
|
5
|
+
model: string;
|
|
6
|
+
/**
|
|
7
|
+
* The wasm compatible tokenizer to use
|
|
8
|
+
* If not provided, it will use the default tokenizer for the model
|
|
9
|
+
*/
|
|
10
|
+
tokenizer?: string;
|
|
11
|
+
/**
|
|
12
|
+
* The number of dimensions of the embeddings
|
|
13
|
+
*
|
|
14
|
+
* We will attempt to infer this from the model config if not provided.
|
|
15
|
+
* Since there isn't a standard way to get this information from the model,
|
|
16
|
+
* you may need to manually specify this if using a model that doesn't have a 'hidden_size' in the config.
|
|
17
|
+
* */
|
|
18
|
+
ndims?: number;
|
|
19
|
+
/** Options for the tokenizer */
|
|
20
|
+
tokenizerOptions?: {
|
|
21
|
+
textPair?: string | string[];
|
|
22
|
+
padding?: boolean | "max_length";
|
|
23
|
+
addSpecialTokens?: boolean;
|
|
24
|
+
truncation?: boolean;
|
|
25
|
+
maxLength?: number;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
export declare class TransformersEmbeddingFunction extends EmbeddingFunction<string, Partial<XenovaTransformerOptions>> {
|
|
29
|
+
#private;
|
|
30
|
+
constructor(optionsRaw?: Partial<XenovaTransformerOptions>);
|
|
31
|
+
init(): Promise<void>;
|
|
32
|
+
ndims(): number;
|
|
33
|
+
embeddingDataType(): Float;
|
|
34
|
+
computeSourceEmbeddings(data: string[]): Promise<number[][]>;
|
|
35
|
+
computeQueryEmbeddings(data: string): Promise<number[]>;
|
|
36
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright The LanceDB Authors
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.TransformersEmbeddingFunction = void 0;
|
|
6
|
+
const arrow_1 = require("../arrow");
|
|
7
|
+
const embedding_function_1 = require("./embedding_function");
|
|
8
|
+
const registry_1 = require("./registry");
|
|
9
|
+
class TransformersEmbeddingFunction extends embedding_function_1.EmbeddingFunction {
|
|
10
|
+
#model;
|
|
11
|
+
#tokenizer;
|
|
12
|
+
#modelName;
|
|
13
|
+
#initialized = false;
|
|
14
|
+
#tokenizerOptions;
|
|
15
|
+
#ndims;
|
|
16
|
+
constructor(optionsRaw = {
|
|
17
|
+
model: "Xenova/all-MiniLM-L6-v2",
|
|
18
|
+
}) {
|
|
19
|
+
super();
|
|
20
|
+
const options = this.resolveVariables(optionsRaw);
|
|
21
|
+
const modelName = options?.model ?? "Xenova/all-MiniLM-L6-v2";
|
|
22
|
+
this.#tokenizerOptions = {
|
|
23
|
+
padding: true,
|
|
24
|
+
...options.tokenizerOptions,
|
|
25
|
+
};
|
|
26
|
+
this.#ndims = options.ndims;
|
|
27
|
+
this.#modelName = modelName;
|
|
28
|
+
}
|
|
29
|
+
async init() {
|
|
30
|
+
let transformers;
|
|
31
|
+
try {
|
|
32
|
+
// SAFETY:
|
|
33
|
+
// since typescript transpiles `import` to `require`, we need to do this in an unsafe way
|
|
34
|
+
// We can't use `require` because `@huggingface/transformers` is an ESM module
|
|
35
|
+
// and we can't use `import` directly because typescript will transpile it to `require`.
|
|
36
|
+
// and we want to remain compatible with both ESM and CJS modules
|
|
37
|
+
// so we use `eval` to bypass typescript for this specific import.
|
|
38
|
+
transformers = await eval('import("../../../transformers/dist/transformers.mjs")');
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
throw new Error(`error loading @huggingface/transformers\nReason: ${e}`);
|
|
42
|
+
}
|
|
43
|
+
try {
|
|
44
|
+
this.#model = await transformers.AutoModel.from_pretrained(this.#modelName, { dtype: "fp32" });
|
|
45
|
+
}
|
|
46
|
+
catch (e) {
|
|
47
|
+
throw new Error(`error loading model ${this.#modelName}. Make sure you are using a wasm compatible model.\nReason: ${e}`);
|
|
48
|
+
}
|
|
49
|
+
try {
|
|
50
|
+
this.#tokenizer = await transformers.AutoTokenizer.from_pretrained(this.#modelName);
|
|
51
|
+
}
|
|
52
|
+
catch (e) {
|
|
53
|
+
throw new Error(`error loading tokenizer for ${this.#modelName}. Make sure you are using a wasm compatible model:\nReason: ${e}`);
|
|
54
|
+
}
|
|
55
|
+
this.#initialized = true;
|
|
56
|
+
}
|
|
57
|
+
ndims() {
|
|
58
|
+
if (this.#ndims) {
|
|
59
|
+
return this.#ndims;
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
const config = this.#model.config;
|
|
63
|
+
// biome-ignore lint/style/useNamingConvention: we don't control this name.
|
|
64
|
+
const ndims = config.hidden_size;
|
|
65
|
+
if (!ndims) {
|
|
66
|
+
throw new Error("hidden_size not found in model config, you may need to manually specify the embedding dimensions. ");
|
|
67
|
+
}
|
|
68
|
+
return ndims;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
embeddingDataType() {
|
|
72
|
+
return new arrow_1.Float32();
|
|
73
|
+
}
|
|
74
|
+
async computeSourceEmbeddings(data) {
|
|
75
|
+
// this should only happen if the user is trying to use the function directly.
|
|
76
|
+
// Anything going through the registry should already be initialized.
|
|
77
|
+
if (!this.#initialized) {
|
|
78
|
+
return Promise.reject(new Error("something went wrong: embedding function not initialized. Please call init()"));
|
|
79
|
+
}
|
|
80
|
+
const tokenizer = this.#tokenizer;
|
|
81
|
+
const model = this.#model;
|
|
82
|
+
const inputs = await tokenizer(data, this.#tokenizerOptions);
|
|
83
|
+
let tokens = await model.forward(inputs);
|
|
84
|
+
tokens = tokens[Object.keys(tokens)[0]];
|
|
85
|
+
const [nItems, nTokens] = tokens.dims;
|
|
86
|
+
tokens = tensorDiv(tokens.sum(1), nTokens);
|
|
87
|
+
// TODO: support other data types
|
|
88
|
+
const tokenData = tokens.data;
|
|
89
|
+
const stride = this.ndims();
|
|
90
|
+
const embeddings = [];
|
|
91
|
+
for (let i = 0; i < nItems; i++) {
|
|
92
|
+
const start = i * stride;
|
|
93
|
+
const end = start + stride;
|
|
94
|
+
const slice = tokenData.slice(start, end);
|
|
95
|
+
embeddings.push(Array.from(slice)); // TODO: Avoid copy here
|
|
96
|
+
}
|
|
97
|
+
return embeddings;
|
|
98
|
+
}
|
|
99
|
+
async computeQueryEmbeddings(data) {
|
|
100
|
+
return (await this.computeSourceEmbeddings([data]))[0];
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
exports.TransformersEmbeddingFunction = TransformersEmbeddingFunction;
|
|
104
|
+
(0, registry_1.registerBuiltIn)("huggingface", TransformersEmbeddingFunction);
|
|
105
|
+
const tensorDiv = (src, divBy) => {
|
|
106
|
+
for (let i = 0; i < src.data.length; ++i) {
|
|
107
|
+
src.data[i] /= divBy;
|
|
108
|
+
}
|
|
109
|
+
return src;
|
|
110
|
+
};
|