Skip to content

Commit 23f2672

Browse files
committed
fit and finish javascript and ts quickstarts
1 parent 78debe2 commit 23f2672

22 files changed

+1151
-808
lines changed

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ For the recommended keyless authentication with Microsoft Entra ID, you need to:
4343
npm init -y
4444
```
4545

46-
1. Install the OpenAI Assistants client library for JavaScript with:
46+
1. Install the OpenAI client library for JavaScript with:
4747

4848
```console
4949
npm install openai
@@ -93,8 +93,8 @@ An individual assistant can access up to 128 tools including `code interpreter`,
9393
} = require("@azure/identity");
9494
9595
// Get environment variables
96-
const azureOpenAIEndpoint = process.env.AZURE_OPENAI_ENDPOINT || "https://<your-resource-name>.openai.azure.com/";
97-
const azureOpenAIDeployment = process.env.AZURE_OPENAI_DEPLOYMENT_NAME || "Your Deployment Name";
96+
const azureOpenAIEndpoint = process.env.AZURE_OPENAI_ENDPOINT || "Your endpoint";
97+
const azureOpenAIDeployment = process.env.AZURE_OPENAI_DEPLOYMENT_NAME || "Your deployment name";
9898
const azureOpenAIVersion = process.env.OPENAI_API_VERSION || "A supported API version";
9999
100100
// Check env variables
@@ -186,8 +186,6 @@ An individual assistant can access up to 128 tools including `code interpreter`,
186186
node index.js
187187
```
188188
189-
190-
191189
#### [API key](#tab/api-key)
192190
193191
1. Create the `index.js` file with the following code:
@@ -196,15 +194,15 @@ An individual assistant can access up to 128 tools including `code interpreter`,
196194
const { AzureOpenAI } = require("openai");
197195
198196
// Get environment variables
199-
const azureOpenAIKey = process.env.AZURE_OPENAI_KEY;
200-
const azureOpenAIEndpoint = process.env.AZURE_OPENAI_ENDPOINT;
201-
const azureOpenAIDeployment = process.env.AZURE_OPENAI_DEPLOYMENT_NAME;
202-
const azureOpenAIVersion = process.env.OPENAI_API_VERSION;
197+
const azureOpenAIKey = process.env.AZURE_OPENAI_KEY || "Your API key";
198+
const azureOpenAIEndpoint = process.env.AZURE_OPENAI_ENDPOINT || "Your endpoint";
199+
const azureOpenAIDeployment = process.env.AZURE_OPENAI_DEPLOYMENT_NAME || "Your deployment name";
200+
const azureOpenAIVersion = process.env.OPENAI_API_VERSION || "A supported API version";
203201
204202
// Check env variables
205203
if (!azureOpenAIKey || !azureOpenAIEndpoint || !azureOpenAIDeployment || !azureOpenAIVersion) {
206204
throw new Error(
207-
"Please set AZURE_OPENAI_KEY and AZURE_OPENAI_ENDPOINT and AZURE_OPENAI_DEPLOYMENT_NAME in your environment variables."
205+
"You need to set the endpoint, deployment name, and API version."
208206
);
209207
}
210208

articles/ai-services/openai/includes/assistants-typescript.md

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ For the recommended keyless authentication with Microsoft Entra ID, you need to:
5151
```
5252

5353

54-
1. Install the OpenAI Assistants client library for JavaScript with:
54+
1. Install the OpenAI client library for JavaScript with:
5555

5656
```console
5757
npm install openai
@@ -186,8 +186,6 @@ An individual assistant can access up to 128 tools including `code interpreter`,
186186
}
187187
}
188188
```
189-
190-
191189
192190
1. Create the `tsconfig.json` file to transpile the TypeScript code and copy the following code for ECMAScript.
193191
@@ -222,8 +220,6 @@ An individual assistant can access up to 128 tools including `code interpreter`,
222220
node index.js
223221
```
224222
225-
226-
227223
#### [API key](#tab/typescript-key)
228224
229225
1. Create the `index.ts` file with the following code:
@@ -240,16 +236,16 @@ An individual assistant can access up to 128 tools including `code interpreter`,
240236
import { Thread } from "openai/resources/beta/threads/threads";
241237
242238
// Get environment variables
243-
const azureOpenAIKey = process.env.AZURE_OPENAI_KEY as string;
244-
const azureOpenAIEndpoint = process.env.AZURE_OPENAI_ENDPOINT as string;
239+
const azureOpenAIKey = process.env.AZURE_OPENAI_KEY || "Your API key" as string;
240+
const azureOpenAIEndpoint = process.env.AZURE_OPENAI_ENDPOINT || "Your endpoint" as string;
245241
const azureOpenAIDeployment = process.env
246-
.AZURE_OPENAI_DEPLOYMENT_NAME as string;
247-
const openAIVersion = process.env.OPENAI_API_VERSION as string;
242+
.AZURE_OPENAI_DEPLOYMENT_NAME || "Your deployment name" as string;
243+
const openAIVersion = process.env.OPENAI_API_VERSION || "A supported API version" as string;
248244
249245
// Check env variables
250246
if (!azureOpenAIKey || !azureOpenAIEndpoint || !azureOpenAIDeployment || !openAIVersion) {
251247
throw new Error(
252-
"Please set AZURE_OPENAI_KEY and AZURE_OPENAI_ENDPOINT and AZURE_OPENAI_DEPLOYMENT_NAME in your environment variables."
248+
"You need to set the endpoint, deployment name, and API version."
253249
);
254250
}
255251
@@ -314,7 +310,6 @@ An individual assistant can access up to 128 tools including `code interpreter`,
314310
}
315311
}
316312
```
317-
318313
319314
1. Create the `tsconfig.json` file to transpile the TypeScript code and copy the following code for ECMAScript.
320315

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

