@lancedb/lancedb 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/.eslintignore +3 -0
  2. package/Cargo.toml +28 -0
  3. package/README.md +49 -0
  4. package/build.rs +5 -0
  5. package/eslint.config.js +28 -0
  6. package/examples/js/index.mjs +40 -0
  7. package/examples/js/package.json +14 -0
  8. package/examples/js-openai/index.mjs +43 -0
  9. package/examples/js-openai/package-lock.json +256 -0
  10. package/examples/js-openai/package.json +15 -0
  11. package/examples/js-transformers/index.mjs +65 -0
  12. package/examples/js-transformers/package-lock.json +1418 -0
  13. package/examples/js-transformers/package.json +15 -0
  14. package/examples/js-youtube-transcripts/index.mjs +135 -0
  15. package/examples/js-youtube-transcripts/package.json +15 -0
  16. package/examples/ts/data/sample-lancedb/vectors.lance/_latest.manifest +0 -0
  17. package/examples/ts/data/sample-lancedb/vectors.lance/_transactions/0-adde4e05-fcfc-415c-86a6-5b252cb9e79a.txn +0 -0
  18. package/examples/ts/data/sample-lancedb/vectors.lance/_versions/1.manifest +0 -0
  19. package/examples/ts/data/sample-lancedb/vectors.lance/data/3618b33e-3eea-4b5e-a0fc-7d1f718d551e.lance +0 -0
  20. package/examples/ts/package-lock.json +1340 -0
  21. package/examples/ts/package.json +22 -0
  22. package/examples/ts/tsconfig.json +10 -0
  23. package/jest.config.js +7 -0
  24. package/lancedb/arrow.ts +650 -0
  25. package/lancedb/connection.ts +176 -0
  26. package/lancedb/embedding/embedding_function.ts +78 -0
  27. package/lancedb/embedding/index.ts +2 -0
  28. package/lancedb/embedding/openai.ts +62 -0
  29. package/lancedb/index.ts +69 -0
  30. package/lancedb/indices.ts +203 -0
  31. package/lancedb/query.ts +375 -0
  32. package/lancedb/sanitize.ts +516 -0
  33. package/lancedb/table.ts +353 -0
  34. package/package.json +82 -0
  35. package/tsconfig.json +23 -0
