|
| 1 | +--- |
| 2 | +ms.topic: include |
| 3 | +manager: nitinme |
| 4 | +ms.service: azure-ai-openai |
| 5 | +ms.topic: include |
| 6 | +ms.date: 09/23/2024 |
| 7 | +ms.reviewer: v-baolianzou |
| 8 | +ms.author: alexwolf |
| 9 | +author: alexwolfmsft |
| 10 | +recommendations: false |
| 11 | +--- |
| 12 | + |
| 13 | +## Prerequisites |
| 14 | + |
| 15 | +- An Azure subscription. You can [create one for free](https://azure.microsoft.com/free/cognitive-services?azure-portal=true). |
| 16 | +- An Azure OpenAI resource with a Whisper model deployed in a [supported region](../concepts/models.md#whisper-models). For more information, see [Create a resource and deploy a model with Azure OpenAI](../how-to/create-resource.md). |
| 17 | +- [The .NET 8.0 SDK](https://dotnet.microsoft.com/en-us/download) |
| 18 | + |
| 19 | +## Create the .NET app |
| 20 | + |
| 21 | +1. Create a .NET app using the `dotnet new` command: |
| 22 | + |
| 23 | + ```dotnetcli |
| 24 | + dotnet new console -n TextToSpeech |
| 25 | + ``` |
| 26 | +
|
| 27 | +1. Change into the directory of the new app: |
| 28 | +
|
| 29 | + ```dotnetcli |
| 30 | + cd OpenAISpeech |
| 31 | + ``` |
| 32 | +
|
| 33 | +1. Install the [`Azure.OpenAI`](https://www.nuget.org/packages/Azure.AI.OpenAI/) client library: |
| 34 | + |
| 35 | + ```dotnetcli |
| 36 | + dotnet add package Azure.AI.OpenAI |
| 37 | + ``` |
| 38 | +
|
| 39 | +## Authenticate and connect to Azure OpenAI |
| 40 | +
|
| 41 | +To make requests to your Azure OpenAI service, you need the service endpoint as well as authentication credentials via one of the following options: |
| 42 | +
|
| 43 | +- [Microsoft Entra ID](/entra/fundamentals/whatis) is the recommended approach for authenticating to Azure services and is more secure than key-based alternatives. |
| 44 | +- Access keys allow you to provide a secret key to connect to your resource. |
| 45 | +
|
| 46 | + > [!IMPORTANT] |
| 47 | + > Access keys should be used with caution. If your service access key is lost or accidentally exposed in an insecure location, your service may become vulnerable. Anyone who has the access key is able to authorize requests against the Azure OpenAI service. |
| 48 | +
|
| 49 | +### Get the Azure OpenAI endpoint |
| 50 | +
|
| 51 | +The service endpoint 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/`. |
| 52 | +
|
| 53 | +:::image type="content" source="../media/quickstarts/endpoint.png" alt-text="Screenshot of the overview UI for an Azure OpenAI resource in the Azure portal with the endpoint & access keys location highlighted." lightbox="../media/quickstarts/endpoint.png"::: |
| 54 | +
|
| 55 | +### Authenticate using Microsoft Entra ID |
| 56 | +
|
| 57 | +If you choose to use Microsoft Entra ID authentication, you'll need to complete the following: |
| 58 | +
|
| 59 | +1. Add the [`Azure.Identity`](https://www.nuget.org/packages/Azure.Identity) package. |
| 60 | +
|
| 61 | + ```dotnetcli |
| 62 | + dotnet add package Azure.Identity |
| 63 | + ``` |
| 64 | +
|
| 65 | +1. 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**. |
| 66 | +1. Sign-in to Azure using Visual Studio or the Azure CLI via `az login`. |
| 67 | +
|
| 68 | +### Authenticate using keys |
| 69 | +
|
| 70 | +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. |
| 71 | +
|
| 72 | +## Update the app code |
| 73 | +
|
| 74 | +1. Replace the contents of `program.cs` with the following code and update the placeholder values with your own. |
| 75 | +
|
| 76 | + ```csharp |
| 77 | + using Azure; |
| 78 | + using Azure.AI.OpenAI; |
| 79 | + using Azure.Identity; // Required for Passwordless auth |
| 80 | + |
| 81 | + var endpoint = new Uri( |
| 82 | + Environment.GetEnvironmentVariable("YOUR_OPENAI_ENDPOINT") ?? throw new ArgumentNullException()); |
| 83 | + var credentials = new DefaultAzureCredential(); |
| 84 | +
|
| 85 | + // Use this line for key auth |
| 86 | + // var credentials = new AzureKeyCredential( |
| 87 | + // Environment.GetEnvironmentVariable("YOUR_OPENAI_KEY") ?? throw new ArgumentNullException()); |
| 88 | +
|
| 89 | + var deploymentName = "tts"; // Default deployment name, update with your own if necessary |
| 90 | + var speechFilePath = "YOUR_AUDIO_FILE_PATH"; |
| 91 | + |
| 92 | + var openAIClient = new AzureOpenAIClient(endpoint, credentials); |
| 93 | + var audioClient = openAIClient.GetAudioClient(deploymentName); |
| 94 | + |
| 95 | + var result = await audioClient.GenerateSpeechAsync( |
| 96 | + "the quick brown chicken jumped over the lazy dogs"); |
| 97 | + |
| 98 | + Console.WriteLine("Streaming response to ${speechFilePath}"); |
| 99 | + await File.WriteAllBytesAsync(speechFilePath, result.Value.ToArray()); |
| 100 | + Console.WriteLine("Finished streaming"); |
| 101 | + ``` |
| 102 | +
|
| 103 | + > [!IMPORTANT] |
| 104 | + > For production, store and access your credentials using a secure method, such as [Azure Key Vault](/azure/key-vault/general/overview). For more information about credential security, see [Azure AI services security](../../security-features.md). |
| 105 | +
|
| 106 | +1. Run the application using the `dotnet run` command or the run button at the top of Visual Studio: |
| 107 | +
|
| 108 | + ```dotnetcli |
| 109 | + dotnet run |
| 110 | + ``` |
| 111 | +
|
| 112 | + 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. |
0 commit comments