Skip to content

Commit 7f2bafe

Browse files
committed
add completions
1 parent bbe66aa commit 7f2bafe

File tree

1 file changed

+136
-3
lines changed

1 file changed

+136
-3
lines changed

articles/ai-services/openai/includes/javascript.md

Lines changed: 136 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ms.service: azure-ai-openai
88
ms.topic: include
99
author: mrbullwinkle
1010
ms.author: mbullwin
11-
ms.date: 05/20/2024
11+
ms.date: 10/22/2024
1212
---
1313

1414
[Source code](https://github.com/openai/openai-node) | [Package (npm)](https://www.npmjs.com/package/openai) | [Samples](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/openai/openai/samples)
@@ -23,6 +23,7 @@ ms.date: 05/20/2024
2323
- An Azure subscription - [Create one for free](https://azure.microsoft.com/free/cognitive-services?azure-portal=true)
2424
- [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule)
2525
- [TypeScript](https://www.typescriptlang.org/download/)
26+
- [Azure CLI](/cli/azure/install-azure-cli) used for passwordless authentication in a local development environment, create the necessary context by signing in with the Azure CLI.
2627
- An Azure OpenAI Service resource with the `gpt-35-turbo-instruct` model deployed. For more information about model deployment, see the [resource deployment guide](../how-to/create-resource.md).
2728

2829
> [!div class="nextstepaction"]
@@ -32,6 +33,7 @@ ms.date: 05/20/2024
3233

3334
- An Azure subscription - [Create one for free](https://azure.microsoft.com/free/cognitive-services?azure-portal=true)
3435
- [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule)
36+
- [Azure CLI](/cli/azure/install-azure-cli) used for passwordless authentication in a local development environment, create the necessary context by signing in with the Azure CLI.
3537
- An Azure OpenAI Service resource with the `gpt-35-turbo-instruct` model deployed. For more information about model deployment, see the [resource deployment guide](../how-to/create-resource.md).
3638

3739
> [!div class="nextstepaction"]
@@ -64,7 +66,138 @@ Your app's _package.json_ file will be updated with the dependencies.
6466

6567
Open a command prompt where you created the new project, and create a new file named Completion.js. Copy the following code into the Completion.js file.
6668

67-
## [**TypeScript**](#tab/typescript)
69+
## [**TypeScript (Entra id)**](#tab/typescript-keyless)
70+
71+
```typescript
72+
import {
73+
DefaultAzureCredential,
74+
getBearerTokenProvider
75+
} from "@azure/identity";
76+
import { AzureOpenAI } from "openai";
77+
import { type Completion } from "openai/resources/index";
78+
79+
// Load the .env file if it exists
80+
const dotenv = require("dotenv");
81+
dotenv.config();
82+
83+
// You will need to set these environment variables or edit the following values
84+
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>";
85+
const apiKey = process.env["AZURE_OPENAI_API_KEY"] || "<api key>";
86+
87+
// Required Azure OpenAI deployment name and API version
88+
const apiVersion = "2024-08-01-preview";
89+
const deploymentName = "gpt-35-turbo-instruct";
90+
91+
// keyless authentication
92+
const credential = new DefaultAzureCredential();
93+
const scope = "https://cognitiveservices.azure.com/.default";
94+
const azureADTokenProvider = getBearerTokenProvider(credential, scope);
95+
96+
// Chat prompt and max tokens
97+
const prompt = ["When was Microsoft founded?"];
98+
const maxTokens = 128;
99+
100+
function getClient(): AzureOpenAI {
101+
return new AzureOpenAI({
102+
endpoint,
103+
azureADTokenProvider,
104+
apiVersion,
105+
deployment: deploymentName,
106+
});
107+
}
108+
async function getCompletion(
109+
client: AzureOpenAI,
110+
prompt: string[],
111+
max_tokens: number
112+
): Promise<Completion> {
113+
return client.completions.create({
114+
prompt,
115+
model: "",
116+
max_tokens,
117+
});
118+
}
119+
async function printChoices(completion: Completion): Promise<void> {
120+
for (const choice of completion.choices) {
121+
console.log(choice.text);
122+
}
123+
}
124+
export async function main() {
125+
console.log("== Get completions Sample ==");
126+
127+
const client = getClient();
128+
const completion = await getCompletion(client, prompt, maxTokens);
129+
await printChoices(completion);
130+
}
131+
132+
main().catch((err) => {
133+
console.error("Error occurred:", err);
134+
});
135+
```
136+
137+
Build the script with the following command:
138+
139+
```cmd
140+
tsc
141+
```
142+
143+
Run the script with the following command:
144+
145+
```cmd
146+
node.exe Completion.js
147+
```
148+
149+
## [**JavaScript (Entra id)**](#tab/javascript-keyless)
150+
151+
```javascript
152+
const { AzureOpenAI } = require("openai");
153+
import {
154+
DefaultAzureCredential,
155+
getBearerTokenProvider
156+
} from "@azure/identity";
157+
158+
// Load the .env file if it exists
159+
const dotenv = require("dotenv");
160+
dotenv.config();
161+
162+
// You will need to set these environment variables or edit the following values
163+
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>";
164+
const apiKey = process.env["AZURE_OPENAI_API_KEY"] || "<api key>";
165+
const apiVersion = "2024-04-01-preview";
166+
const deployment = "gpt-35-turbo-instruct"; //The deployment name for your completions API model. The instruct model is the only new model that supports the legacy API.
167+
168+
// keyless authentication
169+
const credential = new DefaultAzureCredential();
170+
const scope = "https://cognitiveservices.azure.com/.default";
171+
const azureADTokenProvider = getBearerTokenProvider(credential, scope);
172+
173+
const prompt = ["When was Microsoft founded?"];
174+
175+
async function main() {
176+
console.log("== Get completions Sample ==");
177+
178+
const client = new AzureOpenAI({ endpoint, azureADTokenProvider, apiVersion, deployment });
179+
180+
const result = await client.completions.create({ prompt, model: deployment, max_tokens: 128 });
181+
182+
for (const choice of result.choices) {
183+
console.log(choice.text);
184+
}
185+
}
186+
187+
main().catch((err) => {
188+
console.error("Error occurred:", err);
189+
});
190+
191+
module.exports = { main };
192+
```
193+
194+
Run the script with the following command:
195+
196+
```cmd
197+
node.exe Completion.js
198+
```
199+
200+
## [**TypeScript (API key)**](#tab/typescript-key)
68201

69202
```typescript
70203
import "dotenv/config";
@@ -132,7 +265,7 @@ Run the script with the following command:
132265
node.exe Completion.js
133266
```
134267

135-
## [**JavaScript**](#tab/javascript)
268+
## [**JavaScript (API key)**](#tab/javascript-key)
136269

137270
```javascript
138271
const { AzureOpenAI } = require("openai");

0 commit comments

Comments
 (0)