-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-usage.ts
More file actions
388 lines (336 loc) · 11.8 KB
/
basic-usage.ts
File metadata and controls
388 lines (336 loc) · 11.8 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
/**
* Basic Usage Examples for HelpingAI JavaScript SDK
*
* This example demonstrates the fundamental features of the HelpingAI SDK:
* - Client initialization
* - Simple chat completions
* - Parameter configuration
* - Response handling
* - Error handling
*/
import { HelpingAI } from '../src';
/**
* Example 1: Simple Chat Completion
*/
async function basicChatCompletion(): Promise<void> {
console.log('=== Example 1: Basic Chat Completion ===');
// Initialize the client
const client = new HelpingAI({
apiKey: 'your-api-key', // Replace with your actual API key
});
try {
const response = await client.chat.completions.create({
model: 'Dhanishtha-2.0-preview',
messages: [
{ role: 'system', content: 'You are an expert in emotional intelligence.' },
{ role: 'user', content: 'What makes a good leader?' },
],
});
if ('choices' in response) {
console.log('Response:', response.choices[0].message.content);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
console.error('Error:', error.message || error);
} finally {
await client.cleanup();
}
}
/**
* Example 2: Chat Completion with Advanced Parameters
*/
async function advancedChatCompletion(): Promise<void> {
console.log('\n=== Example 2: Advanced Parameters ===');
const client = new HelpingAI({
apiKey: 'your-api-key',
});
try {
const response = await client.chat.completions.create({
model: 'Dhanishtha-2.0-preview',
messages: [{ role: 'user', content: 'Write a story about empathy' }],
temperature: 0.7, // Controls randomness (0-1)
max_tokens: 500, // Maximum length of response
top_p: 0.9, // Nucleus sampling parameter
frequency_penalty: 0.3, // Reduces repetition
presence_penalty: 0.3, // Encourages new topics
hide_think: true, // Filter out reasoning blocks
});
if ('choices' in response) {
console.log('Story:', response.choices[0].message.content);
console.log('\nUsage:', response.usage);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
console.error('Error:', error.message || error);
} finally {
await client.cleanup();
}
}
/**
* Example 3: Multiple Messages Conversation
*/
async function conversationExample(): Promise<void> {
console.log('\n=== Example 3: Conversation ===');
const client = new HelpingAI({
apiKey: 'your-api-key',
});
// Build a conversation
const messages: Array<{
role: 'system' | 'user' | 'assistant' | 'tool';
content: string;
}> = [
{ role: 'system', content: 'You are a helpful assistant with emotional intelligence.' },
{ role: 'user', content: "I'm feeling stressed about work." },
];
try {
// First response
const response1 = await client.chat.completions.create({
model: 'Dhanishtha-2.0-preview',
messages,
});
if ('choices' in response1) {
const assistantMessage = response1.choices[0].message.content;
console.log('Assistant:', assistantMessage);
// Add assistant's response to conversation
messages.push({ role: 'assistant', content: assistantMessage || '' });
// User follow-up
messages.push({ role: 'user', content: 'What specific techniques can help?' });
// Second response
const response2 = await client.chat.completions.create({
model: 'Dhanishtha-2.0-preview',
messages,
});
if ('choices' in response2) {
console.log('Assistant:', response2.choices[0].message.content);
}
}
} catch (error: any) {
console.error('Error:', error.message || error);
} finally {
await client.cleanup();
}
}
/**
* Example 4: Different Models
*/
async function differentModelsExample(): Promise<void> {
console.log('\n=== Example 4: Different Models ===');
const client = new HelpingAI({
apiKey: 'your-api-key',
});
const prompt = 'Explain the concept of emotional intelligence in simple terms.';
try {
// Using Dhanishtha-2.0-preview (full reasoning model)
console.log('Using Dhanishtha-2.0-preview:');
const response1 = await client.chat.completions.create({
model: 'Dhanishtha-2.0-preview',
messages: [{ role: 'user', content: prompt }],
hide_think: false, // Show reasoning process
});
if ('choices' in response1) {
console.log('Response:', response1.choices[0].message.content);
}
// Using Dhanishtha-2.0-preview-mini (lightweight version)
console.log('\nUsing Dhanishtha-2.0-preview-mini:');
const response2 = await client.chat.completions.create({
model: 'Dhanishtha-2.0-preview-mini',
messages: [{ role: 'user', content: prompt }],
});
if ('choices' in response2) {
console.log('Response:', response2.choices[0].message.content);
}
} catch (error: any) {
console.error('Error:', error.message || error);
} finally {
await client.cleanup();
}
}
/**
* Example 5: Client Configuration Options
*/
async function clientConfigurationExample(): Promise<void> {
console.log('\n=== Example 5: Client Configuration ===');
// Advanced client configuration
const client = new HelpingAI({
apiKey: 'your-api-key',
baseURL: 'https://api.helpingai.co/v1', // Custom base URL
timeout: 30000, // Request timeout (30 seconds)
organization: 'your-org-id', // Organization ID
defaultHeaders: {
// Custom headers
'X-Custom-Header': 'custom-value',
},
});
try {
const response = await client.chat.completions.create({
model: 'Dhanishtha-2.0-preview',
messages: [{ role: 'user', content: 'Hello, how are you?' }],
});
if ('choices' in response) {
console.log('Response:', response.choices[0].message.content);
}
} catch (error: any) {
console.error('Error:', error.message || error);
} finally {
await client.cleanup();
}
}
/**
* Example 6: Error Handling Best Practices
*/
async function errorHandlingExample(): Promise<void> {
console.log('\n=== Example 6: Error Handling ===');
const client = new HelpingAI({
apiKey: 'invalid-api-key', // Intentionally invalid for demonstration
});
try {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _response = await client.chat.completions.create({
model: 'Dhanishtha-2.0-preview',
messages: [{ role: 'user', content: 'This will fail due to invalid API key' }],
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
// Handle different types of errors
if (error.name === 'AuthenticationError') {
console.log('❌ Authentication failed - check your API key');
} else if (error.name === 'RateLimitError') {
console.log('❌ Rate limit exceeded - please wait before retrying');
console.log(` Retry after: ${error.retryAfter} seconds`);
} else if (error.name === 'InvalidRequestError') {
console.log('❌ Invalid request:', error.message);
} else if (error.name === 'TimeoutError') {
console.log('❌ Request timed out - try again later');
} else {
console.log('❌ Unexpected error:', error.message || error);
}
} finally {
await client.cleanup();
}
}
/**
* Example 7: Working with Models API
*/
async function modelsAPIExample(): Promise<void> {
console.log('\n=== Example 7: Models API ===');
const client = new HelpingAI({
apiKey: 'your-api-key',
});
try {
// List all available models
const models = await client.models.list();
console.log('Available models:');
models.data.forEach(model => {
console.log(`- ${model.id}: ${model.description || 'No description'}`);
});
// Get specific model information
const model = await client.models.retrieve('Dhanishtha-2.0-preview');
console.log('\nModel details:');
console.log(`Name: ${model.name || model.id}`);
console.log(`Owner: ${model.owned_by}`);
console.log(`Created: ${new Date(model.created * 1000).toISOString()}`);
} catch (error: any) {
console.error('Error:', error.message || error);
} finally {
await client.cleanup();
}
}
/**
* Example 8: Environment-based Configuration
*/
async function environmentConfigExample(): Promise<void> {
console.log('\n=== Example 8: Environment Configuration ===');
// The client will automatically use HAI_API_KEY environment variable if available
const client = new HelpingAI(); // No explicit API key needed if env var is set
try {
const response = await client.chat.completions.create({
model: 'Dhanishtha-2.0-preview',
messages: [{ role: 'user', content: 'Test with environment configuration' }],
});
if ('choices' in response) {
console.log('✅ Environment configuration working');
console.log('Response:', response.choices[0].message.content);
}
} catch (error: any) {
console.log('❌ Environment configuration failed:', error.message);
console.log('💡 Set HAI_API_KEY environment variable or pass apiKey in constructor');
} finally {
await client.cleanup();
}
}
/**
* Example 9: Response Inspection
*/
async function responseInspectionExample(): Promise<void> {
console.log('\n=== Example 9: Response Inspection ===');
const client = new HelpingAI({
apiKey: 'your-api-key',
});
try {
const response = await client.chat.completions.create({
model: 'Dhanishtha-2.0-preview',
messages: [{ role: 'user', content: 'Tell me about artificial intelligence' }],
});
if ('choices' in response) {
console.log('Response Details:');
console.log(`- ID: ${response.id}`);
console.log(`- Model: ${response.model}`);
console.log(`- Created: ${new Date(response.created * 1000).toISOString()}`);
console.log(`- Choices: ${response.choices.length}`);
console.log(`- Finish Reason: ${response.choices[0].finish_reason}`);
if (response.usage) {
console.log('Token Usage:');
console.log(`- Prompt tokens: ${response.usage.prompt_tokens}`);
console.log(`- Completion tokens: ${response.usage.completion_tokens}`);
console.log(`- Total tokens: ${response.usage.total_tokens}`);
}
console.log('\nContent:', response.choices[0].message.content);
}
} catch (error: any) {
console.error('Error:', error.message || error);
} finally {
await client.cleanup();
}
}
// Main execution function
async function main(): Promise<void> {
console.log('🚀 HelpingAI JavaScript SDK - Basic Usage Examples\n');
console.log('⚠️ Remember to replace "your-api-key" with your actual API key');
console.log(' Get your API key from: https://helpingai.co/dashboard\n');
try {
await basicChatCompletion();
await advancedChatCompletion();
await conversationExample();
await differentModelsExample();
await clientConfigurationExample();
await errorHandlingExample();
await modelsAPIExample();
await environmentConfigExample();
await responseInspectionExample();
console.log('\n✅ All basic usage examples completed!');
console.log('📚 Next steps:');
console.log(' - Check out streaming.ts for streaming examples');
console.log(' - See tool-calling.ts for tool usage');
console.log(' - Explore mcp-integration.ts for MCP servers');
} catch (error) {
console.error('Error in main:', error);
}
}
// Run the example if this file is executed directly
declare const require: any;
declare const module: any;
if (typeof require !== 'undefined' && typeof module !== 'undefined' && require.main === module) {
main().catch(console.error);
}
// Export functions for use in other modules
export {
basicChatCompletion,
advancedChatCompletion,
conversationExample,
differentModelsExample,
clientConfigurationExample,
errorHandlingExample,
modelsAPIExample,
environmentConfigExample,
responseInspectionExample,
};