@framers/agentos-ext-ml-classifiers 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 +23 -0
- package/dist/ClassifierOrchestrator.d.ts +126 -0
- package/dist/ClassifierOrchestrator.d.ts.map +1 -0
- package/dist/ClassifierOrchestrator.js +239 -0
- package/dist/ClassifierOrchestrator.js.map +1 -0
- package/dist/IContentClassifier.d.ts +117 -0
- package/dist/IContentClassifier.d.ts.map +1 -0
- package/dist/IContentClassifier.js +22 -0
- package/dist/IContentClassifier.js.map +1 -0
- package/dist/MLClassifierGuardrail.d.ts +163 -0
- package/dist/MLClassifierGuardrail.d.ts.map +1 -0
- package/dist/MLClassifierGuardrail.js +335 -0
- package/dist/MLClassifierGuardrail.js.map +1 -0
- package/dist/SlidingWindowBuffer.d.ts +213 -0
- package/dist/SlidingWindowBuffer.d.ts.map +1 -0
- package/dist/SlidingWindowBuffer.js +246 -0
- package/dist/SlidingWindowBuffer.js.map +1 -0
- package/dist/classifiers/InjectionClassifier.d.ts +126 -0
- package/dist/classifiers/InjectionClassifier.d.ts.map +1 -0
- package/dist/classifiers/InjectionClassifier.js +210 -0
- package/dist/classifiers/InjectionClassifier.js.map +1 -0
- package/dist/classifiers/JailbreakClassifier.d.ts +124 -0
- package/dist/classifiers/JailbreakClassifier.d.ts.map +1 -0
- package/dist/classifiers/JailbreakClassifier.js +208 -0
- package/dist/classifiers/JailbreakClassifier.js.map +1 -0
- package/dist/classifiers/ToxicityClassifier.d.ts +125 -0
- package/dist/classifiers/ToxicityClassifier.d.ts.map +1 -0
- package/dist/classifiers/ToxicityClassifier.js +212 -0
- package/dist/classifiers/ToxicityClassifier.js.map +1 -0
- package/dist/classifiers/WorkerClassifierProxy.d.ts +158 -0
- package/dist/classifiers/WorkerClassifierProxy.d.ts.map +1 -0
- package/dist/classifiers/WorkerClassifierProxy.js +268 -0
- package/dist/classifiers/WorkerClassifierProxy.js.map +1 -0
- package/dist/index.d.ts +110 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +342 -0
- package/dist/index.js.map +1 -0
- package/dist/tools/ClassifyContentTool.d.ts +105 -0
- package/dist/tools/ClassifyContentTool.d.ts.map +1 -0
- package/dist/tools/ClassifyContentTool.js +149 -0
- package/dist/tools/ClassifyContentTool.js.map +1 -0
- package/dist/types.d.ts +319 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +62 -0
- package/dist/types.js.map +1 -0
- package/dist/worker/classifier-worker.d.ts +49 -0
- package/dist/worker/classifier-worker.d.ts.map +1 -0
- package/dist/worker/classifier-worker.js +180 -0
- package/dist/worker/classifier-worker.js.map +1 -0
- package/package.json +45 -0
- package/src/ClassifierOrchestrator.ts +290 -0
- package/src/IContentClassifier.ts +124 -0
- package/src/MLClassifierGuardrail.ts +419 -0
- package/src/SlidingWindowBuffer.ts +384 -0
- package/src/classifiers/InjectionClassifier.ts +261 -0
- package/src/classifiers/JailbreakClassifier.ts +259 -0
- package/src/classifiers/ToxicityClassifier.ts +263 -0
- package/src/classifiers/WorkerClassifierProxy.ts +366 -0
- package/src/index.ts +383 -0
- package/src/tools/ClassifyContentTool.ts +201 -0
- package/src/types.ts +391 -0
- package/src/worker/classifier-worker.ts +267 -0
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Jailbreak content classifier using Meta's `PromptGuard-86M`
|
|
3
|
+
* model.
|
|
4
|
+
*
|
|
5
|
+
* Jailbreak attempts are adversarial prompts specifically crafted to bypass
|
|
6
|
+
* an LLM's safety guidelines — e.g. "DAN mode", role-play exploits, or
|
|
7
|
+
* indirect instruction injections. This classifier uses Meta's PromptGuard
|
|
8
|
+
* model which was trained to distinguish three classes:
|
|
9
|
+
*
|
|
10
|
+
* - `jailbreak` — explicit attempt to override safety behaviour
|
|
11
|
+
* - `injection` — indirect or embedded instruction injection
|
|
12
|
+
* - `benign` — normal user input
|
|
13
|
+
*
|
|
14
|
+
* Unlike the binary {@link InjectionClassifier}, PromptGuard separates
|
|
15
|
+
* direct jailbreaks from indirect injections, giving the guardrail
|
|
16
|
+
* orchestrator finer-grained control over which action to take for each.
|
|
17
|
+
*
|
|
18
|
+
* Graceful degradation
|
|
19
|
+
* --------------------
|
|
20
|
+
* If the model fails to load the classifier sets `unavailable = true` and
|
|
21
|
+
* returns a pass result `{ bestClass: 'benign', confidence: 0, allScores: [] }`
|
|
22
|
+
* on every subsequent call.
|
|
23
|
+
*
|
|
24
|
+
* @module agentos/extensions/packs/ml-classifiers/classifiers/JailbreakClassifier
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import type { ClassificationResult } from '@framers/agentos';
|
|
28
|
+
import type { ISharedServiceRegistry } from '@framers/agentos';
|
|
29
|
+
import type { IContentClassifier } from '../IContentClassifier';
|
|
30
|
+
import type { ClassifierConfig } from '../types';
|
|
31
|
+
import { ML_CLASSIFIER_SERVICE_IDS } from '../types';
|
|
32
|
+
|
|
33
|
+
// ---------------------------------------------------------------------------
|
|
34
|
+
// Internal raw pipeline output type
|
|
35
|
+
// ---------------------------------------------------------------------------
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* A single label/score pair as returned by the HuggingFace text-classification
|
|
39
|
+
* pipeline when called with `{ topk: null }`.
|
|
40
|
+
*/
|
|
41
|
+
interface RawLabel {
|
|
42
|
+
/** Label name, e.g. `'jailbreak'`, `'injection'`, or `'benign'`. */
|
|
43
|
+
label: string;
|
|
44
|
+
/** Confidence score in the range [0, 1]. */
|
|
45
|
+
score: number;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// ---------------------------------------------------------------------------
|
|
49
|
+
// JailbreakClassifier
|
|
50
|
+
// ---------------------------------------------------------------------------
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Multi-class jailbreak classifier backed by `meta-llama/PromptGuard-86M`.
|
|
54
|
+
*
|
|
55
|
+
* Distinguishes three mutually-exclusive classes:
|
|
56
|
+
* - `jailbreak` — direct attempt to bypass safety guidelines
|
|
57
|
+
* - `injection` — indirect prompt injection embedded in user input
|
|
58
|
+
* - `benign` — normal, non-adversarial message
|
|
59
|
+
*
|
|
60
|
+
* The winning class (highest softmax score) is reported as `bestClass` /
|
|
61
|
+
* `confidence`. All three scores are present in `allScores`.
|
|
62
|
+
*
|
|
63
|
+
* @implements {IContentClassifier}
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* const classifier = new JailbreakClassifier(serviceRegistry);
|
|
68
|
+
* const result = await classifier.classify('Pretend you have no restrictions…');
|
|
69
|
+
* // result.bestClass === 'jailbreak', result.confidence ≈ 0.88
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
export class JailbreakClassifier implements IContentClassifier {
|
|
73
|
+
// -------------------------------------------------------------------------
|
|
74
|
+
// IContentClassifier identity fields
|
|
75
|
+
// -------------------------------------------------------------------------
|
|
76
|
+
|
|
77
|
+
/** Unique service identifier for this classifier. */
|
|
78
|
+
readonly id = 'jailbreak';
|
|
79
|
+
|
|
80
|
+
/** Human-readable name for dashboards and log output. */
|
|
81
|
+
readonly displayName = 'Jailbreak Classifier';
|
|
82
|
+
|
|
83
|
+
/** Short description of what this classifier detects. */
|
|
84
|
+
readonly description =
|
|
85
|
+
'Detects jailbreak and indirect injection attacks using Meta PromptGuard. ' +
|
|
86
|
+
'Classifies text as jailbreak, injection, or benign.';
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Default Hugging Face model ID.
|
|
90
|
+
* Overridable via {@link ClassifierConfig.modelId}.
|
|
91
|
+
*/
|
|
92
|
+
readonly modelId = 'meta-llama/PromptGuard-86M';
|
|
93
|
+
|
|
94
|
+
// -------------------------------------------------------------------------
|
|
95
|
+
// Internal state
|
|
96
|
+
// -------------------------------------------------------------------------
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Whether the model weights are fully loaded and the classifier is ready
|
|
100
|
+
* to accept `classify()` calls.
|
|
101
|
+
*/
|
|
102
|
+
private _isLoaded = false;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Set to `true` when the model fails to load. Once `unavailable`, every
|
|
106
|
+
* subsequent `classify()` call immediately returns the pass result rather
|
|
107
|
+
* than retrying the expensive model load.
|
|
108
|
+
*/
|
|
109
|
+
private unavailable = false;
|
|
110
|
+
|
|
111
|
+
// -------------------------------------------------------------------------
|
|
112
|
+
// Constructor
|
|
113
|
+
// -------------------------------------------------------------------------
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* @param services - Shared service registry used to lazily create and cache
|
|
117
|
+
* the underlying HuggingFace pipeline instance.
|
|
118
|
+
* @param config - Optional per-classifier configuration. When
|
|
119
|
+
* `config.modelId` is provided it overrides the default `modelId` when
|
|
120
|
+
* loading the model.
|
|
121
|
+
*/
|
|
122
|
+
constructor(
|
|
123
|
+
private readonly services: ISharedServiceRegistry,
|
|
124
|
+
private readonly config?: ClassifierConfig,
|
|
125
|
+
) {}
|
|
126
|
+
|
|
127
|
+
// -------------------------------------------------------------------------
|
|
128
|
+
// IContentClassifier.isLoaded (getter)
|
|
129
|
+
// -------------------------------------------------------------------------
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Whether the underlying model pipeline has been successfully initialised.
|
|
133
|
+
* The flag is set to `true` after the first successful `classify()` call.
|
|
134
|
+
*/
|
|
135
|
+
get isLoaded(): boolean {
|
|
136
|
+
return this._isLoaded;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// -------------------------------------------------------------------------
|
|
140
|
+
// classify
|
|
141
|
+
// -------------------------------------------------------------------------
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Run jailbreak inference on `text`.
|
|
145
|
+
*
|
|
146
|
+
* Lazily loads the pipeline on the first call via the shared service
|
|
147
|
+
* registry, then calls it with `{ topk: null }` to retrieve scores for all
|
|
148
|
+
* three classes.
|
|
149
|
+
*
|
|
150
|
+
* @param text - The text to evaluate.
|
|
151
|
+
* @returns A promise that resolves with the classification result. If the
|
|
152
|
+
* model is unavailable the pass result is returned instead of throwing.
|
|
153
|
+
*/
|
|
154
|
+
async classify(text: string): Promise<ClassificationResult> {
|
|
155
|
+
// Return the pass result immediately if the model previously failed to load.
|
|
156
|
+
if (this.unavailable) {
|
|
157
|
+
return this.passResult();
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Lazily obtain (or create) the HuggingFace pipeline from the shared
|
|
161
|
+
// registry — the model is only downloaded and initialised once.
|
|
162
|
+
let pipeline: (text: string, opts: { topk: null }) => Promise<RawLabel[]>;
|
|
163
|
+
try {
|
|
164
|
+
pipeline = await this.services.getOrCreate(
|
|
165
|
+
ML_CLASSIFIER_SERVICE_IDS.JAILBREAK_PIPELINE,
|
|
166
|
+
async () => {
|
|
167
|
+
// Dynamic import so the ONNX runtime is excluded from the initial
|
|
168
|
+
// bundle and environments without the package are unaffected.
|
|
169
|
+
const { pipeline: createPipeline } = await import(
|
|
170
|
+
'@huggingface/transformers'
|
|
171
|
+
);
|
|
172
|
+
return createPipeline(
|
|
173
|
+
'text-classification',
|
|
174
|
+
// Honour a caller-supplied model override; fall back to the default.
|
|
175
|
+
this.config?.modelId ?? this.modelId,
|
|
176
|
+
{ quantized: true },
|
|
177
|
+
);
|
|
178
|
+
},
|
|
179
|
+
{
|
|
180
|
+
/** Release ONNX/WASM resources when the registry entry is evicted. */
|
|
181
|
+
dispose: async (p: any) => p?.dispose?.(),
|
|
182
|
+
/** Tags used for diagnostics and capability discovery. */
|
|
183
|
+
tags: ['ml', 'classifier', 'jailbreak', 'onnx'],
|
|
184
|
+
},
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
// Mark the classifier as ready now that the pipeline is available.
|
|
188
|
+
this._isLoaded = true;
|
|
189
|
+
} catch {
|
|
190
|
+
// Model failed to load — mark as unavailable and return the pass result.
|
|
191
|
+
this.unavailable = true;
|
|
192
|
+
return this.passResult();
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Run inference and request scores for all three classes.
|
|
196
|
+
const raw = await pipeline(text, { topk: null });
|
|
197
|
+
return this.mapResult(raw);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// -------------------------------------------------------------------------
|
|
201
|
+
// dispose (optional IContentClassifier lifecycle hook)
|
|
202
|
+
// -------------------------------------------------------------------------
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Release the pipeline instance from the shared service registry.
|
|
206
|
+
*
|
|
207
|
+
* Idempotent — safe to call multiple times.
|
|
208
|
+
*/
|
|
209
|
+
async dispose(): Promise<void> {
|
|
210
|
+
await this.services.release(ML_CLASSIFIER_SERVICE_IDS.JAILBREAK_PIPELINE);
|
|
211
|
+
this._isLoaded = false;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// -------------------------------------------------------------------------
|
|
215
|
+
// Private helpers
|
|
216
|
+
// -------------------------------------------------------------------------
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Returns a "pass" result used when the model is unavailable.
|
|
220
|
+
*
|
|
221
|
+
* A pass result reports `bestClass: 'benign'` with zero confidence so the
|
|
222
|
+
* guardrail orchestrator will always choose {@link GuardrailAction.ALLOW}.
|
|
223
|
+
*/
|
|
224
|
+
private passResult(): ClassificationResult {
|
|
225
|
+
return { bestClass: 'benign', confidence: 0, allScores: [] };
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Map the raw pipeline output to a {@link ClassificationResult}.
|
|
230
|
+
*
|
|
231
|
+
* For multi-class classification the label with the highest softmax score
|
|
232
|
+
* becomes `bestClass` / `confidence`. All three labels are included in
|
|
233
|
+
* `allScores`.
|
|
234
|
+
*
|
|
235
|
+
* @param raw - Array returned by the pipeline when called with `topk: null`.
|
|
236
|
+
*/
|
|
237
|
+
private mapResult(raw: RawLabel[]): ClassificationResult {
|
|
238
|
+
if (!raw || raw.length === 0) {
|
|
239
|
+
return this.passResult();
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// Find the class with the highest probability (winner-takes-all).
|
|
243
|
+
let best = raw[0];
|
|
244
|
+
for (const item of raw) {
|
|
245
|
+
if (item.score > best.score) {
|
|
246
|
+
best = item;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
return {
|
|
251
|
+
bestClass: best.label,
|
|
252
|
+
confidence: best.score,
|
|
253
|
+
allScores: raw.map((item) => ({
|
|
254
|
+
classLabel: item.label,
|
|
255
|
+
score: item.score,
|
|
256
|
+
})),
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
}
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Toxicity content classifier using the `unitary/toxic-bert` model.
|
|
3
|
+
*
|
|
4
|
+
* This classifier uses a multi-label BERT-based model trained on the Jigsaw
|
|
5
|
+
* Toxic Comment dataset. It assigns independent confidence scores to six
|
|
6
|
+
* toxicity categories and surfaces the highest-scoring label as `bestClass`.
|
|
7
|
+
*
|
|
8
|
+
* The model is loaded lazily the first time `classify()` is called and
|
|
9
|
+
* cached in the shared service registry so it is only initialised once even
|
|
10
|
+
* if multiple parts of the system hold a reference to this classifier.
|
|
11
|
+
*
|
|
12
|
+
* Graceful degradation
|
|
13
|
+
* --------------------
|
|
14
|
+
* If the model fails to load (e.g. network unavailable, ONNX runtime missing)
|
|
15
|
+
* the classifier sets `unavailable = true` and returns a **pass result**
|
|
16
|
+
* `{ bestClass: 'benign', confidence: 0, allScores: [] }` on every subsequent
|
|
17
|
+
* call instead of throwing. This ensures the guardrail pipeline degrades
|
|
18
|
+
* gracefully rather than crashing the agent.
|
|
19
|
+
*
|
|
20
|
+
* @module agentos/extensions/packs/ml-classifiers/classifiers/ToxicityClassifier
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import type { ClassificationResult } from '@framers/agentos';
|
|
24
|
+
import type { ISharedServiceRegistry } from '@framers/agentos';
|
|
25
|
+
import type { IContentClassifier } from '../IContentClassifier';
|
|
26
|
+
import type { ClassifierConfig } from '../types';
|
|
27
|
+
import { ML_CLASSIFIER_SERVICE_IDS } from '../types';
|
|
28
|
+
|
|
29
|
+
// ---------------------------------------------------------------------------
|
|
30
|
+
// Internal raw pipeline output type
|
|
31
|
+
// ---------------------------------------------------------------------------
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* A single label/score pair as returned by the `@huggingface/transformers`
|
|
35
|
+
* text-classification pipeline when called with `{ topk: null }`.
|
|
36
|
+
*/
|
|
37
|
+
interface RawLabel {
|
|
38
|
+
/** Label name, e.g. `'toxic'`, `'insult'`. */
|
|
39
|
+
label: string;
|
|
40
|
+
/** Confidence score in the range [0, 1]. */
|
|
41
|
+
score: number;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// ---------------------------------------------------------------------------
|
|
45
|
+
// ToxicityClassifier
|
|
46
|
+
// ---------------------------------------------------------------------------
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Multi-label toxicity classifier backed by `unitary/toxic-bert`.
|
|
50
|
+
*
|
|
51
|
+
* Evaluates text against six toxicity categories:
|
|
52
|
+
* - `toxic`
|
|
53
|
+
* - `severe_toxic`
|
|
54
|
+
* - `obscene`
|
|
55
|
+
* - `threat`
|
|
56
|
+
* - `insult`
|
|
57
|
+
* - `identity_hate`
|
|
58
|
+
*
|
|
59
|
+
* Each category receives an independent confidence score. The label with
|
|
60
|
+
* the highest score is reported as `bestClass` and its score as `confidence`.
|
|
61
|
+
* All six scores are included in `allScores` so the pack orchestrator can
|
|
62
|
+
* apply per-label thresholds.
|
|
63
|
+
*
|
|
64
|
+
* @implements {IContentClassifier}
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```typescript
|
|
68
|
+
* const classifier = new ToxicityClassifier(serviceRegistry);
|
|
69
|
+
* const result = await classifier.classify('You are terrible!');
|
|
70
|
+
* // result.bestClass === 'insult', result.confidence ≈ 0.87
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
export class ToxicityClassifier implements IContentClassifier {
|
|
74
|
+
// -------------------------------------------------------------------------
|
|
75
|
+
// IContentClassifier identity fields
|
|
76
|
+
// -------------------------------------------------------------------------
|
|
77
|
+
|
|
78
|
+
/** Unique service identifier for this classifier. */
|
|
79
|
+
readonly id = 'toxicity';
|
|
80
|
+
|
|
81
|
+
/** Human-readable name for dashboards and log output. */
|
|
82
|
+
readonly displayName = 'Toxicity Classifier';
|
|
83
|
+
|
|
84
|
+
/** Short description of what this classifier detects. */
|
|
85
|
+
readonly description =
|
|
86
|
+
'Detects toxic, hateful, or abusive language across six categories: ' +
|
|
87
|
+
'toxic, severe_toxic, obscene, threat, insult, and identity_hate.';
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Default Hugging Face model ID.
|
|
91
|
+
* Overridable via {@link ClassifierConfig.modelId}.
|
|
92
|
+
*/
|
|
93
|
+
readonly modelId = 'unitary/toxic-bert';
|
|
94
|
+
|
|
95
|
+
// -------------------------------------------------------------------------
|
|
96
|
+
// Internal state
|
|
97
|
+
// -------------------------------------------------------------------------
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Whether the model weights are fully loaded and the classifier is ready
|
|
101
|
+
* to accept `classify()` calls.
|
|
102
|
+
*/
|
|
103
|
+
private _isLoaded = false;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Set to `true` when the model fails to load. Once `unavailable`, every
|
|
107
|
+
* subsequent `classify()` call immediately returns the pass result rather
|
|
108
|
+
* than retrying the expensive model load.
|
|
109
|
+
*/
|
|
110
|
+
private unavailable = false;
|
|
111
|
+
|
|
112
|
+
// -------------------------------------------------------------------------
|
|
113
|
+
// Constructor
|
|
114
|
+
// -------------------------------------------------------------------------
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* @param services - Shared service registry used to lazily create and cache
|
|
118
|
+
* the underlying HuggingFace pipeline instance.
|
|
119
|
+
* @param config - Optional per-classifier configuration. When
|
|
120
|
+
* `config.modelId` is provided it overrides the default `modelId` when
|
|
121
|
+
* loading the model.
|
|
122
|
+
*/
|
|
123
|
+
constructor(
|
|
124
|
+
private readonly services: ISharedServiceRegistry,
|
|
125
|
+
private readonly config?: ClassifierConfig,
|
|
126
|
+
) {}
|
|
127
|
+
|
|
128
|
+
// -------------------------------------------------------------------------
|
|
129
|
+
// IContentClassifier.isLoaded (getter)
|
|
130
|
+
// -------------------------------------------------------------------------
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Whether the underlying model pipeline has been successfully initialised.
|
|
134
|
+
* The flag is set to `true` after the first successful `classify()` call.
|
|
135
|
+
*/
|
|
136
|
+
get isLoaded(): boolean {
|
|
137
|
+
return this._isLoaded;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// -------------------------------------------------------------------------
|
|
141
|
+
// classify
|
|
142
|
+
// -------------------------------------------------------------------------
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Run toxicity inference on `text`.
|
|
146
|
+
*
|
|
147
|
+
* Lazily loads the pipeline on the first call via the shared service
|
|
148
|
+
* registry, then calls it with `{ topk: null }` to retrieve scores for
|
|
149
|
+
* every label.
|
|
150
|
+
*
|
|
151
|
+
* @param text - The text to evaluate.
|
|
152
|
+
* @returns A promise that resolves with the classification result. If the
|
|
153
|
+
* model is unavailable the pass result is returned instead of throwing.
|
|
154
|
+
*/
|
|
155
|
+
async classify(text: string): Promise<ClassificationResult> {
|
|
156
|
+
// Return the pass result immediately if the model previously failed to load.
|
|
157
|
+
if (this.unavailable) {
|
|
158
|
+
return this.passResult();
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Lazily obtain (or create) the HuggingFace pipeline instance from the
|
|
162
|
+
// shared service registry. The registry ensures the model is only loaded
|
|
163
|
+
// once even under concurrent calls.
|
|
164
|
+
let pipeline: (text: string, opts: { topk: null }) => Promise<RawLabel[]>;
|
|
165
|
+
try {
|
|
166
|
+
pipeline = await this.services.getOrCreate(
|
|
167
|
+
ML_CLASSIFIER_SERVICE_IDS.TOXICITY_PIPELINE,
|
|
168
|
+
async () => {
|
|
169
|
+
// Dynamic import keeps the heavy ONNX runtime out of the initial
|
|
170
|
+
// bundle and allows environments without the package to skip loading.
|
|
171
|
+
const { pipeline: createPipeline } = await import(
|
|
172
|
+
'@huggingface/transformers'
|
|
173
|
+
);
|
|
174
|
+
return createPipeline(
|
|
175
|
+
'text-classification',
|
|
176
|
+
// Honour a caller-supplied model override; fall back to the default.
|
|
177
|
+
this.config?.modelId ?? this.modelId,
|
|
178
|
+
{ quantized: true },
|
|
179
|
+
);
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
/** Release ONNX/WASM resources when the registry entry is evicted. */
|
|
183
|
+
dispose: async (p: any) => p?.dispose?.(),
|
|
184
|
+
/** Tags used for diagnostics and capability discovery. */
|
|
185
|
+
tags: ['ml', 'classifier', 'toxicity', 'onnx'],
|
|
186
|
+
},
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
// Mark the classifier as ready now that the pipeline is available.
|
|
190
|
+
this._isLoaded = true;
|
|
191
|
+
} catch {
|
|
192
|
+
// Model failed to load — mark as unavailable and return the pass result
|
|
193
|
+
// so the guardrail pipeline can continue operating.
|
|
194
|
+
this.unavailable = true;
|
|
195
|
+
return this.passResult();
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// Run inference — request scores for ALL labels (topk: null).
|
|
199
|
+
const raw = await pipeline(text, { topk: null });
|
|
200
|
+
return this.mapResult(raw);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// -------------------------------------------------------------------------
|
|
204
|
+
// dispose (optional IContentClassifier lifecycle hook)
|
|
205
|
+
// -------------------------------------------------------------------------
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Release the pipeline instance from the shared service registry.
|
|
209
|
+
*
|
|
210
|
+
* Idempotent — safe to call multiple times.
|
|
211
|
+
*/
|
|
212
|
+
async dispose(): Promise<void> {
|
|
213
|
+
await this.services.release(ML_CLASSIFIER_SERVICE_IDS.TOXICITY_PIPELINE);
|
|
214
|
+
this._isLoaded = false;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// -------------------------------------------------------------------------
|
|
218
|
+
// Private helpers
|
|
219
|
+
// -------------------------------------------------------------------------
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Returns a "pass" result used when the model is unavailable.
|
|
223
|
+
*
|
|
224
|
+
* A pass result reports `bestClass: 'benign'` with zero confidence so the
|
|
225
|
+
* guardrail orchestrator will always choose {@link GuardrailAction.ALLOW}.
|
|
226
|
+
*/
|
|
227
|
+
private passResult(): ClassificationResult {
|
|
228
|
+
return { bestClass: 'benign', confidence: 0, allScores: [] };
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Map the raw pipeline output (array of `{ label, score }` objects) to a
|
|
233
|
+
* {@link ClassificationResult}.
|
|
234
|
+
*
|
|
235
|
+
* The label with the highest score becomes `bestClass` / `confidence`.
|
|
236
|
+
* Every label is included in `allScores` for downstream threshold logic.
|
|
237
|
+
*
|
|
238
|
+
* @param raw - Array returned by the pipeline when called with `topk: null`.
|
|
239
|
+
*/
|
|
240
|
+
private mapResult(raw: RawLabel[]): ClassificationResult {
|
|
241
|
+
if (!raw || raw.length === 0) {
|
|
242
|
+
// No output from the model — treat as benign.
|
|
243
|
+
return this.passResult();
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// Find the label with the maximum confidence score.
|
|
247
|
+
let best = raw[0];
|
|
248
|
+
for (const item of raw) {
|
|
249
|
+
if (item.score > best.score) {
|
|
250
|
+
best = item;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return {
|
|
255
|
+
bestClass: best.label,
|
|
256
|
+
confidence: best.score,
|
|
257
|
+
allScores: raw.map((item) => ({
|
|
258
|
+
classLabel: item.label,
|
|
259
|
+
score: item.score,
|
|
260
|
+
})),
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
}
|