safetygates 0.1.0 → 0.1.1
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/package.json +1 -1
- package/src/index.d.ts +21 -47
- package/src/index.js +16 -68
package/package.json
CHANGED
package/src/index.d.ts
CHANGED
|
@@ -5,37 +5,34 @@
|
|
|
5
5
|
* Licensed under the MIT License
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
export interface GateResult {
|
|
9
|
-
label: boolean;
|
|
10
|
-
confidence: number;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
8
|
export interface ClassifyResult {
|
|
14
|
-
|
|
9
|
+
safe: boolean;
|
|
10
|
+
confidence: number;
|
|
11
|
+
categories: {
|
|
12
|
+
toxic: number;
|
|
13
|
+
spam: number;
|
|
14
|
+
hate: number;
|
|
15
|
+
nsfw: number;
|
|
16
|
+
harassment: number;
|
|
17
|
+
};
|
|
15
18
|
latency_us: number;
|
|
16
19
|
}
|
|
17
20
|
|
|
18
21
|
export interface BatchClassifyResult {
|
|
19
|
-
results: Array<
|
|
22
|
+
results: Array<{
|
|
23
|
+
toxic: number;
|
|
24
|
+
spam: number;
|
|
25
|
+
hate: number;
|
|
26
|
+
nsfw: number;
|
|
27
|
+
harassment: number;
|
|
28
|
+
}>;
|
|
20
29
|
total_latency_us: number;
|
|
21
30
|
items_per_second: number;
|
|
22
31
|
}
|
|
23
32
|
|
|
24
|
-
export interface GateInfo {
|
|
25
|
-
id: string;
|
|
26
|
-
category: string;
|
|
27
|
-
description: string;
|
|
28
|
-
threshold: number;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export interface GatesListResult {
|
|
32
|
-
gates: GateInfo[];
|
|
33
|
-
}
|
|
34
|
-
|
|
35
33
|
export interface HealthResult {
|
|
36
34
|
status: string;
|
|
37
35
|
gates_loaded: number;
|
|
38
|
-
available_gates: string[];
|
|
39
36
|
}
|
|
40
37
|
|
|
41
38
|
export interface SafetyGatesOptions {
|
|
@@ -43,19 +40,6 @@ export interface SafetyGatesOptions {
|
|
|
43
40
|
timeout?: number;
|
|
44
41
|
}
|
|
45
42
|
|
|
46
|
-
export declare const GATES: {
|
|
47
|
-
TOXIC: 'toxic';
|
|
48
|
-
SPAM: 'spam';
|
|
49
|
-
HATE: 'hate';
|
|
50
|
-
NSFW: 'nsfw';
|
|
51
|
-
HARASSMENT: 'harassment';
|
|
52
|
-
TOXIC_ES: 'toxic_es';
|
|
53
|
-
SPAM_ES: 'spam_es';
|
|
54
|
-
HATE_ES: 'hate_es';
|
|
55
|
-
TOXIC_PT: 'toxic_pt';
|
|
56
|
-
TOXIC_FR: 'toxic_fr';
|
|
57
|
-
};
|
|
58
|
-
|
|
59
43
|
export declare class SafetyGatesError extends Error {
|
|
60
44
|
statusCode: number;
|
|
61
45
|
detail: any;
|
|
@@ -66,29 +50,19 @@ export declare class SafetyGates {
|
|
|
66
50
|
constructor(apiKey: string, options?: SafetyGatesOptions);
|
|
67
51
|
|
|
68
52
|
/**
|
|
69
|
-
* Classify
|
|
53
|
+
* Classify text for safety
|
|
70
54
|
*/
|
|
71
|
-
classify(text: string
|
|
55
|
+
classify(text: string): Promise<ClassifyResult>;
|
|
72
56
|
|
|
73
57
|
/**
|
|
74
58
|
* Classify multiple texts in batch
|
|
75
59
|
*/
|
|
76
|
-
classifyBatch(texts: string[]
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Check if text is toxic (convenience method)
|
|
80
|
-
*/
|
|
81
|
-
isToxic(text: string, threshold?: number): Promise<boolean>;
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Check if text is spam (convenience method)
|
|
85
|
-
*/
|
|
86
|
-
isSpam(text: string, threshold?: number): Promise<boolean>;
|
|
60
|
+
classifyBatch(texts: string[]): Promise<BatchClassifyResult>;
|
|
87
61
|
|
|
88
62
|
/**
|
|
89
|
-
*
|
|
63
|
+
* Check if text is safe (convenience method)
|
|
90
64
|
*/
|
|
91
|
-
|
|
65
|
+
isSafe(text: string): Promise<boolean>;
|
|
92
66
|
|
|
93
67
|
/**
|
|
94
68
|
* Health check
|
package/src/index.js
CHANGED
|
@@ -20,26 +20,6 @@ const http = require('http');
|
|
|
20
20
|
const DEFAULT_BASE_URL = 'https://sg-api.cyclecore.ai';
|
|
21
21
|
const DEFAULT_TIMEOUT = 10000;
|
|
22
22
|
|
|
23
|
-
/**
|
|
24
|
-
* Available classification gates
|
|
25
|
-
*/
|
|
26
|
-
const GATES = {
|
|
27
|
-
// English
|
|
28
|
-
TOXIC: 'toxic',
|
|
29
|
-
SPAM: 'spam',
|
|
30
|
-
HATE: 'hate',
|
|
31
|
-
NSFW: 'nsfw',
|
|
32
|
-
HARASSMENT: 'harassment',
|
|
33
|
-
// Spanish
|
|
34
|
-
TOXIC_ES: 'toxic_es',
|
|
35
|
-
SPAM_ES: 'spam_es',
|
|
36
|
-
HATE_ES: 'hate_es',
|
|
37
|
-
// Portuguese
|
|
38
|
-
TOXIC_PT: 'toxic_pt',
|
|
39
|
-
// French
|
|
40
|
-
TOXIC_FR: 'toxic_fr',
|
|
41
|
-
};
|
|
42
|
-
|
|
43
23
|
class SafetyGatesError extends Error {
|
|
44
24
|
constructor(message, statusCode, detail) {
|
|
45
25
|
super(message);
|
|
@@ -129,87 +109,56 @@ class SafetyGates {
|
|
|
129
109
|
/**
|
|
130
110
|
* Classify a single text
|
|
131
111
|
* @param {string} text - Text to classify
|
|
132
|
-
* @param {string[]} gates - Gates to check (default: ['toxic'])
|
|
133
112
|
* @returns {Promise<ClassifyResult>}
|
|
134
113
|
*
|
|
135
114
|
* @example
|
|
136
|
-
* const result = await client.classify('you suck'
|
|
137
|
-
* console.log(result.
|
|
138
|
-
* console.log(result.
|
|
115
|
+
* const result = await client.classify('you suck');
|
|
116
|
+
* console.log(result.safe); // false
|
|
117
|
+
* console.log(result.confidence); // 0.92
|
|
139
118
|
*/
|
|
140
|
-
async classify(text
|
|
119
|
+
async classify(text) {
|
|
141
120
|
if (!text || typeof text !== 'string') {
|
|
142
121
|
throw new Error('Text must be a non-empty string');
|
|
143
122
|
}
|
|
144
|
-
if (!Array.isArray(gates) || gates.length === 0) {
|
|
145
|
-
throw new Error('Gates must be a non-empty array');
|
|
146
|
-
}
|
|
147
123
|
|
|
148
|
-
return this._request('POST', '/v1/classify', { text
|
|
124
|
+
return this._request('POST', '/v1/classify', { text });
|
|
149
125
|
}
|
|
150
126
|
|
|
151
127
|
/**
|
|
152
128
|
* Classify multiple texts in batch
|
|
153
129
|
* @param {string[]} texts - Array of texts to classify (max 10,000)
|
|
154
|
-
* @param {string[]} gates - Gates to check (default: ['toxic'])
|
|
155
130
|
* @returns {Promise<BatchClassifyResult>}
|
|
156
131
|
*
|
|
157
132
|
* @example
|
|
158
|
-
* const result = await client.classifyBatch(
|
|
159
|
-
* ['hello', 'you suck', 'nice game'],
|
|
160
|
-
* ['toxic']
|
|
161
|
-
* );
|
|
133
|
+
* const result = await client.classifyBatch(['hello', 'you suck', 'nice game']);
|
|
162
134
|
* result.results.forEach((r, i) => {
|
|
163
|
-
* console.log(`Text ${i}:
|
|
135
|
+
* console.log(`Text ${i}: safe=${r.safe}`);
|
|
164
136
|
* });
|
|
165
137
|
*/
|
|
166
|
-
async classifyBatch(texts
|
|
138
|
+
async classifyBatch(texts) {
|
|
167
139
|
if (!Array.isArray(texts) || texts.length === 0) {
|
|
168
140
|
throw new Error('Texts must be a non-empty array');
|
|
169
141
|
}
|
|
170
142
|
if (texts.length > 10000) {
|
|
171
143
|
throw new Error('Batch size cannot exceed 10,000');
|
|
172
144
|
}
|
|
173
|
-
if (!Array.isArray(gates) || gates.length === 0) {
|
|
174
|
-
throw new Error('Gates must be a non-empty array');
|
|
175
|
-
}
|
|
176
145
|
|
|
177
|
-
return this._request('POST', '/v1/classify/batch', { texts
|
|
146
|
+
return this._request('POST', '/v1/classify/batch', { texts });
|
|
178
147
|
}
|
|
179
148
|
|
|
180
149
|
/**
|
|
181
|
-
* Check if text is
|
|
150
|
+
* Check if text is safe (convenience method)
|
|
182
151
|
* @param {string} text - Text to check
|
|
183
|
-
* @param {number} threshold - Confidence threshold (default: 0.5)
|
|
184
152
|
* @returns {Promise<boolean>}
|
|
185
153
|
*
|
|
186
154
|
* @example
|
|
187
|
-
* if (await client.
|
|
188
|
-
* console.log('
|
|
155
|
+
* if (await client.isSafe('hello world')) {
|
|
156
|
+
* console.log('Content is safe');
|
|
189
157
|
* }
|
|
190
158
|
*/
|
|
191
|
-
async
|
|
192
|
-
const result = await this.classify(text
|
|
193
|
-
return result.
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* Check if text is spam (convenience method)
|
|
198
|
-
* @param {string} text - Text to check
|
|
199
|
-
* @param {number} threshold - Confidence threshold (default: 0.5)
|
|
200
|
-
* @returns {Promise<boolean>}
|
|
201
|
-
*/
|
|
202
|
-
async isSpam(text, threshold = 0.5) {
|
|
203
|
-
const result = await this.classify(text, ['spam']);
|
|
204
|
-
return result.results.spam.confidence >= threshold;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* Get available gates
|
|
209
|
-
* @returns {Promise<GatesListResult>}
|
|
210
|
-
*/
|
|
211
|
-
async listGates() {
|
|
212
|
-
return this._request('GET', '/v1/gates');
|
|
159
|
+
async isSafe(text) {
|
|
160
|
+
const result = await this.classify(text);
|
|
161
|
+
return result.safe;
|
|
213
162
|
}
|
|
214
163
|
|
|
215
164
|
/**
|
|
@@ -221,8 +170,7 @@ class SafetyGates {
|
|
|
221
170
|
}
|
|
222
171
|
}
|
|
223
172
|
|
|
224
|
-
// Export class
|
|
173
|
+
// Export class
|
|
225
174
|
module.exports = SafetyGates;
|
|
226
175
|
module.exports.SafetyGates = SafetyGates;
|
|
227
176
|
module.exports.SafetyGatesError = SafetyGatesError;
|
|
228
|
-
module.exports.GATES = GATES;
|