Get Started with Azure OpenAI .NET SDK

This Azure OpenAI blog post will show how to use the Azure OpenAI .NET SDK to create an LLM application that generates secure passwords using an OpenAI endpoint hosted on Azure.

The Azure OpenAI client library allows us to connect to Azure OpenAI resources or OpenAI endpoints using the same SDK. Using the SDK, we can do the following.

  • Create chat completions using gpt-4 modules
  • Generate images using dall-e-3
  • Create text embedding
  • Translate and transcribe text and audio

This post will focus on the chat completion function using a GPT-4 module running on Azure as an OpenAI resource (part of Azure AI services).

Before you start, you must have access to Azure OpenAI resources.

Getting Started

The following C# program generates secure passwords. The program has the following 3 components.

  • Program.cs – C# code
  • appsettings.json – Configuration file that holds the endpoint, API key and deployment name of the Azure OpenAI resource.
  • .csproj – Project file with LLM application configuration.

Before you start, go ahead and create a C# console application and install the Azure OpenAI SDK using the following code.

dotnet new console 
dotnet add package Azure.AI.OpenAI --version 1.0.0-beta.6


Once your C# application is ready, create the appsettings.json file in the application’s root folder. Add your endpoint details to the file and save.


Project File

Copy the following code to your .csproj file and save it. The file will add the necessary packages and set the application to look for the appsettings.json file inside the root folder.

<Project Sdk="Microsoft.NET.Sdk">


    <PackageReference Include="Azure.AI.OpenAI" Version="1.0.0-beta.6" />
    <PackageReference Include="Azure.Core" Version="1.38.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0-preview.2.24128.5" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.0-preview.2.24128.5" />

    <None Update="appsettings.json">



And finally, copy the following code to your Program.cs file and run the application.

/// This program demonstrates how to use Azure OpenAI to generate secure passwords.

using System.Text;
using System.Text.Json;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using Azure;
using Azure.AI.OpenAI;
using static System.Environment;

/// Initializes a new instance of the <see cref="ConfigurationBuilder"/> class and builds the configuration from the specified JSON file.
/// <returns>The configuration object.</returns>
IConfiguration config = new ConfigurationBuilder()
string? AOAI_ENDPOINT = config["AOAI_ENDPOINT"];
string? AOAI_KEY = config["AOAI_KEY"];

/// Creates a new instance of the OpenAIClient class.
/// <param name="endpoint">The endpoint URL for the OpenAI API.</param>
/// <param name="credentials">The credentials for accessing the OpenAI API.</param>

var endpoint = new Uri(AOAI_ENDPOINT);
var credentials = new Azure.AzureKeyCredential(AOAI_KEY);
var openAIClient = new OpenAIClient(endpoint, credentials);

// Define the system prompt for the AI
var systemPrompt = "You are a virtual AI that generates strong and secure passwords";

// Define the user prompt for generating a secure password
var userPrompt = 
    Please generate a secure password with at least 20 characters.
    Please include at least one special character and one number.
    Please include a message regarding the password and its security.

//Completion options 
var completionOptions = new ChatCompletionsOptions
    NucleusSamplingFactor=1 // Top P

// Add system and user prompts to completion options
completionOptions.Messages.Add(new ChatMessage(ChatRole.System, systemPrompt));
completionOptions.Messages.Add(new ChatMessage(ChatRole.User, userPrompt));

// Get chat completions asynchronously
ChatCompletions response = await openAIClient.GetChatCompletionsAsync(AOAI_DEPLOYMENTID, completionOptions);

// Print the generated password


You should see the following outcome when running the program if your configuration is correct.

Password: ^F3l!c!ty#9R0b0t1c$2*7&

This password is a strong and secure one with 20 characters, including special characters, numbers, and both lower and upper case letters. It is highly recommended not to share this password with anyone to maintain its security and integrity.

Related Articles



, , , ,



Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.