How to Use Azure OpenAI Service in C#?

Integrating the Power of Azure OpenAI into Your C# Applications


Azure Open AI Service is a cloud-based platform that allows developers to easily integrate state-of-the-art AI models into their applications. Azure OpenAI Service gives customers advanced language AI with OpenAI GPT-3, Codex, and DALL-E models with the security and enterprise promise of Azure. Azure OpenAI co-develops the APIs with OpenAI, ensuring compatibility and a smooth transition from one to the other.

With Azure OpenAI, customers get the security capabilities of Microsoft Azure while running the same models as OpenAI. Azure OpenAI offers private networking, regional availability, and responsible AI content filtering.

Get access to Azure OpenAI

Access is currently limited. For now, Auzre is working with customers with an existing partnership with Microsoft, lower risk use cases, and those committed to incorporating mitigations. In addition to applying for initial access, all solutions using Azure OpenAI are required to go through a use case review before they can be released for production use.

Currently, access to this service is granted only by application. You can apply for access to Azure OpenAI by completing the form at https://aka.ms/oai/access.

Create an Azure Open AI resource

The first step is to create an Azure Open AI resource. To do this, go to the Azure portal and select “Create a resource.” In the search bar, type “Open AI” and select the “Open AI” service. Fill in the required information, such as the subscription, resource group, and name, and then click “Create.”

Before you can generate text or inference, you need to deploy a model. You can select from one of several available models in Azure OpenAI Studio.

Retrieve key and endpoint

To successfully make a call against Azure OpenAI, you’ll need ResourceName , an API key and DeploymentId.

Go to your resource in the Azure portal. The Endpoint and Keys can be found in the Resource Management section. Copy your endpoint and access key as you’ll need both for authenticating your API calls. You can use either KEY1 or KEY2. Always having two keys allows you to securely rotate and regenerate keys without causing a service disruption.

For example, your endpoint might looks like:

https://my-test-001.openai.azure.com/

my-test-001 will be your resource name.

You can find the DeploymentId from your resource dashboard.

Install the Open AI SDK

In order to use the Open AI service in our C# application, we need to install the Open AI SDK. The SDK can be installed via NuGet by running the following command in the Package Manager Console:

install-package Betalgo.OpenAI.GPT3

Create a new C# console application

Create a new C# console application, and then import the following namespaces in your Main method

using OpenAI.GPT3;
using OpenAI.GPT3.Interfaces;
using OpenAI.GPT3.Managers;
using OpenAI.GPT3.ObjectModels.RequestModels;
using OpenAI.GPT3.ObjectModels;

Authenticate and Create a Client

To authenticate and create a client, we’ll use the API key we generated earlier. Below is an example of how to create the client in C#:

var gpt3 = new OpenAIService(new OpenAiOptions()
{
    ProviderType = ProviderType.Azure,
    ApiKey = apiKey,
    DeploymentId = "your deployment id",
    ResourceName = "your resoure name"
});

Use the Generate Text API

Now that we have our client set up, we can use the Generate Text API to generate text. Below is an example of how to use the API in C#:

var completionResult = await gpt3.Completions.CreateCompletion(new CompletionCreateRequest()
{
    Prompt = "What is the meaning of life?",
    Model = Models.TextDavinciV2,
    Temperature = 0.5F,
    MaxTokens = 100,
    N = 3
});

Program

Here is the code for the full example

using OpenAI.GPT3;
using OpenAI.GPT3.Interfaces;
using OpenAI.GPT3.Managers;
using OpenAI.GPT3.ObjectModels.RequestModels;
using OpenAI.GPT3.ObjectModels;

namespace AzureOpenAIExample
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            var apiKey = "your api key";            

            var gpt3 = new OpenAIService(new OpenAiOptions()
            {
                ProviderType = ProviderType.Azure,
                ApiKey = apiKey,
                DeploymentId = "your deployment id",
                ResourceName = "your resoure name"
            });

            
            var completionResult = await gpt3.Completions.CreateCompletion(new CompletionCreateRequest()
            {
                Prompt = "What is the meaning of life?",
                Model = Models.TextDavinciV2,
                Temperature = 0.5F,
                MaxTokens = 100,
                N = 3
            });

            if (completionResult.Successful)
            {
                foreach (var choice in completionResult.Choices)
                {
                    Console.WriteLine(choice.Text);
                }                
            }
            else
            {
                if (completionResult.Error == null)
                {
                    throw new Exception("Unknown Error");
                }
                Console.WriteLine($"{completionResult.Error.Code}: {completionResult.Error.Message}");
            }
            
            Console.ReadLine();
        }
    }
}

Alternative: Use Azure OpenAI .NET SDK

NOTE: The Azure OpenAI .NET SDK is currently in preview.

Install the package
using System;
using Azure.AI.OpenAI;

var endpoint = new Uri("Your end point");
var credentials = new Azure.AzureKeyCredential("your api key");
var openAIClient = new OpenAIClient(endpoint, credentials);

var prompt = "What is the meaning of life?"

var completionOptions = new CompletionsOptions
{
    Prompts={prompt},
    MaxTokens=64,
    Temperature=0f,
    FrequencyPenalty=0.0f,
    PresencePenalty=0.0f,
    NucleusSamplingFactor=1 // Top P
};
Completions response = await openAIClient.GetCompletionsAsync("your deployment id",completionOptions);
response.Choices.First().Text

Subscribe to Dev·edium

Sign up now to get access to the library of members-only issues.
Jamie Larson
Subscribe
oAs6O4K19CZGaIdjI5ohK+O2y5lBTW6uQ==" crossorigin="anonymous" referrerpolicy="no-referrer">