Lines changed: 120 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -29,127 +29,144 @@ For the recommended keyless authentication with Microsoft Entra ID, you need to:
2929
- Install the [Azure CLI](/cli/azure/install-azure-cli) used for keyless authentication with Microsoft Entra ID.
3030
- Assign the `Cognitive Services User` role to your user account. You can assign roles in the Azure portal under **Access control (IAM)** > **Add role assignment**.
3131

32-
## Retrieve resource information
32+
## Set up
33+
34+
1. Create a new folder `chat-quickstart` to contain the application and open Visual Studio Code in that folder with the following command:
3335

34-
[!INCLUDE [resource authentication](resource-authentication.md)]
36+
```shell
37+
mkdir chat-quickstart && cd chat-quickstart
38+
```
3539

36-
> [!CAUTION]
37-
> To use the recommended keyless authentication with the SDK, make sure that the `AZURE_OPENAI_API_KEY` environment variable isn't set.
40+
1. Create the `package.json` with the following command:
3841

42+
```shell
43+
npm init -y
44+
```
3945

40-
## Create a Node application
46+
1. Install the OpenAI client library for JavaScript with:
4147

42-
In a console window (such as cmd, PowerShell, or Bash), create a new directory for your app, and navigate to it.
48+
```console
49+
npm install openai
50+
```
4351

44-
## Install the client library
52+
1. For the **recommended** passwordless authentication:
4553

46-
Install the required packages for JavaScript with npm from within the context of your new directory:
54+
```console
55+
npm install @azure/identity
56+
```
4757

48-
```console
49-
npm install openai @azure/identity
50-
```
51-
52-
Your app's _package.json_ file is updated with the dependencies.
58+
## Retrieve resource information
5359

54-
## Create a sample application
60+
[!INCLUDE [resource authentication](resource-authentication.md)]
5561

