@@ -97,28 +97,14 @@ In a console window (such as cmd, PowerShell, or Bash), create a new directory f
97
97
npm init
98
98
```
99
99
100
-
101
-
102
100
## Install the client library
103
101
104
102
Install the client libraries with:
105
103
106
- ## [ ** TypeScript** ] ( #tab/typescript )
107
-
108
- ``` console
109
- npm install openai @azure/openai @azure/identity
110
- ```
111
-
112
- The ` @azure/openai ` package provides the types the Azure service objects.
113
-
114
- ## [ ** JavaScript** ] ( #tab/javascript )
115
-
116
104
``` console
117
105
npm install openai @azure/identity
118
106
```
119
107
120
- ---
121
-
122
108
Your app's _ package.json_ file will be updated with the dependencies.
123
109
124
110
## Create a speech file
@@ -129,6 +115,58 @@ Your app's _package.json_ file will be updated with the dependencies.
129
115
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:
130
116
131
117
``` typescript
118
+ import " dotenv/config" ;
119
+ import { writeFile } from " fs/promises" ;
120
+ import { AzureOpenAI } from " openai" ;
121
+ import type { SpeechCreateParams } from " openai/resources/audio/speech" ;
122
+ import " openai/shims/node" ;
123
+
124
+ // You will need to set these environment variables or edit the following values
125
+ const endpoint = process .env [" AZURE_OPENAI_ENDPOINT" ] || " <endpoint>" ;
126
+ const apiKey = process .env [" AZURE_OPENAI_API_KEY" ] || " <api key>" ;
127
+ const speechFilePath =
128
+ process .env [" SPEECH_FILE_PATH" ] || " <path to save the speech file>" ;
129
+
130
+ // Required Azure OpenAI deployment name and API version
131
+ const deploymentName = " tts" ;
132
+ const apiVersion = " 2024-07-01-preview" ;
133
+
134
+ function getClient(): AzureOpenAI {
135
+ return new AzureOpenAI ({
136
+ endpoint ,
137
+ apiKey ,
138
+ apiVersion ,
139
+ deployment: deploymentName ,
140
+ });
141
+ }
142
+
143
+ async function generateAudioStream(
144
+ client : AzureOpenAI ,
145
+ params : SpeechCreateParams
146
+ ): Promise <NodeJS .ReadableStream > {
147
+ const response = await client .audio .speech .create (params );
148
+ if (response .ok ) return response .body ;
149
+ throw new Error (` Failed to generate audio stream: ${response .statusText } ` );
150
+ }
151
+ export async function main() {
152
+ console .log (" == Text to Speech Sample ==" );
153
+
154
+ const client = getClient ();
155
+ const streamToRead = await generateAudioStream (client , {
156
+ model: deploymentName ,
157
+ voice: " alloy" ,
158
+ input: " the quick brown chicken jumped over the lazy dogs" ,
159
+ });
160
+
161
+ console .log (` Streaming response to ${speechFilePath } ` );
162
+ await writeFile (speechFilePath , streamToRead );
163
+ console .log (" Finished streaming" );
164
+ }
165
+
166
+ main ().catch ((err ) => {
167
+ console .error (" The sample encountered an error:" , err );
168
+ });
169
+
132
170
```
133
171
134
172
1 . Build the application with the following command:
@@ -149,6 +187,57 @@ Create a new file named _Text-to-speech.ts_ and open it in your preferred code e
149
187
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:
150
188
151
189
```javascript
190
+ require("dotenv/config");
191
+ const { writeFile } = require("fs/promises");
192
+ const { AzureOpenAI } = require("openai");
193
+ require("openai/shims/node");
194
+
195
+ // You will need to set these environment variables or edit the following values
196
+ const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>";
197
+ const apiKey = process.env["AZURE_OPENAI_API_KEY"] || "<api key>";
198
+ const speechFilePath =
199
+ process.env["SPEECH_FILE_PATH"] || "<path to save the speech file>";
200
+
201
+ // Required Azure OpenAI deployment name and API version
202
+ const deploymentName = "tts";
203
+ const apiVersion = "2024-07-01-preview";
204
+
205
+ function getClient() {
206
+ return new AzureOpenAI({
207
+ endpoint,
208
+ apiKey,
209
+ apiVersion,
210
+ deployment: deploymentName,
211
+ });
212
+ }
213
+
214
+ async function generateAudioStream(
215
+ client,
216
+ params
217
+ ) {
218
+ const response = await client.audio.speech.create(params);
219
+ if (response.ok) return response.body;
220
+ throw new Error(`Failed to generate audio stream: ${response.statusText}`);
221
+ }
222
+ export async function main() {
223
+ console.log("== Text to Speech Sample ==");
224
+
225
+ const client = getClient();
226
+ const streamToRead = await generateAudioStream(client, {
227
+ model: deploymentName,
228
+ voice: "alloy",
229
+ input: "the quick brown chicken jumped over the lazy dogs",
230
+ });
231
+
232
+ console.log(`Streaming response to ${speechFilePath}`);
233
+ await writeFile(speechFilePath, streamToRead);
234
+ console.log("Finished streaming");
235
+ }
236
+
237
+ main().catch((err) => {
238
+ console.error("The sample encountered an error:", err);
239
+ });
240
+
152
241
```
153
242
154
243
Run the script with the following command:
0 commit comments