/
listChatCompletions.js
44 lines (34 loc) · 1.36 KB
/
listChatCompletions.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
* Demonstrates how to list chat completions for a chat context.
*
* @summary list chat completions.
*/
const { OpenAIClient, AzureKeyCredential } = require("@azure/openai");
// Load the .env file if it exists
require("dotenv").config();
// You will need to set these environment variables or edit the following values
const endpoint = process.env["ENDPOINT"] || "<endpoint>";
const azureApiKey = process.env["AZURE_API_KEY"] || "<api key>";
const messages = [
{ role: "system", content: "You are a helpful assistant. You will talk like a pirate." },
{ role: "user", content: "Can you help me?" },
{ role: "assistant", content: "Arrrr! Of course, me hearty! What can I do for ye?" },
{ role: "user", content: "What's the best way to train a parrot?" },
];
async function main() {
console.log("== Streaming Chat Completions Sample ==");
const client = new OpenAIClient(endpoint, new AzureKeyCredential(azureApiKey));
const deploymentId = "gpt-35-turbo";
const events = client.listChatCompletions(deploymentId, messages, { maxTokens: 128 });
for await (const event of events) {
for (const choice of event.choices) {
console.log(choice.delta?.content);
}
}
}
main().catch((err) => {
console.error("The sample encountered an error:", err);
});
module.exports = { main };