Overview

This exercise demonstrates how to develop an AI-powered application using Azure OpenAI Service. The goal is to integrate generative AI into a chatbot or other applications using REST APIs or SDKs.


Steps & Configuration Details

1. Clone the Repository

  • Open Visual Studio Code.
  • Run the following command to clone the repository:
    git clone https://github.com/MicrosoftLearning/mslearn-openai
    
  • Open the cloned folder in Visual Studio Code.

2. Provision an Azure OpenAI Resource

  • Sign into Azure Portal (https://portal.azure.com).
  • Create an Azure OpenAI resource with the following settings:
    • Subscription: Your Azure subscription.
    • Resource Group: Select or create a resource group.
    • Region: Choose from:
      • East US
      • East US 2
      • North Central US
      • South Central US
      • Sweden Central
      • West US
      • West US 3
    • Name: A unique name.
    • Pricing Tier: Standard S0.

3. Deploy a Model

  • Open Azure Cloud Shell (Bash environment).
  • Run the following command, replacing placeholders with actual values:
    az cognitiveservices account deployment create \
      -g <your_resource_group> \
      -n <your_OpenAI_service> \
      --deployment-name gpt-4o \
      --model-name gpt-4o \
      --model-version 2024-05-13 \
      --model-format OpenAI \
      --sku-name "Standard" \
      --sku-capacity 5
    
  • Configuration Items:
    • Deployment Name: gpt-4o
    • Model Name: gpt-4o
    • Model Version: 2024-05-13
    • SKU Capacity: 5 (measured in thousands of tokens per minute).

4. Configure Your Application

  • Open Visual Studio Code.
  • Navigate to:
    • C#: Labfiles/01-app-develop/CSharp
    • Python: Labfiles/01-app-develop/Python
  • Open an integrated terminal and install the Azure OpenAI SDK:
    • C#:
      dotnet add package Azure.AI.OpenAI --version 2.1.0
      
    • Python:
      pip install openai==1.65.2
      
  • Open the configuration file:
    • C#: appsettings.json
    • Python: .env
  • Update Configuration Values:
    • Azure OpenAI Endpoint
    • API Key
    • Deployment Name
  • Save the configuration file.

5. Add Code to Use Azure OpenAI

  • Open the code file:
    • C#: Program.cs
    • Python: application.py
  • Add the Azure OpenAI package:
    • C#:
      using Azure.AI.OpenAI;
      using OpenAI.Chat;
      
    • Python:
      from openai import AsyncAzureOpenAI
      
  • Configure the Azure OpenAI client:
    • C#:
      AzureOpenAIClient azureClient = new (
          new Uri(oaiEndpoint),
          new ApiKeyCredential(oaiKey)
      );
      ChatClient chatClient = azureClient.GetChatClient(oaiDeploymentName);
      
    • Python:
      client = AsyncAzureOpenAI(
          azure_endpoint=azure_oai_endpoint,
          api_key=azure_oai_key,
          api_version="2024-02-15-preview"
      )
      
  • Format and send the request:
    • C#:
      ChatCompletionOptions chatCompletionOptions = new ChatCompletionOptions() {
          Temperature = 0.7f,
          MaxOutputTokenCount = 800
      };
      ChatCompletion completion = chatClient.CompleteChat(
          [new SystemChatMessage(systemMessage), new UserChatMessage(userMessage)],
          chatCompletionOptions
      );
      Console.WriteLine($"{completion.Role}: {completion.Content[0].Text}");
      
    • Python:
      messages = [
          {"role": "system", "content": system_message},
          {"role": "user", "content": user_message},
      ]
      response = await client.chat.completions.create(
          model=model,
          messages=messages,
          temperature=0.7,
          max_tokens=800
      )
      print(response.choices[0].message.content)
      

6. Run Your Application

  • Open Visual Studio Code.
  • Run the application:
    • C#:
      dotnet run
      
    • Python:
      python application.py
      
  • Test different prompts to observe AI responses.

7. Clean Up

  • Delete Azure resources to avoid unnecessary costs:

Related Posts