@anvia/transformers 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 +21 -0
- package/README.md +91 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +61 -0
- package/dist/index.js.map +1 -0
- package/package.json +35 -0
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,91 @@
|
|
|
1
|
+
# @anvia/transformers
|
|
2
|
+
|
|
3
|
+
Transformers.js embedding model adapter for Anvia.
|
|
4
|
+
|
|
5
|
+
Use this package when you want local embedding generation through `@huggingface/transformers`, especially for development or lightweight RAG workflows.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pnpm add @anvia/transformers @anvia/core @huggingface/transformers
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
In this monorepo, the package is available through the workspace:
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
pnpm --filter @anvia/transformers build
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { embedDocuments, InMemoryVectorStore } from "@anvia/core";
|
|
23
|
+
import { createTransformersEmbeddingModel } from "@anvia/transformers";
|
|
24
|
+
|
|
25
|
+
const embeddingModel = await createTransformersEmbeddingModel();
|
|
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
|
+
Xenova/all-MiniLM-L6-v2
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
You can pass another feature-extraction model:
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
const embeddingModel = await createTransformersEmbeddingModel({
|
|
70
|
+
model: "Xenova/all-MiniLM-L6-v2",
|
|
71
|
+
pooling: "mean",
|
|
72
|
+
normalize: true,
|
|
73
|
+
maxBatchSize: 16,
|
|
74
|
+
});
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Exports
|
|
78
|
+
|
|
79
|
+
- `TransformersEmbeddingModel`
|
|
80
|
+
- `createTransformersEmbeddingModel`
|
|
81
|
+
- `DEFAULT_TRANSFORMERS_EMBEDDING_MODEL`
|
|
82
|
+
- `TransformersEmbeddingModelOptions`
|
|
83
|
+
- `TransformersPooling`
|
|
84
|
+
|
|
85
|
+
## Development
|
|
86
|
+
|
|
87
|
+
```sh
|
|
88
|
+
pnpm --filter @anvia/transformers typecheck
|
|
89
|
+
pnpm --filter @anvia/transformers test
|
|
90
|
+
pnpm --filter @anvia/transformers build
|
|
91
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { EmbeddingModel, Embedding } from '@anvia/core';
|
|
2
|
+
|
|
3
|
+
declare const DEFAULT_TRANSFORMERS_EMBEDDING_MODEL = "Xenova/all-MiniLM-L6-v2";
|
|
4
|
+
type TransformersPooling = "mean" | "cls";
|
|
5
|
+
type TransformersFeatureExtractionPipeline = (texts: string[], options: {
|
|
6
|
+
pooling: TransformersPooling;
|
|
7
|
+
normalize: boolean;
|
|
8
|
+
}) => Promise<{
|
|
9
|
+
tolist(): unknown;
|
|
10
|
+
}>;
|
|
11
|
+
type TransformersEmbeddingModelOptions = {
|
|
12
|
+
model?: string | undefined;
|
|
13
|
+
pooling?: TransformersPooling | undefined;
|
|
14
|
+
normalize?: boolean | undefined;
|
|
15
|
+
maxBatchSize?: number | undefined;
|
|
16
|
+
};
|
|
17
|
+
declare class TransformersEmbeddingModel implements EmbeddingModel {
|
|
18
|
+
private readonly extractor;
|
|
19
|
+
readonly model: string;
|
|
20
|
+
readonly maxBatchSize: number;
|
|
21
|
+
private readonly pooling;
|
|
22
|
+
private readonly normalize;
|
|
23
|
+
constructor(extractor: TransformersFeatureExtractionPipeline, options?: TransformersEmbeddingModelOptions);
|
|
24
|
+
static create(options?: TransformersEmbeddingModelOptions): Promise<TransformersEmbeddingModel>;
|
|
25
|
+
embedTexts(texts: string[]): Promise<Embedding[]>;
|
|
26
|
+
}
|
|
27
|
+
declare function createTransformersEmbeddingModel(options?: TransformersEmbeddingModelOptions): Promise<TransformersEmbeddingModel>;
|
|
28
|
+
|
|
29
|
+
export { DEFAULT_TRANSFORMERS_EMBEDDING_MODEL, TransformersEmbeddingModel, type TransformersEmbeddingModelOptions, type TransformersFeatureExtractionPipeline, type TransformersPooling, createTransformersEmbeddingModel };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { pipeline as transformersPipeline } from "@huggingface/transformers";
|
|
3
|
+
var DEFAULT_TRANSFORMERS_EMBEDDING_MODEL = "Xenova/all-MiniLM-L6-v2";
|
|
4
|
+
var TransformersEmbeddingModel = class _TransformersEmbeddingModel {
|
|
5
|
+
constructor(extractor, options = {}) {
|
|
6
|
+
this.extractor = extractor;
|
|
7
|
+
this.model = options.model ?? DEFAULT_TRANSFORMERS_EMBEDDING_MODEL;
|
|
8
|
+
this.pooling = options.pooling ?? "mean";
|
|
9
|
+
this.normalize = options.normalize ?? true;
|
|
10
|
+
this.maxBatchSize = Math.max(1, Math.trunc(options.maxBatchSize ?? 16));
|
|
11
|
+
}
|
|
12
|
+
extractor;
|
|
13
|
+
model;
|
|
14
|
+
maxBatchSize;
|
|
15
|
+
pooling;
|
|
16
|
+
normalize;
|
|
17
|
+
static async create(options = {}) {
|
|
18
|
+
const model = options.model ?? DEFAULT_TRANSFORMERS_EMBEDDING_MODEL;
|
|
19
|
+
const extractor = await transformersPipeline(
|
|
20
|
+
"feature-extraction",
|
|
21
|
+
model
|
|
22
|
+
);
|
|
23
|
+
return new _TransformersEmbeddingModel(extractor, { ...options, model });
|
|
24
|
+
}
|
|
25
|
+
async embedTexts(texts) {
|
|
26
|
+
if (texts.length === 0) {
|
|
27
|
+
return [];
|
|
28
|
+
}
|
|
29
|
+
const output = await this.extractor(texts, {
|
|
30
|
+
pooling: this.pooling,
|
|
31
|
+
normalize: this.normalize
|
|
32
|
+
});
|
|
33
|
+
const vectors = parseVectors(output.tolist(), texts.length);
|
|
34
|
+
return texts.map((document, index) => ({
|
|
35
|
+
document,
|
|
36
|
+
vector: vectors[index]
|
|
37
|
+
}));
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
function createTransformersEmbeddingModel(options = {}) {
|
|
41
|
+
return TransformersEmbeddingModel.create(options);
|
|
42
|
+
}
|
|
43
|
+
function parseVectors(value, expectedLength) {
|
|
44
|
+
if (!Array.isArray(value) || value.length !== expectedLength) {
|
|
45
|
+
throw new Error(
|
|
46
|
+
`Transformers embedding model returned ${Array.isArray(value) ? value.length : 0} embeddings for ${expectedLength} texts`
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
return value.map((vector, index) => {
|
|
50
|
+
if (!Array.isArray(vector) || !vector.every((item) => typeof item === "number")) {
|
|
51
|
+
throw new Error(`Transformers embedding model returned an invalid vector at index ${index}`);
|
|
52
|
+
}
|
|
53
|
+
return vector;
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
export {
|
|
57
|
+
DEFAULT_TRANSFORMERS_EMBEDDING_MODEL,
|
|
58
|
+
TransformersEmbeddingModel,
|
|
59
|
+
createTransformersEmbeddingModel
|
|
60
|
+
};
|
|
61
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { Embedding, EmbeddingModel } from \"@anvia/core\";\nimport { pipeline as transformersPipeline } from \"@huggingface/transformers\";\n\nexport const DEFAULT_TRANSFORMERS_EMBEDDING_MODEL = \"Xenova/all-MiniLM-L6-v2\";\n\nexport type TransformersPooling = \"mean\" | \"cls\";\n\nexport type TransformersFeatureExtractionPipeline = (\n texts: string[],\n options: { pooling: TransformersPooling; normalize: boolean },\n) => Promise<{ tolist(): unknown }>;\n\nexport type TransformersEmbeddingModelOptions = {\n model?: string | undefined;\n pooling?: TransformersPooling | undefined;\n normalize?: boolean | undefined;\n maxBatchSize?: number | undefined;\n};\n\nexport class TransformersEmbeddingModel implements EmbeddingModel {\n readonly model: string;\n readonly maxBatchSize: number;\n\n private readonly pooling: TransformersPooling;\n private readonly normalize: boolean;\n\n constructor(\n private readonly extractor: TransformersFeatureExtractionPipeline,\n options: TransformersEmbeddingModelOptions = {},\n ) {\n this.model = options.model ?? DEFAULT_TRANSFORMERS_EMBEDDING_MODEL;\n this.pooling = options.pooling ?? \"mean\";\n this.normalize = options.normalize ?? true;\n this.maxBatchSize = Math.max(1, Math.trunc(options.maxBatchSize ?? 16));\n }\n\n static async create(\n options: TransformersEmbeddingModelOptions = {},\n ): Promise<TransformersEmbeddingModel> {\n const model = options.model ?? DEFAULT_TRANSFORMERS_EMBEDDING_MODEL;\n const extractor = (await transformersPipeline(\n \"feature-extraction\",\n model,\n )) as TransformersFeatureExtractionPipeline;\n\n return new TransformersEmbeddingModel(extractor, { ...options, model });\n }\n\n async embedTexts(texts: string[]): Promise<Embedding[]> {\n if (texts.length === 0) {\n return [];\n }\n\n const output = await this.extractor(texts, {\n pooling: this.pooling,\n normalize: this.normalize,\n });\n const vectors = parseVectors(output.tolist(), texts.length);\n\n return texts.map((document, index) => ({\n document,\n vector: vectors[index] as number[],\n }));\n }\n}\n\nexport function createTransformersEmbeddingModel(\n options: TransformersEmbeddingModelOptions = {},\n): Promise<TransformersEmbeddingModel> {\n return TransformersEmbeddingModel.create(options);\n}\n\nfunction parseVectors(value: unknown, expectedLength: number): number[][] {\n if (!Array.isArray(value) || value.length !== expectedLength) {\n throw new Error(\n `Transformers embedding model returned ${Array.isArray(value) ? value.length : 0} embeddings for ${expectedLength} texts`,\n );\n }\n\n return value.map((vector, index) => {\n if (!Array.isArray(vector) || !vector.every((item) => typeof item === \"number\")) {\n throw new Error(`Transformers embedding model returned an invalid vector at index ${index}`);\n }\n return vector;\n });\n}\n"],"mappings":";AACA,SAAS,YAAY,4BAA4B;AAE1C,IAAM,uCAAuC;AAgB7C,IAAM,6BAAN,MAAM,4BAAqD;AAAA,EAOhE,YACmB,WACjB,UAA6C,CAAC,GAC9C;AAFiB;AAGjB,SAAK,QAAQ,QAAQ,SAAS;AAC9B,SAAK,UAAU,QAAQ,WAAW;AAClC,SAAK,YAAY,QAAQ,aAAa;AACtC,SAAK,eAAe,KAAK,IAAI,GAAG,KAAK,MAAM,QAAQ,gBAAgB,EAAE,CAAC;AAAA,EACxE;AAAA,EAPmB;AAAA,EAPV;AAAA,EACA;AAAA,EAEQ;AAAA,EACA;AAAA,EAYjB,aAAa,OACX,UAA6C,CAAC,GACT;AACrC,UAAM,QAAQ,QAAQ,SAAS;AAC/B,UAAM,YAAa,MAAM;AAAA,MACvB;AAAA,MACA;AAAA,IACF;AAEA,WAAO,IAAI,4BAA2B,WAAW,EAAE,GAAG,SAAS,MAAM,CAAC;AAAA,EACxE;AAAA,EAEA,MAAM,WAAW,OAAuC;AACtD,QAAI,MAAM,WAAW,GAAG;AACtB,aAAO,CAAC;AAAA,IACV;AAEA,UAAM,SAAS,MAAM,KAAK,UAAU,OAAO;AAAA,MACzC,SAAS,KAAK;AAAA,MACd,WAAW,KAAK;AAAA,IAClB,CAAC;AACD,UAAM,UAAU,aAAa,OAAO,OAAO,GAAG,MAAM,MAAM;AAE1D,WAAO,MAAM,IAAI,CAAC,UAAU,WAAW;AAAA,MACrC;AAAA,MACA,QAAQ,QAAQ,KAAK;AAAA,IACvB,EAAE;AAAA,EACJ;AACF;AAEO,SAAS,iCACd,UAA6C,CAAC,GACT;AACrC,SAAO,2BAA2B,OAAO,OAAO;AAClD;AAEA,SAAS,aAAa,OAAgB,gBAAoC;AACxE,MAAI,CAAC,MAAM,QAAQ,KAAK,KAAK,MAAM,WAAW,gBAAgB;AAC5D,UAAM,IAAI;AAAA,MACR,yCAAyC,MAAM,QAAQ,KAAK,IAAI,MAAM,SAAS,CAAC,mBAAmB,cAAc;AAAA,IACnH;AAAA,EACF;AAEA,SAAO,MAAM,IAAI,CAAC,QAAQ,UAAU;AAClC,QAAI,CAAC,MAAM,QAAQ,MAAM,KAAK,CAAC,OAAO,MAAM,CAAC,SAAS,OAAO,SAAS,QAAQ,GAAG;AAC/E,YAAM,IAAI,MAAM,oEAAoE,KAAK,EAAE;AAAA,IAC7F;AACA,WAAO;AAAA,EACT,CAAC;AACH;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@anvia/transformers",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Transformers.js 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
|
+
"@huggingface/transformers": "^4.2.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
|
+
}
|