Skip to content

Commit efaa187

Browse files
committed
tabs
1 parent 4b906c5 commit efaa187

File tree

2 files changed

+200
-199
lines changed

2 files changed

+200
-199
lines changed

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

Lines changed: 114 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -114,64 +114,64 @@ Your app's _package.json_ file will be updated with the dependencies.
114114

115115
#### [TypeScript](#tab/typescript)
116116

117-
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:
118-
119-
```typescript
120-
import "dotenv/config";
121-
import { writeFile } from "fs/promises";
122-
import { AzureOpenAI } from "openai";
123-
import type { SpeechCreateParams } from "openai/resources/audio/speech";
124-
import "openai/shims/node";
125-
126-
// You will need to set these environment variables or edit the following values
127-
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>";
128-
const apiKey = process.env["AZURE_OPENAI_API_KEY"] || "<api key>";
129-
const speechFilePath =
130-
process.env["SPEECH_FILE_PATH"] || "<path to save the speech file>";
131-
132-
// Required Azure OpenAI deployment name and API version
133-
const deploymentName = "tts";
134-
const apiVersion = "2024-07-01-preview";
135-
136-
function getClient(): AzureOpenAI {
137-
return new AzureOpenAI({
138-
endpoint,
139-
apiKey,
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 "openai/shims/node" import supports the ability for the a stream type that changes depending on whether you run in Node.js or in another environment such as a browser.
117+
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:
118+
119+
```typescript
120+
import "dotenv/config";
121+
import { writeFile } from "fs/promises";
122+
import { AzureOpenAI } from "openai";
123+
import type { SpeechCreateParams } from "openai/resources/audio/speech";
124+
import "openai/shims/node";
125+
126+
// You will need to set these environment variables or edit the following values
127+
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>";
128+
const apiKey = process.env["AZURE_OPENAI_API_KEY"] || "<api key>";
129+
const speechFilePath =
130+
process.env["SPEECH_FILE_PATH"] || "<path to save the speech file>";
131+
132+
// Required Azure OpenAI deployment name and API version
133+
const deploymentName = "tts";
134+
const apiVersion = "2024-07-01-preview";
135+
136+
function getClient(): AzureOpenAI {
137+
return new AzureOpenAI({
138+
endpoint,
139+
apiKey,
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 "openai/shims/node" import supports the ability for the a stream type that changes depending on whether you run in Node.js or in another environment such as a browser.
175175

176176
1. Build the application with the following command:
177177

@@ -188,63 +188,63 @@ The "openai/shims/node" import supports the ability for the a stream type that c
188188

189189
#### [JavaScript](#tab/javascript)
190190

191-
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:
192-
193-
```javascript
194-
require("dotenv/config");
195-
const { writeFile } = require("fs/promises");
196-
const { AzureOpenAI } = require("openai");
197-
require("openai/shims/node");
198-
199-
// You will need to set these environment variables or edit the following values
200-
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>";
201-
const apiKey = process.env["AZURE_OPENAI_API_KEY"] || "<api key>";
202-
const speechFilePath =
203-
process.env["SPEECH_FILE_PATH"] || "<path to save the speech file>";
204-
205-
// Required Azure OpenAI deployment name and API version
206-
const deploymentName = "tts";
207-
const apiVersion = "2024-07-01-preview";
208-
209-
function getClient() {
210-
return new AzureOpenAI({
211-
endpoint,
212-
apiKey,
213-
apiVersion,
214-
deployment: deploymentName,
215-
});
216-
}
217-
218-
async function generateAudioStream(
219-
client,
220-
params
221-
) {
222-
const response = await client.audio.speech.create(params);
223-
if (response.ok) return response.body;
224-
throw new Error(`Failed to generate audio stream: ${response.statusText}`);
225-
}
226-
export async function main() {
227-
console.log("== Text to Speech Sample ==");
228-
229-
const client = getClient();
230-
const streamToRead = await generateAudioStream(client, {
231-
model: deploymentName,
232-
voice: "alloy",
233-
input: "the quick brown chicken jumped over the lazy dogs",
234-
});
235-
236-
console.log(`Streaming response to ${speechFilePath}`);
237-
await writeFile(speechFilePath, streamToRead);
238-
console.log("Finished streaming");
239-
}
240-
241-
main().catch((err) => {
242-
console.error("The sample encountered an error:", err);
243-
});
244-
245-
```
191+
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:
192+
193+
```javascript
194+
require("dotenv/config");
195+
const { writeFile } = require("fs/promises");
196+
const { AzureOpenAI } = require("openai");
197+
require("openai/shims/node");
198+
199+
// You will need to set these environment variables or edit the following values
200+
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>";
201+
const apiKey = process.env["AZURE_OPENAI_API_KEY"] || "<api key>";
202+
const speechFilePath =
203+
process.env["SPEECH_FILE_PATH"] || "<path to save the speech file>";
204+
205+
// Required Azure OpenAI deployment name and API version
206+
const deploymentName = "tts";
207+
const apiVersion = "2024-07-01-preview";
208+
209+
function getClient() {
210+
return new AzureOpenAI({
211+
endpoint,
212+
apiKey,
213+
apiVersion,
214+
deployment: deploymentName,
215+
});
216+
}
217+
218+
async function generateAudioStream(
219+
client,
220+
params
221+
) {
222+
const response = await client.audio.speech.create(params);
223+
if (response.ok) return response.body;
224+
throw new Error(`Failed to generate audio stream: ${response.statusText}`);
225+
}
226+
export async function main() {
227+
console.log("== Text to Speech Sample ==");
228+
229+
const client = getClient();
230+
const streamToRead = await generateAudioStream(client, {
231+
model: deploymentName,
232+
voice: "alloy",
233+
input: "the quick brown chicken jumped over the lazy dogs",
234+
});
235+
236+
console.log(`Streaming response to ${speechFilePath}`);
237+
await writeFile(speechFilePath, streamToRead);
238+
console.log("Finished streaming");
239+
}
240+
241+
main().catch((err) => {
242+
console.error("The sample encountered an error:", err);
243+
});
244+
245+
```
246246

247-
Run the script with the following command:
247+
1. Run the script with the following command:
248248

249249
```console
250250
node Text-to-speech.js

0 commit comments

Comments
 (0)