forked from deepset-ai/haystack-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathazure-openai-basic.ts
More file actions
56 lines (47 loc) · 1.88 KB
/
Copy pathazure-openai-basic.ts
File metadata and controls
56 lines (47 loc) · 1.88 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
/**
* Azure OpenAI Basic Example
*
* Demonstrates basic usage of TealAzureOpenAI client with Azure-specific configuration
*/
import { TealAzureOpenAI } from 'tealtiger';
async function main() {
// Initialize TealAzureOpenAI client with Azure-specific configuration
const client = new TealAzureOpenAI({
apiKey: process.env.AZURE_OPENAI_API_KEY || 'your-azure-api-key',
endpoint: process.env.AZURE_OPENAI_ENDPOINT || 'https://your-resource.openai.azure.com',
deployment: 'gpt-4', // Your Azure deployment name
apiVersion: '2024-02-15-preview', // Azure API version
agentId: 'azure-demo-agent',
});
console.log('🔷 TealTiger Azure OpenAI Basic Example\n');
try {
// Simple chat completion
console.log('📝 Sending chat completion request...');
const response = await client.chat.completions.create({
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'What are the benefits of using Azure OpenAI?' },
],
max_tokens: 150,
});
console.log('\n✅ Response received:');
console.log(response.choices[0].message.content);
// Display Azure-specific metadata
if (response.security) {
console.log('\n🔒 Security Metadata:');
if (response.security.costRecord) {
console.log(` 💰 Cost: $${response.security.costRecord.actualCost.toFixed(6)}`);
console.log(` 📊 Tokens: ${response.security.costRecord.actualTokens.totalTokens}`);
}
}
// Display Azure configuration
const config = client.getConfig();
console.log('\n⚙️ Azure Configuration:');
console.log(` Endpoint: ${config.endpoint}`);
console.log(` Deployment: ${config.deployment}`);
console.log(` API Version: ${config.apiVersion}`);
} catch (error) {
console.error('❌ Error:', error instanceof Error ? error.message : error);
}
}
main();