Skip to content

Commit 074547c

Browse files
committed
pivot
1 parent 31761e3 commit 074547c

File tree

7 files changed

+506
-294
lines changed

7 files changed

+506
-294
lines changed

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

Lines changed: 2 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,12 @@ recommendations: false
1414

1515
## Prerequisites
1616

17-
#### [JavaScript](#tab/javascript)
18-
1917
- An Azure subscription - [Create one for free](https://azure.microsoft.com/free/cognitive-services?azure-portal=true)
2018
- [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule)
2119
- [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.
2220
- 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).
2321

2422

25-
#### [TypeScript](#tab/typescript)
26-
27-
- An Azure subscription - [Create one for free](https://azure.microsoft.com/free/cognitive-services?azure-portal=true)
28-
- [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule)
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.
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).
32-
33-
34-
35-
---
36-
3723
## Set up
3824

3925
### Retrieve key and endpoint
@@ -108,83 +94,7 @@ Your app's _package.json_ file will be updated with the dependencies.
10894

10995

11096

111-
#### [TypeScript (Microsoft Entra ID)](#tab/typescript-keyless)
112-
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 { writeFile } from "fs/promises";
117-
import { AzureOpenAI } from "openai";
118-
import { DefaultAzureCredential, getBearerTokenProvider } from "@azure/identity";
119-
import type { SpeechCreateParams } from "openai/resources/audio/speech";
120-
import "openai/shims/node";
121-
122-
// You will need to set these environment variables or edit the following values
123-
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>";
124-
const speechFilePath =
125-
process.env["SPEECH_FILE_PATH"] || "<path to save the speech file>";
126-
127-
// Required Azure OpenAI deployment name and API version
128-
const deploymentName = "tts";
129-
const apiVersion = "2024-08-01-preview";
130-
131-
// keyless authentication
132-
const credential = new DefaultAzureCredential();
133-
const scope = "https://cognitiveservices.azure.com/.default";
134-
const azureADTokenProvider = getBearerTokenProvider(credential, scope);
135-
136-
function getClient(): AzureOpenAI {
137-
return new AzureOpenAI({
138-
endpoint,
139-
azureADTokenProvider,
140-
apiVersion,
141-
deployment: deploymentName,
142-
});
143-
}
144-
145-
async function generateAudioStream(
146-
client: AzureOpenAI,
147-
params: SpeechCreateParams
148-
): Promise<NodeJS.ReadableStream> {
149-
const response = await client.audio.speech.create(params);
150-
if (response.ok) return response.body;
151-
throw new Error(`Failed to generate audio stream: ${response.statusText}`);
152-
}
153-
export async function main() {
154-
console.log("== Text to Speech Sample ==");
155-
156-
const client = getClient();
157-
const streamToRead = await generateAudioStream(client, {
158-
model: deploymentName,
159-
voice: "alloy",
160-
input: "the quick brown chicken jumped over the lazy dogs",
161-
});
162-
163-
console.log(`Streaming response to ${speechFilePath}`);
164-
await writeFile(speechFilePath, streamToRead);
165-
console.log("Finished streaming");
166-
}
167-
168-
main().catch((err) => {
169-
console.error("The sample encountered an error:", err);
170-
});
171-
172-
```
173-
174-
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`.
175-
176-
1. Build the application with the following command:
177-
178-
```console
179-
tsc
180-
```
181-
182-
1. Run the application with the following command:
183-
184-
```console
185-
node Text-to-speech.js
186-
```
187-
#### [JavaScript (Microsoft Entra ID)](#tab/javascript-keyless)
97+
#### [Microsoft Entra ID](#tab/javascript-keyless)
18898

18999
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:
190100

@@ -252,80 +162,7 @@ Your app's _package.json_ file will be updated with the dependencies.
252162
node Text-to-speech.js
253163
```
254164

255-
256-
#### [TypeScript (API key)](#tab/typescript-key)
257-
258-
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:
259-
260-
```typescript
261-
import { writeFile } from "fs/promises";
262-
import { AzureOpenAI } from "openai";
263-
import type { SpeechCreateParams } from "openai/resources/audio/speech";
264-
import "openai/shims/node";
265-
266-
// You will need to set these environment variables or edit the following values
267-
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>";
268-
const apiKey = process.env["AZURE_OPENAI_API_KEY"] || "<api key>";
269-
const speechFilePath =
270-
process.env["SPEECH_FILE_PATH"] || "<path to save the speech file>";
271-
272-
// Required Azure OpenAI deployment name and API version
273-
const deploymentName = "tts";
274-
const apiVersion = "2024-08-01-preview";
275-
276-
function getClient(): AzureOpenAI {
277-
return new AzureOpenAI({
278-
endpoint,
279-
apiKey,
280-
apiVersion,
281-
deployment: deploymentName,
282-
});
283-
}
284-
285-
async function generateAudioStream(
286-
client: AzureOpenAI,
287-
params: SpeechCreateParams
288-
): Promise<NodeJS.ReadableStream> {
289-
const response = await client.audio.speech.create(params);
290-
if (response.ok) return response.body;
291-
throw new Error(`Failed to generate audio stream: ${response.statusText}`);
292-
}
293-
export async function main() {
294-
console.log("== Text to Speech Sample ==");
295-
296-
const client = getClient();
297-
const streamToRead = await generateAudioStream(client, {
298-
model: deploymentName,
299-
voice: "alloy",
300-
input: "the quick brown chicken jumped over the lazy dogs",
301-
});
302-
303-
console.log(`Streaming response to ${speechFilePath}`);
304-
await writeFile(speechFilePath, streamToRead);
305-
console.log("Finished streaming");
306-
}
307-
308-
main().catch((err) => {
309-
console.error("The sample encountered an error:", err);
310-
});
311-
312-
```
313-
314-
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`.
315-
316-
1. Build the application with the following command:
317-
318-
```console
319-
tsc
320-
```
321-
322-
1. Run the application with the following command:
323-
324-
```console
325-
node Text-to-speech.js
326-
```
327-
328-
#### [JavaScript (API key)](#tab/javascript-key)
165+
#### [API key](#tab/javascript-key)
329166

330167
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:
331168

0 commit comments

Comments
 (0)