-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
completionsRest.ts
44 lines (34 loc) · 1.2 KB
/
completionsRest.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
44
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
* Demonstrates how to get completions for the provided prompt.
*
* @summary get completions.
* @azsdk-weight 100
*/
import { AzureKeyCredential } from "@azure/core-auth";
import OpenAIClient, { isUnexpected } from "@azure/openai/rest";
// Load the .env file if it exists
import * as dotenv from "dotenv";
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 prompt = ["What is Azure OpenAI?"];
export async function main() {
console.log("== Get completions Sample ==");
const client = OpenAIClient(endpoint, new AzureKeyCredential(azureApiKey));
const deploymentId = "text-davinci-003";
const result = await client.path("/deployments/{deploymentId}/completions", deploymentId).post({
body: { prompt, max_tokens: 128 },
});
if (isUnexpected(result)) {
throw result;
}
for (const choice of result.body.choices) {
console.log(choice.text);
}
}
main().catch((err) => {
console.error("The sample encountered an error:", err);
});