|
| 1 | +--- |
| 2 | +services: ai-services |
| 3 | +author: mrbullwinkle |
| 4 | +ms.author: mbullwin |
| 5 | +ms.service: openai |
| 6 | +ms.topic: include |
| 7 | +ms.date: 3/19/2024 |
| 8 | +--- |
| 9 | + |
| 10 | +## Prerequisites |
| 11 | + |
| 12 | +- An Azure subscription. You can [create one for free](https://azure.microsoft.com/free/cognitive-services?azure-portal=true). |
| 13 | +- 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). |
| 14 | +- [The .NET 8.0 SDK](https://dotnet.microsoft.com/en-us/download) |
| 15 | + |
| 16 | +## Set up |
| 17 | + |
| 18 | +### Retrieve key and endpoint |
| 19 | + |
| 20 | +To successfully make a call against Azure OpenAI, you need an *endpoint* and a *key*. |
| 21 | + |
| 22 | +|Variable name | Value | |
| 23 | +|--------------------------|-------------| |
| 24 | +| `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/`.| |
| 25 | +| `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`.| |
| 26 | + |
| 27 | +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'll 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. |
| 28 | + |
| 29 | +:::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 circled in red." lightbox="../media/quickstarts/endpoint.png"::: |
| 30 | + |
| 31 | +## Create the .NET app |
| 32 | + |
| 33 | +1. Create a .NET app using the `dotnet new` command: |
| 34 | + |
| 35 | + ```dotnetcli |
| 36 | + dotnet new console -n OpenAIWhisper |
| 37 | + ``` |
| 38 | +
|
| 39 | +1. Change into the directory of the new app: |
| 40 | +
|
| 41 | + ```dotnetcli |
| 42 | + cd OpenAIWhisper |
| 43 | + ``` |
| 44 | +
|
| 45 | +1. Install the [`Azure.OpenAI`](https://www.nuget.org/packages/Azure.AI.OpenAI/) client library: |
| 46 | +
|
| 47 | + ```dotnetcli |
| 48 | + dotnet add package Azure.AI.OpenAI |
| 49 | + ``` |
| 50 | +
|
| 51 | +## Passwordless authentication is recommended |
| 52 | +
|
| 53 | +Passwordless authentication is more secure than key-based alternatives and is the recommended approach for connecting to Azure services. If you choose to use Passwordless authentication, you'll need to complete the following: |
| 54 | +
|
| 55 | +1. Add the [`Azure.Identity`](https://www.nuget.org/packages/Azure.Identity) package. |
| 56 | +
|
| 57 | + ```dotnetcli |
| 58 | + dotnet add package Azure.Identity |
| 59 | + ``` |
| 60 | +
|
| 61 | +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**. |
| 62 | +1. Sign-in to Azure using Visual Studio or the Azure CLI via `az login`. |
| 63 | +
|
| 64 | +## Update the app code |
| 65 | +
|
| 66 | +1. Replace the contents of `program.cs` with the following code and update the placeholder values with your own. |
| 67 | +
|
| 68 | + > [!NOTE] |
| 69 | + > You can get sample audio files, such as *wikipediaOcelot.wav*, from the [Azure AI Speech SDK repository at GitHub](https://github.com/Azure-Samples/cognitive-services-speech-sdk/tree/master/sampledata/audiofiles). |
| 70 | + |
| 71 | + ```csharp |
| 72 | + using Azure; |
| 73 | + using Azure.AI.OpenAI; |
| 74 | + using Azure.Identity; // Required for Passwordless auth |
| 75 | + |
| 76 | + var endpoint = new Uri("YOUR_OPENAI_ENDPOINT"); |
| 77 | + var credentials = new AzureKeyCredential("YOUR_OPENAI_KEY"); |
| 78 | + // var credentials = new DefaultAzureCredential(); // Use this line for Passwordless auth |
| 79 | + var deploymentName = "whisper"; // Default deployment name, update with your own if necessary |
| 80 | + var audioFilePath = "YOUR_AUDIO_FILE_PATH"; |
| 81 | + |
| 82 | + var openAIClient = new AzureOpenAIClient(endpoint, credentials); |
| 83 | + |
| 84 | + var audioClient = openAIClient.GetAudioClient(deploymentName); |
| 85 | + |
| 86 | + var result = await audioClient.TranscribeAudioAsync(audioFilePath); |
| 87 | + |
| 88 | + Console.WriteLine("Transcribed text:"); |
| 89 | + foreach (var item in result.Value.Text) |
| 90 | + { |
| 91 | + Console.Write(item); |
| 92 | + } |
| 93 | + ``` |
| 94 | +
|
| 95 | + > [!IMPORTANT] |
| 96 | + > 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). |
| 97 | +
|
| 98 | +1. Run the application using the `dotnet run` command or the run button at the top of Visual Studio: |
| 99 | +
|
| 100 | + ```dotnetcli |
| 101 | + dotnet run |
| 102 | + ``` |
| 103 | +
|
| 104 | + If you are using the sample audio file, you should see the following text printed out in the console: |
| 105 | +
|
| 106 | + ```text |
| 107 | + The ocelot, Lepardus paradalis, is a small wild cat native to the southwestern United States, |
| 108 | + Mexico, and Central and South America. This medium-sized cat is characterized by solid |
| 109 | + black spots and streaks on its coat, round ears... |
| 110 | + ``` |
| 111 | + |
0 commit comments