56-
Open a command prompt where you want the new project, and create a new file named ChatCompletion.js. Copy the following code into the ChatCompletion.js file.
62+
> [!CAUTION]
63+
> To use the recommended keyless authentication with the SDK, make sure that the `AZURE_OPENAI_API_KEY` environment variable isn't set.
5764
65+
## Create a sample application
5866
5967
## [Microsoft Entra ID](#tab/keyless)
6068
61-
```javascript
62-
const { AzureOpenAI } = require("openai");
63-
const {
64-
DefaultAzureCredential,
65-
getBearerTokenProvider
66-
} = require("@azure/identity");
67-
68-
// You will need to set these environment variables or edit the following values
69-
const endpoint = process.env.AZURE_OPENAI_ENDPOINT || "Your endpoint";
70-
const apiVersion = process.env.OPENAI_API_VERSION || "2024-05-01-preview";
71-
const deployment = process.env.AZURE_OPENAI_DEPLOYMENT_NAME || "gpt-4o"; //This must match your deployment name.
72-
73-
74-
// keyless authentication
75-
const credential = new DefaultAzureCredential();
76-
const scope = "https://cognitiveservices.azure.com/.default";
77-
const azureADTokenProvider = getBearerTokenProvider(credential, scope);
78-
79-
async function main() {
80-
81-
const client = new AzureOpenAI({ endpoint, apiKey, azureADTokenProvider, deployment });
82-
const result = await client.chat.completions.create({
83-
messages: [
84-
{ role: "system", content: "You are a helpful assistant." },
85-
{ role: "user", content: "Does Azure OpenAI support customer managed keys?" },
86-
{ role: "assistant", content: "Yes, customer managed keys are supported by Azure OpenAI?" },
87-
{ role: "user", content: "Do other Azure AI services support this too?" },
88-
],
89-
model: "",
90-
});
91-
92-
for (const choice of result.choices) {
93-
console.log(choice.message);
94-
}
95-
}
96-
97-
main().catch((err) => {
98-
console.error("The sample encountered an error:", err);
99-
});
100-
101-
module.exports = { main };
102-
```
103-
104-
Run the script with the following command:
105-
106-
```cmd
107-
node.exe ChatCompletion.js
108-
```
69+
1. Create the `index.js` file with the following code:
70+
71+
```javascript
72+
const { AzureOpenAI } = require("openai");
73+
const {
74+
DefaultAzureCredential,
75+
getBearerTokenProvider
76+
} = require("@azure/identity");
77+
78+
// You will need to set these environment variables or edit the following values
79+
const endpoint = process.env.AZURE_OPENAI_ENDPOINT || "Your endpoint";
80+
const apiVersion = process.env.OPENAI_API_VERSION || "2024-05-01-preview";
81+
const deployment = process.env.AZURE_OPENAI_DEPLOYMENT_NAME || "gpt-4o"; //This must match your deployment name.
82+
83+
// keyless authentication
84+
const credential = new DefaultAzureCredential();
85+
const scope = "https://cognitiveservices.azure.com/.default";
86+
const azureADTokenProvider = getBearerTokenProvider(credential, scope);
87+
88+
async function main() {
89+
90+
const client = new AzureOpenAI({ endpoint, apiKey, azureADTokenProvider, deployment });
91+
const result = await client.chat.completions.create({
92+
messages: [
93+
{ role: "system", content: "You are a helpful assistant." },
94+
{ role: "user", content: "Does Azure OpenAI support customer managed keys?" },
95+
{ role: "assistant", content: "Yes, customer managed keys are supported by Azure OpenAI?" },
96+
{ role: "user", content: "Do other Azure AI services support this too?" },
97+
],
98+
model: "",
99+
});
100+
101+
for (const choice of result.choices) {
102+
console.log(choice.message);
103+
}
104+
}
105+
106+
main().catch((err) => {
107+
console.error("The sample encountered an error:", err);
108+
});
109+
110+
module.exports = { main };
111+
```
112+
113+
1. Sign in to Azure with the following command:
114+
115+
```shell
116+
az login
117+
```
118+
119+
1. Run the JavaScript file.
120+
121+
```shell
122+
node index.js
123+
```
109124
110125
111126
## [API key](#tab/api-key)
112127
113-
```javascript
114-
const { AzureOpenAI } = require("openai");
115-
116-
// You will need to set these environment variables or edit the following values
117-
const endpoint = process.env.AZURE_OPENAI_ENDPOINT || "Your endpoint";
118-
const apiKey = process.env.AZURE_OPENAI_API_KEY || "Your API key";
119-
const apiVersion = process.env.OPENAI_API_VERSION || "2024-05-01-preview";
120-
const deployment = process.env.AZURE_OPENAI_DEPLOYMENT_NAME || "gpt-4o"; //This must match your deployment name.
121-
122-
async function main() {
123-
124-
const client = new AzureOpenAI({ endpoint, apiKey, apiVersion, deployment });
125-
const result = await client.chat.completions.create({
126-
messages: [
127-
{ role: "system", content: "You are a helpful assistant." },
128-
{ role: "user", content: "Does Azure OpenAI support customer managed keys?" },
129-
{ role: "assistant", content: "Yes, customer managed keys are supported by Azure OpenAI?" },
130-
{ role: "user", content: "Do other Azure AI services support this too?" },
131-
],
132-
model: "",
133-
});
134-
135-
for (const choice of result.choices) {
136-
console.log(choice.message);
137-
}
138-
}
139-
140-
main().catch((err) => {
141-
console.error("The sample encountered an error:", err);
142-
});
143-
144-
module.exports = { main };
145-
```
146-
147-
Run the script with the following command:
148-
149-
```cmd
150-
node.exe ChatCompletion.js
151-
```
152-
128+
1. Create the `index.js` file with the following code:
129+
130+
```javascript
131+
const { AzureOpenAI } = require("openai");
132+
133+
// You will need to set these environment variables or edit the following values
134+
const endpoint = process.env.AZURE_OPENAI_ENDPOINT || "Your endpoint";
135+
const apiKey = process.env.AZURE_OPENAI_API_KEY || "Your API key";
136+
const apiVersion = process.env.OPENAI_API_VERSION || "2024-05-01-preview";
137+
const deployment = process.env.AZURE_OPENAI_DEPLOYMENT_NAME || "gpt-4o"; //This must match your deployment name.
138+
139+
async function main() {
140+
141+
const client = new AzureOpenAI({ endpoint, apiKey, apiVersion, deployment });
142+
const result = await client.chat.completions.create({
143+
messages: [
144+
{ role: "system", content: "You are a helpful assistant." },
145+
{ role: "user", content: "Does Azure OpenAI support customer managed keys?" },
146+
{ role: "assistant", content: "Yes, customer managed keys are supported by Azure OpenAI?" },
147+
{ role: "user", content: "Do other Azure AI services support this too?" },
148+
],
149+
model: "",
150+
});
151+
152+
for (const choice of result.choices) {
153+
console.log(choice.message);
154+
}
155+
}
156+
157+
main().catch((err) => {
158+
console.error("The sample encountered an error:", err);
159+
});
160+
161+
module.exports = { main };
162+
```
163+
164+
1. Run the JavaScript file.
165+
166+
```shell
167+
node index.js
168+
```
169+
153170
---
154171
155172
## Output

0 commit comments

Comments
 (0)