-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexample.js
29 lines (24 loc) · 1.12 KB
/
example.js
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
import process from 'node:process';
import { DefaultAzureCredential, getBearerTokenProvider } from '@azure/identity';
import { AzureOpenAI } from 'openai';
import 'dotenv/config';
if (!process.env.AZURE_OPENAI_ENDPOINT) {
throw new Error('Azure OpenAI env variables not set. See README for details.');
}
// Use the current user identity to authenticate.
// No secrets needed, it uses `az login` or `azd auth login` locally,
// and managed identity when deployed on Azure.
const credentials = new DefaultAzureCredential();
const azureOpenAiScope = 'https://cognitiveservices.azure.com/.default';
const azureADTokenProvider = getBearerTokenProvider(credentials, azureOpenAiScope);
const openai = new AzureOpenAI({ azureADTokenProvider });
const response = await openai.chat.completions.create({
messages: [
{ role: 'system', content: `You provide brief and straight to the point answers ending with emojis.` },
{ role: 'user', content: `Write an haiku about a hungry cat` },
],
temperature: 0.7,
model: process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME,
});
console.log('Response:');
console.log(response.choices[0].message.content);