-
Notifications
You must be signed in to change notification settings - Fork 760
/
azure.ts
executable file
·43 lines (35 loc) · 1.35 KB
/
azure.ts
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
#!/usr/bin/env -S npm run tsn -T
import { AzureOpenAI } from 'openai';
import { getBearerTokenProvider, DefaultAzureCredential } from '@azure/identity';
// Corresponds to your Model deployment within your OpenAI resource, e.g. gpt-4-1106-preview
// Navigate to the Azure OpenAI Studio to deploy a model.
const deployment = 'gpt-4-1106-preview';
const credential = new DefaultAzureCredential();
const scope = 'https://cognitiveservices.azure.com/.default';
const azureADTokenProvider = getBearerTokenProvider(credential, scope);
// Make sure to set AZURE_OPENAI_ENDPOINT with the endpoint of your Azure resource.
// You can find it in the Azure Portal.
const openai = new AzureOpenAI({ azureADTokenProvider });
async function main() {
console.log('Non-streaming:');
const result = await openai.chat.completions.create({
model: deployment,
messages: [{ role: 'user', content: 'Say hello!' }],
});
console.log(result.choices[0]!.message?.content);
console.log();
console.log('Streaming:');
const stream = await openai.chat.completions.create({
model: deployment,
messages: [{ role: 'user', content: 'Say hello!' }],
stream: true,
});
for await (const part of stream) {
process.stdout.write(part.choices[0]?.delta?.content ?? '');
}
process.stdout.write('\n');
}
main().catch((err) => {
console.error(err);
process.exit(1);
});