title | titleSuffix | description | services | manager | ms.service | ms.topic | ms.custom | ms.date |
---|---|---|---|---|---|---|---|---|
Quickstart: Use GPT-4 Turbo with Vision on your images with the .NET SDK |
Azure OpenAI |
Get started using the Azure OpenAI .NET SDK to deploy and use the GPT-4 Turbo with Vision model. |
cognitive-services |
nitinme |
azure-ai-openai |
include |
references_regions |
3/11/2025 |
Use this article to get started using the Azure OpenAI .NET SDK to deploy and use the GPT-4 Turbo with Vision model.
- An Azure subscription. You can create one for free.
- The .NET 8.0 SDK
- An Azure OpenAI Service resource with a GPT-4 Turbo with Vision model deployed. See GPT-4 and GPT-4 Turbo Preview model availability for available regions. For more information about resource creation, see the resource deployment guide.
For the recommended keyless authentication with Microsoft Entra ID, you need to:
- Install the Azure CLI used for keyless authentication with Microsoft Entra ID.
- Assign the
Cognitive Services User
role to your user account. You can assign roles in the Azure portal under Access control (IAM) > Add role assignment.
-
Create a new folder
vision-quickstart
and go to the quickstart folder with the following command:mkdir vision-quickstart && cd vision-quickstart
-
Create a new console application with the following command:
dotnet new console
-
Install the OpenAI .NET client library with the dotnet add package command:
dotnet add package Azure.AI.OpenAI
-
For the recommended keyless authentication with Microsoft Entra ID, install the Azure.Identity package with:
dotnet add package Azure.Identity
-
For the recommended keyless authentication with Microsoft Entra ID, sign in to Azure with the following command:
az login
[!INCLUDE resource authentication]
The sample code in this quickstart uses Microsoft Entra ID for the recommended keyless authentication. If you prefer to use an API key, you can replace the DefaultAzureCredential
object with an AzureKeyCredential
object.
AzureOpenAIClient openAIClient = new AzureOpenAIClient(new Uri(endpoint), new DefaultAzureCredential());
AzureOpenAIClient openAIClient = new AzureOpenAIClient(new Uri(endpoint), new AzureKeyCredential(key));
To run the quickstart, follow these steps:
-
Replace the contents of
Program.cs
with the following code and update the placeholder values with your own.using Azure; using Azure.AI.OpenAI; using Azure.Identity; using OpenAI.Chat; // Required for Passwordless auth string deploymentName = "gpt-4"; string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? "https://<your-resource-name>.openai.azure.com/"; string key = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY") ?? "<your-key>"; // Use the recommended keyless credential instead of the AzureKeyCredential credential. AzureOpenAIClient openAIClient = new AzureOpenAIClient(new Uri(endpoint), new DefaultAzureCredential()); //AzureOpenAIClient openAIClient = new AzureOpenAIClient(new Uri(endpoint), new AzureKeyCredential(key)); var chatClient = openAIClient.GetChatClient(deploymentName); var imageUri = "YOUR_IMAGE_URL"; var chatMessages = new List<ChatMessage> { new SystemChatMessage("You are a helpful assistant."), new UserChatMessage($"Describe this picture: {imageUrl}") }; ChatCompletion chatCompletion = await chatClient.CompleteChatAsync(chatMessages); Console.WriteLine($"[ASSISTANT]:"); Console.WriteLine($"{chatCompletion.Content[0].Text}");
-
Run the application using the
dotnet run
command or the run button at the top of Visual Studio:dotnet run
The output of the application will be a description of the image you provided in the imageUri
variable. The assistant will analyze the image and provide a detailed description based on its content.
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.