Skip to content

Commit 7fd8ec7

Browse files
committed
Quickstart JS - Audio - Entra for JS & TS
1 parent b2a8256 commit 7fd8ec7

File tree

2 files changed

+276
-10
lines changed

2 files changed

+276
-10
lines changed

articles/ai-services/openai/includes/text-to-speech-javascript.md

Lines changed: 154 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ recommendations: false
1818

1919
- An Azure subscription - [Create one for free](https://azure.microsoft.com/free/cognitive-services?azure-portal=true)
2020
- [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.
2122
- 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).
2223

2324

@@ -26,6 +27,7 @@ recommendations: false
2627
- An Azure subscription - [Create one for free](https://azure.microsoft.com/free/cognitive-services?azure-portal=true)
2728
- [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule)
2829
- [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.
2931
- 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).
3032

3133

@@ -104,32 +106,114 @@ Your app's _package.json_ file will be updated with the dependencies.
104106

105107
## Create a speech file
106108

109+
107110

111+
#### [TypeScript (Microsoft Entra Id)](#tab/typescript-keyless)
108112

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)
110189

111190
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:
112191

113192
```javascript
114193
require("dotenv/config");
115194
const { writeFile } = require("fs/promises");
116195
const { AzureOpenAI } = require("openai");
196+
const { DefaultAzureCredential, getBearerTokenProvider } = require("@azure/identity");
117197
require("openai/shims/node");
118198
119199
// You will need to set these environment variables or edit the following values
120200
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>";
121-
const apiKey = process.env["AZURE_OPENAI_API_KEY"] || "<api key>";
122201
const speechFilePath =
123202
process.env["SPEECH_FILE_PATH"] || "<path to save the speech file>";
124203
125204
// Required Azure OpenAI deployment name and API version
126205
const deploymentName = "tts";
127206
const apiVersion = "2024-08-01-preview";
128207
208+
// keyless authentication
209+
const credential = new DefaultAzureCredential();
210+
const scope = "https://cognitiveservices.azure.com/.default";
211+
const azureADTokenProvider = getBearerTokenProvider(credential, scope);
212+
129213
function getClient() {
130214
return new AzureOpenAI({
131215
endpoint,
132-
apiKey,
216+
azureADTokenProvider,
133217
apiVersion,
134218
deployment: deploymentName,
135219
});
@@ -169,9 +253,9 @@ Your app's _package.json_ file will be updated with the dependencies.
169253
```console
170254
node Text-to-speech.js
171255
```
172-
173256

174-
#### [TypeScript](#tab/typescript)
257+
258+
#### [TypeScript (API key)](#tab/typescript-key)
175259

176260
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:
177261

@@ -244,4 +328,69 @@ Your app's _package.json_ file will be updated with the dependencies.
244328
node Text-to-speech.js
245329
```
246330

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+
247396
---

0 commit comments

Comments
 (0)