package/.eslintignore ADDED
@@ -0,0 +1,3 @@
1
+ **/dist/**/*
2
+ **/native.js
3
+ **/native.d.ts
package/Cargo.toml ADDED
@@ -0,0 +1,28 @@
1
+ [package]
2
+ name = "lancedb-nodejs"
3
+ edition.workspace = true
4
+ version = "0.0.0"
5
+ license.workspace = true
6
+ description.workspace = true
7
+ repository.workspace = true
8
+ keywords.workspace = true
9
+ categories.workspace = true
10
+
11
+ [lib]
12
+ crate-type = ["cdylib"]
13
+
14
+ [dependencies]
15
+ arrow-ipc.workspace = true
16
+ futures.workspace = true
17
+ lancedb = { path = "../rust/lancedb" }
18
+ napi = { version = "2.15", default-features = false, features = [
19
+ "napi7",
20
+ "async",
21
+ ] }
22
+ napi-derive = "2"
23
+
24
+ # Prevent dynamic linking of lzma, which comes from datafusion
25
+ lzma-sys = { version = "*", features = ["static"] }
26
+
27
+ [build-dependencies]
28
+ napi-build = "2.1"
package/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # (New) LanceDB NodeJS SDK
2
+
3
+ It will replace the NodeJS SDK when it is ready.
4
+
5
+ ## Development
6
+
7
+ ```sh
8
+ npm run build
9
+ npm t
10
+ ```
11
+
12
+ ### Running lint / format
13
+
14
+ LanceDb uses eslint for linting. VSCode does not need any plugins to use eslint. However, it
15
+ may need some additional configuration. Make sure that eslint.experimental.useFlatConfig is
16
+ set to true. Also, if your vscode root folder is the repo root then you will need to set
17
+ the eslint.workingDirectories to ["nodejs"]. To manually lint your code you can run:
18
+
19
+ ```sh
20
+ npm run lint
21
+ ```
22
+
23
+ LanceDb uses prettier for formatting. If you are using VSCode you will need to install the
24
+ "Prettier - Code formatter" extension. You should then configure it to be the default formatter
25
+ for typescript and you should enable format on save. To manually check your code's format you
26
+ can run:
27
+
28
+ ```sh
29
+ npm run chkformat
30
+ ```
31
+
32
+ If you need to manually format your code you can run:
33
+
34
+ ```sh
35
+ npx prettier --write .
36
+ ```
37
+
38
+ ### Generating docs
39
+
40
+ ```sh
41
+ npm run docs
42
+
43
+ cd ../docs
44
+ # Asssume the virtual environment was created
45
+ # python3 -m venv venv
46
+ # pip install -r requirements.txt
47
+ . ./venv/bin/activate
48
+ mkdocs build
49
+ ```
package/build.rs ADDED
@@ -0,0 +1,5 @@
1
+ extern crate napi_build;
2
+
3
+ fn main() {
4
+ napi_build::setup();
5
+ }
@@ -0,0 +1,28 @@
1
+ /* eslint-disable @typescript-eslint/naming-convention */
2
+ // @ts-check
3
+
4
+ const eslint = require("@eslint/js");
5
+ const tseslint = require("typescript-eslint");
6
+ const eslintConfigPrettier = require("eslint-config-prettier");
7
+ const jsdoc = require("eslint-plugin-jsdoc");
8
+
9
+ module.exports = tseslint.config(
10
+ eslint.configs.recommended,
11
+ jsdoc.configs["flat/recommended"],
12
+ eslintConfigPrettier,
13
+ ...tseslint.configs.recommended,
14
+ {
15
+ rules: {
16
+ "@typescript-eslint/naming-convention": "error",
17
+ "jsdoc/require-returns": "off",
18
+ "jsdoc/require-param": "off",
19
+ "jsdoc/require-jsdoc": [
20
+ "error",
21
+ {
22
+ publicOnly: true,
23
+ },
24
+ ],
25
+ },
26
+ plugins: jsdoc,
27
+ },
28
+ );
@@ -0,0 +1,40 @@
1
+ // Copyright 2024 Lance Developers.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ /* global console */
16
+
17
+ "use strict";
18
+
19
+ import * as lancedb from "@lancedb/lancedb";
20
+
21
+ async function example() {
22
+ const db = await lancedb.connect("data/sample-lancedb");
23
+
24
+ const data = [
25
+ { id: 1, vector: [0.1, 0.2], price: 10 },
26
+ { id: 2, vector: [1.1, 1.2], price: 50 },
27
+ ];
28
+
29
+ const table = await db.createTable("vectors", data, { existOk: true });
30
+ console.log(await db.tableNames());
31
+
32
+ const results = await table.vectorSearch([0.1, 0.3]).limit(20);
33
+ for await (const batch of results) {
34
+ console.log(batch.toArray());
35
+ }
36
+ }
37
+
38
+ example()
39
+ .then(() => console.log("All done!"))
40
+ .catch(console.error);
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "lancedb-example-js",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "index.mjs",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "author": "Lance Devs",
10
+ "license": "Apache-2.0",
11
+ "dependencies": {
12
+ "@lancedb/lancedb": "file:../.."
13
+ }
14
+ }
@@ -0,0 +1,43 @@
1
+ // Copyright 2023 Lance Developers.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ /* global console, process */
16
+
17
+ "use strict";
18
+
19
+ import * as lancedb from "@lancedb/lancedb";
20
+
21
+ async function example() {
22
+ // You need to provide an OpenAI API key, here we read it from the OPENAI_API_KEY environment variable
23
+ const apiKey = process.env.OPENAI_API_KEY;
24
+ // The embedding function will create embeddings for the 'text' column(text in this case)
25
+ const embedding = new lancedb.OpenAIEmbeddingFunction("text", apiKey);
26
+
27
+ const db = await lancedb.connect("data/sample-lancedb");
28
+
29
+ const data = [
30
+ { id: 1, text: "Black T-Shirt", price: 10 },
31
+ { id: 2, text: "Leather Jacket", price: 50 },
32
+ ];
33
+
34
+ const table = await db.createTable("vectors", data, embedding);
35
+ console.log(await db.tableNames());
36
+
37
+ const results = await table.search("keeps me warm").limit(1).toArray();
38
+ console.log(results[0].text);
39
+ }
40
+
41
+ example()
42
+ .then(() => console.log("All done!"))
43
+ .catch(console.error);
@@ -0,0 +1,256 @@
1
+ {
2
+ "name": "lancedb-example-js-openai",
3
+ "version": "1.0.0",
4
+ "lockfileVersion": 2,
5
+ "requires": true,
6
+ "packages": {
7
+ "": {
8
+ "name": "lancedb-example-js-openai",
9
+ "version": "1.0.0",
10
+ "license": "Apache-2.0",
11
+ "dependencies": {
12
+ "@lancedb/lancedb": "file:../..",
13
+ "openai": "^3.2.1"
14
+ }
15
+ },
16
+ "../..": {
17
+ "version": "0.4.3",
18
+ "cpu": [
19
+ "x64",
20
+ "arm64"
21
+ ],
22
+ "license": "Apache 2.0",
23
+ "os": [
24
+ "darwin",
25
+ "linux",
26
+ "win32"
27
+ ],
28
+ "dependencies": {
29
+ "openai": "^4.29.2"
30
+ },
31
+ "devDependencies": {
32
+ "@napi-rs/cli": "^2.18.0",
33
+ "@types/jest": "^29.1.2",
34
+ "@types/tmp": "^0.2.6",
35
+ "@typescript-eslint/eslint-plugin": "^6.19.0",
36
+ "@typescript-eslint/parser": "^6.19.0",
37
+ "apache-arrow-old": "npm:apache-arrow@13.0.0",
38
+ "eslint": "^8.57.0",
39
+ "eslint-config-prettier": "^9.1.0",
40
+ "eslint-plugin-jsdoc": "^48.2.1",
41
+ "jest": "^29.7.0",
42
+ "prettier": "^3.1.0",
43
+ "shx": "^0.3.4",
44
+ "tmp": "^0.2.3",
45
+ "ts-jest": "^29.1.2",
46
+ "typedoc": "^0.25.7",
47
+ "typedoc-plugin-markdown": "^3.17.1",
48
+ "typescript": "^5.3.3",
49
+ "typescript-eslint": "^7.1.0"
50
+ },
51
+ "engines": {
52
+ "node": ">= 18"
53
+ },
54
+ "optionalDependencies": {
55
+ "@lancedb/lancedb-darwin-arm64": "0.4.3",
56
+ "@lancedb/lancedb-darwin-x64": "0.4.3",
57
+ "@lancedb/lancedb-linux-arm64-gnu": "0.4.3",
58
+ "@lancedb/lancedb-linux-x64-gnu": "0.4.3",
59
+ "@lancedb/lancedb-win32-x64-msvc": "0.4.3"
60
+ },
61
+ "peerDependencies": {
62
+ "apache-arrow": "^15.0.0"
63
+ }
64
+ },
65
+ "node_modules/@lancedb/lancedb": {
66
+ "resolved": "../..",
67
+ "link": true
68
+ },
69
+ "node_modules/asynckit": {
70
+ "version": "0.4.0",
71
+ "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
72
+ "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q=="
73
+ },
74
+ "node_modules/axios": {
75
+ "version": "0.26.1",
76
+ "resolved": "https://registry.npmjs.org/axios/-/axios-0.26.1.tgz",
77
+ "integrity": "sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==",
78
+ "dependencies": {
79
+ "follow-redirects": "^1.14.8"
80
+ }
81
+ },
82
+ "node_modules/combined-stream": {
83
+ "version": "1.0.8",
84
+ "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
85
+ "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==",
86
+ "dependencies": {
87
+ "delayed-stream": "~1.0.0"
88
+ },
89
+ "engines": {
90
+ "node": ">= 0.8"
91
+ }
92
+ },
93
+ "node_modules/delayed-stream": {
94
+ "version": "1.0.0",
95
+ "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
96
+ "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==",
97
+ "engines": {
98
+ "node": ">=0.4.0"
99
+ }
100
+ },
101
+ "node_modules/follow-redirects": {
102
+ "version": "1.15.6",
103
+ "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz",
104
+ "integrity": "sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==",
105
+ "funding": [
106
+ {
107
+ "type": "individual",
108
+ "url": "https://github.com/sponsors/RubenVerborgh"
109
+ }
110
+ ],
111
+ "engines": {
112
+ "node": ">=4.0"
113
+ },
114
+ "peerDependenciesMeta": {
115
+ "debug": {
116
+ "optional": true
117
+ }
118
+ }
119
+ },
120
+ "node_modules/form-data": {
121
+ "version": "4.0.0",
122
+ "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz",
123
+ "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==",
124
+ "dependencies": {
125
+ "asynckit": "^0.4.0",
126
+ "combined-stream": "^1.0.8",
127
+ "mime-types": "^2.1.12"
128
+ },
129
+ "engines": {
130
+ "node": ">= 6"
131
+ }
132
+ },
133
+ "node_modules/mime-db": {
134
+ "version": "1.52.0",
135
+ "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz",
136
+ "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==",
137
+ "engines": {
138
+ "node": ">= 0.6"
139
+ }
140
+ },
141
+ "node_modules/mime-types": {
142
+ "version": "2.1.35",
143
+ "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz",
144
+ "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==",
145
+ "dependencies": {
146
+ "mime-db": "1.52.0"
147
+ },
148
+ "engines": {
149
+ "node": ">= 0.6"
150
+ }
151
+ },
152
+ "node_modules/openai": {
153
+ "version": "3.3.0",
154
+ "resolved": "https://registry.npmjs.org/openai/-/openai-3.3.0.tgz",
155
+ "integrity": "sha512-uqxI/Au+aPRnsaQRe8CojU0eCR7I0mBiKjD3sNMzY6DaC1ZVrc85u98mtJW6voDug8fgGN+DIZmTDxTthxb7dQ==",
156
+ "dependencies": {
157
+ "axios": "^0.26.0",
158
+ "form-data": "^4.0.0"
159
+ }
160
+ }
161
+ },
162
+ "dependencies": {
163
+ "@lancedb/lancedb": {
164
+ "version": "file:../..",
165
+ "requires": {
166
+ "@lancedb/lancedb-darwin-arm64": "0.4.3",
167
+ "@lancedb/lancedb-darwin-x64": "0.4.3",
168
+ "@lancedb/lancedb-linux-arm64-gnu": "0.4.3",
169
+ "@lancedb/lancedb-linux-x64-gnu": "0.4.3",
170
+ "@lancedb/lancedb-win32-x64-msvc": "0.4.3",
171
+ "@napi-rs/cli": "^2.18.0",
172
+ "@types/jest": "^29.1.2",
173
+ "@types/tmp": "^0.2.6",
174
+ "@typescript-eslint/eslint-plugin": "^6.19.0",
175
+ "@typescript-eslint/parser": "^6.19.0",
176
+ "apache-arrow-old": "npm:apache-arrow@13.0.0",
177
+ "eslint": "^8.57.0",
178
+ "eslint-config-prettier": "^9.1.0",
179
+ "eslint-plugin-jsdoc": "^48.2.1",
180
+ "jest": "^29.7.0",
181
+ "openai": "^4.29.2",
182
+ "prettier": "^3.1.0",
183
+ "shx": "^0.3.4",
184
+ "tmp": "^0.2.3",
185
+ "ts-jest": "^29.1.2",
186
+ "typedoc": "^0.25.7",
187
+ "typedoc-plugin-markdown": "^3.17.1",
188
+ "typescript": "^5.3.3",
189
+ "typescript-eslint": "^7.1.0"
190
+ }
191
+ },
192
+ "asynckit": {
193
+ "version": "0.4.0",
194
+ "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
195
+ "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q=="
196
+ },
197
+ "axios": {
198
+ "version": "0.26.1",
199
+ "resolved": "https://registry.npmjs.org/axios/-/axios-0.26.1.tgz",
200
+ "integrity": "sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==",
201
+ "requires": {
202
+ "follow-redirects": "^1.14.8"
203
+ }
204
+ },
205
+ "combined-stream": {
206
+ "version": "1.0.8",
207
+ "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
208
+ "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==",
209
+ "requires": {
210
+ "delayed-stream": "~1.0.0"
211
+ }
212
+ },
213
+ "delayed-stream": {
214
+ "version": "1.0.0",
215
+ "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
216
+ "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ=="
217
+ },
218
+ "follow-redirects": {
219
+ "version": "1.15.6",
220
+ "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz",
221
+ "integrity": "sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA=="
222
+ },
223
+ "form-data": {
224
+ "version": "4.0.0",
225
+ "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz",
226
+ "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==",
227
+ "requires": {
228
+ "asynckit": "^0.4.0",
229
+ "combined-stream": "^1.0.8",
230
+ "mime-types": "^2.1.12"
231
+ }
232
+ },
233
+ "mime-db": {
234
+ "version": "1.52.0",
235
+ "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz",
236
+ "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg=="
237
+ },
238
+ "mime-types": {
239
+ "version": "2.1.35",
240
+ "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz",
241
+ "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==",
242
+ "requires": {
243
+ "mime-db": "1.52.0"
244
+ }
245
+ },
246
+ "openai": {
247
+ "version": "3.3.0",
248
+ "resolved": "https://registry.npmjs.org/openai/-/openai-3.3.0.tgz",
249
+ "integrity": "sha512-uqxI/Au+aPRnsaQRe8CojU0eCR7I0mBiKjD3sNMzY6DaC1ZVrc85u98mtJW6voDug8fgGN+DIZmTDxTthxb7dQ==",
250
+ "requires": {
251
+ "axios": "^0.26.0",
252
+ "form-data": "^4.0.0"
253
+ }
254
+ }
255
+ }
256
+ }
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "lancedb-example-js-openai",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "index.mjs",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "author": "Lance Devs",
10
+ "license": "Apache-2.0",
11
+ "dependencies": {
12
+ "@lancedb/lancedb": "file:../..",
13
+ "openai": "^3.2.1"
14
+ }
15
+ }
@@ -0,0 +1,65 @@
1
+ // Copyright 2023 Lance Developers.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ "use strict";
16
+
17
+ /* global console */
18
+
19
+ import * as lancedb from "@lancedb/lancedb";
20
+
21
+ async function example() {
22
+ // Import transformers and the all-MiniLM-L6-v2 model (https://huggingface.co/Xenova/all-MiniLM-L6-v2)
23
+ const { pipeline } = await import("@xenova/transformers");
24
+ const pipe = await pipeline("feature-extraction", "Xenova/all-MiniLM-L6-v2");
25
+
26
+ // Create embedding function from pipeline which returns a list of vectors from batch
27
+ // sourceColumn is the name of the column in the data to be embedded
28
+ //
29
+ // Output of pipe is a Tensor { data: Float32Array(384) }, so filter for the vector
30
+ const embedFun = {};
31
+ embedFun.sourceColumn = "text";
32
+ embedFun.embed = async function (batch) {
33
+ let result = [];
34
+ for (let text of batch) {
35
+ const res = await pipe(text, { pooling: "mean", normalize: true });
36
+ result.push(Array.from(res["data"]));
37
+ }
38
+ return result;
39
+ };
40
+
41
+ // Link a folder and create a table with data
42
+ const db = await lancedb.connect("data/sample-lancedb");
43
+
44
+ const data = [
45
+ { id: 1, text: "Cherry", type: "fruit" },
46
+ { id: 2, text: "Carrot", type: "vegetable" },
47
+ { id: 3, text: "Potato", type: "vegetable" },
48
+ { id: 4, text: "Apple", type: "fruit" },
49
+ { id: 5, text: "Banana", type: "fruit" },
50
+ ];
51
+
52
+ const table = await db.createTable("food_table", data, embedFun);
53
+
54
+ // Query the table
55
+ const results = await table
56
+ .search("a sweet fruit to eat")
57
+ .metricType("cosine")
58
+ .limit(2)
59
+ .execute();
60
+ console.log(results.map((r) => r.text));
61
+ }
62
+
63
+ example()
64
+ .then(() => console.log("All done!"))
65
+ .catch(console.error);