-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
395 lines (358 loc) Β· 12.6 KB
/
index.js
File metadata and controls
395 lines (358 loc) Β· 12.6 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#!/usr/bin/env node
/**
* Rahyana AI API Examples - Main Entry Point
*
* This is the main entry point for all Rahyana AI API examples.
* Run this file to see all available examples and their descriptions.
*
* Usage:
* node index.js # Show all examples
* node index.js --help # Show help
* node index.js --list # List all examples
* node index.js --run <example> # Run specific example
* node index.js --test # Run all tests
* node index.js --performance # Run performance tests
*/
import { readFileSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Colors for console output
const colors = {
reset: '\x1b[0m',
bright: '\x1b[1m',
dim: '\x1b[2m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
magenta: '\x1b[35m',
cyan: '\x1b[36m',
white: '\x1b[37m'
};
// Example categories
const examples = {
'Basic Examples': {
description: 'Fundamental API usage examples',
examples: [
{
name: 'Basic Chat',
file: 'chat-completions/basic-chat.js',
description: 'Simple text chat with AI',
keywords: ['chat', 'text', 'basic']
},
{
name: 'Streaming Chat',
file: 'chat-completions/streaming-chat.js',
description: 'Real-time streaming responses',
keywords: ['streaming', 'realtime', 'chat']
},
{
name: 'Image Analysis',
file: 'chat-completions/image-analysis.js',
description: 'Analyze images with AI',
keywords: ['image', 'vision', 'analysis']
},
{
name: 'Audio Processing',
file: 'chat-completions/audio-processing.js',
description: 'Process audio files with AI',
keywords: ['audio', 'speech', 'processing']
},
{
name: 'PDF Processing',
file: 'chat-completions/pdf-processing.js',
description: 'Extract and analyze PDF content',
keywords: ['pdf', 'document', 'extraction']
},
{
name: 'Web Search',
file: 'chat-completions/web-search.js',
description: 'Search the web with AI',
keywords: ['web', 'search', 'internet']
},
{
name: 'Tool Calling',
file: 'chat-completions/tool-calling.js',
description: 'Use AI with external tools',
keywords: ['tools', 'functions', 'integration']
},
{
name: 'JSON Mode',
file: 'chat-completions/json-mode.js',
description: 'Get structured JSON responses',
keywords: ['json', 'structured', 'data']
}
]
},
'Legacy Examples': {
description: 'Legacy API endpoints',
examples: [
{
name: 'Legacy Completions',
file: 'completions/legacy-completions.js',
description: 'Text completion using legacy endpoint',
keywords: ['legacy', 'completion', 'text']
}
]
},
'Model Management': {
description: 'Model listing and management',
examples: [
{
name: 'Get Models',
file: 'models/get-models.js',
description: 'List available AI models',
keywords: ['models', 'list', 'available']
}
]
},
'Real-World Projects': {
description: 'Complete production-ready projects',
examples: [
{
name: 'AI Content Generator',
file: 'real-world-projects/ai-content-generator.js',
description: 'Complete content generation system',
keywords: ['content', 'generation', 'marketing', 'blog', 'social']
},
{
name: 'AI Chatbot',
file: 'real-world-projects/ai-chatbot.js',
description: 'Advanced conversational AI system',
keywords: ['chatbot', 'conversation', 'support', 'sales']
},
{
name: 'AI Code Assistant',
file: 'real-world-projects/ai-code-assistant.js',
description: 'AI-powered coding assistant',
keywords: ['code', 'programming', 'assistant', 'development']
}
]
},
'Advanced Examples (2025)': {
description: 'Cutting-edge AI applications for 2025',
examples: [
{
name: 'AI Voice Assistant',
file: 'advanced-examples/ai-voice-assistant.js',
description: 'Voice recognition and synthesis system',
keywords: ['voice', 'speech', 'assistant', 'audio']
},
{
name: 'AI Data Analyzer',
file: 'advanced-examples/ai-data-analyzer.js',
description: 'Advanced data analysis and visualization',
keywords: ['data', 'analysis', 'visualization', 'insights']
},
{
name: 'AI Automation Suite',
file: 'advanced-examples/ai-automation-suite.js',
description: 'Complete automation and workflow system',
keywords: ['automation', 'workflow', 'tasks', 'scheduling']
},
{
name: 'AI Web3 Integration',
file: 'advanced-examples/ai-web3-integration.js',
description: 'AI integration with Web3 and blockchain',
keywords: ['web3', 'blockchain', 'nft', 'defi', 'crypto']
}
]
}
};
// Display banner
function displayBanner() {
console.log(colors.cyan + colors.bright);
console.log('ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ');
console.log('β Rahyana AI API Examples β');
console.log('β JavaScript 2025 β');
console.log('ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ');
console.log(colors.reset);
console.log(colors.dim + 'Complete JavaScript examples for Rahyana AI API integration' + colors.reset);
console.log(colors.dim + 'Built for 2025 with cutting-edge AI capabilities' + colors.reset);
console.log();
}
// Display help
function displayHelp() {
console.log(colors.bright + 'Usage:' + colors.reset);
console.log(' node index.js [options]');
console.log();
console.log(colors.bright + 'Options:' + colors.reset);
console.log(' --help, -h Show this help message');
console.log(' --list, -l List all available examples');
console.log(' --run <example> Run a specific example');
console.log(' --test, -t Run all tests');
console.log(' --performance, -p Run performance tests');
console.log(' --version, -v Show version information');
console.log();
console.log(colors.bright + 'Examples:' + colors.reset);
console.log(' node index.js --run "Basic Chat"');
console.log(' node index.js --run "AI Content Generator"');
console.log(' node index.js --test');
console.log(' node index.js --performance');
console.log();
}
// Display all examples
function displayExamples() {
displayBanner();
Object.entries(examples).forEach(([category, info]) => {
console.log(colors.bright + colors.blue + `π ${category}` + colors.reset);
console.log(colors.dim + ` ${info.description}` + colors.reset);
console.log();
info.examples.forEach((example, index) => {
console.log(colors.green + ` ${index + 1}. ${example.name}` + colors.reset);
console.log(colors.dim + ` ${example.description}` + colors.reset);
console.log(colors.dim + ` File: ${example.file}` + colors.reset);
console.log(colors.dim + ` Keywords: ${example.keywords.join(', ')}` + colors.reset);
console.log();
});
});
console.log(colors.bright + 'π Quick Start:' + colors.reset);
console.log('1. Set your API key: export RAHYANA_API_KEY="your_api_key_here"');
console.log('2. Run an example: node index.js --run "Basic Chat"');
console.log('3. Explore real-world projects: node index.js --run "AI Content Generator"');
console.log();
console.log(colors.bright + 'π Documentation:' + colors.reset);
console.log(' https://rahyana.ir/docs');
console.log(' https://github.com/rahyana/examples');
console.log();
}
// List examples
function listExamples() {
console.log(colors.bright + 'Available Examples:' + colors.reset);
console.log();
Object.entries(examples).forEach(([category, info]) => {
console.log(colors.blue + category + colors.reset);
info.examples.forEach(example => {
console.log(` β’ ${example.name} (${example.file})`);
});
console.log();
});
}
// Run specific example
async function runExample(exampleName) {
const example = findExample(exampleName);
if (!example) {
console.log(colors.red + `β Example "${exampleName}" not found` + colors.reset);
console.log('Use --list to see available examples');
return;
}
console.log(colors.bright + `π Running: ${example.name}` + colors.reset);
console.log(colors.dim + example.description + colors.reset);
console.log();
try {
const modulePath = join(__dirname, example.file);
const module = await import(modulePath);
if (module.default) {
await module.default();
} else if (typeof module === 'function') {
await module();
} else {
console.log(colors.yellow + 'β οΈ Example does not export a runnable function' + colors.reset);
}
} catch (error) {
console.log(colors.red + `β Error running example: ${error.message}` + colors.reset);
console.log(colors.dim + error.stack + colors.reset);
}
}
// Find example by name
function findExample(name) {
for (const category of Object.values(examples)) {
const example = category.examples.find(ex =>
ex.name.toLowerCase().includes(name.toLowerCase())
);
if (example) return example;
}
return null;
}
// Run tests
async function runTests() {
console.log(colors.bright + 'π§ͺ Running Tests...' + colors.reset);
console.log();
try {
const { exec } = await import('child_process');
const { promisify } = await import('util');
const execAsync = promisify(exec);
const { stdout, stderr } = await execAsync('npm test');
console.log(stdout);
if (stderr) console.log(stderr);
} catch (error) {
console.log(colors.red + `β Test error: ${error.message}` + colors.reset);
}
}
// Run performance tests
async function runPerformanceTests() {
console.log(colors.bright + 'β‘ Running Performance Tests...' + colors.reset);
console.log();
try {
const { exec } = await import('child_process');
const { promisify } = await import('util');
const execAsync = promisify(exec);
const { stdout, stderr } = await execAsync('npm run test:performance');
console.log(stdout);
if (stderr) console.log(stderr);
} catch (error) {
console.log(colors.red + `β Performance test error: ${error.message}` + colors.reset);
}
}
// Show version
function showVersion() {
try {
const packageJson = JSON.parse(readFileSync(join(__dirname, 'package.json'), 'utf8'));
console.log(colors.bright + `Rahyana AI API Examples v${packageJson.version}` + colors.reset);
console.log(colors.dim + `Node.js ${process.version}` + colors.reset);
} catch (error) {
console.log(colors.red + 'β Could not read version information' + colors.reset);
}
}
// Main function
async function main() {
const args = process.argv.slice(2);
if (args.length === 0) {
displayExamples();
return;
}
const command = args[0];
switch (command) {
case '--help':
case '-h':
displayHelp();
break;
case '--list':
case '-l':
listExamples();
break;
case '--run':
if (args.length < 2) {
console.log(colors.red + 'β Please specify an example name' + colors.reset);
console.log('Use --list to see available examples');
return;
}
await runExample(args[1]);
break;
case '--test':
case '-t':
await runTests();
break;
case '--performance':
case '-p':
await runPerformanceTests();
break;
case '--version':
case '-v':
showVersion();
break;
default:
console.log(colors.red + `β Unknown command: ${command}` + colors.reset);
console.log('Use --help to see available commands');
}
}
// Run main function
if (import.meta.url === `file://${process.argv[1]}`) {
main().catch(error => {
console.error(colors.red + 'β Fatal error:', error.message + colors.reset);
process.exit(1);
});
}
export { examples, displayExamples, runExample, runTests };