Skip to content

Commit 1fd3ef1

Browse files
committed
update
1 parent bf8d9a6 commit 1fd3ef1

File tree

1 file changed

+47
-9
lines changed

1 file changed

+47
-9
lines changed

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

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ In a console window (such as cmd, PowerShell, or Bash), create a new directory f
3737
Install the required packages for JavaScript with npm from within the context of your new directory:
3838

3939
```console
40-
npm install openai @azure/openai dotenv
40+
npm install openai dotenv @azure/identity
4141
```
4242

4343
Your app's _package.json_ file will be updated with the dependencies.
@@ -50,24 +50,26 @@ Your app's _package.json_ file will be updated with the dependencies.
5050
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.
5151

5252
```javascript
53-
const { OpenAIClient, AzureKeyCredential } = require("@azure/openai");
53+
const { AzureOpenAI } = require("openai");
5454

5555
// Load the .env file if it exists
5656
const dotenv = require("dotenv");
5757
dotenv.config();
5858

5959
// You will need to set these environment variables or edit the following values
6060
const endpoint = process.env["ENDPOINT"] || "<endpoint>";
61-
const azureApiKey = process.env["AZURE_API_KEY"] || "<api key>";
61+
const apiKey = process.env["AZURE_API_KEY"] || "<api key>";
62+
const apiVersion = "2024-04-01-preview";
63+
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.
6264

6365
const prompt = ["When was Microsoft founded?"];
6466

6567
async function main() {
6668
console.log("== Get completions Sample ==");
6769

68-
const client = new OpenAIClient(endpoint, new AzureKeyCredential(azureApiKey));
69-
const deploymentId = "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.
70-
const result = await client.getCompletions(deploymentId, prompt, { maxTokens: 128 });
70+
const client = new AzureOpenAI({ endpoint, apiKey, apiVersion, deployment });
71+
72+
const result = await client.completions.create({ prompt, model: deployment, max_tokens: 128 });
7173

7274
for (const choice of result.choices) {
7375
console.log(choice.text);
@@ -81,9 +83,6 @@ main().catch((err) => {
8183
module.exports = { main };
8284
```
8385

84-
> [!IMPORTANT]
85-
> For production, use a secure way of storing and accessing your credentials like [Azure Key Vault](../../../key-vault/general/overview.md). For more information about credential security, see the Azure AI services [security](../../security-features.md) article.
86-
8786
Run the script with the following command:
8887

8988
```cmd
@@ -98,6 +97,45 @@ node.exe Completion.js
9897
Microsoft was founded on April 4, 1975.
9998
```
10099

100+
## Microsoft Entra ID
101+
102+
> [!IMPORTANT]
103+
> In the previous example we are demonstrating key-based authentication. Once you have tested with key based authentication successfully, we recommend using the more secure [Microsoft Entra ID](/entra/fundamentals/whatis) for authentication which is demonstrated in the next code sample.
104+
105+
```javascript
106+
const { AzureOpenAI } = require("openai");
107+
const { DefaultAzureCredential, getBearerTokenProvider } = require("@azure/identity");
108+
109+
// Set AZURE_OPENAI_ENDPOINT to the endpoint of your
110+
// OpenAI resource. You can find this in the Azure portal.
111+
// Load the .env file if it exists
112+
require("dotenv/config");
113+
114+
const prompt = ["When was Microsoft founded?"];
115+
116+
async function main() {
117+
console.log("== Get completions Sample ==");
118+
119+
const scope = "https://cognitiveservices.azure.com/.default";
120+
const azureADTokenProvider = getBearerTokenProvider(new DefaultAzureCredential(), scope);
121+
const deployment = "gpt-35-turbo-instruct";
122+
const apiVersion = "2024-04-01-preview";
123+
const client = new AzureOpenAI({ azureADTokenProvider, deployment, apiVersion });
124+
const result = await client.completions.create({ prompt, model: deployment, max_tokens: 128 });
125+
126+
for (const choice of result.choices) {
127+
console.log(choice.text);
128+
}
129+
}
130+
131+
main().catch((err) => {
132+
console.error("Error occurred:", err);
133+
});
134+
135+
module.exports = { main };
136+
```
137+
138+
101139
> [!div class="nextstepaction"]
102140
> [I ran into an issue when running the code sample.](https://microsoft.qualtrics.com/jfe/form/SV_0Cl5zkG3CnDjq6O?PLanguage=JAVASCRIPT&Pillar=AOAI&&Product=gpt&Page=quickstart&Section=Create-application)
103141

0 commit comments

Comments
 (0)