In this Azure OpenAI .NET SDK blog post we will show you how to use the OpenAIClient class and connect to an Azure OpenAI resource.
The Azure SDK for .NET (preview) allows us to connect, create, consume, and manage Azure resources using .NET applications like C#, ASP, Blazor, and more. The SDK also allows us to connect and consume OpenAI endpoints.
Use the OpenAIClient Class
We first need to install the Azure AI OpenAI package to use the class.
dotnet add package Azure.AI.OpenAI --version 1.0.0-beta.6
To use the package in a C# class or application, we use:
using Azure.AI.OpenAI;
Configure the OpenAI Client
Before connecting to Azure OpenAI, we must configure the client with the OpenAI endpoint, Access Key and deployment name.
var AOAI_ENDPOINT = Environment.GetEnvironmentVariable("AOAI_ENDPOINT");
var AOAI_KEY = Environment.GetEnvironmentVariable("AOAI_KEY");
var AOAI_DEPLOYMENTID = Environment.GetEnvironmentVariable("AOAI_DEPLOYMENTID");
Use Client
To use the client using the client we use the following code.
var endpoint = new Uri(AOAI_ENDPOINT);
var credentials = new Azure.AzureKeyCredential(AOAI_KEY);
var openAIClient = new OpenAIClient(endpoint, credentials);
Configure Completion Options
The following code configures the completion options, which control the results of the OpenAI output.
var completionOptions = new ChatCompletionsOptions
{
MaxTokens=100,
Temperature=0.5f,
FrequencyPenalty=0.0f,
PresencePenalty=0.0f,
NucleusSamplingFactor=1 // Top P
};
Configure Prompt
Once the OpenAIClient and completion options are ready, we can define the prompt sent to the endpoint and handle the reply (assistance).
The following code will define the prompt.
var systemPrompt = "You are a virtual agent that generates secure passwords.";
var userPrompt =
"""
generete a secure password with 20 characters that contains at least one number and one special character.
""";
Generate Output
The following code block will generate the completions returned from the OpenAI endpoint.
completionOptions.Messages.Add(new ChatMessage(ChatRole.System, systemPrompt));
completionOptions.Messages.Add(new ChatMessage(ChatRole.User, userPrompt));
ChatCompletions response = await openAIClient.GetChatCompletionsAsync(AOAI_DEPLOYMENTID,completionOptions);
response.Choices.First().Message.Content
More AI articles
- Get Started With Azure Open AI .NET SDK
- Use Azure OpenAI with C# Application
- Deploy Azure OpenAI Resource With Terraform
- Connect to Azure OpenAI Using Postman
- Azure AI vs Azure Open AI
- Azure AI Studio vs Microsoft Copilot Studio
- Get Started With Azure AI Studio
- Create Prompt Flow for an LLM App in Azure AI Studio
- Detect Language using Azure AI Services
- Use GitHub Copilot to Write and Manage Code
- Install and use GitHub Copilot with Visual Studio
- Build Copilot With Azure AI Studio and Add Your Data
- Create Azure OpenAI Resource Using Azure CLI
- How to use the AzureOpenAI Client Class
- List All Azure AI LLM Model List With Azure CLI
1 thought on “How to Use The Azure OpenAIClient Class”