|
| 1 | +--- |
| 2 | +title: 'Quickstart: Use GPT-4 Turbo with Vision on your images and videos with the .NET SDK' |
| 3 | +titleSuffix: Azure OpenAI |
| 4 | +description: Get started using the Azure OpenAI .NET SDK to deploy and use the GPT-4 Turbo with Vision model. |
| 5 | +services: cognitive-services |
| 6 | +manager: nitinme |
| 7 | +ms.service: azure-ai-openai |
| 8 | +ms.topic: include |
| 9 | +ms.custom: references_regions |
| 10 | +ms.date: 01/22/2024 |
| 11 | +--- |
| 12 | + |
| 13 | +Use this article to get started using the Azure OpenAI .NET SDK to deploy and use the GPT-4 Turbo with Vision model. |
| 14 | + |
| 15 | +## Prerequisites |
| 16 | + |
| 17 | +- An Azure subscription. You can [create one for free](https://azure.microsoft.com/free/cognitive-services?azure-portal=true). |
| 18 | +- [The .NET 8.0 SDK](https://dotnet.microsoft.com/en-us/download) |
| 19 | +- An Azure OpenAI Service resource with a GPT-4 Turbo with Vision model deployed. See [GPT-4 and GPT-4 Turbo Preview model availability](../concepts/models.md#gpt-4-and-gpt-4-turbo-model-availability) for available regions. For more information about resource creation, see the [resource deployment guide](/azure/ai-services/openai/how-to/create-resource). |
| 20 | + |
| 21 | +## Set up |
| 22 | + |
| 23 | +### Retrieve key and endpoint |
| 24 | + |
| 25 | +To successfully make a call against Azure OpenAI, you need an **endpoint** and a **key**. |
| 26 | + |
| 27 | +|Variable name | Value | |
| 28 | +|--------------------------|-------------| |
| 29 | +| `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/`.| |
| 30 | +| `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`.| |
| 31 | + |
| 32 | +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. |
| 33 | + |
| 34 | +:::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"::: |
| 35 | + |
| 36 | + |
| 37 | +## Create the .NET app |
| 38 | + |
| 39 | +1. Create a .NET app using the `dotnet new` command: |
| 40 | + |
| 41 | + ```dotnetcli |
| 42 | + dotnet new console -n OpenAISpeech |
| 43 | + ``` |
| 44 | +
|
| 45 | +1. Change into the directory of the new app: |
| 46 | +
|
| 47 | + ```dotnetcli |
| 48 | + cd OpenAISpeech |
| 49 | + ``` |
| 50 | +
|
| 51 | +## Install the client library |
| 52 | +
|
| 53 | +Install the [`Azure.OpenAI`](https://www.nuget.org/packages/Azure.AI.OpenAI/) client library: |
| 54 | +
|
| 55 | +```dotnetcli |
| 56 | +dotnet add package Azure.AI.OpenAI |
| 57 | +``` |
| 58 | + |
| 59 | +## Passwordless authentication is recommended |
| 60 | + |
| 61 | +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: |
| 62 | + |
| 63 | +1. Add the [`Azure.Identity`](https://www.nuget.org/packages/Azure.Identity) package. |
| 64 | + |
| 65 | + ```dotnetcli |
| 66 | + dotnet add package Azure.Identity |
| 67 | + ``` |
| 68 | +
|
| 69 | +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**. |
| 70 | +1. Sign-in to Azure using Visual Studio or the Azure CLI via `az login`. |
| 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; |
| 80 | + using OpenAI.Chat; // Required for Passwordless auth |
| 81 | + |
| 82 | + var endpoint = new Uri("YOUR_AZURE_OPENAI_ENDPOINT"); |
| 83 | + var credentials = new AzureKeyCredential("YOUR_AZURE_OPENAI_KEY"); |
| 84 | + // var credentials = new DefaultAzureCredential(); // Use this line for Passwordless auth |
| 85 | + var deploymentName = "gpt-4"; // Default name, update with your own if needed |
| 86 | + |
| 87 | + var openAIClient = new AzureOpenAIClient(endpoint, credentials); |
| 88 | + var chatClient = openAIClient.GetChatClient(deploymentName); |
| 89 | + |
| 90 | + var imageUri = "YOUR_IMAGE_URL"; |
| 91 | + |
| 92 | + List<ChatMessage> messages = [ |
| 93 | + new UserChatMessage( |
| 94 | + ChatMessageContentPart.CreateTextMessageContentPart("Please describe the following image:"), |
| 95 | + ChatMessageContentPart.CreateImageMessageContentPart(new Uri(imageUri), "image/png")) |
| 96 | + ]; |
| 97 | + |
| 98 | + ChatCompletion chatCompletion = await chatClient.CompleteChatAsync(messages); |
| 99 | + |
| 100 | + Console.WriteLine($"[ASSISTANT]:"); |
| 101 | + Console.WriteLine($"{chatCompletion.Content[0].Text}"); |
| 102 | + ``` |
| 103 | +
|
| 104 | + > [!IMPORTANT] |
| 105 | + > 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). |
| 106 | +
|
| 107 | +1. Run the application using the `dotnet run` command or the run button at the top of Visual Studio: |
| 108 | +
|
| 109 | + ```dotnetcli |
| 110 | + dotnet run |
| 111 | + ``` |
| 112 | +
|
| 113 | + 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. |
| 114 | +
|
| 115 | +## Clean up resources |
| 116 | +
|
| 117 | +If you want to clean up and remove an Azure OpenAI resource, you can delete the resource or resource group. Deleting the resource group also deletes any other resources associated with it. |
| 118 | +
|
| 119 | +- [Azure portal](../../multi-service-resource.md?pivots=azportal#clean-up-resources) |
| 120 | +- [Azure CLI](../../multi-service-resource.md?pivots=azcli#clean-up-resources) |
| 121 | +
|
| 122 | +
|
0 commit comments