Skip to content

Latest commit

 

History

History
132 lines (88 loc) · 5.05 KB

gpt-v-dotnet.md

File metadata and controls

132 lines (88 loc) · 5.05 KB
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.

Prerequisites

Microsoft Entra ID prerequisites

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.

Set up

  1. Create a new folder vision-quickstart and go to the quickstart folder with the following command:

    mkdir vision-quickstart && cd vision-quickstart
  2. Create a new console application with the following command:

    dotnet new console
  3. Install the OpenAI .NET client library with the dotnet add package command:

    dotnet add package Azure.AI.OpenAI
  4. For the recommended keyless authentication with Microsoft Entra ID, install the Azure.Identity package with:

    dotnet add package Azure.Identity
  5. For the recommended keyless authentication with Microsoft Entra ID, sign in to Azure with the following command:

    az login

Retrieve resource information

[!INCLUDE resource authentication]

Run the quickstart

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:

  1. 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}");
  2. Run the application using the dotnet run command or the run button at the top of Visual Studio:

    dotnet run
    

Output

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.

Clean up resources

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.