@@ -18,6 +18,7 @@ recommendations: false
18
18
19
19
- An Azure subscription - [ Create one for free] ( https://azure.microsoft.com/free/cognitive-services?azure-portal=true )
20
20
- [ LTS versions of Node.js] ( https://github.com/nodejs/release#release-schedule )
21
+ - [ 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.
21
22
- An Azure OpenAI resource created in a supported region (see [ Region availability] ( /azure/ai-services/openai/concepts/models#model-summary-table-and-region-availability ) ). For more information, see [ Create a resource and deploy a model with Azure OpenAI] ( ../how-to/create-resource.md ) .
22
23
23
24
@@ -26,6 +27,7 @@ recommendations: false
26
27
- An Azure subscription - [ Create one for free] ( https://azure.microsoft.com/free/cognitive-services?azure-portal=true )
27
28
- [ LTS versions of Node.js] ( https://github.com/nodejs/release#release-schedule )
28
29
- [ TypeScript] ( https://www.typescriptlang.org/download/ )
30
+ - [ 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.
29
31
- An Azure OpenAI resource created in a supported region (see [ Region availability] ( /azure/ai-services/openai/concepts/models#model-summary-table-and-region-availability ) ). For more information, see [ Create a resource and deploy a model with Azure OpenAI] ( ../how-to/create-resource.md ) .
30
32
31
33
@@ -104,32 +106,114 @@ Your app's _package.json_ file will be updated with the dependencies.
104
106
105
107
## Create a speech file
106
108
109
+
107
110
111
+ #### [ TypeScript (Microsoft Entra Id)] ( #tab/typescript-keyless )
108
112
109
- #### [ JavaScript] ( #tab/javascript )
113
+ 1 . Create a new file named _ Text-to-speech.ts_ and open it in your preferred code editor. Copy the following code into the _ Text-to-speech.ts_ file:
114
+
115
+ ``` typescript
116
+ import " dotenv/config" ;
117
+ import { writeFile } from " fs/promises" ;
118
+ import { AzureOpenAI } from " openai" ;
119
+ import { DefaultAzureCredential , getBearerTokenProvider } from " @azure/identity" ;
120
+ import type { SpeechCreateParams } from " openai/resources/audio/speech" ;
121
+ import " openai/shims/node" ;
122
+
123
+ // You will need to set these environment variables or edit the following values
124
+ const endpoint = process .env [" AZURE_OPENAI_ENDPOINT" ] || " <endpoint>" ;
125
+ const speechFilePath =
126
+ process .env [" SPEECH_FILE_PATH" ] || " <path to save the speech file>" ;
127
+
128
+ // Required Azure OpenAI deployment name and API version
129
+ const deploymentName = " tts" ;
130
+ const apiVersion = " 2024-08-01-preview" ;
131
+
132
+ // keyless authentication
133
+ const credential = new DefaultAzureCredential ();
134
+ const scope = " https://cognitiveservices.azure.com/.default" ;
135
+ const azureADTokenProvider = getBearerTokenProvider (credential , scope );
136
+
137
+ function getClient(): AzureOpenAI {
138
+ return new AzureOpenAI ({
139
+ endpoint ,
140
+ azureADTokenProvider ,
141
+ apiVersion ,
142
+ deployment: deploymentName ,
143
+ });
144
+ }
145
+
146
+ async function generateAudioStream(
147
+ client : AzureOpenAI ,
148
+ params : SpeechCreateParams
149
+ ): Promise <NodeJS .ReadableStream > {
150
+ const response = await client .audio .speech .create (params );
151
+ if (response .ok ) return response .body ;
152
+ throw new Error (` Failed to generate audio stream: ${response .statusText } ` );
153
+ }
154
+ export async function main() {
155
+ console .log (" == Text to Speech Sample ==" );
156
+
157
+ const client = getClient ();
158
+ const streamToRead = await generateAudioStream (client , {
159
+ model: deploymentName ,
160
+ voice: " alloy" ,
161
+ input: " the quick brown chicken jumped over the lazy dogs" ,
162
+ });
163
+
164
+ console .log (` Streaming response to ${speechFilePath } ` );
165
+ await writeFile (speechFilePath , streamToRead );
166
+ console .log (" Finished streaming" );
167
+ }
168
+
169
+ main ().catch ((err ) => {
170
+ console .error (" The sample encountered an error:" , err );
171
+ });
172
+
173
+ ```
174
+
175
+ The import of ` "openai/shims/node" ` is necessary when running the code in a Node .js environment . It ensures that the output type of the ` client.audio.speech.create ` method is correctly set to ` NodeJS.ReadableStream ` .
176
+
177
+ 1. Build the application with the following command :
178
+
179
+ ` ` ` console
180
+ tsc
181
+ ` ` `
182
+
183
+ 1. Run the application with the following command :
184
+
185
+ ` ` ` console
186
+ node Text-to-speech.js
187
+ ` ` `
188
+ #### [JavaScript (Microsoft Entra Id )](#tab /javascript -keyless )
110
189
111
190
1. Create a new file named _Text -to -speech .js_ and open it in your preferred code editor . Copy the following code into the _Text -to -speech .js_ file :
112
191
113
192
` ` ` javascript
114
193
require("dotenv/config");
115
194
const { writeFile } = require("fs/promises");
116
195
const { AzureOpenAI } = require("openai");
196
+ const { DefaultAzureCredential, getBearerTokenProvider } = require("@azure/identity");
117
197
require("openai/shims/node");
118
198
119
199
// You will need to set these environment variables or edit the following values
120
200
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>";
121
- const apiKey = process .env [" AZURE_OPENAI_API_KEY" ] || " <api key>" ;
122
201
const speechFilePath =
123
202
process.env["SPEECH_FILE_PATH"] || "<path to save the speech file>";
124
203
125
204
// Required Azure OpenAI deployment name and API version
126
205
const deploymentName = "tts";
127
206
const apiVersion = "2024-08-01-preview";
128
207
208
+ // keyless authentication
209
+ const credential = new DefaultAzureCredential();
210
+ const scope = "https://cognitiveservices.azure.com/.default";
211
+ const azureADTokenProvider = getBearerTokenProvider(credential, scope);
212
+
129
213
function getClient() {
130
214
return new AzureOpenAI({
131
215
endpoint,
132
- apiKey ,
216
+ azureADTokenProvider ,
133
217
apiVersion,
134
218
deployment: deploymentName,
135
219
});
@@ -169,9 +253,9 @@ Your app's _package.json_ file will be updated with the dependencies.
169
253
` ` ` console
170
254
node Text-to-speech.js
171
255
` ` `
172
-
173
256
174
- #### [TypeScript](#tab/ typescript)
257
+
258
+ #### [TypeScript (API key )](#tab /typescript -key )
175
259
176
260
1. Create a new file named _Text -to -speech .ts_ and open it in your preferred code editor . Copy the following code into the _Text -to -speech .ts_ file :
177
261
@@ -244,4 +328,69 @@ Your app's _package.json_ file will be updated with the dependencies.
244
328
node Text-to-speech.js
245
329
` ` `
246
330
331
+ #### [JavaScript (API key )](#tab /javascript -key )
332
+
333
+ 1. Create a new file named _Text -to -speech .js_ and open it in your preferred code editor . Copy the following code into the _Text -to -speech .js_ file :
334
+
335
+ ` ` ` javascript
336
+ require("dotenv/config");
337
+ const { writeFile } = require("fs/promises");
338
+ const { AzureOpenAI } = require("openai");
339
+ require("openai/shims/node");
340
+
341
+ // You will need to set these environment variables or edit the following values
342
+ const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>";
343
+ const apiKey = process.env["AZURE_OPENAI_API_KEY"] || "<api key>";
344
+ const speechFilePath =
345
+ process.env["SPEECH_FILE_PATH"] || "<path to save the speech file>";
346
+
347
+ // Required Azure OpenAI deployment name and API version
348
+ const deploymentName = "tts";
349
+ const apiVersion = "2024-08-01-preview";
350
+
351
+ function getClient() {
352
+ return new AzureOpenAI({
353
+ endpoint,
354
+ apiKey,
355
+ apiVersion,
356
+ deployment: deploymentName,
357
+ });
358
+ }
359
+
360
+ async function generateAudioStream(
361
+ client,
362
+ params
363
+ ) {
364
+ const response = await client.audio.speech.create(params);
365
+ if (response.ok) return response.body;
366
+ throw new Error( ` Failed to generate audio stream : $ {response .statusText }` );
367
+ }
368
+ export async function main() {
369
+ console.log("== Text to Speech Sample ==");
370
+
371
+ const client = getClient();
372
+ const streamToRead = await generateAudioStream(client, {
373
+ model: deploymentName,
374
+ voice: "alloy",
375
+ input: "the quick brown chicken jumped over the lazy dogs",
376
+ });
377
+
378
+ console.log( ` Streaming response to $ {speechFilePath }` );
379
+ await writeFile(speechFilePath, streamToRead);
380
+ console.log("Finished streaming");
381
+ }
382
+
383
+ main().catch((err) => {
384
+ console.error("The sample encountered an error:", err);
385
+ });
386
+
387
+ ` ` `
388
+
389
+ 1. Run the script with the following command :
390
+
391
+ ` ` ` console
392
+ node Text-to-speech.js
393
+ ` ` `
394
+
395
+
247
396
---
0 commit comments