Quickstart: Text to speech with the Azure OpenAI Service
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.
Prerequisites
- An Azure subscription - Create one for free.
- An Azure OpenAI resource created in the North Central US or Sweden Central regions with the
tts-1
ortts-1-hd
model deployed. For more information, see Create a resource and deploy a model with Azure OpenAI.
Set up
Retrieve key and endpoint
To successfully make a call against Azure OpenAI, you need an endpoint and a key.
Variable name | Value |
---|---|
AZURE_OPENAI_ENDPOINT |
This value can be found in the Keys & Endpoint section when examining your resource from the Azure portal. Alternatively, you can find the value in the Azure OpenAI Studio > Playground > Code View. An example endpoint is: https://aoai-docs.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.
Environment variables
Create and assign persistent environment variables for your key and endpoint.
Important
If you use an API key, store it securely somewhere else, such as in Azure Key Vault. Don't include the API key directly in your code, and never post it publicly.
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"
Create a REST request and response
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
Prerequisites
- An Azure subscription - Create one for free
- LTS versions of Node.js
- An Azure OpenAI resource created in a supported region (see Region availability). For more information, see Create a resource and deploy a model with Azure OpenAI.
Set up
Retrieve key and endpoint
To successfully make a call against Azure OpenAI, you need an endpoint and a key.
Variable name | Value |
---|---|
AZURE_OPENAI_ENDPOINT |
This value can be found in the Keys & Endpoint section when examining your resource from the Azure portal. Alternatively, you can find the value in the Azure OpenAI Studio > Playground > Code View. An example endpoint is: https://aoai-docs.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.
Environment variables
Create and assign persistent environment variables for your key and endpoint.
Important
If you use an API key, store it securely somewhere else, such as in Azure Key Vault. Don't include the API key directly in your code, and never post it publicly.
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"
Create a Node application
In a console window (such as cmd, PowerShell, or Bash), create a new directory for your app, and navigate to it. Then run the npm init
command to create a node application with a package.json file.
npm init
Install the client library
Install the client libraries with:
npm install openai @azure/identity
Your app's package.json file will be updated with the dependencies.
Create a speech file
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:
require("dotenv/config"); const { writeFile } = require("fs/promises"); const { AzureOpenAI } = require("openai"); require("openai/shims/node"); // You will need to set these environment variables or edit the following values const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>"; const apiKey = process.env["AZURE_OPENAI_API_KEY"] || "<api key>"; const speechFilePath = process.env["SPEECH_FILE_PATH"] || "<path to save the speech file>"; // Required Azure OpenAI deployment name and API version const deploymentName = "tts"; const apiVersion = "2024-08-01-preview"; function getClient() { return new AzureOpenAI({ endpoint, apiKey, 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); });
Run the script with the following command:
node Text-to-speech.js
Clean up resources
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.
Next steps
- Learn more about how to work with text to speech with Azure OpenAI Service in the Azure OpenAI Service reference documentation.
- For more examples, check out the Azure OpenAI Samples GitHub repository