|
| 1 | +--- |
| 2 | +lab: |
| 3 | + title: 'Generate code using Azure OpenAI' |
| 4 | +--- |
| 5 | + |
| 6 | +# Generate code using Azure OpenAI |
| 7 | + |
| 8 | +The Azure OpenAI Service models can generate code for you using natural language prompts, fixing bugs in completed code, and providing code comments. These models can also explain and simplify existing code to help you understand what it does and how to improve it. |
| 9 | + |
| 10 | +This exercise will take approximately **25** minutes. |
| 11 | + |
| 12 | +## Before you start |
| 13 | + |
| 14 | +You will need an Azure subscription that has been approved for access to the Azure OpenAI service. |
| 15 | + |
| 16 | +- To sign up for a free Azure subscription, visit [https://azure.microsoft.com/free](https://azure.microsoft.com/free). |
| 17 | +- To request access to the Azure OpenAI service, visit [https://aka.ms/oaiapply](https://aka.ms/oaiapply). |
| 18 | + |
| 19 | +## Provision an Azure OpenAI resource |
| 20 | + |
| 21 | +Before you can use Azure OpenAI models, you must provision an Azure OpenAI resource in your Azure subscription. |
| 22 | + |
| 23 | +1. Sign into the [Azure portal](https://portal.azure.com). |
| 24 | +2. Create an **Azure OpenAI** resource with the following settings: |
| 25 | + - **Subscription**: An Azure subscription that has been approved for access to the Azure OpenAI service. |
| 26 | + - **Resource group**: Create a new resource group with a name of your choice. |
| 27 | + - **Region**: Choose any available region. |
| 28 | + - **Name**: A unique name of your choice. |
| 29 | + - **Pricing tier**: Standard S0 |
| 30 | +3. Wait for deployment to complete. Then go to the deployed Azure OpenAI resource in the Azure portal. |
| 31 | +4. Navigate to **Keys and Endpoint** page, and save those to a text file to use later. |
| 32 | + |
| 33 | +## Deploy a model |
| 34 | + |
| 35 | +To use the Azure OpenAI API for code generation, you must first deploy a model to use through the **Azure OpenAI Studio**. Once deployed, we will use the model with the playground and reference that model in our app. |
| 36 | + |
| 37 | +1. On the **Overview** page for your Azure OpenAI resource, use the **Explore** button to open Azure OpenAI Studio in a new browser tab. Alternatively, navigate to [Azure OpenAI Studio](https://oai.azure.com/?azure-portal=true) directly. |
| 38 | +2. In Azure OpenAI Studio, create a new deployment with the following settings: |
| 39 | + - **Model name**: gpt-35-turbo |
| 40 | + - **Deployment name**: 35turbo |
| 41 | + |
| 42 | +> **Note**: Each Azure OpenAI model is optimized for a different balance of capabilities and performance. We'll use the **3.5 Turbo** model series in the **GPT-3** model family in this exercise, which is highly capable for both language and code understanding. |
| 43 | +
|
| 44 | +## Generate code in chat playground |
| 45 | + |
| 46 | +Before using your app, examine how prompt engineering improves the model response in the playground. In this first example, imagine you are trying to write a python app of animals with fun names. |
| 47 | + |
| 48 | +1. In [Azure OpenAI Studio](https://oai.azure.com/?azure-portal=true), navigate to the **Chat** playground in the left pane. |
| 49 | +1. In the **Assistant setup** section at the top, select the **Default** system message template. |
| 50 | +1. In the **Chat session** section, enter the following prompt and press *Enter*. |
| 51 | + |
| 52 | + ```code |
| 53 | + Write a function in python that takes a character and string as input, and returns how many times that character appears in the string |
| 54 | + ``` |
| 55 | +
|
| 56 | +1. The model will likely respond with a function, with some explanation of what the function does and how to call it. |
| 57 | +1. Next, send the prompt `Do the same thing, but this time write it in C#`. |
| 58 | +1. Observe the output. The model likely responded very similarly as the first time, but this time coding in C#. You can ask it again for a different language of your choice, or a function to complete a different task such as reversing the input string. |
| 59 | +1. Next, let's explore using AI to understand code with this example of a random function you saw written in Ruby. Send the following prompt as the user message. |
| 60 | +
|
| 61 | + ```code |
| 62 | + What does the following function do? |
| 63 | + --- |
| 64 | + def random_func(n) |
| 65 | + start = [0, 1] |
| 66 | + (n - 2).times do |
| 67 | + start << start[-1] + start[-2] |
| 68 | + end |
| 69 | + start.shuffle.each do |num| |
| 70 | + puts num |
| 71 | + end |
| 72 | + end |
| 73 | + ``` |
| 74 | +
|
| 75 | +1. Observe the output, which explains what the function does in natural language. Try asking the model to rewrite it in a language you are familiar with. |
| 76 | +
|
| 77 | +## Set up an application in Cloud Shell |
| 78 | +
|
| 79 | +To show how to integrate with an Azure OpenAI model, we'll use a short command-line application that runs in Cloud Shell on Azure. Open up a new browser tab to work with Cloud Shell. |
| 80 | +
|
| 81 | +1. In the [Azure portal](https://portal.azure.com?azure-portal=true), select the **[>_]** (*Cloud Shell*) button at the top of the page to the right of the search box. A Cloud Shell pane will open at the bottom of the portal. |
| 82 | +
|
| 83 | +  |
| 84 | +
|
| 85 | +2. The first time you open the Cloud Shell, you may be prompted to choose the type of shell you want to use (*Bash* or *PowerShell*). Select **Bash**. If you don't see this option, skip the step. |
| 86 | +
|
| 87 | +3. If you're prompted to create storage for your Cloud Shell, ensure your subscription is specified and select **Create storage**. Then wait a minute or so for the storage to be created. |
| 88 | +
|
| 89 | +4. Make sure the type of shell indicated on the top left of the Cloud Shell pane is switched to *Bash*. If it's *PowerShell*, switch to *Bash* by using the drop-down menu. |
| 90 | +
|
| 91 | +5. Once the terminal starts, enter the following command to download the sample application and save it to a folder called `azure-openai`. |
| 92 | +
|
| 93 | + ```bash |
| 94 | + rm -r azure-openai -f |
| 95 | + git clone https://github.com/MicrosoftLearning/mslearn-openai azure-openai |
| 96 | + ``` |
| 97 | +
|
| 98 | +6. The files are downloaded to a folder named **azure-openai**. Navigate to the lab files for this exercise using the following command. |
| 99 | +
|
| 100 | + ```bash |
| 101 | + cd azure-openai/Labfiles/04-code-generation |
| 102 | + ``` |
| 103 | +
|
| 104 | + Applications for both C# and Python have been provided, as well as sample code we'll be using in this lab. |
| 105 | +
|
| 106 | + Open the built-in code editor, and you can observe the code files we'll be using in `sample-code`. Use the following command to open the lab files in the code editor. |
| 107 | +
|
| 108 | + ```bash |
| 109 | + code . |
| 110 | + ``` |
| 111 | +
|
| 112 | +## Configure your application |
| 113 | +
|
| 114 | +For this exercise, you'll complete some key parts of the application to enable using your Azure OpenAI resource. |
| 115 | +
|
| 116 | +1. In the code editor, expand the language folder for your preferred language. |
| 117 | +
|
| 118 | +2. Open the configuration file for your language. |
| 119 | +
|
| 120 | + - **C#**: `appsettings.json` |
| 121 | + - **Python**: `.env` |
| 122 | +
|
| 123 | +3. Update the configuration values to include the **endpoint** and **key** from the Azure OpenAI resource you created, as well as the name of your deployment, `35turbo`. Save the file. |
| 124 | +
|
| 125 | +4. Navigate to the folder for your preferred language and install the necessary packages. |
| 126 | +
|
| 127 | + **C#** |
| 128 | +
|
| 129 | + ```bash |
| 130 | + cd CSharp |
| 131 | + dotnet add package Azure.AI.OpenAI --version 1.0.0-beta.5 |
| 132 | + ``` |
| 133 | +
|
| 134 | + **Python** |
| 135 | +
|
| 136 | + ```bash |
| 137 | + cd Python |
| 138 | + pip install python-dotenv |
| 139 | + pip install openai |
| 140 | + ``` |
| 141 | +
|
| 142 | +5. Select the code file in this folder for your language and add the necessary libraries. |
| 143 | +
|
| 144 | + **C#** |
| 145 | +
|
| 146 | + `Program.cs` |
| 147 | +
|
| 148 | + ```csharp |
| 149 | + // Add Azure OpenAI package |
| 150 | + using Azure.AI.OpenAI; |
| 151 | + ``` |
| 152 | +
|
| 153 | + **Python** |
| 154 | +
|
| 155 | + `code-generation.py` |
| 156 | +
|
| 157 | + ```python |
| 158 | + # Add OpenAI import |
| 159 | + import openai |
| 160 | + ``` |
| 161 | +
|
| 162 | +6. Add the necessary code for configuring the client. |
| 163 | +
|
| 164 | + **C#** |
| 165 | +
|
| 166 | + ```csharp |
| 167 | + // Initialize the Azure OpenAI client |
| 168 | + OpenAIClient client = new OpenAIClient(new Uri(oaiEndpoint), new AzureKeyCredential(oaiKey)); |
| 169 | + ``` |
| 170 | +
|
| 171 | + **Python** |
| 172 | +
|
| 173 | + ```python |
| 174 | + # Set OpenAI configuration settings |
| 175 | + openai.api_type = "azure" |
| 176 | + openai.api_base = azure_oai_endpoint |
| 177 | + openai.api_version = "2023-05-15" |
| 178 | + openai.api_key = azure_oai_key |
| 179 | + ``` |
| 180 | +
|
| 181 | +7. In the function that calls the Azure OpenAI model, add the code to format and send the request to the model. |
| 182 | +
|
| 183 | + **C#** |
| 184 | +
|
| 185 | + ```csharp |
| 186 | + // Create chat completion options |
| 187 | + var chatCompletionsOptions = new ChatCompletionsOptions() |
| 188 | + { |
| 189 | + Messages = |
| 190 | + { |
| 191 | + new ChatMessage(ChatRole.System, systemPrompt), |
| 192 | + new ChatMessage(ChatRole.User, userPrompt) |
| 193 | + }, |
| 194 | + Temperature = 0.7f, |
| 195 | + MaxTokens = 1000, |
| 196 | + }; |
| 197 | +
|
| 198 | + // Get response from Azure OpenAI |
| 199 | + Response<ChatCompletions> response = await client.GetChatCompletionsAsync( |
| 200 | + oaiModelName, |
| 201 | + chatCompletionsOptions |
| 202 | + ); |
| 203 | +
|
| 204 | + ChatCompletions completions = response.Value; |
| 205 | + string completion = completions.Choices[0].Message.Content; |
| 206 | + ``` |
| 207 | +
|
| 208 | + **Python** |
| 209 | +
|
| 210 | + ```python |
| 211 | + # Build the messages array |
| 212 | + messages =[ |
| 213 | + {"role": "system", "content": system_message}, |
| 214 | + {"role": "user", "content": user_message}, |
| 215 | + ] |
| 216 | +
|
| 217 | + # Call the Azure OpenAI model |
| 218 | + response = openai.ChatCompletion.create( |
| 219 | + engine=model, |
| 220 | + messages=messages, |
| 221 | + temperature=0.7, |
| 222 | + max_tokens=1000 |
| 223 | + ) |
| 224 | + ``` |
| 225 | +
|
| 226 | +## Run your application |
| 227 | +
|
| 228 | +Now that your app has been configured, run it to try generating code for each use case. The use case is numbered in the app, and can be run in any order. |
| 229 | +
|
| 230 | +> **Note**: Some users may experience rate limiting if calling the model too frequently. If you hit an error about a token rate limit, wait for a minute then try again. |
| 231 | +
|
| 232 | +1. In the code editor, expand the `sample-code` folder and briefly observe the function and the app for your language. These files will be used for the tasks in the app. |
| 233 | +1. In the Cloud Shell bash terminal, navigate to the folder for your preferred language. |
| 234 | +1. Run the application. |
| 235 | +
|
| 236 | + - **C#**: `dotnet run` |
| 237 | + - **Python**: `python code-generation.py` |
| 238 | +
|
| 239 | +1. Choose option **1** to add comments to your code. Note, the response might take a few seconds for each of these tasks. |
| 240 | +1. The results will be put into `result/app.txt`. Open that file up, and compare it to the function file in `sample-code`. |
| 241 | +1. Next, choose option **2** to write unit tests for that same function. |
| 242 | +1. The results will replace what was in `result/app.txt`, and details four unit tests for that function. |
| 243 | +1. Next, choose option **3** to fix bugs in an app for playing Go Fish. |
| 244 | +1. The results will replace what was in `result/app.txt`, and should have very similar code with a few things corrected. |
| 245 | +
|
| 246 | + - **C#**: Fixes are made on line 30 and 59 |
| 247 | + - **Python**: Fixes are made on line 18 and 31 |
| 248 | +
|
| 249 | +The app for Go Fish in `sample-code` can be run, if you replace the lines with bugs with the response from Azure OpenAI. If you run it without the fixes, it will not work correctly. |
| 250 | +
|
| 251 | +It's important to note that even though the code for this Go Fish app was corrected for some syntax, it's not a strictly accurate representation of the game. If you look closely, there are issues with not checking if the deck is empty when drawing cards, not removing pairs from the players hand when they get a pair, and a few other bugs that require understanding of card games to realize. This is a great example of how useful generative AI models can be to assist with code generation, but can't be trusted as correct and need to be verified by the developer. |
| 252 | +
|
| 253 | +If you would like to see the full response from Azure OpenAI, you can set the `printFullResponse` variable to `True`, and rerun the app. |
| 254 | +
|
| 255 | +## Clean up |
| 256 | +
|
| 257 | +When you're done with your Azure OpenAI resource, remember to delete the deployment or the entire resource in the [Azure portal](https://portal.azure.com/?azure-portal=true). |
0 commit comments