Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
In this quickstart, you use the Azure OpenAI Service for text to speech with OpenAI voices.
The available voices are: alloy
, echo
, fable
, onyx
, nova
, and shimmer
. For more information, see Azure OpenAI Service reference documentation for text to speech.
tts-1
or tts-1-hd
model deployed. For more information, see Create a resource and deploy a model with Azure OpenAI.To successfully make a call against Azure OpenAI, you need an endpoint and a key.
Variable name | Value |
---|---|
AZURE_OPENAI_ENDPOINT |
The service endpoint can be found in the Keys & Endpoint section when examining your resource from the Azure portal. Alternatively, you can find the endpoint via the Deployments page in Azure AI Foundry portal. An example endpoint is: https://docs-test-001.openai.azure.com/ . |
AZURE_OPENAI_API_KEY |
This value can be found in the Keys & Endpoint section when examining your resource from the Azure portal. You can use either KEY1 or KEY2 . |
Go to your resource in the Azure portal. The Endpoint and Keys can be found in the Resource Management section. Copy your endpoint and access key as you need both for authenticating your API calls. You can use either KEY1
or KEY2
. Always having two keys allows you to securely rotate and regenerate keys without causing a service disruption.
Create and assign persistent environment variables for your key and endpoint.
Important
Use API keys with caution. Don't include the API key directly in your code, and never post it publicly. If you use an API key, store it securely in Azure Key Vault. For more information about using API keys securely in your apps, see API keys with Azure Key Vault.
For more information about AI services security, see Authenticate requests to Azure AI services.
setx AZURE_OPENAI_API_KEY "REPLACE_WITH_YOUR_KEY_VALUE_HERE"
setx AZURE_OPENAI_ENDPOINT "REPLACE_WITH_YOUR_ENDPOINT_HERE"
In a bash shell, run the following command. You need to replace YourDeploymentName
with the deployment name you chose when you deployed the text to speech model. The deployment name isn't necessarily the same as the model name. Entering the model name results in an error unless you chose a deployment name that is identical to the underlying model name.
curl $AZURE_OPENAI_ENDPOINT/openai/deployments/YourDeploymentName/audio/speech?api-version=2024-02-15-preview \
-H "api-key: $AZURE_OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "tts-1-hd",
"input": "I'm excited to try text to speech.",
"voice": "alloy"
}' --output speech.mp3
The format of your first line of the command with an example endpoint would appear as follows curl https://aoai-docs.openai.azure.com/openai/deployments/{YourDeploymentName}/audio/speech?api-version=2024-02-15-preview \
.
Important
For production, use a secure way of storing and accessing your credentials like Azure Key Vault. For more information about credential security, see the Azure AI services security article.
Source code | Package (npm) | Samples
For the recommended keyless authentication with Microsoft Entra ID, you need to:
Cognitive Services User
role to your user account. You can assign roles in the Azure portal under Access control (IAM) > Add role assignment.Create a new folder synthesis-quickstart
to contain the application and open Visual Studio Code in that folder with the following command:
mkdir synthesis-quickstart && cd synthesis-quickstart
Create the package.json
with the following command:
npm init -y
Install the OpenAI client library for JavaScript with:
npm install openai
For the recommended passwordless authentication:
npm install @azure/identity
You need to retrieve the following information to authenticate your application with your Azure OpenAI resource:
Variable name | Value |
---|---|
AZURE_OPENAI_ENDPOINT |
This value can be found in the Keys and Endpoint section when examining your resource from the Azure portal. |
AZURE_OPENAI_DEPLOYMENT_NAME |
This value will correspond to the custom name you chose for your deployment when you deployed a model. This value can be found under Resource Management > Model Deployments in the Azure portal. |
OPENAI_API_VERSION |
Learn more about API Versions. You can change the version in code or use an environment variable. |
Learn more about keyless authentication and setting environment variables.
Caution
To use the recommended keyless authentication with the SDK, make sure that the AZURE_OPENAI_API_KEY
environment variable isn't set.
Create the index.js
file with the following code:
const { writeFile } = require("fs/promises");
const { AzureOpenAI } = require("openai");
const { DefaultAzureCredential, getBearerTokenProvider } = require("@azure/identity");
require("openai/shims/node");
// You will need to set these environment variables or edit the following values
const endpoint = process.env.AZURE_OPENAI_ENDPOINT || "Your endpoint";
const speechFilePath = "<path to save the speech file>";
// Required Azure OpenAI deployment name and API version
const deploymentName = process.env.AZURE_OPENAI_DEPLOYMENT_NAME || "tts";
const apiVersion = process.env.OPENAI_API_VERSION || "2024-08-01-preview";
// keyless authentication
const credential = new DefaultAzureCredential();
const scope = "https://cognitiveservices.azure.com/.default";
const azureADTokenProvider = getBearerTokenProvider(credential, scope);
function getClient() {
return new AzureOpenAI({
endpoint,
azureADTokenProvider,
apiVersion,
deployment: deploymentName,
});
}
async function generateAudioStream(
client,
params
) {
const response = await client.audio.speech.create(params);
if (response.ok) return response.body;
throw new Error(`Failed to generate audio stream: ${response.statusText}`);
}
export async function main() {
console.log("== Text to Speech Sample ==");
const client = getClient();
const streamToRead = await generateAudioStream(client, {
model: deploymentName,
voice: "alloy",
input: "the quick brown chicken jumped over the lazy dogs",
});
console.log(`Streaming response to ${speechFilePath}`);
await writeFile(speechFilePath, streamToRead);
console.log("Finished streaming");
}
main().catch((err) => {
console.error("The sample encountered an error:", err);
});
Sign in to Azure with the following command:
az login
Run the JavaScript file.
node index.js
Source code | Package (npm) | Samples
For the recommended keyless authentication with Microsoft Entra ID, you need to:
Cognitive Services User
role to your user account. You can assign roles in the Azure portal under Access control (IAM) > Add role assignment.Create a new folder assistants-quickstart
to contain the application and open Visual Studio Code in that folder with the following command:
mkdir assistants-quickstart && cd assistants-quickstart
Create the package.json
with the following command:
npm init -y
Update the package.json
to ECMAScript with the following command:
npm pkg set type=module
Install the OpenAI client library for JavaScript with:
npm install openai
For the recommended passwordless authentication:
npm install @azure/identity
You need to retrieve the following information to authenticate your application with your Azure OpenAI resource:
Variable name | Value |
---|---|
AZURE_OPENAI_ENDPOINT |
This value can be found in the Keys and Endpoint section when examining your resource from the Azure portal. |
AZURE_OPENAI_DEPLOYMENT_NAME |
This value will correspond to the custom name you chose for your deployment when you deployed a model. This value can be found under Resource Management > Model Deployments in the Azure portal. |
OPENAI_API_VERSION |
Learn more about API Versions. You can change the version in code or use an environment variable. |
Learn more about keyless authentication and setting environment variables.
Caution
To use the recommended keyless authentication with the SDK, make sure that the AZURE_OPENAI_API_KEY
environment variable isn't set.
Create the index.ts
file with the following code:
import { writeFile } from "fs/promises";
import { AzureOpenAI } from "openai";
import { DefaultAzureCredential, getBearerTokenProvider } from "@azure/identity";
import type { SpeechCreateParams } from "openai/resources/audio/speech";
import "openai/shims/node";
// You will need to set these environment variables or edit the following values
const endpoint = process.env.AZURE_OPENAI_ENDPOINT || "Your endpoint";
const speechFilePath = "<path to save the speech file>";
// Required Azure OpenAI deployment name and API version
const deploymentName = process.env.AZURE_OPENAI_DEPLOYMENT_NAME || "tts";
const apiVersion = process.env.OPENAI_API_VERSION || "2024-08-01-preview";
// keyless authentication
const credential = new DefaultAzureCredential();
const scope = "https://cognitiveservices.azure.com/.default";
const azureADTokenProvider = getBearerTokenProvider(credential, scope);
function getClient(): AzureOpenAI {
return new AzureOpenAI({
endpoint,
azureADTokenProvider,
apiVersion,
deployment: deploymentName,
});
}
async function generateAudioStream(
client: AzureOpenAI,
params: SpeechCreateParams
): Promise<NodeJS.ReadableStream> {
const response = await client.audio.speech.create(params);
if (response.ok) return response.body;
throw new Error(`Failed to generate audio stream: ${response.statusText}`);
}
export async function main() {
console.log("== Text to Speech Sample ==");
const client = getClient();
const streamToRead = await generateAudioStream(client, {
model: deploymentName,
voice: "alloy",
input: "the quick brown chicken jumped over the lazy dogs",
});
console.log(`Streaming response to ${speechFilePath}`);
await writeFile(speechFilePath, streamToRead);
console.log("Finished streaming");
}
main().catch((err) => {
console.error("The sample encountered an error:", err);
});
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
.
Create the tsconfig.json
file to transpile the TypeScript code and copy the following code for ECMAScript.
{
"compilerOptions": {
"module": "NodeNext",
"target": "ES2022", // Supports top-level await
"moduleResolution": "NodeNext",
"skipLibCheck": true, // Avoid type errors from node_modules
"strict": true // Enable strict type-checking options
},
"include": ["*.ts"]
}
Transpile from TypeScript to JavaScript.
tsc
Sign in to Azure with the following command:
az login
Run the code with the following command:
node index.js
Create a .NET app using the dotnet new
command:
dotnet new console -n TextToSpeech
Change into the directory of the new app:
cd OpenAISpeech
Install the Azure.OpenAI
client library:
dotnet add package Azure.AI.OpenAI
To make requests to your Azure OpenAI service, you need the service endpoint as well as authentication credentials via one of the following options:
Important
Use API keys with caution. Don't include the API key directly in your code, and never post it publicly. If you use an API key, store it securely in Azure Key Vault. For more information about using API keys securely in your apps, see API keys with Azure Key Vault.
For more information about AI services security, see Authenticate requests to Azure AI services.
The service endpoint can be found in the Keys & Endpoint section when examining your resource from the Azure portal. Alternatively, you can find the endpoint via the Deployments page in Azure AI Foundry portal. An example endpoint is: https://docs-test-001.openai.azure.com/
.
If you choose to use Microsoft Entra ID authentication, you'll need to complete the following:
Add the Azure.Identity
package.
dotnet add package Azure.Identity
Assign the Cognitive Services User
role to your user account. This can be done in the Azure portal on your OpenAI resource under Access control (IAM) > Add role assignment.
Sign-in to Azure using Visual Studio or the Azure CLI via az login
.
The access key value can be found in the Keys & Endpoint section when examining your resource from the Azure portal. You can use either KEY1
or KEY2
. Always having two keys allows you to securely rotate and regenerate keys without causing a service disruption.
Replace the contents of program.cs
with the following code and update the placeholder values with your own.
using Azure;
using Azure.AI.OpenAI;
using Azure.Identity; // Required for Passwordless auth
var endpoint = new Uri(
Environment.GetEnvironmentVariable("YOUR_OPENAI_ENDPOINT") ?? throw new ArgumentNullException());
var credentials = new DefaultAzureCredential();
// Use this line for key auth
// var credentials = new AzureKeyCredential(
// Environment.GetEnvironmentVariable("YOUR_OPENAI_KEY") ?? throw new ArgumentNullException());
var deploymentName = "tts"; // Default deployment name, update with your own if necessary
var speechFilePath = "YOUR_AUDIO_FILE_PATH";
var openAIClient = new AzureOpenAIClient(endpoint, credentials);
var audioClient = openAIClient.GetAudioClient(deploymentName);
var result = await audioClient.GenerateSpeechAsync(
"the quick brown chicken jumped over the lazy dogs");
Console.WriteLine("Streaming response to ${speechFilePath}");
await File.WriteAllBytesAsync(speechFilePath, result.Value.ToArray());
Console.WriteLine("Finished streaming");
Run the application using the dotnet run
command or the run button at the top of Visual Studio:
dotnet run
The app generates an audio file at the location you specified for the speechFilePath
variable. Play the file on your device to hear the generated audio.
If you want to clean up and remove an Azure OpenAI resource, you can delete the resource. Before deleting the resource, you must first delete any deployed models.
Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Create your first Azure AI services text to speech application - Training
In this module, you'll learn how to use Azure AI services to create a text to speech application.
Certification
Microsoft Certified: Azure AI Engineer Associate - Certifications
Design and implement an Azure AI solution using Azure AI services, Azure AI Search, and Azure Open AI.