@fgv/ts-extras-transformers 5.1.0-29
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/.rush/temp/cad001c0dd0af20627d8849ed0e5fa4904160f67.tar.log +30 -0
- package/.rush/temp/chunked-rush-logs/ts-extras-transformers.build.chunks.jsonl +9 -0
- package/.rush/temp/chunked-rush-logs/ts-extras-transformers.test.chunks.jsonl +28 -0
- package/.rush/temp/operation/build/all.log +9 -0
- package/.rush/temp/operation/build/log-chunks.jsonl +9 -0
- package/.rush/temp/operation/build/state.json +3 -0
- package/.rush/temp/operation/test/all.log +28 -0
- package/.rush/temp/operation/test/log-chunks.jsonl +28 -0
- package/.rush/temp/operation/test/state.json +3 -0
- package/.rush/temp/shrinkwrap-deps.json +742 -0
- package/CHANGELOG.json +4 -0
- package/README.md +121 -0
- package/config/api-extractor.json +38 -0
- package/config/jest.config.json +13 -0
- package/config/rig.json +6 -0
- package/dist/index.js +137 -0
- package/dist/index.js.map +1 -0
- package/dist/test/unit/transformers.test.js +197 -0
- package/dist/test/unit/transformers.test.js.map +1 -0
- package/dist/ts-extras-transformers.d.ts +137 -0
- package/dist/tsdoc-metadata.json +11 -0
- package/eslint.config.js +15 -0
- package/etc/ts-extras-transformers.api.md +43 -0
- package/lib/index.d.ts +112 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +143 -0
- package/lib/index.js.map +1 -0
- package/lib/test/unit/transformers.test.d.ts +2 -0
- package/lib/test/unit/transformers.test.d.ts.map +1 -0
- package/lib/test/unit/transformers.test.js +232 -0
- package/lib/test/unit/transformers.test.js.map +1 -0
- package/package.json +80 -0
- package/rush-logs/ts-extras-transformers.build.cache.log +3 -0
- package/rush-logs/ts-extras-transformers.build.log +9 -0
- package/rush-logs/ts-extras-transformers.test.cache.log +1 -0
- package/rush-logs/ts-extras-transformers.test.log +28 -0
- package/src/index.ts +179 -0
- package/src/test/unit/transformers.test.ts +267 -0
- package/temp/build/lint/_eslint-5eVG3S6w.json +14 -0
- package/temp/build/typescript/ts_8nwakTlr.json +1 -0
- package/temp/coverage/base.css +224 -0
- package/temp/coverage/block-navigation.js +87 -0
- package/temp/coverage/favicon.png +0 -0
- package/temp/coverage/index.html +116 -0
- package/temp/coverage/index.ts.html +622 -0
- package/temp/coverage/lcov-report/base.css +224 -0
- package/temp/coverage/lcov-report/block-navigation.js +87 -0
- package/temp/coverage/lcov-report/favicon.png +0 -0
- package/temp/coverage/lcov-report/index.html +116 -0
- package/temp/coverage/lcov-report/index.ts.html +622 -0
- package/temp/coverage/lcov-report/prettify.css +1 -0
- package/temp/coverage/lcov-report/prettify.js +2 -0
- package/temp/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/temp/coverage/lcov-report/sorter.js +210 -0
- package/temp/coverage/lcov.info +209 -0
- package/temp/coverage/prettify.css +1 -0
- package/temp/coverage/prettify.js +2 -0
- package/temp/coverage/sort-arrow-sprite.png +0 -0
- package/temp/coverage/sorter.js +210 -0
- package/temp/test/jest/haste-map-502913d9c10083bec2207119c45d7122-cd1aa4a0322f87b67f395127aa5e94ac-091f69b131db2c080ae4b15b7b2d4a16 +0 -0
- package/temp/test/jest/jest-transform-cache-502913d9c10083bec2207119c45d7122-79ef2876fae7ca75eedb2aa53dc48338/8d/package_8dec8c7ec8d6c787063a4a014e769a65 +53 -0
- package/temp/test/jest/jest-transform-cache-502913d9c10083bec2207119c45d7122-79ef2876fae7ca75eedb2aa53dc48338/d5/package_d56b0a8a49dbfccad646fc2e21950522 +203 -0
- package/temp/test/jest/jest-transform-cache-502913d9c10083bec2207119c45d7122-79ef2876fae7ca75eedb2aa53dc48338/fc/versions_fc561f31b0dfb504443c5b8531747bfb +31 -0
- package/temp/test/jest/perf-cache-502913d9c10083bec2207119c45d7122-da39a3ee5e6b4b0d3255bfef95601890 +1 -0
- package/temp/ts-extras-transformers.api.json +675 -0
- package/temp/ts-extras-transformers.api.md +43 -0
- package/tsconfig.json +8 -0
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
jest.mock('@huggingface/transformers');
|
|
37
|
+
require("@fgv/ts-utils-jest");
|
|
38
|
+
const upstream = __importStar(require("@huggingface/transformers"));
|
|
39
|
+
const index_1 = require("../../index");
|
|
40
|
+
// ─── loadPipeline ──────────────────────────────────────────────────────────────
|
|
41
|
+
describe('loadPipeline', () => {
|
|
42
|
+
beforeEach(() => {
|
|
43
|
+
jest.resetAllMocks();
|
|
44
|
+
});
|
|
45
|
+
test('returns Success wrapping the upstream pipeline on success', async () => {
|
|
46
|
+
const mockPipeline = jest.fn();
|
|
47
|
+
jest.mocked(upstream.pipeline).mockResolvedValueOnce(mockPipeline);
|
|
48
|
+
const result = await (0, index_1.loadPipeline)('text-classification', 'Xenova/distilbert-base-uncased-finetuned-sst-2-english');
|
|
49
|
+
expect(result).toSucceedWith(mockPipeline);
|
|
50
|
+
});
|
|
51
|
+
test('passes task and model to the upstream pipeline factory', async () => {
|
|
52
|
+
const mockPipeline = jest.fn();
|
|
53
|
+
jest.mocked(upstream.pipeline).mockResolvedValueOnce(mockPipeline);
|
|
54
|
+
await (0, index_1.loadPipeline)('text-classification', 'some-model-id');
|
|
55
|
+
expect(upstream.pipeline).toHaveBeenCalledWith('text-classification', 'some-model-id', undefined);
|
|
56
|
+
});
|
|
57
|
+
test('passes options through to the upstream pipeline factory', async () => {
|
|
58
|
+
const mockPipeline = jest.fn();
|
|
59
|
+
jest.mocked(upstream.pipeline).mockResolvedValueOnce(mockPipeline);
|
|
60
|
+
const opts = { device: 'cpu' };
|
|
61
|
+
await (0, index_1.loadPipeline)('text-classification', 'some-model-id', opts);
|
|
62
|
+
expect(upstream.pipeline).toHaveBeenCalledWith('text-classification', 'some-model-id', opts);
|
|
63
|
+
});
|
|
64
|
+
test('returns Success when model is omitted', async () => {
|
|
65
|
+
const mockPipeline = jest.fn();
|
|
66
|
+
jest.mocked(upstream.pipeline).mockResolvedValueOnce(mockPipeline);
|
|
67
|
+
const result = await (0, index_1.loadPipeline)('feature-extraction');
|
|
68
|
+
expect(result).toSucceed();
|
|
69
|
+
expect(upstream.pipeline).toHaveBeenCalledWith('feature-extraction', undefined, undefined);
|
|
70
|
+
});
|
|
71
|
+
test('returns Failure capturing upstream error message on network failure', async () => {
|
|
72
|
+
jest
|
|
73
|
+
.mocked(upstream.pipeline)
|
|
74
|
+
.mockRejectedValueOnce(new Error('Could not locate the model: model-not-found'));
|
|
75
|
+
const result = await (0, index_1.loadPipeline)('text-classification', 'model-not-found');
|
|
76
|
+
expect(result).toFailWith(/could not locate the model/i);
|
|
77
|
+
});
|
|
78
|
+
test('returns Failure capturing upstream error on ONNX init failure', async () => {
|
|
79
|
+
jest.mocked(upstream.pipeline).mockRejectedValueOnce(new Error('Failed to initialize ONNX runtime'));
|
|
80
|
+
const result = await (0, index_1.loadPipeline)('text-classification', 'some-model');
|
|
81
|
+
expect(result).toFailWith(/failed to initialize onnx runtime/i);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
// ─── classify ─────────────────────────────────────────────────────────────────
|
|
85
|
+
describe('classify', () => {
|
|
86
|
+
let mockClassifier;
|
|
87
|
+
beforeEach(() => {
|
|
88
|
+
jest.resetAllMocks();
|
|
89
|
+
mockClassifier = jest.fn();
|
|
90
|
+
});
|
|
91
|
+
test('returns Success wrapping TextClassificationOutput on success', async () => {
|
|
92
|
+
const mockOutput = [{ label: 'POSITIVE', score: 0.9998 }];
|
|
93
|
+
mockClassifier.mockResolvedValueOnce(mockOutput);
|
|
94
|
+
const result = await (0, index_1.classify)(mockClassifier, 'I love transformers!');
|
|
95
|
+
expect(result).toSucceedWith(mockOutput);
|
|
96
|
+
});
|
|
97
|
+
test('passes text to the upstream classifier', async () => {
|
|
98
|
+
const mockOutput = [{ label: 'NEGATIVE', score: 0.9997 }];
|
|
99
|
+
mockClassifier.mockResolvedValueOnce(mockOutput);
|
|
100
|
+
await (0, index_1.classify)(mockClassifier, 'I hate bugs');
|
|
101
|
+
expect(mockClassifier).toHaveBeenCalledWith('I hate bugs', undefined);
|
|
102
|
+
});
|
|
103
|
+
test('passes options through to the upstream classifier', async () => {
|
|
104
|
+
const mockOutput = [
|
|
105
|
+
{ label: 'POSITIVE', score: 0.9998 },
|
|
106
|
+
{ label: 'NEGATIVE', score: 0.0002 }
|
|
107
|
+
];
|
|
108
|
+
mockClassifier.mockResolvedValueOnce(mockOutput);
|
|
109
|
+
const opts = { top_k: null };
|
|
110
|
+
await (0, index_1.classify)(mockClassifier, 'Hello world', opts);
|
|
111
|
+
expect(mockClassifier).toHaveBeenCalledWith('Hello world', opts);
|
|
112
|
+
});
|
|
113
|
+
test('normalises flat array result correctly', async () => {
|
|
114
|
+
const mockOutput = [
|
|
115
|
+
{ label: 'SAFE', score: 0.95 },
|
|
116
|
+
{ label: 'UNSAFE', score: 0.05 }
|
|
117
|
+
];
|
|
118
|
+
mockClassifier.mockResolvedValueOnce(mockOutput);
|
|
119
|
+
expect(await (0, index_1.classify)(mockClassifier, 'hello')).toSucceedWith(mockOutput);
|
|
120
|
+
});
|
|
121
|
+
test('normalises nested array result by flattening one level', async () => {
|
|
122
|
+
// When upstream returns array-of-arrays (e.g. batch input path leaked through),
|
|
123
|
+
// we flatten one level so consumers always receive a flat TextClassificationOutput.
|
|
124
|
+
const inner = [{ label: 'POSITIVE', score: 0.9 }];
|
|
125
|
+
mockClassifier.mockResolvedValueOnce([inner]);
|
|
126
|
+
expect(await (0, index_1.classify)(mockClassifier, 'hello')).toSucceedAndSatisfy((output) => {
|
|
127
|
+
expect(output).toEqual([{ label: 'POSITIVE', score: 0.9 }]);
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
test('returns Failure capturing upstream error on inference failure', async () => {
|
|
131
|
+
mockClassifier.mockRejectedValueOnce(new Error('Inference session error: out of memory'));
|
|
132
|
+
const result = await (0, index_1.classify)(mockClassifier, 'some text');
|
|
133
|
+
expect(result).toFailWith(/inference session error/i);
|
|
134
|
+
});
|
|
135
|
+
test('returns Failure capturing upstream tokenisation error', async () => {
|
|
136
|
+
mockClassifier.mockRejectedValueOnce(new Error('Tokenisation failed: unexpected token'));
|
|
137
|
+
const result = await (0, index_1.classify)(mockClassifier, 'bad input');
|
|
138
|
+
expect(result).toFailWith(/tokenisation failed/i);
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
// ─── classifyAll ──────────────────────────────────────────────────────────────
|
|
142
|
+
describe('classifyAll', () => {
|
|
143
|
+
let mockClassifier;
|
|
144
|
+
beforeEach(() => {
|
|
145
|
+
jest.resetAllMocks();
|
|
146
|
+
mockClassifier = jest.fn();
|
|
147
|
+
});
|
|
148
|
+
test('forces top_k: null on the underlying classify call', async () => {
|
|
149
|
+
const mockOutput = [
|
|
150
|
+
{ label: 'POSITIVE', score: 0.9998 },
|
|
151
|
+
{ label: 'NEGATIVE', score: 0.0002 }
|
|
152
|
+
];
|
|
153
|
+
mockClassifier.mockResolvedValueOnce(mockOutput);
|
|
154
|
+
await (0, index_1.classifyAll)(mockClassifier, 'hello');
|
|
155
|
+
expect(mockClassifier).toHaveBeenCalledWith('hello', { top_k: null });
|
|
156
|
+
});
|
|
157
|
+
test('overrides caller-supplied top_k with null', async () => {
|
|
158
|
+
const mockOutput = [
|
|
159
|
+
{ label: 'POSITIVE', score: 0.9998 },
|
|
160
|
+
{ label: 'NEGATIVE', score: 0.0002 }
|
|
161
|
+
];
|
|
162
|
+
mockClassifier.mockResolvedValueOnce(mockOutput);
|
|
163
|
+
await (0, index_1.classifyAll)(mockClassifier, 'hello', { top_k: 1 });
|
|
164
|
+
expect(mockClassifier).toHaveBeenCalledWith('hello', { top_k: null });
|
|
165
|
+
});
|
|
166
|
+
test('returns all labels on success', async () => {
|
|
167
|
+
const mockOutput = [
|
|
168
|
+
{ label: 'POSITIVE', score: 0.9998 },
|
|
169
|
+
{ label: 'NEGATIVE', score: 0.0002 }
|
|
170
|
+
];
|
|
171
|
+
mockClassifier.mockResolvedValueOnce(mockOutput);
|
|
172
|
+
const result = await (0, index_1.classifyAll)(mockClassifier, 'I love transformers!');
|
|
173
|
+
expect(result).toSucceedWith(mockOutput);
|
|
174
|
+
});
|
|
175
|
+
test('propagates upstream inference failure as Failure', async () => {
|
|
176
|
+
mockClassifier.mockRejectedValueOnce(new Error('Inference session error: out of memory'));
|
|
177
|
+
const result = await (0, index_1.classifyAll)(mockClassifier, 'some text');
|
|
178
|
+
expect(result).toFailWith(/inference session error/i);
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
// ─── embed ────────────────────────────────────────────────────────────────────
|
|
182
|
+
describe('embed', () => {
|
|
183
|
+
let mockExtractor;
|
|
184
|
+
beforeEach(() => {
|
|
185
|
+
jest.resetAllMocks();
|
|
186
|
+
mockExtractor = jest.fn();
|
|
187
|
+
});
|
|
188
|
+
test('returns Success wrapping the upstream Tensor on success', async () => {
|
|
189
|
+
const mockTensor = {
|
|
190
|
+
type: 'float32',
|
|
191
|
+
data: new Float32Array([0.1, 0.2, 0.3]),
|
|
192
|
+
dims: [1, 3]
|
|
193
|
+
};
|
|
194
|
+
mockExtractor.mockResolvedValueOnce(mockTensor);
|
|
195
|
+
const result = await (0, index_1.embed)(mockExtractor, 'This is a test.');
|
|
196
|
+
expect(result).toSucceedWith(mockTensor);
|
|
197
|
+
});
|
|
198
|
+
test('passes text to the upstream extractor', async () => {
|
|
199
|
+
const mockTensor = { type: 'float32', data: new Float32Array([0.1]), dims: [1, 1] };
|
|
200
|
+
mockExtractor.mockResolvedValueOnce(mockTensor);
|
|
201
|
+
await (0, index_1.embed)(mockExtractor, 'hello world');
|
|
202
|
+
expect(mockExtractor).toHaveBeenCalledWith('hello world', undefined);
|
|
203
|
+
});
|
|
204
|
+
test('passes string array to the upstream extractor', async () => {
|
|
205
|
+
const mockTensor = {
|
|
206
|
+
type: 'float32',
|
|
207
|
+
data: new Float32Array([0.1, 0.2]),
|
|
208
|
+
dims: [2, 1]
|
|
209
|
+
};
|
|
210
|
+
mockExtractor.mockResolvedValueOnce(mockTensor);
|
|
211
|
+
await (0, index_1.embed)(mockExtractor, ['text one', 'text two']);
|
|
212
|
+
expect(mockExtractor).toHaveBeenCalledWith(['text one', 'text two'], undefined);
|
|
213
|
+
});
|
|
214
|
+
test('passes options through to the upstream extractor', async () => {
|
|
215
|
+
const mockTensor = { type: 'float32', data: new Float32Array([0.5]), dims: [1, 1] };
|
|
216
|
+
mockExtractor.mockResolvedValueOnce(mockTensor);
|
|
217
|
+
const opts = { pooling: 'mean', normalize: true };
|
|
218
|
+
await (0, index_1.embed)(mockExtractor, 'some text', opts);
|
|
219
|
+
expect(mockExtractor).toHaveBeenCalledWith('some text', opts);
|
|
220
|
+
});
|
|
221
|
+
test('returns Failure capturing upstream inference error', async () => {
|
|
222
|
+
mockExtractor.mockRejectedValueOnce(new Error('Inference session error: out of memory'));
|
|
223
|
+
const result = await (0, index_1.embed)(mockExtractor, 'some text');
|
|
224
|
+
expect(result).toFailWith(/inference session error/i);
|
|
225
|
+
});
|
|
226
|
+
test('returns Failure capturing upstream tokenisation error', async () => {
|
|
227
|
+
mockExtractor.mockRejectedValueOnce(new Error('Tokenisation failed: input too long'));
|
|
228
|
+
const result = await (0, index_1.embed)(mockExtractor, 'bad input');
|
|
229
|
+
expect(result).toFailWith(/tokenisation failed/i);
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
//# sourceMappingURL=transformers.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transformers.test.js","sourceRoot":"","sources":["../../../src/test/unit/transformers.test.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAI,CAAC,IAAI,CAAC,2BAA2B,CAAC,CAAC;AAEvC,8BAA4B;AAC5B,oEAAsD;AACtD,uCAUqB;AAErB,kFAAkF;AAElF,QAAQ,CAAC,cAAc,EAAE,GAAG,EAAE;IAC5B,UAAU,CAAC,GAAG,EAAE;QACd,IAAI,CAAC,aAAa,EAAE,CAAC;IACvB,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,2DAA2D,EAAE,KAAK,IAAI,EAAE;QAC3E,MAAM,YAAY,GAAG,IAAI,CAAC,EAAE,EAAgD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AAC,qCAAqC,CAAC,CAAC,CAAC;QAEtF,MAAM,MAAM,GAAG,MAAM,IAAA,aAAK,EAAC,aAAa,EAAE,WAAW,CAAC,CAAC;QACvD,MAAM,CAAC,MAAM,CAAC,CAAC,UAAU,CAAC,sBAAsB,CAAC,CAAC;IACpD,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["jest.mock('@huggingface/transformers');\n\nimport '@fgv/ts-utils-jest';\nimport * as upstream from '@huggingface/transformers';\nimport {\n loadPipeline,\n classify,\n classifyAll,\n embed,\n type TextClassificationPipeline,\n type TextClassificationOutput,\n type FeatureExtractionPipeline,\n type Tensor,\n type AllTasks\n} from '../../index';\n\n// ─── loadPipeline ──────────────────────────────────────────────────────────────\n\ndescribe('loadPipeline', () => {\n beforeEach(() => {\n jest.resetAllMocks();\n });\n\n test('returns Success wrapping the upstream pipeline on success', async () => {\n const mockPipeline = jest.fn() as unknown as AllTasks['text-classification'];\n jest.mocked(upstream.pipeline).mockResolvedValueOnce(mockPipeline);\n\n const result = await loadPipeline(\n 'text-classification',\n 'Xenova/distilbert-base-uncased-finetuned-sst-2-english'\n );\n expect(result).toSucceedWith(mockPipeline);\n });\n\n test('passes task and model to the upstream pipeline factory', async () => {\n const mockPipeline = jest.fn() as unknown as AllTasks['text-classification'];\n jest.mocked(upstream.pipeline).mockResolvedValueOnce(mockPipeline);\n\n await loadPipeline('text-classification', 'some-model-id');\n expect(upstream.pipeline).toHaveBeenCalledWith('text-classification', 'some-model-id', undefined);\n });\n\n test('passes options through to the upstream pipeline factory', async () => {\n const mockPipeline = jest.fn() as unknown as AllTasks['text-classification'];\n jest.mocked(upstream.pipeline).mockResolvedValueOnce(mockPipeline);\n\n const opts = { device: 'cpu' } as Parameters<typeof upstream.pipeline>[2];\n await loadPipeline('text-classification', 'some-model-id', opts);\n expect(upstream.pipeline).toHaveBeenCalledWith('text-classification', 'some-model-id', opts);\n });\n\n test('returns Success when model is omitted', async () => {\n const mockPipeline = jest.fn() as unknown as AllTasks['feature-extraction'];\n jest.mocked(upstream.pipeline).mockResolvedValueOnce(mockPipeline);\n\n const result = await loadPipeline('feature-extraction');\n expect(result).toSucceed();\n expect(upstream.pipeline).toHaveBeenCalledWith('feature-extraction', undefined, undefined);\n });\n\n test('returns Failure capturing upstream error message on network failure', async () => {\n jest\n .mocked(upstream.pipeline)\n .mockRejectedValueOnce(new Error('Could not locate the model: model-not-found'));\n\n const result = await loadPipeline('text-classification', 'model-not-found');\n expect(result).toFailWith(/could not locate the model/i);\n });\n\n test('returns Failure capturing upstream error on ONNX init failure', async () => {\n jest.mocked(upstream.pipeline).mockRejectedValueOnce(new Error('Failed to initialize ONNX runtime'));\n\n const result = await loadPipeline('text-classification', 'some-model');\n expect(result).toFailWith(/failed to initialize onnx runtime/i);\n });\n});\n\n// ─── classify ─────────────────────────────────────────────────────────────────\n\ndescribe('classify', () => {\n let mockClassifier: jest.MockedFunction<TextClassificationPipeline>;\n\n beforeEach(() => {\n jest.resetAllMocks();\n mockClassifier = jest.fn() as unknown as jest.MockedFunction<TextClassificationPipeline>;\n });\n\n test('returns Success wrapping TextClassificationOutput on success', async () => {\n const mockOutput: TextClassificationOutput = [{ label: 'POSITIVE', score: 0.9998 }];\n mockClassifier.mockResolvedValueOnce(mockOutput);\n\n const result = await classify(mockClassifier, 'I love transformers!');\n expect(result).toSucceedWith(mockOutput);\n });\n\n test('passes text to the upstream classifier', async () => {\n const mockOutput: TextClassificationOutput = [{ label: 'NEGATIVE', score: 0.9997 }];\n mockClassifier.mockResolvedValueOnce(mockOutput);\n\n await classify(mockClassifier, 'I hate bugs');\n expect(mockClassifier).toHaveBeenCalledWith('I hate bugs', undefined);\n });\n\n test('passes options through to the upstream classifier', async () => {\n const mockOutput: TextClassificationOutput = [\n { label: 'POSITIVE', score: 0.9998 },\n { label: 'NEGATIVE', score: 0.0002 }\n ];\n mockClassifier.mockResolvedValueOnce(mockOutput);\n\n const opts = { top_k: null };\n await classify(mockClassifier, 'Hello world', opts);\n expect(mockClassifier).toHaveBeenCalledWith('Hello world', opts);\n });\n\n test('normalises flat array result correctly', async () => {\n const mockOutput: TextClassificationOutput = [\n { label: 'SAFE', score: 0.95 },\n { label: 'UNSAFE', score: 0.05 }\n ];\n mockClassifier.mockResolvedValueOnce(mockOutput);\n\n expect(await classify(mockClassifier, 'hello')).toSucceedWith(mockOutput);\n });\n\n test('normalises nested array result by flattening one level', async () => {\n // When upstream returns array-of-arrays (e.g. batch input path leaked through),\n // we flatten one level so consumers always receive a flat TextClassificationOutput.\n const inner: TextClassificationOutput = [{ label: 'POSITIVE', score: 0.9 }];\n mockClassifier.mockResolvedValueOnce([inner] as unknown as TextClassificationOutput);\n\n expect(await classify(mockClassifier, 'hello')).toSucceedAndSatisfy((output) => {\n expect(output).toEqual([{ label: 'POSITIVE', score: 0.9 }]);\n });\n });\n\n test('returns Failure capturing upstream error on inference failure', async () => {\n mockClassifier.mockRejectedValueOnce(new Error('Inference session error: out of memory'));\n\n const result = await classify(mockClassifier, 'some text');\n expect(result).toFailWith(/inference session error/i);\n });\n\n test('returns Failure capturing upstream tokenisation error', async () => {\n mockClassifier.mockRejectedValueOnce(new Error('Tokenisation failed: unexpected token'));\n\n const result = await classify(mockClassifier, 'bad input');\n expect(result).toFailWith(/tokenisation failed/i);\n });\n});\n\n// ─── classifyAll ──────────────────────────────────────────────────────────────\n\ndescribe('classifyAll', () => {\n let mockClassifier: jest.MockedFunction<TextClassificationPipeline>;\n\n beforeEach(() => {\n jest.resetAllMocks();\n mockClassifier = jest.fn() as unknown as jest.MockedFunction<TextClassificationPipeline>;\n });\n\n test('forces top_k: null on the underlying classify call', async () => {\n const mockOutput: TextClassificationOutput = [\n { label: 'POSITIVE', score: 0.9998 },\n { label: 'NEGATIVE', score: 0.0002 }\n ];\n mockClassifier.mockResolvedValueOnce(mockOutput);\n\n await classifyAll(mockClassifier, 'hello');\n expect(mockClassifier).toHaveBeenCalledWith('hello', { top_k: null });\n });\n\n test('overrides caller-supplied top_k with null', async () => {\n const mockOutput: TextClassificationOutput = [\n { label: 'POSITIVE', score: 0.9998 },\n { label: 'NEGATIVE', score: 0.0002 }\n ];\n mockClassifier.mockResolvedValueOnce(mockOutput);\n\n await classifyAll(mockClassifier, 'hello', { top_k: 1 });\n expect(mockClassifier).toHaveBeenCalledWith('hello', { top_k: null });\n });\n\n test('returns all labels on success', async () => {\n const mockOutput: TextClassificationOutput = [\n { label: 'POSITIVE', score: 0.9998 },\n { label: 'NEGATIVE', score: 0.0002 }\n ];\n mockClassifier.mockResolvedValueOnce(mockOutput);\n\n const result = await classifyAll(mockClassifier, 'I love transformers!');\n expect(result).toSucceedWith(mockOutput);\n });\n\n test('propagates upstream inference failure as Failure', async () => {\n mockClassifier.mockRejectedValueOnce(new Error('Inference session error: out of memory'));\n\n const result = await classifyAll(mockClassifier, 'some text');\n expect(result).toFailWith(/inference session error/i);\n });\n});\n\n// ─── embed ────────────────────────────────────────────────────────────────────\n\ndescribe('embed', () => {\n let mockExtractor: jest.MockedFunction<FeatureExtractionPipeline>;\n\n beforeEach(() => {\n jest.resetAllMocks();\n mockExtractor = jest.fn() as unknown as jest.MockedFunction<FeatureExtractionPipeline>;\n });\n\n test('returns Success wrapping the upstream Tensor on success', async () => {\n const mockTensor = {\n type: 'float32',\n data: new Float32Array([0.1, 0.2, 0.3]),\n dims: [1, 3]\n } as unknown as Tensor;\n mockExtractor.mockResolvedValueOnce(mockTensor);\n\n const result = await embed(mockExtractor, 'This is a test.');\n expect(result).toSucceedWith(mockTensor);\n });\n\n test('passes text to the upstream extractor', async () => {\n const mockTensor = { type: 'float32', data: new Float32Array([0.1]), dims: [1, 1] } as unknown as Tensor;\n mockExtractor.mockResolvedValueOnce(mockTensor);\n\n await embed(mockExtractor, 'hello world');\n expect(mockExtractor).toHaveBeenCalledWith('hello world', undefined);\n });\n\n test('passes string array to the upstream extractor', async () => {\n const mockTensor = {\n type: 'float32',\n data: new Float32Array([0.1, 0.2]),\n dims: [2, 1]\n } as unknown as Tensor;\n mockExtractor.mockResolvedValueOnce(mockTensor);\n\n await embed(mockExtractor, ['text one', 'text two']);\n expect(mockExtractor).toHaveBeenCalledWith(['text one', 'text two'], undefined);\n });\n\n test('passes options through to the upstream extractor', async () => {\n const mockTensor = { type: 'float32', data: new Float32Array([0.5]), dims: [1, 1] } as unknown as Tensor;\n mockExtractor.mockResolvedValueOnce(mockTensor);\n\n const opts = { pooling: 'mean' as const, normalize: true };\n await embed(mockExtractor, 'some text', opts);\n expect(mockExtractor).toHaveBeenCalledWith('some text', opts);\n });\n\n test('returns Failure capturing upstream inference error', async () => {\n mockExtractor.mockRejectedValueOnce(new Error('Inference session error: out of memory'));\n\n const result = await embed(mockExtractor, 'some text');\n expect(result).toFailWith(/inference session error/i);\n });\n\n test('returns Failure capturing upstream tokenisation error', async () => {\n mockExtractor.mockRejectedValueOnce(new Error('Tokenisation failed: input too long'));\n\n const result = await embed(mockExtractor, 'bad input');\n expect(result).toFailWith(/tokenisation failed/i);\n });\n});\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fgv/ts-extras-transformers",
|
|
3
|
+
"version": "5.1.0-29",
|
|
4
|
+
"description": "Result-integration boundary over @huggingface/transformers for Node consumers (loadPipeline, classify, classifyAll, embed)",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"types": "dist/ts-extras-transformers.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"node": {
|
|
10
|
+
"import": "./lib/index.js",
|
|
11
|
+
"require": "./lib/index.js"
|
|
12
|
+
},
|
|
13
|
+
"default": {
|
|
14
|
+
"import": "./lib/index.js",
|
|
15
|
+
"require": "./lib/index.js"
|
|
16
|
+
},
|
|
17
|
+
"types": "./dist/ts-extras-transformers.d.ts"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "heft build --clean",
|
|
22
|
+
"clean": "heft clean",
|
|
23
|
+
"test": "heft test --clean",
|
|
24
|
+
"test-handles": "jest --runInBand --detectOpenHandles",
|
|
25
|
+
"clean-jest": "jest --clear-cache",
|
|
26
|
+
"update-snapshot": "jest --updateSnapshot",
|
|
27
|
+
"coverage": "jest --coverage",
|
|
28
|
+
"lint": "eslint src --ext .ts",
|
|
29
|
+
"fixlint": "eslint src --ext .ts --fix"
|
|
30
|
+
},
|
|
31
|
+
"keywords": [
|
|
32
|
+
"typescript",
|
|
33
|
+
"transformers",
|
|
34
|
+
"huggingface",
|
|
35
|
+
"result",
|
|
36
|
+
"local-ai"
|
|
37
|
+
],
|
|
38
|
+
"author": "Erik Fortune",
|
|
39
|
+
"license": "MIT",
|
|
40
|
+
"bugs": {
|
|
41
|
+
"url": "https://github.com/ErikFortune/fgv/issues"
|
|
42
|
+
},
|
|
43
|
+
"homepage": "https://github.com/ErikFortune/fgv/tree/main/libraries/ts-extras-transformers#readme",
|
|
44
|
+
"repository": {
|
|
45
|
+
"type": "git",
|
|
46
|
+
"url": "https://github.com/ErikFortune/fgv.git"
|
|
47
|
+
},
|
|
48
|
+
"sideEffects": false,
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@fgv/heft-dual-rig": "workspace:*",
|
|
51
|
+
"@fgv/ts-utils": "workspace:*",
|
|
52
|
+
"@huggingface/transformers": "~4.2.0",
|
|
53
|
+
"@fgv/ts-utils-jest": "workspace:*",
|
|
54
|
+
"@microsoft/api-extractor": "^7.55.2",
|
|
55
|
+
"@rushstack/eslint-config": "4.6.4",
|
|
56
|
+
"@rushstack/heft": "1.2.7",
|
|
57
|
+
"@rushstack/heft-jest-plugin": "1.2.6",
|
|
58
|
+
"@rushstack/heft-node-rig": "2.11.27",
|
|
59
|
+
"@types/heft-jest": "1.0.6",
|
|
60
|
+
"@types/jest": "^29.5.14",
|
|
61
|
+
"@types/node": "^20.14.9",
|
|
62
|
+
"@typescript-eslint/eslint-plugin": "^8.52.0",
|
|
63
|
+
"@typescript-eslint/parser": "^8.52.0",
|
|
64
|
+
"eslint": "^9.39.2",
|
|
65
|
+
"eslint-plugin-import": "^2.32.0",
|
|
66
|
+
"eslint-plugin-n": "^17.23.1",
|
|
67
|
+
"eslint-plugin-node": "^11.1.0",
|
|
68
|
+
"eslint-plugin-promise": "^7.2.1",
|
|
69
|
+
"eslint-plugin-tsdoc": "~0.5.2",
|
|
70
|
+
"jest": "^29.7.0",
|
|
71
|
+
"rimraf": "^6.1.2",
|
|
72
|
+
"ts-jest": "^29.4.6",
|
|
73
|
+
"ts-node": "^10.9.2",
|
|
74
|
+
"typescript": "5.9.3"
|
|
75
|
+
},
|
|
76
|
+
"peerDependencies": {
|
|
77
|
+
"@fgv/ts-utils": "workspace:*",
|
|
78
|
+
"@huggingface/transformers": "~4.2.0"
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
Invoking: heft build --clean
|
|
2
|
+
---- build started ----
|
|
3
|
+
[build:typescript] The TypeScript compiler version 5.9.3 is newer than the latest version that was tested with Heft (5.8); it may not work correctly.
|
|
4
|
+
[build:typescript] Using TypeScript version 5.9.3
|
|
5
|
+
[build:lint] Using ESLint version 9.39.4
|
|
6
|
+
[build:api-extractor] Using API Extractor version 7.58.7
|
|
7
|
+
[build:api-extractor] Analysis will use the bundled TypeScript version 5.9.3
|
|
8
|
+
---- build finished (2.666s) ----
|
|
9
|
+
-------------------- Finished (2.667s) --------------------
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
This project does not define the caching behavior of the "test" command, so caching has been disabled.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
Invoking: heft test --clean
|
|
2
|
+
---- build started ----
|
|
3
|
+
[build:clean] Deleted 0 files and 3 folders
|
|
4
|
+
[build:typescript] The TypeScript compiler version 5.9.3 is newer than the latest version that was tested with Heft (5.8); it may not work correctly.
|
|
5
|
+
[build:typescript] Using TypeScript version 5.9.3
|
|
6
|
+
[build:lint] Using ESLint version 9.39.4
|
|
7
|
+
[build:api-extractor] Using API Extractor version 7.58.7
|
|
8
|
+
[build:api-extractor] Analysis will use the bundled TypeScript version 5.9.3
|
|
9
|
+
---- build finished (2.842s) ----
|
|
10
|
+
---- test started ----
|
|
11
|
+
[test:jest] Using Jest version 29.5.0
|
|
12
|
+
[test:jest]
|
|
13
|
+
[test:jest] Run start. 1 test suite
|
|
14
|
+
[test:jest] START lib/test/unit/transformers.test.js
|
|
15
|
+
[test:jest] PASS lib/test/unit/transformers.test.js (duration: 2.076s, 23 passed, 0 failed)
|
|
16
|
+
[test:jest]
|
|
17
|
+
[test:jest] Tests finished:
|
|
18
|
+
[test:jest] Successes: 23
|
|
19
|
+
[test:jest] Failures: 0
|
|
20
|
+
[test:jest] Total: 23
|
|
21
|
+
----------|---------|----------|---------|---------|-------------------
|
|
22
|
+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
|
|
23
|
+
----------|---------|----------|---------|---------|-------------------
|
|
24
|
+
All files | 100 | 100 | 100 | 100 |
|
|
25
|
+
index.ts | 100 | 100 | 100 | 100 |
|
|
26
|
+
----------|---------|----------|---------|---------|-------------------
|
|
27
|
+
---- test finished (2.889s) ----
|
|
28
|
+
-------------------- Finished (5.736s) --------------------
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@fgv/ts-extras-transformers` — Result-integration boundary over `@huggingface/transformers`
|
|
3
|
+
* (Node-side).
|
|
4
|
+
*
|
|
5
|
+
* A thin facade that wraps `@huggingface/transformers` calls in `Result<T>` from `@fgv/ts-utils`,
|
|
6
|
+
* mirroring the discipline established by `@fgv/ts-extras-webauthn`: one-line `captureAsyncResult`
|
|
7
|
+
* wrappers around upstream primitives with **no opinionated orchestration** above the boundary.
|
|
8
|
+
*
|
|
9
|
+
* **In scope:** `loadPipeline`, `classify`, `classifyAll`, `embed` — the surface needed by the
|
|
10
|
+
* B-3/B-4a local-classifier and embedding scenarios. `generate` is explicitly deferred until a
|
|
11
|
+
* concrete consumer use case surfaces (see `phase-b2-result.md`).
|
|
12
|
+
*
|
|
13
|
+
* **Explicitly NOT in scope:**
|
|
14
|
+
* - Pipeline cache / lifecycle management
|
|
15
|
+
* - Model registry or download management
|
|
16
|
+
* - GPU/CPU device selection policy
|
|
17
|
+
* - Quantization selection
|
|
18
|
+
* - Embedding-store integration
|
|
19
|
+
* - Classifier label allowlists
|
|
20
|
+
* - Request batching
|
|
21
|
+
* - Pipeline dispose semantics
|
|
22
|
+
*
|
|
23
|
+
* For any of the above, use `@huggingface/transformers` directly (with `captureAsyncResult` for
|
|
24
|
+
* your own Result wrapping).
|
|
25
|
+
*
|
|
26
|
+
* @packageDocumentation
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import {
|
|
30
|
+
pipeline as _pipeline,
|
|
31
|
+
type TextClassificationPipeline,
|
|
32
|
+
type TextClassificationOutput,
|
|
33
|
+
type FeatureExtractionPipeline,
|
|
34
|
+
type Tensor,
|
|
35
|
+
type AllTasks,
|
|
36
|
+
type PipelineType
|
|
37
|
+
} from '@huggingface/transformers';
|
|
38
|
+
import { captureAsyncResult, type Result } from '@fgv/ts-utils';
|
|
39
|
+
|
|
40
|
+
export type {
|
|
41
|
+
TextClassificationPipeline,
|
|
42
|
+
TextClassificationOutput,
|
|
43
|
+
FeatureExtractionPipeline,
|
|
44
|
+
Tensor,
|
|
45
|
+
AllTasks,
|
|
46
|
+
PipelineType
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export type { PretrainedModelOptions } from '@huggingface/transformers';
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Result-integration wrapper around `@huggingface/transformers`'s `pipeline` factory.
|
|
53
|
+
* Loads a model and returns a ready-to-use pipeline object.
|
|
54
|
+
*
|
|
55
|
+
* The returned pipeline is the upstream `AllTasks[T]` instance — consumers retain full access
|
|
56
|
+
* to the upstream API. Lifecycle management (caching, disposal, GPU/CPU selection) is the
|
|
57
|
+
* consumer's responsibility.
|
|
58
|
+
*
|
|
59
|
+
* Returns `Promise<Result<AllTasks[T]>>`; upstream errors (network, model-not-found, ONNX
|
|
60
|
+
* initialization failures) are captured as `Failure` with the original message.
|
|
61
|
+
*
|
|
62
|
+
* @param task - The pipeline task type (e.g. `'text-classification'`, `'feature-extraction'`).
|
|
63
|
+
* @param model - The model identifier (HuggingFace Hub ID or local path). If omitted, the
|
|
64
|
+
* upstream default for the task is used.
|
|
65
|
+
* @param options - Optional `PretrainedModelOptions` (device, dtype, cache_dir, etc.). Passed
|
|
66
|
+
* through verbatim to the upstream `pipeline()` call.
|
|
67
|
+
*
|
|
68
|
+
* @see https://huggingface.co/docs/transformers.js
|
|
69
|
+
* @public
|
|
70
|
+
*/
|
|
71
|
+
export async function loadPipeline<T extends PipelineType>(
|
|
72
|
+
task: T,
|
|
73
|
+
model?: string,
|
|
74
|
+
options?: Parameters<typeof _pipeline>[2]
|
|
75
|
+
): Promise<Result<AllTasks[T]>> {
|
|
76
|
+
return captureAsyncResult(() => _pipeline(task, model, options));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Normalises the upstream pipeline's output to a flat `TextClassificationOutput`.
|
|
81
|
+
*
|
|
82
|
+
* The upstream `TextClassificationPipeline` has an overloaded call signature: a single-string
|
|
83
|
+
* input returns `TextClassificationOutput` (flat array), while a string-array input returns
|
|
84
|
+
* `TextClassificationOutput[]` (array-of-arrays). Since `classify` always passes a single
|
|
85
|
+
* string, the flat-array path is the live path. The nested-array branch is defensive and
|
|
86
|
+
* ensures consumers always receive a flat array even if the upstream type union leaks through.
|
|
87
|
+
*/
|
|
88
|
+
function flattenIfNeeded(
|
|
89
|
+
result: TextClassificationOutput | TextClassificationOutput[]
|
|
90
|
+
): TextClassificationOutput {
|
|
91
|
+
if (Array.isArray(result) && result.length > 0 && !Array.isArray(result[0])) {
|
|
92
|
+
return result as TextClassificationOutput;
|
|
93
|
+
}
|
|
94
|
+
// Defensive: if somehow an array-of-arrays came back, flatten one level.
|
|
95
|
+
return (result as unknown as TextClassificationOutput[]).flat();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Result-integration wrapper that invokes a `TextClassificationPipeline` on a single text input.
|
|
100
|
+
* Returns the classification results as a flat `TextClassificationOutput` (array of
|
|
101
|
+
* `{ label: string; score: number }` entries).
|
|
102
|
+
*
|
|
103
|
+
* Callers should retrieve the pipeline via `loadPipeline('text-classification', modelId)` (or the
|
|
104
|
+
* `'sentiment-analysis'` alias). This helper always passes a single string to the upstream
|
|
105
|
+
* pipeline and normalises the result to `TextClassificationOutput` so consumers don't need to
|
|
106
|
+
* handle the `string | string[]` overload union.
|
|
107
|
+
*
|
|
108
|
+
* Returns `Promise<Result<TextClassificationOutput>>`; upstream errors (inference failures,
|
|
109
|
+
* tokenisation errors) are captured as `Failure` with the original message.
|
|
110
|
+
*
|
|
111
|
+
* @param classifier - A `TextClassificationPipeline` obtained from `loadPipeline`.
|
|
112
|
+
* @param text - The text to classify.
|
|
113
|
+
* @param options - Optional upstream classification options (e.g. `{ top_k: null }` to return
|
|
114
|
+
* all labels). Passed through verbatim to the pipeline call.
|
|
115
|
+
*
|
|
116
|
+
* @see https://huggingface.co/docs/transformers.js
|
|
117
|
+
* @public
|
|
118
|
+
*/
|
|
119
|
+
export async function classify(
|
|
120
|
+
classifier: TextClassificationPipeline,
|
|
121
|
+
text: string,
|
|
122
|
+
options?: Parameters<TextClassificationPipeline>[1]
|
|
123
|
+
): Promise<Result<TextClassificationOutput>> {
|
|
124
|
+
return captureAsyncResult(async () => {
|
|
125
|
+
const result = await classifier(text, options);
|
|
126
|
+
return flattenIfNeeded(result);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Convenience wrapper over `classify` that forces `top_k: null` so the full per-label vector
|
|
132
|
+
* is returned for every call. Callers no longer need to remember to pass `{ top_k: null }`.
|
|
133
|
+
*
|
|
134
|
+
* Any caller-supplied options are honoured except `top_k`, which is always overridden to `null`.
|
|
135
|
+
*
|
|
136
|
+
* Returns `Promise<Result<TextClassificationOutput>>`; upstream errors are captured as `Failure`
|
|
137
|
+
* with the original message.
|
|
138
|
+
*
|
|
139
|
+
* @param classifier - A `TextClassificationPipeline` obtained from `loadPipeline`.
|
|
140
|
+
* @param text - The text to classify.
|
|
141
|
+
* @param options - Optional upstream classification options. `top_k` is always set to `null`
|
|
142
|
+
* regardless of any value supplied here.
|
|
143
|
+
*
|
|
144
|
+
* @see https://huggingface.co/docs/transformers.js
|
|
145
|
+
* @public
|
|
146
|
+
*/
|
|
147
|
+
export async function classifyAll(
|
|
148
|
+
classifier: TextClassificationPipeline,
|
|
149
|
+
text: string,
|
|
150
|
+
options?: Parameters<TextClassificationPipeline>[1]
|
|
151
|
+
): Promise<Result<TextClassificationOutput>> {
|
|
152
|
+
return classify(classifier, text, { ...options, top_k: null });
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Result-integration wrapper that invokes a `FeatureExtractionPipeline` on a text input.
|
|
157
|
+
* Returns the upstream `Tensor` Result-wrapped — no pooling, normalisation, or reshaping is
|
|
158
|
+
* applied. Callers receive the raw output and are responsible for any downstream processing.
|
|
159
|
+
*
|
|
160
|
+
* Callers should retrieve the extractor via `loadPipeline('feature-extraction', modelId)`.
|
|
161
|
+
*
|
|
162
|
+
* Returns `Promise<Result<Tensor>>`; upstream errors (inference failures, tokenisation errors)
|
|
163
|
+
* are captured as `Failure` with the original message.
|
|
164
|
+
*
|
|
165
|
+
* @param extractor - A `FeatureExtractionPipeline` obtained from `loadPipeline`.
|
|
166
|
+
* @param text - The text (or texts) to embed.
|
|
167
|
+
* @param options - Optional upstream feature-extraction options (e.g. `pooling`, `normalize`).
|
|
168
|
+
* Passed through verbatim to the pipeline call.
|
|
169
|
+
*
|
|
170
|
+
* @see https://huggingface.co/docs/transformers.js
|
|
171
|
+
* @public
|
|
172
|
+
*/
|
|
173
|
+
export async function embed(
|
|
174
|
+
extractor: FeatureExtractionPipeline,
|
|
175
|
+
text: string | string[],
|
|
176
|
+
options?: Parameters<FeatureExtractionPipeline['_call']>[1]
|
|
177
|
+
): Promise<Result<Tensor>> {
|
|
178
|
+
return captureAsyncResult(() => extractor(text, options));
|
|
179
|
+
}
|