@anvia/fastembed 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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Indra Zulfi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,90 @@
1
+ # @anvia/fastembed
2
+
3
+ FastEmbed embedding model adapter for Anvia.
4
+
5
+ Use this package when you want local embedding generation through `fastembed`, especially for RAG workflows that should avoid remote embedding APIs.
6
+
7
+ ## Installation
8
+
9
+ ```sh
10
+ pnpm add @anvia/fastembed @anvia/core fastembed
11
+ ```
12
+
13
+ In this monorepo, the package is available through the workspace:
14
+
15
+ ```sh
16
+ pnpm --filter @anvia/fastembed build
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ```ts
22
+ import { embedDocuments, InMemoryVectorStore } from "@anvia/core";
23
+ import { createFastEmbedEmbeddingModel } from "@anvia/fastembed";
24
+
25
+ const embeddingModel = await createFastEmbedEmbeddingModel();
26
+
27
+ const documents = await embedDocuments(
28
+ embeddingModel,
29
+ [
30
+ {
31
+ id: "password-reset",
32
+ title: "Password reset policy",
33
+ body: "Password reset links expire after 30 minutes.",
34
+ },
35
+ {
36
+ id: "priority-support",
37
+ title: "Priority support",
38
+ body: "Enterprise customers receive priority support.",
39
+ },
40
+ ],
41
+ {
42
+ id: (document) => document.id,
43
+ content: (document) => `${document.title}\n${document.body}`,
44
+ },
45
+ );
46
+
47
+ const store = InMemoryVectorStore.fromDocuments(documents);
48
+ const index = store.index(embeddingModel);
49
+
50
+ const results = await index.search({
51
+ query: "How long does a password reset link last?",
52
+ topK: 3,
53
+ });
54
+
55
+ console.log(results);
56
+ ```
57
+
58
+ ## Default Model
59
+
60
+ The default embedding model is:
61
+
62
+ ```ts
63
+ fast-bge-small-en-v1.5
64
+ ```
65
+
66
+ You can pass another FastEmbed model name:
67
+
68
+ ```ts
69
+ const embeddingModel = await createFastEmbedEmbeddingModel({
70
+ model: "fast-bge-base-en-v1.5",
71
+ maxBatchSize: 32,
72
+ });
73
+ ```
74
+
75
+ ## Exports
76
+
77
+ - `FastEmbedEmbeddingModel`
78
+ - `createFastEmbedEmbeddingModel`
79
+ - `DEFAULT_FASTEMBED_EMBEDDING_MODEL`
80
+ - `FastEmbedEmbeddingModelName`
81
+ - `FastEmbedEmbeddingModelOptions`
82
+ - `FastEmbedRuntime`
83
+
84
+ ## Development
85
+
86
+ ```sh
87
+ pnpm --filter @anvia/fastembed typecheck
88
+ pnpm --filter @anvia/fastembed test
89
+ pnpm --filter @anvia/fastembed build
90
+ ```
@@ -0,0 +1,30 @@
1
+ import { EmbeddingModel, Embedding } from '@anvia/core';
2
+ import { EmbeddingModel as EmbeddingModel$1, ExecutionProvider } from 'fastembed';
3
+
4
+ declare const DEFAULT_FASTEMBED_EMBEDDING_MODEL = FastEmbedModel.BGESmallENV15;
5
+ type FastEmbedEmbeddingModelName = `${Exclude<EmbeddingModel$1, EmbeddingModel$1.CUSTOM>}`;
6
+ type FastEmbedRuntime = {
7
+ embed(texts: string[], batchSize?: number): AsyncIterable<unknown>;
8
+ };
9
+ type FastEmbedEmbeddingModelOptions = {
10
+ model?: FastEmbedEmbeddingModelName | undefined;
11
+ maxBatchSize?: number | undefined;
12
+ initOptions?: {
13
+ executionProviders?: ExecutionProvider[] | undefined;
14
+ maxLength?: number | undefined;
15
+ cacheDir?: string | undefined;
16
+ showDownloadProgress?: boolean | undefined;
17
+ modelName?: string | undefined;
18
+ } | undefined;
19
+ };
20
+ declare class FastEmbedEmbeddingModel implements EmbeddingModel {
21
+ private readonly runtime;
22
+ readonly model: string;
23
+ readonly maxBatchSize: number;
24
+ constructor(runtime: FastEmbedRuntime, options?: FastEmbedEmbeddingModelOptions);
25
+ static create(options?: FastEmbedEmbeddingModelOptions): Promise<FastEmbedEmbeddingModel>;
26
+ embedTexts(texts: string[]): Promise<Embedding[]>;
27
+ }
28
+ declare function createFastEmbedEmbeddingModel(options?: FastEmbedEmbeddingModelOptions): Promise<FastEmbedEmbeddingModel>;
29
+
30
+ export { DEFAULT_FASTEMBED_EMBEDDING_MODEL, FastEmbedEmbeddingModel, type FastEmbedEmbeddingModelName, type FastEmbedEmbeddingModelOptions, type FastEmbedRuntime, createFastEmbedEmbeddingModel };
package/dist/index.js ADDED
@@ -0,0 +1,71 @@
1
+ // src/index.ts
2
+ import { EmbeddingModel as FastEmbedModel, FlagEmbedding } from "fastembed";
3
+ var DEFAULT_FASTEMBED_EMBEDDING_MODEL = FastEmbedModel.BGESmallENV15;
4
+ var FastEmbedEmbeddingModel = class _FastEmbedEmbeddingModel {
5
+ constructor(runtime, options = {}) {
6
+ this.runtime = runtime;
7
+ this.model = options.model ?? DEFAULT_FASTEMBED_EMBEDDING_MODEL;
8
+ this.maxBatchSize = Math.max(1, Math.trunc(options.maxBatchSize ?? 256));
9
+ }
10
+ runtime;
11
+ model;
12
+ maxBatchSize;
13
+ static async create(options = {}) {
14
+ const model = options.model ?? DEFAULT_FASTEMBED_EMBEDDING_MODEL;
15
+ const runtime = await FlagEmbedding.init({
16
+ ...options.initOptions ?? {},
17
+ model
18
+ });
19
+ return new _FastEmbedEmbeddingModel(runtime, { ...options, model });
20
+ }
21
+ async embedTexts(texts) {
22
+ if (texts.length === 0) {
23
+ return [];
24
+ }
25
+ const vectors = [];
26
+ for await (const batch of this.runtime.embed(texts, this.maxBatchSize)) {
27
+ vectors.push(...parseBatch(batch, vectors.length));
28
+ }
29
+ if (vectors.length !== texts.length) {
30
+ throw new Error(
31
+ `FastEmbed embedding model returned ${vectors.length} embeddings for ${texts.length} texts`
32
+ );
33
+ }
34
+ return texts.map((document, index) => ({
35
+ document,
36
+ vector: vectors[index]
37
+ }));
38
+ }
39
+ };
40
+ function createFastEmbedEmbeddingModel(options = {}) {
41
+ return FastEmbedEmbeddingModel.create(options);
42
+ }
43
+ function parseBatch(batch, offset) {
44
+ if (!Array.isArray(batch)) {
45
+ throw new Error(`FastEmbed embedding model returned an invalid batch at offset ${offset}`);
46
+ }
47
+ return batch.map((vector, index) => {
48
+ const values = vectorToArray(vector);
49
+ if (values === void 0) {
50
+ throw new Error(
51
+ `FastEmbed embedding model returned an invalid vector at index ${offset + index}`
52
+ );
53
+ }
54
+ return values;
55
+ });
56
+ }
57
+ function vectorToArray(vector) {
58
+ if (Array.isArray(vector) && vector.every((item) => typeof item === "number")) {
59
+ return vector;
60
+ }
61
+ if (ArrayBuffer.isView(vector) && !(vector instanceof DataView)) {
62
+ return Array.from(vector);
63
+ }
64
+ return void 0;
65
+ }
66
+ export {
67
+ DEFAULT_FASTEMBED_EMBEDDING_MODEL,
68
+ FastEmbedEmbeddingModel,
69
+ createFastEmbedEmbeddingModel
70
+ };
71
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { Embedding, EmbeddingModel } from \"@anvia/core\";\nimport type { ExecutionProvider } from \"fastembed\";\nimport { EmbeddingModel as FastEmbedModel, FlagEmbedding } from \"fastembed\";\n\nexport const DEFAULT_FASTEMBED_EMBEDDING_MODEL = FastEmbedModel.BGESmallENV15;\n\nexport type FastEmbedEmbeddingModelName = `${Exclude<FastEmbedModel, FastEmbedModel.CUSTOM>}`;\n\nexport type FastEmbedRuntime = {\n embed(texts: string[], batchSize?: number): AsyncIterable<unknown>;\n};\n\nexport type FastEmbedEmbeddingModelOptions = {\n model?: FastEmbedEmbeddingModelName | undefined;\n maxBatchSize?: number | undefined;\n initOptions?:\n | {\n executionProviders?: ExecutionProvider[] | undefined;\n maxLength?: number | undefined;\n cacheDir?: string | undefined;\n showDownloadProgress?: boolean | undefined;\n modelName?: string | undefined;\n }\n | undefined;\n};\n\nexport class FastEmbedEmbeddingModel implements EmbeddingModel {\n readonly model: string;\n readonly maxBatchSize: number;\n\n constructor(\n private readonly runtime: FastEmbedRuntime,\n options: FastEmbedEmbeddingModelOptions = {},\n ) {\n this.model = options.model ?? DEFAULT_FASTEMBED_EMBEDDING_MODEL;\n this.maxBatchSize = Math.max(1, Math.trunc(options.maxBatchSize ?? 256));\n }\n\n static async create(\n options: FastEmbedEmbeddingModelOptions = {},\n ): Promise<FastEmbedEmbeddingModel> {\n const model = options.model ?? DEFAULT_FASTEMBED_EMBEDDING_MODEL;\n const runtime = await FlagEmbedding.init({\n ...(options.initOptions ?? {}),\n model,\n } as never);\n\n return new FastEmbedEmbeddingModel(runtime, { ...options, model });\n }\n\n async embedTexts(texts: string[]): Promise<Embedding[]> {\n if (texts.length === 0) {\n return [];\n }\n\n const vectors: number[][] = [];\n for await (const batch of this.runtime.embed(texts, this.maxBatchSize)) {\n vectors.push(...parseBatch(batch, vectors.length));\n }\n\n if (vectors.length !== texts.length) {\n throw new Error(\n `FastEmbed embedding model returned ${vectors.length} embeddings for ${texts.length} texts`,\n );\n }\n\n return texts.map((document, index) => ({\n document,\n vector: vectors[index] as number[],\n }));\n }\n}\n\nexport function createFastEmbedEmbeddingModel(\n options: FastEmbedEmbeddingModelOptions = {},\n): Promise<FastEmbedEmbeddingModel> {\n return FastEmbedEmbeddingModel.create(options);\n}\n\nfunction parseBatch(batch: unknown, offset: number): number[][] {\n if (!Array.isArray(batch)) {\n throw new Error(`FastEmbed embedding model returned an invalid batch at offset ${offset}`);\n }\n\n return batch.map((vector, index) => {\n const values = vectorToArray(vector);\n if (values === undefined) {\n throw new Error(\n `FastEmbed embedding model returned an invalid vector at index ${offset + index}`,\n );\n }\n return values;\n });\n}\n\nfunction vectorToArray(vector: unknown): number[] | undefined {\n if (Array.isArray(vector) && vector.every((item) => typeof item === \"number\")) {\n return vector;\n }\n\n if (ArrayBuffer.isView(vector) && !(vector instanceof DataView)) {\n return Array.from(vector as unknown as ArrayLike<number>);\n }\n\n return undefined;\n}\n"],"mappings":";AAEA,SAAS,kBAAkB,gBAAgB,qBAAqB;AAEzD,IAAM,oCAAoC,eAAe;AAsBzD,IAAM,0BAAN,MAAM,yBAAkD;AAAA,EAI7D,YACmB,SACjB,UAA0C,CAAC,GAC3C;AAFiB;AAGjB,SAAK,QAAQ,QAAQ,SAAS;AAC9B,SAAK,eAAe,KAAK,IAAI,GAAG,KAAK,MAAM,QAAQ,gBAAgB,GAAG,CAAC;AAAA,EACzE;AAAA,EALmB;AAAA,EAJV;AAAA,EACA;AAAA,EAUT,aAAa,OACX,UAA0C,CAAC,GACT;AAClC,UAAM,QAAQ,QAAQ,SAAS;AAC/B,UAAM,UAAU,MAAM,cAAc,KAAK;AAAA,MACvC,GAAI,QAAQ,eAAe,CAAC;AAAA,MAC5B;AAAA,IACF,CAAU;AAEV,WAAO,IAAI,yBAAwB,SAAS,EAAE,GAAG,SAAS,MAAM,CAAC;AAAA,EACnE;AAAA,EAEA,MAAM,WAAW,OAAuC;AACtD,QAAI,MAAM,WAAW,GAAG;AACtB,aAAO,CAAC;AAAA,IACV;AAEA,UAAM,UAAsB,CAAC;AAC7B,qBAAiB,SAAS,KAAK,QAAQ,MAAM,OAAO,KAAK,YAAY,GAAG;AACtE,cAAQ,KAAK,GAAG,WAAW,OAAO,QAAQ,MAAM,CAAC;AAAA,IACnD;AAEA,QAAI,QAAQ,WAAW,MAAM,QAAQ;AACnC,YAAM,IAAI;AAAA,QACR,sCAAsC,QAAQ,MAAM,mBAAmB,MAAM,MAAM;AAAA,MACrF;AAAA,IACF;AAEA,WAAO,MAAM,IAAI,CAAC,UAAU,WAAW;AAAA,MACrC;AAAA,MACA,QAAQ,QAAQ,KAAK;AAAA,IACvB,EAAE;AAAA,EACJ;AACF;AAEO,SAAS,8BACd,UAA0C,CAAC,GACT;AAClC,SAAO,wBAAwB,OAAO,OAAO;AAC/C;AAEA,SAAS,WAAW,OAAgB,QAA4B;AAC9D,MAAI,CAAC,MAAM,QAAQ,KAAK,GAAG;AACzB,UAAM,IAAI,MAAM,iEAAiE,MAAM,EAAE;AAAA,EAC3F;AAEA,SAAO,MAAM,IAAI,CAAC,QAAQ,UAAU;AAClC,UAAM,SAAS,cAAc,MAAM;AACnC,QAAI,WAAW,QAAW;AACxB,YAAM,IAAI;AAAA,QACR,iEAAiE,SAAS,KAAK;AAAA,MACjF;AAAA,IACF;AACA,WAAO;AAAA,EACT,CAAC;AACH;AAEA,SAAS,cAAc,QAAuC;AAC5D,MAAI,MAAM,QAAQ,MAAM,KAAK,OAAO,MAAM,CAAC,SAAS,OAAO,SAAS,QAAQ,GAAG;AAC7E,WAAO;AAAA,EACT;AAEA,MAAI,YAAY,OAAO,MAAM,KAAK,EAAE,kBAAkB,WAAW;AAC/D,WAAO,MAAM,KAAK,MAAsC;AAAA,EAC1D;AAEA,SAAO;AACT;","names":[]}
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@anvia/fastembed",
3
+ "version": "0.1.0",
4
+ "description": "FastEmbed embedding model adapter for Anvia.",
5
+ "author": "anvia",
6
+ "maintainer": "Indra Zulfi",
7
+ "license": "MIT",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "type": "module",
12
+ "main": "./dist/index.js",
13
+ "types": "./dist/index.d.ts",
14
+ "exports": {
15
+ ".": {
16
+ "types": "./dist/index.d.ts",
17
+ "import": "./dist/index.js"
18
+ }
19
+ },
20
+ "dependencies": {
21
+ "fastembed": "^2.1.0",
22
+ "@anvia/core": "0.1.0"
23
+ },
24
+ "devDependencies": {
25
+ "@types/node": "^24.9.1",
26
+ "tsup": "^8.5.0",
27
+ "typescript": "^5.9.3",
28
+ "vitest": "^4.0.8"
29
+ },
30
+ "scripts": {
31
+ "build": "tsup src/index.ts --format esm --dts --sourcemap --clean",
32
+ "test": "vitest run",
33
+ "typecheck": "tsc --noEmit"
34
+ }
35
+ }