Skip to content

Commit 3879cf5

Browse files
committed
after code approval
1 parent ca906df commit 3879cf5

File tree

2 files changed

+133
-80
lines changed

2 files changed

+133
-80
lines changed

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

Lines changed: 90 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ ms.author: mbullwin
1111
ms.date: 05/21/2024
1212
---
1313

14-
[Source code](https://github.com/openai/openai-node) | [Package (npm)](https://www.npmjs.com/package/openai)
14+
[Reference documentation](https://platform.openai.com/docs/api-reference/chat) | [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)
1515

1616
> [!NOTE]
1717
> This article has been updated to use the [latest OpenAI npm package](https://www.npmjs.com/package/openai) which now fully supports Azure OpenAI. If you are looking for code examples for the legacy Azure OpenAI JavaScript SDK they are currently still [available in this repo](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/openai/openai/samples/v2-beta/javascript).
@@ -69,47 +69,61 @@ Open a command prompt where you want the new project, and create a new file name
6969
## [**TypeScript**](#tab/typescript)
7070

7171
```typescript
72-
import { AzureOpenAI } from "openai";
73-
import { type ChatCompletion, type ChatCompletionCreateParamsNonStreaming } from "openai/resources/index";
7472
import "dotenv/config";
73+
import { AzureOpenAI } from "openai";
74+
import type {
75+
ChatCompletion,
76+
ChatCompletionCreateParamsNonStreaming,
77+
} from "openai/resources/index";
7578

7679
// You will need to set these environment variables or edit the following values
7780
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>";
7881
const apiKey = process.env["AZURE_OPENAI_API_KEY"] || "<api key>";
7982

80-
const apiVersion = "2024-05-01-preview";
81-
const deployment = "gpt-4o"; //This must match your deployment name.
83+
// Required Azure OpenAI deployment name and API version
84+
const apiVersion = "2024-07-01-preview";
85+
const deploymentName = "gpt-4"; //This must match your deployment name.
8286

8387
function getClient(): AzureOpenAI {
84-
return new AzureOpenAI({ endpoint, apiKey, apiVersion, deployment });
88+
return new AzureOpenAI({
89+
endpoint,
90+
apiKey,
91+
apiVersion,
92+
deployment: deploymentName,
93+
});
8594
}
8695

8796
function createMessages(): ChatCompletionCreateParamsNonStreaming {
88-
return {
89-
messages: [
90-
{ role: "system", content: "You are a helpful assistant." },
91-
{ role: "user", content: "Does Azure OpenAI support customer managed keys?" },
92-
{ role: "assistant", content: "Yes, customer managed keys are supported by Azure OpenAI?" },
93-
{ role: "user", content: "Do other Azure AI services support this too?" },
94-
],
95-
model: ""
96-
}
97+
return {
98+
messages: [
99+
{ role: "system", content: "You are a helpful assistant." },
100+
{
101+
role: "user",
102+
content: "Does Azure OpenAI support customer managed keys?",
103+
},
104+
{
105+
role: "assistant",
106+
content: "Yes, customer managed keys are supported by Azure OpenAI?",
107+
},
108+
{ role: "user", content: "Do other Azure AI services support this too?" },
109+
],
110+
model: "",
111+
};
97112
}
98-
async function getChoices(completion: ChatCompletion): Promise<void> {
99-
for (const choice of completion.choices) {
100-
console.log(choice.message);
101-
}
113+
async function printChoices(completion: ChatCompletion): Promise<void> {
114+
for (const choice of completion.choices) {
115+
console.log(choice.message);
116+
}
102117
}
103118
export async function main() {
104-
105-
const client = getClient();
106-
const messages = createMessages();
107-
const result = await client.chat.completions.create(messages);
108-
await getChoices(result);
119+
const client = getClient();
120+
const messages = createMessages();
121+
const result = await client.chat.completions.create(messages);
122+
await printChoices(result);
109123
}
110124

111125
main().catch((err) => {
112-
console.error("The sample encountered an error:", err);
126+
console.error("The sample encountered an error:", err);
113127
});
114128
```
115129

@@ -192,47 +206,69 @@ node.exe ChatCompletion.js
192206
## [**TypeScript**](#tab/typescript)
193207

194208
```typescript
195-
import { AzureOpenAI } from "openai";
196-
import { type ChatCompletionCreateParamsNonStreaming, type ChatCompletion } from "openai/resources/index";
197-
import { DefaultAzureCredential, getBearerTokenProvider } from "@azure/identity";
209+
import {
210+
DefaultAzureCredential,
211+
getBearerTokenProvider,
212+
} from "@azure/identity";
198213
import "dotenv/config";
214+
import { AzureOpenAI } from "openai";
215+
import type {
216+
ChatCompletion,
217+
ChatCompletionCreateParamsNonStreaming,
218+
} from "openai/resources/index";
199219

200-
const apiVersion = "2024-05-01-preview";
201-
const deployment = "gpt-4o"; //This must match your deployment name.
220+
// You will need to set these environment variables or edit the following values
221+
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>";
222+
223+
// Required Azure OpenAI deployment name and API version
224+
const apiVersion = "2024-07-01-preview";
225+
const deploymentName = "gpt-4o"; //This must match your deployment name.
202226

203227
function getClient(): AzureOpenAI {
204-
const scope = "https://cognitiveservices.azure.com/.default";
205-
const azureADTokenProvider = getBearerTokenProvider(new DefaultAzureCredential(), scope);
206-
return new AzureOpenAI({ azureADTokenProvider, deployment, apiVersion });
207-
208-
}
228+
const scope = "https://cognitiveservices.azure.com/.default";
229+
const azureADTokenProvider = getBearerTokenProvider(
230+
new DefaultAzureCredential(),
231+
scope
232+
);
233+
return new AzureOpenAI({
234+
endpoint,
235+
azureADTokenProvider,
236+
deployment: deploymentName,
237+
apiVersion,
238+
});
239+
}
209240

210241
function createMessages(): ChatCompletionCreateParamsNonStreaming {
211-
return {
212-
messages: [
213-
{ role: "system", content: "You are a helpful assistant." },
214-
{ role: "user", content: "Does Azure OpenAI support customer managed keys?" },
215-
{ role: "assistant", content: "Yes, customer managed keys are supported by Azure OpenAI?" },
216-
{ role: "user", content: "Do other Azure AI services support this too?" },
217-
],
218-
model: ""
219-
}
242+
return {
243+
messages: [
244+
{ role: "system", content: "You are a helpful assistant." },
245+
{
246+
role: "user",
247+
content: "Does Azure OpenAI support customer managed keys?",
248+
},
249+
{
250+
role: "assistant",
251+
content: "Yes, customer managed keys are supported by Azure OpenAI?",
252+
},
253+
{ role: "user", content: "Do other Azure AI services support this too?" },
254+
],
255+
model: "",
256+
};
220257
}
221-
async function getChoices(completion: ChatCompletion): Promise<void> {
222-
for (const choice of completion.choices) {
223-
console.log(choice.message);
224-
}
258+
async function printChoices(completion: ChatCompletion): Promise<void> {
259+
for (const choice of completion.choices) {
260+
console.log(choice.message);
261+
}
225262
}
226263
export async function main() {
227-
228-
const client = getClient();
229-
const messages = createMessages();
230-
const result = await client.chat.completions.create(messages);
231-
await getChoices(result);
264+
const client = getClient();
265+
const messages = createMessages();
266+
const result = await client.chat.completions.create(messages);
267+
await printChoices(result);
232268
}
233269

234270
main().catch((err) => {
235-
console.error("The sample encountered an error:", err);
271+
console.error("The sample encountered an error:", err);
236272
});
237273
```
238274

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

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ ms.author: mbullwin
1111
ms.date: 05/20/2024
1212
---
1313

14-
[Source code](https://github.com/openai/openai-node) | [Package (npm)](https://www.npmjs.com/package/openai)
14+
[Reference documentation](https://platform.openai.com/docs/api-reference/completions) | [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)
1515

1616
> [!NOTE]
1717
> This article has been updated to use the [latest OpenAI npm package](https://www.npmjs.com/package/openai) which now fully supports Azure OpenAI. If you are looking for code examples for the legacy Azure OpenAI JavaScript SDK they are currently still [available in this repo](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/openai/openai/samples/v2-beta/javascript).
@@ -67,38 +67,42 @@ Open a command prompt where you created the new project, and create a new file n
6767
## [**TypeScript**](#tab/typescript)
6868

6969
```typescript
70+
import "dotenv/config";
7071
import { AzureOpenAI } from "openai";
7172
import { type Completion } from "openai/resources/index";
72-
import "dotenv/config";
7373

7474
// You will need to set these environment variables or edit the following values
7575
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>";
7676
const apiKey = process.env["AZURE_OPENAI_API_KEY"] || "<api key>";
7777

78-
// Azure OpenAI API version and deployment
79-
const apiVersion = "2024-04-01-preview";
80-
const deployment = "gpt-35-turbo-instruct";
78+
// Required Azure OpenAI deployment name and API version
79+
const apiVersion = "2024-07-01-preview";
80+
const deploymentName = "gpt-35-turbo-instruct";
8181

8282
// Chat prompt and max tokens
8383
const prompt = ["When was Microsoft founded?"];
8484
const maxTokens = 128;
8585

8686
function getClient(): AzureOpenAI {
87-
return new AzureOpenAI({ endpoint, apiKey, apiVersion, deployment });
87+
return new AzureOpenAI({
88+
endpoint,
89+
apiKey,
90+
apiVersion,
91+
deployment: deploymentName,
92+
});
8893
}
8994
async function getCompletion(
9095
client: AzureOpenAI,
9196
prompt: string[],
92-
model: string,
9397
max_tokens: number
9498
): Promise<Completion> {
95-
return await client.completions.create({
99+
return client.completions.create({
96100
prompt,
97-
model,
101+
model: "",
98102
max_tokens,
99103
});
100104
}
101-
async function getChoices(completion: Completion): Promise<void> {
105+
async function printChoices(completion: Completion): Promise<void> {
102106
for (const choice of completion.choices) {
103107
console.log(choice.text);
104108
}
@@ -107,8 +111,8 @@ export async function main() {
107111
console.log("== Get completions Sample ==");
108112

109113
const client = getClient();
110-
const completion = await getCompletion(client, prompt, deployment, maxTokens);
111-
await getChoices(completion);
114+
const completion = await getCompletion(client, prompt, maxTokens);
115+
await printChoices(completion);
112116
}
113117

114118
main().catch((err) => {
@@ -188,38 +192,50 @@ Microsoft was founded on April 4, 1975.
188192
## [**TypeScript**](#tab/typescript)
189193

190194
```typescript
195+
import {
196+
DefaultAzureCredential,
197+
getBearerTokenProvider,
198+
} from "@azure/identity";
199+
import "dotenv/config";
191200
import { AzureOpenAI } from "openai";
192201
import { type Completion } from "openai/resources/index";
193-
import { DefaultAzureCredential, getBearerTokenProvider } from "@azure/identity";
194-
import "dotenv/config";
195202

196-
// Azure OpenAI API version and deployment
197-
const apiVersion = "2024-04-01-preview";
198-
const deployment = "gpt-35-turbo-instruct";
203+
// You will need to set these environment variables or edit the following values
204+
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>";
205+
206+
// Required Azure OpenAI deployment name and API version
207+
const apiVersion = "2024-07-01-preview";
208+
const deploymentName = "gpt-35-turbo-instruct";
199209

200210
// Chat prompt and max tokens
201211
const prompt = ["When was Microsoft founded?"];
202212
const maxTokens = 128;
203213

204214
function getClient(): AzureOpenAI {
205215
const scope = "https://cognitiveservices.azure.com/.default";
206-
const azureADTokenProvider = getBearerTokenProvider(new DefaultAzureCredential(), scope);
207-
return new AzureOpenAI({ azureADTokenProvider, deployment, apiVersion });
208-
216+
const azureADTokenProvider = getBearerTokenProvider(
217+
new DefaultAzureCredential(),
218+
scope
219+
);
220+
return new AzureOpenAI({
221+
endpoint,
222+
azureADTokenProvider,
223+
deployment: deploymentName,
224+
apiVersion,
225+
});
209226
}
210227
async function getCompletion(
211228
client: AzureOpenAI,
212229
prompt: string[],
213-
model: string,
214230
max_tokens: number
215231
): Promise<Completion> {
216-
return await client.completions.create({
232+
return client.completions.create({
217233
prompt,
218-
model,
234+
model: "",
219235
max_tokens,
220236
});
221237
}
222-
async function getChoices(completion: Completion): Promise<void> {
238+
async function printChoices(completion: Completion): Promise<void> {
223239
for (const choice of completion.choices) {
224240
console.log(choice.text);
225241
}
@@ -228,13 +244,14 @@ export async function main() {
228244
console.log("== Get completions Sample ==");
229245

230246
const client = getClient();
231-
const completion = await getCompletion(client, prompt, deployment, maxTokens);
232-
await getChoices(completion);
247+
const completion = await getCompletion(client, prompt, maxTokens);
248+
await printChoices(completion);
233249
}
234250

235251
main().catch((err) => {
236252
console.error("Error occurred:", err);
237253
});
254+
238255
```
239256

240257
Build the script with the following command:

0 commit comments

Comments
 (0)