-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
84 lines (71 loc) · 2.76 KB
/
example.js
File metadata and controls
84 lines (71 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const { ToxBlock } = require('./dist/index.js');
async function example() {
// Initialize ToxBlock with your Gemini API key
const toxBlock = new ToxBlock({
apiKey: process.env.GEMINI_API_KEY || 'your-gemini-api-key-here'
});
console.log('🛡️ ToxBlock Example Usage\n');
try {
// Example 1: Check clean text
console.log('📝 Example 1: Clean Text');
const cleanResult = await toxBlock.checkText('Hello, how are you today?');
console.log('Text:', 'Hello, how are you today?');
console.log('Is Profane:', cleanResult.isProfane);
console.log('Confidence:', cleanResult.confidence);
console.log('Language:', cleanResult.language);
console.log('');
// Example 2: Check multiple texts
console.log('📝 Example 2: Multiple Texts');
const texts = [
'Good morning everyone!',
'Have a wonderful day!',
'Programming is awesome!'
];
const results = await toxBlock.checkTexts(texts);
results.forEach((result, index) => {
console.log(`Text ${index + 1}: "${texts[index]}"`);
console.log(` - Is Profane: ${result.isProfane}`);
console.log(` - Confidence: ${result.confidence}`);
console.log(` - Language: ${result.language || 'unknown'}`);
});
console.log('');
// Example 3: Multilingual support
console.log('🌍 Example 3: Multilingual Support');
const multilingualTexts = [
'Hola, ¿cómo estás?', // Spanish
'Bonjour, comment allez-vous?', // French
'Guten Tag, wie geht es Ihnen?', // German
'こんにちは、元気ですか?' // Japanese
];
for (const text of multilingualTexts) {
const result = await toxBlock.checkText(text);
console.log(`Text: "${text}"`);
console.log(` - Is Profane: ${result.isProfane}`);
console.log(` - Confidence: ${result.confidence}`);
console.log(` - Language: ${result.language || 'unknown'}`);
console.log('');
}
// Example 4: Configuration
console.log('⚙️ Example 4: Configuration');
const config = toxBlock.getConfig();
console.log('Current Configuration:');
console.log(` - Model: ${config.model}`);
console.log(` - Timeout: ${config.timeout}ms`);
console.log('');
console.log('✅ All examples completed successfully!');
} catch (error) {
console.error('❌ Error occurred:', error.message);
if (error.code) {
console.error('Error Code:', error.code);
}
if (error.message.includes('API key')) {
console.log('\n💡 Tip: Make sure to set your GEMINI_API_KEY environment variable');
console.log(' or replace "your-gemini-api-key-here" with your actual API key.');
}
}
}
// Run the example
if (require.main === module) {
example().catch(console.error);
}
module.exports = { example };