Getting Started with GPT-3.5 Turbo on Azure OpenAI Service in .NET

Introduction

AI is one of the main pillars of any modern application development. With the Azure OpenAI Service, integrating powerful language models like GPT-35-Turbo into our .NET applications has never been easier. This article will explore how to use the Azure OpenAI service to generate text responses based on user prompts.

Why use Azure OpenAI Service?

  • Cloud-based service by Microsoft offering advanced AI models.
  • Provides access to models like GPT for natural language processing.
  • Enables text generation, language translation, and content summarization.
  • Easily integrates AI capabilities into applications.
  • Built on Azure’s secure and scalable infrastructure.

What is GPT-35-Turbo?

GPT-3 Enhanced version called the GPT-35-Turbo. And optimized to speed up execution.5x faster. For real-time performance, which is required on time-critical applications.

Prerequisites

Before diving into the code, ensure you have the following ready.

  • Azure Subscription
  • Azure OpenAI Service Access – Request access to Azure OpenAI.
  • Visual Studio or Visual Studio Code

Step 1. Set Up Azure OpenAI in Azure Portal.

  1. Create an OpenAI resource in Azure
    • Log into https://portal.azure.com/.
    • Search for "Azure OpenAI" and click Create.
    • Provide basic details like resource group, region, and pricing tier.
    • After the resource is deployed, we can find the API endpoint and key in the Keys and Endpoints section.
       API endpoint
  2. Deploy GPT-35-Turbo: Under our Azure OpenAI resource, go to the Models deployments section and deploy the `GPT-35-turbo` model. We will use this in the API calls later.
    OpenAI
  3. Install NuGet Packages: Install the client library for .NET with NuGet: dotnet add package Azure.AI.OpenAI --version 1.0.0-beta.12

Step 2. Create a .NET C# Project

Create a new Console Application: Open Visual Studio and create a new Console App using .NET 6.0+.

Console Application

Step 3. Write Code to Call GPT-35-Turbo API.

using Azure.AI.OpenAI;
using Azure;
namespace GenerativeAI
{
    public class AzureOpenAIGPT
    {
        //Azure OpenAI API Key
        const string key = "***********************************";
        //Endpoint Azure OpenAI URL
        const string endpoint = "https://********.openai.azure.com/";
        //Model name or deployment name
        const string deploymentOrModelName = "MEC";
        public async Task<string> GetContent(string prompt)
        {
            var client = new OpenAIClient(new Uri(endpoint), new AzureKeyCredential(key));
            var chatCompletionsOptions = new ChatCompletionsOptions
            {
                DeploymentName = deploymentOrModelName,
                Temperature = (float)0.5,
                MaxTokens = 800,
                NucleusSamplingFactor = (float)0.95,
                FrequencyPenalty = 0,
                PresencePenalty = 0,
            };
            chatCompletionsOptions.Messages.Add(new ChatRequestUserMessage(prompt));
            var response = await client.GetChatCompletionsAsync(chatCompletionsOptions);
            return response.Value.Choices[0].Message.Content;
        }
    }
    class Program
    {
        static async Task Main(string[] args)
        {
            var azureOpenAIGPT = new AzureOpenAIGPT();
            string prompt = "What is Azure?";
            string result = await azureOpenAIGPT.GetContent(prompt);
            Console.WriteLine(result);
            Console.ReadLine();
        }
    }
}

The above code interacts with the Azure OpenAI API to generate text responses based on our prompts. It defines a class AzureOpenAIGPT that initializes an OpenAI client with API credentials and configuration settings. The GetContent method takes a prompt, sends it to the API, and returns the generated response. The Main method demonstrates its usage by asking "What is Azure?" and printing the response in the terminal.

Step 4. Run the Application.

Run the Console app, and it will send a prompt to GPT-35-Turbo and display the AI-generated response in our terminal.

Output

Conclusion

With Azure OpenAI Service and GPT-35-Turbo, we can build intelligent, responsive applications in .NET. This article covered basic integration with Azure OpenAI API.


Similar Articles