Edit

Share via


Connect to and prompt an AI model

In this quickstart, you learn how to create a .NET console chat app to connect to and prompt an OpenAI or Azure OpenAI model. The app uses the Microsoft.Extensions.AI library so you can write code using AI abstractions rather than a specific SDK. AI abstractions enable you to change the underlying AI model with minimal code changes.

Note

The Microsoft.Extensions.AI library is currently in Preview.

Prerequisites

Prerequisites

Note

You can also use Semantic Kernel to accomplish the tasks in this article. Semantic Kernel is a lightweight, open-source SDK that lets you build AI agents and integrate the latest AI models into your .NET apps.

(Optional) Clone the sample repository

You can create your own app using the steps in the sections ahead, or you can clone the GitHub repository that contains the completed sample apps for all of the quickstarts. If you plan to use Azure OpenAI, the sample repo is also structured as an Azure Developer CLI template that can provision an Azure OpenAI resource for you.

git clone https://github.com/dotnet/ai-samples.git

Create the app

Complete the following steps to create a .NET console app to connect to an AI model.

  1. In an empty directory on your computer, use the dotnet new command to create a new console app:

    dotnet new console -o ExtensionsAI
    
  2. Change directory into the app folder:

    cd ExtensionsAI
    
  3. Install the required packages:

    dotnet add package Azure.AI.OpenAI
    dotnet add package Azure.Identity
    dotnet add package Microsoft.Extensions.AI.OpenAI --prerelease
    dotnet add package Microsoft.Extensions.Configuration
    dotnet add package Microsoft.Extensions.Configuration.UserSecrets
    
    dotnet add package OpenAI
    dotnet add package Microsoft.Extensions.AI.OpenAI --prerelease
    dotnet add package Microsoft.Extensions.Configuration
    dotnet add package Microsoft.Extensions.Configuration.UserSecrets
    
  4. Open the app in Visual Studio Code or your editor of choice.

Create the AI service

  1. To provision an Azure OpenAI service and model, complete the steps in the Create and deploy an Azure OpenAI Service resource article.

  2. From a terminal or command prompt, navigate to the root of your project directory.

  3. Run the following commands to configure your Azure OpenAI endpoint and model name for the sample app:

    dotnet user-secrets init
    dotnet user-secrets set AZURE_OPENAI_ENDPOINT <your-azure-openai-endpoint>
    dotnet user-secrets set AZURE_OPENAI_GPT_NAME <your-azure-openai-model-name>
    

Configure the app

  1. Navigate to the root of your .NET project from a terminal or command prompt.

  2. Run the following commands to configure your OpenAI API key as a secret for the sample app:

    dotnet user-secrets init
    dotnet user-secrets set OpenAIKey <your-openai-key>
    dotnet user-secrets set ModelName <your-openai-model-name>
    

Add the app code

The app uses the Microsoft.Extensions.AI package to send and receive requests to the AI model.

  1. Copy the benefits.md file to your project directory. Configure the project to copy this file to the output directory. If you're using Visual Studio, right-click on the file in Solution Explorer, select Properties, and then set Copy to Output Directory to Copy if newer.

  2. In the Program.cs file, add the following code to connect and authenticate to the AI model.

    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.AI;
    using Azure.AI.OpenAI;
    using Azure.Identity;
    
    var config = new ConfigurationBuilder()
        .AddUserSecrets<Program>()
        .Build();
    string endpoint = config["AZURE_OPENAI_ENDPOINT"];
    string deployment = config["AZURE_OPENAI_GPT_NAME"];
    
    IChatClient client =
        new AzureOpenAIClient(new Uri(endpoint), new DefaultAzureCredential())
            .GetChatClient(deployment)
            .AsIChatClient();
    

    Note

    DefaultAzureCredential searches for authentication credentials from your local tooling. If you aren't using the azd template to provision the Azure OpenAI resource, you'll need to assign the Azure AI Developer role to the account you used to sign-in to Visual Studio or the Azure CLI. For more information, see Authenticate to Azure AI services with .NET.

    using Microsoft.Extensions.AI;
    using Microsoft.Extensions.Configuration;
    using OpenAI;
    
    IConfigurationRoot config = new ConfigurationBuilder()
        .AddUserSecrets<Program>()
        .Build();
    string? model = config["ModelName"];
    string? key = config["OpenAIKey"];
    
    IChatClient client =
        new OpenAIClient(key).GetChatClient(model).AsIChatClient();
    
  3. Add code to read the benefits.md file content and then create a prompt for the model. The prompt instructs the model to summarize the file's text content in 20 words or less.

    string text = File.ReadAllText("benefits.md");
    string prompt = $"""
        Summarize the the following text in 20 words or less:
        {text}
        """;
    
  4. Call the GetResponseAsync method to send the prompt to the model to generate a response.

    // Submit the prompt and print out the response.
    ChatResponse response = await client.GetResponseAsync(
        prompt,
        new ChatOptions { MaxOutputTokens = 400 });
    Console.WriteLine(response);
    
  5. Run the app:

    dotnet run
    

    The app prints out the completion response from the AI model. Customize the text content of the benefits.md file or the length of the summary to see the differences in the responses.

Clean up resources

When you no longer need the sample application or resources, remove the corresponding deployment and all resources.

azd down

Next steps