Skip to content

Commit a6f5c47

Browse files
authored
Merge pull request #441 from alexwolfmsft/dotnet-speech-quickstart
dotnet speech quickstart
2 parents 644905a + a198e80 commit a6f5c47

File tree

3 files changed

+129
-2
lines changed

3 files changed

+129
-2
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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.

articles/ai-services/openai/text-to-speech-quickstart.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ ms.reviewer: v-baolianzou
1010
ms.author: eur
1111
author: eric-urban
1212
recommendations: false
13-
zone_pivot_groups: programming-languages-rest-js
13+
zone_pivot_groups: programming-languages-rest-js-cs
1414
---
1515

1616
# Quickstart: Text to speech with the Azure OpenAI Service
@@ -19,7 +19,6 @@ In this quickstart, you use the Azure OpenAI Service for text to speech with Ope
1919

2020
The available voices are: `alloy`, `echo`, `fable`, `onyx`, `nova`, and `shimmer`. For more information, see [Azure OpenAI Service reference documentation for text to speech](./reference.md#text-to-speech).
2121

22-
2322
::: zone pivot="rest-api"
2423

2524
[!INCLUDE [REST API quickstart](includes/text-to-speech-rest.md)]
@@ -32,6 +31,12 @@ The available voices are: `alloy`, `echo`, `fable`, `onyx`, `nova`, and `shimmer
3231

3332
::: zone-end
3433

34+
::: zone pivot="programming-language-dotnet"
35+
36+
[!INCLUDE [.NET quickstart](includes/text-to-speech-dotnet.md)]
37+
38+
::: zone-end
39+
3540
## Clean up resources
3641

3742
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.

zone-pivots/zone-pivot-groups.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,16 @@ groups:
228228
title: JavaScript
229229
- id: rest-api
230230
title: REST
231+
- id: programming-languages-rest-js-cs
232+
title: Programming languages
233+
prompt: Choose a programming language
234+
pivots:
235+
- id: programming-language-javascript
236+
title: JavaScript
237+
- id: programming-language-dotnet
238+
title: C#
239+
- id: rest-api
240+
title: REST
231241
# Owner: diberry
232242
- id: programming-languages-rest-ps-py-js
233243
title: Programming languages

0 commit comments

Comments
 (0)