diff --git a/Instructions/Exercises/01-get-started-azure-openai.md b/Instructions/Exercises/01-get-started-azure-openai.md new file mode 100644 index 00000000..4ab24313 --- /dev/null +++ b/Instructions/Exercises/01-get-started-azure-openai.md @@ -0,0 +1,115 @@ +--- +lab: + title: 'Get started with Azure OpenAI' +--- + +# Get started with Azure OpenAI Service + +Azure OpenAI Service brings the generative AI models developed by OpenAI to the Azure platform, enabling you to develop powerful AI solutions that benefit from the security, scalability, and integration of services provided by the Azure cloud platform. In this exercise, you'll learn how to get started with Azure OpenAI by provisioning the service as an Azure resource and using Azure OpenAI Studio to deploy and explore OpenAI models. + +This exercise takes approximately **30** minutes. + +## Provision an Azure OpenAI resource + +If you don't already have one, provision an Azure OpenAI resource in your Azure subscription. + +1. Sign into the [Azure portal](https://portal.azure.com) at `https://portal.azure.com`. +2. Create an **Azure OpenAI** resource with the following settings: + - **Subscription**: *Select an Azure subscription that has been approved for access to the Azure OpenAI service* + - **Resource group**: *Choose or create a resource group* + - **Region**: *Make a random choice from any of the available regions*\* + - **Name**: *A unique name of your choice* + - **Pricing tier**: Standard S0 + + > \* Azure OpenAI resources are constrained by regional quotas. Randomly choosing a region reduces the risk of a single region reaching its quota limit in scenarios where you are sharing a subscription with other users. In the event of a quota limit being reached later in the exercise, there's a possibility you may need to create another resource in a different region. + +3. Wait for deployment to complete. Then go to the deployed Azure OpenAI resource in the Azure portal. + +## Deploy a model + +Azure OpenAI provides a web-based portal named **Azure OpenAI Studio**, that you can use to deploy, manage, and explore models. You'll start your exploration of Azure OpenAI by using Azure OpenAI Studio to deploy a model. + +1. On the **Overview** page for your Azure OpenAI resource, use the **Go to Azure OpenAI Studio** button to open Azure OpenAI Studio in a new browser tab. +2. In Azure OpenAI Studio, on the **Deployments** age, view your existing model deployments. If you don't already have one, create a new deployment of the **gpt-35-turbo** model with the following settings: + - **Model**: gpt-35-turbo + - **Model version**: Auto-update to default + - **Deployment name**: *A unique name of your choice* + - **Advanced options** + - **Content filter**: Default + - **Tokens per minute rate limit**: 5K\* + - **Enable dynamic quota**: Enabled + + > \* A rate limit of 5,000 tokens per minute is more than adequate to complete this exercise while leaving capacity for other people using the same subscription. + +## Use the Chat playground + +The *Chat* playground provides a chatbot interface for GPT 3.5 and higher models. + +> **Note:** The *Chat* playground uses the *ChatCompletions* API rather than the older *Completions* API that is used by the *Completions* playground. The Completions playground is provided for compatibility with older models. + +1. In the **Playground** section, select the **Chat** page, and ensure that the **my-gpt-model** model is selected in the configuration pane on the right. +2. In the **Assistant setup** section, in the **System message** box, replace the current text with the following statement: `The system is an AI teacher that helps people learn about AI`. + +3. Below the **System message** box, select **Add an example**, and type the following message and response in the designated boxes: + + - **User**: `What are different types of artificial intelligence?` + - **Assistant**: `There are three main types of artificial intelligence: Narrow or Weak AI (such as virtual assistants like Siri or Alexa, image recognition software, and spam filters), General or Strong AI (AI designed to be as intelligent as a human being. This type of AI does not currently exist and is purely theoretical), and Artificial Superintelligence (AI that is more intelligent than any human being and can perform tasks that are beyond human comprehension. This type of AI is also purely theoretical and has not yet been developed).` + + > **Note**: Few-shot examples are used to provide the model with examples of the types of responses that are expected. The model will attempt to reflect the tone and style of the examples in its own responses. + +4. Save the changes to start a new session and set the behavioral context of the chat system. +5. In the query box at the bottom of the page, enter the user query `What is artificial intelligence?` + + > **Note**: You may receive a response that the API deployment is not yet ready. If so, wait for a few minutes and try again. + +6. Review the response and then submit the following message to continue the conversation: `How is it related to machine learning?` +7. Review the response, noting that context from the previous interaction is retained (so the model understands that "it" refers to artificial intelligence). +8. Use the **View Code** button to view the code for the interaction. The prompt consists of the *system* message, the few-shot examples of *user* and *assistant* messages, and the sequence of *user* and *assistant* messages in the chat session so far. + +## Explore prompts and parameters + +You can use the prompt and parameters to maximize the likelihood of generating the response you need. + +1. In the **Parameters** pane, set the following parameter values: + - **Temperature**: 0 + - **Max length (tokens)**: 500 + +2. Submit the following message + + ``` + Write three multiple choice questions based on the following text, indcating the correct answers. + + Most computer vision solutions are based on machine learning models that can be applied to visual input from cameras, videos, or images.* + + - Image classification involves training a machine learning model to classify images based on their contents. For example, in a traffic monitoring solution you might use an image classification model to classify images based on the type of vehicle they contain, such as taxis, buses, cyclists, and so on.* + + - Object detection machine learning models are trained to classify individual objects within an image, and identify their location with a bounding box. For example, a traffic monitoring solution might use object detection to identify the location of different classes of vehicle.* + + - Semantic segmentation is an advanced machine learning technique in which individual pixels in the image are classified according to the object to which they belong. For example, a traffic monitoring solution might overlay traffic images with "mask" layers to highlight different vehicles using specific colors. + ``` + +3. Review the results, which should consist of multiple-choice questions that a teacher could use to test students on the computer vision topics in the prompt. The total response should be smaller than the maximum length you specified as a parameter. + + Observe the following about the prompt and parameters you used: + + - The prompt specifically states that the desired output should be three multiple choice questions. + - The parameters include *Temperature*, which controls the degree to which response generation includes an element of randomness. The value of **0** used in your submission minimizes randomness, resulting in stable, predictable responses. + +## Explore code-generation + +In addition to generating natural language responses, you can use GPT models to generate code. + +1. In the **Assistant setup** pane, select the **Empty Example** template to reset the system message. +2. Enter the system message: `You are a Python developer.` and save the changes. +3. In the **Chat session** pane, select **Clear chat** to clear the chat history and start a new session. +4. Submit the following user message: + + ``` + Write a Python function named Multiply that multiplies two numeric parameters. + ``` + +5. Review the response, which should include sample Python code that meets the requirement in the prompt. + +## Clean up + +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). diff --git a/Instructions/Exercises/02-natural-language-azure-openai.md b/Instructions/Exercises/02-natural-language-azure-openai.md new file mode 100644 index 00000000..8a67bfa9 --- /dev/null +++ b/Instructions/Exercises/02-natural-language-azure-openai.md @@ -0,0 +1,206 @@ +--- +lab: + title: 'Integrate Azure OpenAI into your app' +--- + +# Integrate Azure OpenAI into your app + +With the Azure OpenAI Service, developers can create chatbots, language models, and other applications that excel at understanding natural human language. The Azure OpenAI provides access to pre-trained AI models, as well as a suite of APIs and tools for customizing and fine-tuning these models to meet the specific requirements of your application. In this exercise, you'll learn how to deploy a model in Azure OpenAI and use it in your own application to summarize text. + +This exercise will take approximately **30** minutes. + +## Provision an Azure OpenAI resource + +If you don't already have one, provision an Azure OpenAI resource in your Azure subscription. + +1. Sign into the [Azure portal](https://portal.azure.com) at `https://portal.azure.com`. +2. Create an **Azure OpenAI** resource with the following settings: + - **Subscription**: *Select an Azure subscription that has been approved for access to the Azure OpenAI service* + - **Resource group**: *Choose or create a resource group* + - **Region**: *Make a random choice from any of the available regions*\* + - **Name**: *A unique name of your choice* + - **Pricing tier**: Standard S0 + + > \* Azure OpenAI resources are constrained by regional quotas. Randomly choosing a region reduces the risk of a single region reaching its quota limit in scenarios where you are sharing a subscription with other users. In the event of a quota limit being reached later in the exercise, there's a possibility you may need to create another resource in a different region. + +3. Wait for deployment to complete. Then go to the deployed Azure OpenAI resource in the Azure portal. + +## Deploy a model + +Azure OpenAI provides a web-based portal named **Azure OpenAI Studio**, that you can use to deploy, manage, and explore models. You'll start your exploration of Azure OpenAI by using Azure OpenAI Studio to deploy a model. + +1. On the **Overview** page for your Azure OpenAI resource, use the **Go to Azure OpenAI Studio** button to open Azure OpenAI Studio in a new browser tab. +2. In Azure OpenAI Studio, on the **Deployments** page, view your existing model deployments. If you don't already have one, create a new deployment of the **gpt-35-turbo** model with the following settings: + - **Model**: gpt-35-turbo + - **Model version**: Auto-update to default + - **Deployment name**: *A unique name of your choice* + - **Advanced options** + - **Content filter**: Default + - **Tokens per minute rate limit**: 5K\* + - **Enable dynamic quota**: Enabled + + > \* A rate limit of 5,000 tokens per minute is more than adequate to complete this exercise while leaving capacity for other people using the same subscription. + +## Set up an application in Cloud Shell + +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. + +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. + + ![Screenshot of starting Cloud Shell by clicking on the icon to the right of the top search box.](../media/cloudshell-launch-portal.png#lightbox) + +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. + +3. If you're prompted to create storage for your Cloud Shell, select **Show advanced settings** and select the following settings: + - **Subscription**: Your subscription + - **Cloud shell regions**: Choose any available region + - **Show VNET isolation setings** Unselected + - **Resource group**: Use the existing resource group where you provisioned your Azure OpenAI resource + - **Storage account**: Create a new storage account with a unique name + - **File share**: Create a new file share with a unique name + + Then wait a minute or so for the storage to be created. + + > **Note**: If you already have a cloud shell set up in your Azure subscription, you may need to use the **Reset user settings** option in the ⚙️ menu to ensure the latest versions of Python and the .NET Framework are installed. + +4. Make sure the type of shell indicated on the top left of the Cloud Shell pane is *Bash*. If it's *PowerShell*, switch to *Bash* by using the drop-down menu. + +5. Once the terminal starts, enter the following command to download the sample application and save it to a folder called `azure-openai`. + + ```bash + rm -r azure-openai -f + git clone https://github.com/MicrosoftLearning/mslearn-openai azure-openai + ``` + +6. The files are downloaded to a folder named **azure-openai**. Navigate to the lab files for this exercise using the following command. + + ```bash + cd azure-openai/Labfiles/02-nlp-azure-openai + ``` + +Applications for both C# and Python have been provided, as well as a sample text file you'll use to test the summarization. Both apps feature the same functionality. + +Open the built-in code editor, and observe the text file that you'll be summarizing with your model located at `text-files/sample-text.txt`. Use the following command to open the lab files in the code editor. + +```bash +code . +``` + +## Configure your application + +For this exercise, you'll complete some key parts of the application to enable using your Azure OpenAI resource. + +1. In the code editor, expand the **CSharp** or **Python** folder, depending on your language preference. + +2. Open the configuration file for your language + + - C#: `appsettings.json` + - Python: `.env` + +3. Update the configuration values to include the **endpoint** and **key** from the Azure OpenAI resource you created, as well as the model name that you deployed, `text-turbo`. Save the file. + +4. Navigate to the folder for your preferred language and install the necessary packages + + **C#** + + ```bash + cd CSharp + dotnet add package Azure.AI.OpenAI --prerelease + ``` + + **Python** + + ```bash + cd Python + pip install python-dotenv + pip install openai + ``` + +5. Navigate to your preferred language folder, select the code file, and add the necessary libraries. + + **C#** + + ```csharp + // Add Azure OpenAI package + using Azure.AI.OpenAI; + ``` + + **Python** + + ```python + # Add OpenAI import + import openai + ``` + +5. Open up the application code for your language and add the necessary code for building the request, which specifies the various parameters for your model such as `prompt` and `temperature`. + + **C#** + + ```csharp + // Initialize the Azure OpenAI client + OpenAIClient client = new OpenAIClient(new Uri(oaiEndpoint), new AzureKeyCredential(oaiKey)); + + // Build completion options object + ChatCompletionsOptions chatCompletionsOptions = new ChatCompletionsOptions() + { + Messages = + { + new ChatMessage(ChatRole.System, "You are a helpful assistant. Summarize the following text in 60 words or less."), + new ChatMessage(ChatRole.User, text), + }, + MaxTokens = 120, + Temperature = 0.7f, + }; + + // Send request to Azure OpenAI model + ChatCompletions response = client.GetChatCompletions( + deploymentOrModelName: oaiModelName, + chatCompletionsOptions); + string completion = response.Choices[0].Message.Content; + + Console.WriteLine("Summary: " + completion + "\n"); + ``` + + **Python** + + ```python + # Set OpenAI configuration settings + openai.api_type = "azure" + openai.api_base = azure_oai_endpoint + openai.api_version = "2023-03-15-preview" + openai.api_key = azure_oai_key + + # Send request to Azure OpenAI model + print("Sending request for summary to Azure OpenAI endpoint...\n\n") + response = openai.ChatCompletion.create( + engine=azure_oai_model, + temperature=0.7, + max_tokens=120, + messages=[ + {"role": "system", "content": "You are a helpful assistant. Summarize the following text in 60 words or less."}, + {"role": "user", "content": text} + ] + ) + + print("Summary: " + response.choices[0].message.content + "\n") + ``` + +## Run your application + +Now that your app has been configured, run it to send your request to your model and observe the response. + +1. In the Cloud Shell bash terminal, navigate to the folder for your preferred language. +1. Run the application. + + - **C#**: `dotnet run` + - **Python**: `python test-openai-model.py` + +1. Observe the summarization of the sample text file. +1. Navigate to your code file for your preferred language, and change the *temperature* value to `1`. Save the file. +1. Run the application again, and observe the output. + +Increasing the temperature often causes the summary to vary, even when provided the same text, due to the increased randomness. You can run it several times to see how the output may change. Try using different values for your temperature with the same input. + +## Clean up + +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). diff --git a/Instructions/Exercises/03-prompt-engineering.md b/Instructions/Exercises/03-prompt-engineering.md new file mode 100644 index 00000000..eab514eb --- /dev/null +++ b/Instructions/Exercises/03-prompt-engineering.md @@ -0,0 +1,323 @@ +--- +lab: + title: 'Utilize prompt engineering in your app' +--- + +# Utilize prompt engineering in your app + +With the Azure OpenAI Service, developers can create chatbots, language models, and other applications that excel at understanding natural human language. The Azure OpenAI provides access to pre-trained AI models, as well as a suite of APIs and tools for customizing and fine-tuning these models to meet the specific requirements of your application. In this exercise, you'll learn how to deploy a model in Azure OpenAI and use it in your own application to summarize text. + +When working with the Azure OpenAI Service, how developers shape their prompt greatly impacts how the generative AI model will respond. Azure OpenAI models are able to tailor and format content, if requested in a clear and concise way. In this exercise, you'll learn how different prompts for similar content help shape the AI model's response to better satisfy your requirements. + +This exercise will take approximately **25** minutes. + +## Before you start + +You will need an Azure subscription that has been approved for access to the Azure OpenAI service. + +- To sign up for a free Azure subscription, visit [https://azure.microsoft.com/free](https://azure.microsoft.com/free). +- To request access to the Azure OpenAI service, visit [https://aka.ms/oaiapply](https://aka.ms/oaiapply). + +## Provision an Azure OpenAI resource + +Before you can use Azure OpenAI models, you must provision an Azure OpenAI resource in your Azure subscription. + +1. Sign into the [Azure portal](https://portal.azure.com). +2. Create an **Azure OpenAI** resource with the following settings: + - **Subscription**: An Azure subscription that has been approved for access to the Azure OpenAI service. + - **Resource group**: Choose an existing resource group, or create a new one with a name of your choice. + - **Region**: Choose any available region. + - **Name**: A unique name of your choice. + - **Pricing tier**: Standard S0 +3. Wait for deployment to complete. Then go to the deployed Azure OpenAI resource in the Azure portal. +4. Navigate to **Keys and Endpoint** page, and save those to a text file to use later. + +## Deploy a model + +To use the Azure OpenAI API, you must first deploy a model to use through the **Azure OpenAI Studio**. Once deployed, we will reference that model in our app. + +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. +2. In Azure OpenAI Studio, create a new deployment with the following settings: + - **Model**: gpt-35-turbo + - **Model version**: *Use the default version* + - **Deployment name**: text-turbo + +> **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 language understanding. This exercise only uses a single model, however deployment and usage of other models you deploy will work in the same way. + +## Apply prompt engineering in chat playground + +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. + +1. In [Azure OpenAI Studio](https://oai.azure.com/?azure-portal=true), navigate to the **Chat** playground in the left pane. +1. In the **Assistant setup** section at the top, enter `You are a helpful AI assistant` as the system message. +1. In the **Chat session** section, enter the following prompt and press *Enter*. + + ```code + 1. Create a list of animals + 2. Create a list of whimsical names for those animals + 3. Combine them randomly into a list of 25 animal and name pairs + ``` + +1. The model will likely respond with an answer to satisfy the prompt, split into a numbered list. This is a good response, but not what we're looking for. +1. Next, update the system message to include instructions `You are an AI assistant helping write python code. Complete the app based on provided comments`. Click **Save changes** +1. Format the instructions as python comments. Send the following prompt to the model. + + ```code + # 1. Create a list of animals + # 2. Create a list of whimsical names for those animals + # 3. Combine them randomly into a list of 25 animal and name pairs + ``` + +1. The model should correctly respond with complete python code doing what the comments requested. +1. Next we'll see the impact of few shot prompting when attempting to classify articles. Return to the system message, and enter `You are a helpful AI assistant` again, and save your changes. This will create a new chat session. +1. Send the following prompt to the model. + + ```code + Severe drought likely in California + + Millions of California residents are bracing for less water and dry lawns as drought threatens to leave a large swath of the region with a growing water shortage. + + In a remarkable indication of drought severity, officials in Southern California have declared a first-of-its-kind action limiting outdoor water use to one day a week for nearly 8 million residents. + + Much remains to be determined about how daily life will change as people adjust to a drier normal. But officials are warning the situation is dire and could lead to even more severe limits later in the year. + ``` + +1. The response will likely be some information about drought in California. While not a bad response, it's not the classification we're looking for. +1. In the **Assistant setup** section near the system message, select the **Add an example** button. Add the following example. + + **User:** + + ```code + New York Baseballers Wins Big Against Chicago + + New York Baseballers mounted a big 5-0 shutout against the Chicago Cyclones last night, solidifying their win with a 3 run homerun late in the bottom of the 7th inning. + + Pitcher Mario Rogers threw 96 pitches with only two hits for New York, marking his best performance this year. + + The Chicago Cyclones' two hits came in the 2nd and the 5th innings, but were unable to get the runner home to score. + ``` + + **Assistant:** + + ```code + Sports + ``` + +1. Add another example with the following text. + + **User:** + + ```code + Joyous moments at the Oscars + + The Oscars this past week where quite something! + + Though a certain scandal might have stolen the show, this year's Academy Awards were full of moments that filled us with joy and even moved us to tears. + These actors and actresses delivered some truly emotional performances, along with some great laughs, to get us through the winter. + + From Robin Kline's history-making win to a full performance by none other than Casey Jensen herself, don't miss tomorrows rerun of all the festivities. + ``` + + **Assistant:** + + ```code + Entertainment + ``` + +1. Save those changed to the assistant setup, and send the same prompt about California drought, provided here again for convenience. + + ```code + Severe drought likely in California + + Millions of California residents are bracing for less water and dry lawns as drought threatens to leave a large swath of the region with a growing water shortage. + + In a remarkable indication of drought severity, officials in Southern California have declared a first-of-its-kind action limiting outdoor water use to one day a week for nearly 8 million residents. + + Much remains to be determined about how daily life will change as people adjust to a drier normal. But officials are warning the situation is dire and could lead to even more severe limits later in the year. + ``` + +1. This time the model should respond with an appropriate classification, even without instructions. + +## Set up an application in Cloud Shell + +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. + +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. + + ![Screenshot of starting Cloud Shell by clicking on the icon to the right of the top search box.](../media/cloudshell-launch-portal.png#lightbox) + +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. + +3. If you're prompted to create storage for your Cloud Shell, select **Show advanced settings** and select the following settings: + - **Subscription**: Your subscription + - **Cloud shell regions**: Choose any available region + - **Show VNET isolation setings** Unselected + - **Resource group**: Use the existing resource group where you provisioned your Azure OpenAI resource + - **Storage account**: Create a new storage account with a unique name + - **File share**: Create a new file share with a unique name + + Then wait a minute or so for the storage to be created. + + > **Note**: If you already have a cloud shell set up in your Azure subscription, you may need to use the **Reset user settings** option in the ⚙️ menu to ensure the latest versions of Python and the .NET Framework are installed. + +4. Make sure the type of shell indicated on the top left of the Cloud Shell pane is *Bash*. If it's *PowerShell*, switch to *Bash* by using the drop-down menu. + +5. Once the terminal starts, enter the following command to download the sample application and save it to a folder called `azure-openai`. + + ```bash + rm -r azure-openai -f + git clone https://github.com/MicrosoftLearning/mslearn-openai azure-openai + ``` + +6. The files are downloaded to a folder named **azure-openai**. Navigate to the lab files for this exercise using the following command. + + ```bash + cd azure-openai/Labfiles/03-prompt-engineering + ``` + + Applications for both C# and Python have been provided, as well as a text files that provide the prompts. Both apps feature the same functionality. + + Open the built-in code editor, and you can observe the prompt files that you'll be using in `prompts`. Use the following command to open the lab files in the code editor. + + ```bash + code . + ``` + +## Configure your application + +For this exercise, you'll complete some key parts of the application to enable using your Azure OpenAI resource. + +1. In the code editor, expand the **CSharp** or **Python** folder, depending on your language preference. + +2. Open the configuration file for your language. + + - C#: `appsettings.json` + - Python: `.env` + +3. Update the configuration values to include the **endpoint** and **key** from the Azure OpenAI resource you created, as well as the model name that you deployed, `text-turbo`. Save the file. + +4. Navigate to the folder for your preferred language and install the necessary packages. + + **C#** + + ```bash + cd CSharp + dotnet add package Azure.AI.OpenAI --version 1.0.0-beta.5 + ``` + + **Python** + + ```bash + cd Python + pip install python-dotenv + pip install openai + ``` + +5. Navigate to your preferred language folder, select the code file, and add the necessary libraries. + + **C#** + + ```csharp + // Add Azure OpenAI package + using Azure.AI.OpenAI; + ``` + + **Python** + + ```python + # Add OpenAI import + import openai + ``` + +5. Open up the application code for your language and add the necessary code for configuring the client. + + **C#** + + ```csharp + // Initialize the Azure OpenAI client + OpenAIClient client = new OpenAIClient(new Uri(oaiEndpoint), new AzureKeyCredential(oaiKey)); + ``` + + **Python** + + ```python + # Set OpenAI configuration settings + openai.api_type = "azure" + openai.api_base = azure_oai_endpoint + openai.api_version = "2023-03-15-preview" + openai.api_key = azure_oai_key + ``` + +6. In the function that calls the Azure OpenAI model, add the code to format and send the request to the model. + + **C#** + + ```csharp + // Create chat completion options + var chatCompletionsOptions = new ChatCompletionsOptions() + { + Messages = + { + new ChatMessage(ChatRole.System, systemPrompt), + new ChatMessage(ChatRole.User, userPrompt) + }, + Temperature = 0.7f, + MaxTokens = 800, + }; + + // Get response from Azure OpenAI + Response response = await client.GetChatCompletionsAsync( + oaiModelName, + chatCompletionsOptions + ); + + ChatCompletions completions = response.Value; + string completion = completions.Choices[0].Message.Content; + ``` + + **Python** + + ```python + # Build the messages array + messages =[ + {"role": "system", "content": system_message}, + {"role": "user", "content": user_message}, + ] + + # Call the Azure OpenAI model + response = openai.ChatCompletion.create( + engine=model, + messages=messages, + temperature=0.7, + max_tokens=800 + ) + ``` + +## Run your application + +Now that your app has been configured, run it to send your request to your model and observe the response. You'll notice the only difference between the different options is the content of the prompt, all other parameters (such as token count and temperature) remain the same for each request. + +Each prompt is displayed in the console as it sends for you to see how differences in prompts produce different responses. + +1. In the Cloud Shell bash terminal, navigate to the folder for your preferred language. +1. Run the application, and expand the terminal to take up most of your browser window. + + - **C#**: `dotnet run` + - **Python**: `python prompt-engineering.py` + +1. Choose option **1** for the most basic prompt. +1. Observe the prompt input, and generated output. The AI model will likely produce a good generic introduction to a wildlife rescue. +1. Next, choose option **2** to give it a prompt asking for an intro email, along with some details about the wildlife rescue. +1. Observe the prompt input, and generated output. This time, you'll likely see the format of an email with the specific animals included, as well as the call for donations. +1. Next, choose option **3** to ask for an email similar to above, but with a formatted table with additional animals included. +1. Observe the prompt input, and generated output. This time, you'll likely see a similar email with text formatted in a specific way (in this case, a table near the end) demonstrating how the generative AI models can format output when requested. +1. Next, choose option **4** to ask for a similar email, but this time specifying different tone in the system message. +1. Observe the prompt input, and generated output. This time you'll likely see the email in a similar format, but with a much more informal tone. You'll likely even see jokes included! + +Increasing the temperature often causes the response to vary, even when provided the same prompt, due to the increased randomness. You can run it multiple times with different temperature or top_p values to see how that impacts the response to the same prompt. + +If you would like to see the full response from Azure OpenAI, you can set the `printFullResponse` variable to `True`, and rerun the app. + +## Clean up + +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). diff --git a/Instructions/Exercises/04-code-generation.md b/Instructions/Exercises/04-code-generation.md new file mode 100644 index 00000000..f16ad01f --- /dev/null +++ b/Instructions/Exercises/04-code-generation.md @@ -0,0 +1,268 @@ +--- +lab: + title: 'Generate and improve code with Azure OpenAI Service' +--- + +# Generate and improve code with Azure OpenAI Service + +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. + +This exercise will take approximately **25** minutes. + +## Before you start + +You will need an Azure subscription that has been approved for access to the Azure OpenAI service. + +- To sign up for a free Azure subscription, visit [https://azure.microsoft.com/free](https://azure.microsoft.com/free). +- To request access to the Azure OpenAI service, visit [https://aka.ms/oaiapply](https://aka.ms/oaiapply). + +## Provision an Azure OpenAI resource + +Before you can use Azure OpenAI models, you must provision an Azure OpenAI resource in your Azure subscription. + +1. Sign into the [Azure portal](https://portal.azure.com). +2. Create an **Azure OpenAI** resource with the following settings: + - **Subscription**: An Azure subscription that has been approved for access to the Azure OpenAI service. + - **Resource group**: Choose an existing resource group, or create a new one with a name of your choice. + - **Region**: Choose any available region. + - **Name**: A unique name of your choice. + - **Pricing tier**: Standard S0 +3. Wait for deployment to complete. Then go to the deployed Azure OpenAI resource in the Azure portal. +4. Navigate to **Keys and Endpoint** page, and save those to a text file to use later. + +## Deploy a model + +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. + +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. +2. In Azure OpenAI Studio, create a new deployment with the following settings: + - **Model**: gpt-35-turbo + - **Model version**: *Use the default version* + - **Deployment name**: 35turbo + +> **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. + +## Generate code in chat playground + +Before using in your app, examine how Azure OpenAI can generate and explain code in the chat playground. + +1. In [Azure OpenAI Studio](https://oai.azure.com/?azure-portal=true), navigate to the **Chat** playground in the left pane. +1. In the **Assistant setup** section at the top, select the **Default** system message template. +1. In the **Chat session** section, enter the following prompt and press *Enter*. + + ```code + Write a function in python that takes a character and string as input, and returns how many times that character appears in the string + ``` + +1. The model will likely respond with a function, with some explanation of what the function does and how to call it. +1. Next, send the prompt `Do the same thing, but this time write it in C#`. +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. +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. + + ```code + What does the following function do? + --- + def random_func(n) + start = [0, 1] + (n - 2).times do + start << start[-1] + start[-2] + end + start.shuffle.each do |num| + puts num + end + end + ``` + +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. + +## Set up an application in Cloud Shell + +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. + +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. + + ![Screenshot of starting Cloud Shell by clicking on the icon to the right of the top search box.](../media/cloudshell-launch-portal.png#lightbox) + +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. + +3. If you're prompted to create storage for your Cloud Shell, select **Show advanced settings** and select the following settings: + - **Subscription**: Your subscription + - **Cloud shell regions**: Choose any available region + - **Show VNET isolation setings** Unselected + - **Resource group**: Use the existing resource group where you provisioned your Azure OpenAI resource + - **Storage account**: Create a new storage account with a unique name + - **File share**: Create a new file share with a unique name + + Then wait a minute or so for the storage to be created. + + > **Note**: If you already have a cloud shell set up in your Azure subscription, you may need to use the **Reset user settings** option in the ⚙️ menu to ensure the latest versions of Python and the .NET Framework are installed. + +4. Make sure the type of shell indicated on the top left of the Cloud Shell pane is *Bash*. If it's *PowerShell*, switch to *Bash* by using the drop-down menu. + +5. Once the terminal starts, enter the following command to download the sample application and save it to a folder called `azure-openai`. + + ```bash + rm -r azure-openai -f + git clone https://github.com/MicrosoftLearning/mslearn-openai azure-openai + ``` + +6. The files are downloaded to a folder named **azure-openai**. Navigate to the lab files for this exercise using the following command. + + ```bash + cd azure-openai/Labfiles/04-code-generation + ``` + + Applications for both C# and Python have been provided, as well as sample code we'll be using in this lab. + + 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. + + ```bash + code . + ``` + +## Configure your application + +For this exercise, you'll complete some key parts of the application to enable using your Azure OpenAI resource. + +1. In the code editor, expand the language folder for your preferred language. + +2. Open the configuration file for your language. + + - **C#**: `appsettings.json` + - **Python**: `.env` + +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. + +4. Navigate to the folder for your preferred language and install the necessary packages. + + **C#** + + ```bash + cd CSharp + dotnet add package Azure.AI.OpenAI --version 1.0.0-beta.5 + ``` + + **Python** + + ```bash + cd Python + pip install python-dotenv + pip install openai + ``` + +5. Select the code file in this folder for your language and add the necessary libraries. + + **C#** + + `Program.cs` + + ```csharp + // Add Azure OpenAI package + using Azure.AI.OpenAI; + ``` + + **Python** + + `code-generation.py` + + ```python + # Add OpenAI import + import openai + ``` + +6. Add the necessary code for configuring the client. + + **C#** + + ```csharp + // Initialize the Azure OpenAI client + OpenAIClient client = new OpenAIClient(new Uri(oaiEndpoint), new AzureKeyCredential(oaiKey)); + ``` + + **Python** + + ```python + # Set OpenAI configuration settings + openai.api_type = "azure" + openai.api_base = azure_oai_endpoint + openai.api_version = "2023-05-15" + openai.api_key = azure_oai_key + ``` + +7. In the function that calls the Azure OpenAI model, add the code to format and send the request to the model. + + **C#** + + ```csharp + // Create chat completion options + var chatCompletionsOptions = new ChatCompletionsOptions() + { + Messages = + { + new ChatMessage(ChatRole.System, systemPrompt), + new ChatMessage(ChatRole.User, userPrompt) + }, + Temperature = 0.7f, + MaxTokens = 1000, + }; + + // Get response from Azure OpenAI + Response response = await client.GetChatCompletionsAsync( + oaiModelName, + chatCompletionsOptions + ); + + ChatCompletions completions = response.Value; + string completion = completions.Choices[0].Message.Content; + ``` + + **Python** + + ```python + # Build the messages array + messages =[ + {"role": "system", "content": system_message}, + {"role": "user", "content": user_message}, + ] + + # Call the Azure OpenAI model + response = openai.ChatCompletion.create( + engine=model, + messages=messages, + temperature=0.7, + max_tokens=1000 + ) + ``` + +## Run your application + +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. + +> **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. + +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. +1. In the Cloud Shell bash terminal, navigate to the folder for your preferred language. +1. Run the application. + + - **C#**: `dotnet run` + - **Python**: `python code-generation.py` + +1. Choose option **1** to add comments to your code. Note, the response might take a few seconds for each of these tasks. +1. The results will be put into `result/app.txt`. Open that file up, and compare it to the function file in `sample-code`. +1. Next, choose option **2** to write unit tests for that same function. +1. The results will replace what was in `result/app.txt`, and details four unit tests for that function. +1. Next, choose option **3** to fix bugs in an app for playing Go Fish. +1. The results will replace what was in `result/app.txt`, and should have very similar code with a few things corrected. + + - **C#**: Fixes are made on line 30 and 59 + - **Python**: Fixes are made on line 18 and 31 + +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. + +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. + +If you would like to see the full response from Azure OpenAI, you can set the `printFullResponse` variable to `True`, and rerun the app. + +## Clean up + +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). diff --git a/Instructions/Exercises/05-generate-images.md b/Instructions/Exercises/05-generate-images.md new file mode 100644 index 00000000..ba7b40e7 --- /dev/null +++ b/Instructions/Exercises/05-generate-images.md @@ -0,0 +1,168 @@ +--- +lab: + title: 'Generate images with a DALL-E model' +--- + +# Generate images with a DALL-E model + +The Azure OpenAI Service includes an image-generation model named DALL-E. You can use this model to submit natural language prompts that describe a desired image, and the model will generate an original image based on the description you provide. + +This exercise will take approximately **25** minutes. + +## Before you start + +You will need an Azure subscription that has been approved for access to the Azure OpenAI service, including DALL-E. If you have previously applied for access to the Azure openAI service, you may need to submit another application to gain access to DALL-E. + +- To sign up for a free Azure subscription, visit [https://azure.microsoft.com/free](https://azure.microsoft.com/free). +- To request access to the Azure OpenAI service, visit [https://aka.ms/oaiapply](https://aka.ms/oaiapply). + +## Provision an Azure OpenAI resource + +Before you can use Azure OpenAI models, you must provision an Azure OpenAI resource in your Azure subscription. + +1. Sign into the [Azure portal](https://portal.azure.com). +2. Create an **Azure OpenAI** resource with the following settings: + - **Subscription**: An Azure subscription that has been approved for access to the Azure OpenAI service. + - **Resource group**: Choose an existing resource group, or create a new one with a name of your choice. + - **Region**: Choose **EastUS** as region + - **Name**: A unique name of your choice. + - **Pricing tier**: Standard S0 +3. Wait for deployment to complete. Then go to the deployed Azure OpenAI resource in the Azure portal. +4. Navigate to **Keys and Endpoint** page. You can retrieve the unique endpoint and authentication keys for your service from here - you'll need these later! + +## Explore image-generation in the DALL-E playground + +You can use the DALL-E playground in **Azure OpenAI Studio** to experiment with image-generation. + +1. In the Azure portal, 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. +2. Select the **DALL-E Playground**. +3. In the **Prompt** box, enter a description of an image you'd like to generate. For example, *An elephant on a skateboard*. Then select **Generate** and view the image that is generated. + + ![The DALL-E Playground in Azure OpenAI Studio with a generated image.](../media/dall-e-playground.png) + +4. Modify the prompt to provide a more specific description. For example *An elephant on a skateboard in the style of Picasso*. Then generate the new image and review the results. + + ![The DALL-E Playground in Azure OpenAI Studio with two generated images.](../media/dall-e-playground-new-image.png) + +## Use the REST API to generate images + +The Azure OpenAI service provides a REST API that you can use to submit prompts for content generation - including images generated by a DALL-E model. + +### Prepare the app environment + +In this exercise, you'll use a simple Python or Microsoft C# app to generate images by calling the REST API. You'll run the code in the cloud shell console interface in the Azure portal. + +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. + + ![Screenshot of starting Cloud Shell by clicking on the icon to the right of the top search box.](../media/cloudshell-launch-portal.png#lightbox) + +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. + +3. If you're prompted to create storage for your Cloud Shell, select **Show advanced settings** and select the following settings: + - **Subscription**: Your subscription + - **Cloud shell regions**: Choose any available region + - **Show VNET isolation setings** Unselected + - **Resource group**: Use the existing resource group where you provisioned your Azure OpenAI resource + - **Storage account**: Create a new storage account with a unique name + - **File share**: Create a new file share with a unique name + + Then wait a minute or so for the storage to be created. + + > **Note**: If you already have a cloud shell set up in your Azure subscription, you may need to use the **Reset user settings** option in the ⚙️ menu to ensure the latest versions of Python and the .NET Framework are installed. + +4. Make sure the type of shell indicated on the top left of the Cloud Shell pane is *Bash*. If it's *PowerShell*, switch to *Bash* by using the drop-down menu. + +5. Once the terminal starts, enter the following command to download the application code you are going to work with. + + ```bash + rm -r azure-openai -f + git clone https://github.com/MicrosoftLearning/mslearn-openai azure-openai + ``` + + The files are downloaded to a folder named **azure-openai**. Applications for both C# and Python have been provided. Both apps feature the same functionality. + +6. Navigate to the folder for the language you want to use by running the appropriate command. + + **Python** + + ```bash + cd azure-openai/Labfiles/05-image-generation/Python + ``` + + **C#** + + ```bash + cd azure-openai/Labfiles/05-image-generation/CSharp + ``` + +7. Use the following command to open the built-in code editor and see the code files you will be working with. + + ```bash + code . + ``` + +### Configure your application + +The application uses a configuration file to store the details needed to connect to your Azure OpenAI service account. + +1. In the code editor, select the configuration file for your app - depending on your language preference. + + - C#: `appsettings.json` + - Python: `.env` + +2. Update the configuration values to include the **Endpoint** and **Key1** for your Azure OpenAI service and then save the file. + + > **Tip**: You can adjust the split at the top of the cloud shell pane to see the Azure portal, and get the endpoint and key values from the **Keys and Endpoint** page for your Azure OpenAI service. + +3. If you are using **Python**, you'll also need to install the **python-dotenv** package used to read the configuration file. In the console prompt pane, ensure the current folder is **~/azure-openai/Labfiles/05-image-generation/Python**. Then enter this command: + + ```bash + pip install python-dotenv + ``` + +### View application code + +Now you're ready to explore the code used to call the REST API and generate an image. + +1. In the code editor pane, select the main code file for your application: + + - C#: `Program.cs` + - Python: `generate-image.py` + +2. Review the code that the file contains, noting the following key features: + - The code makes https requests to the endpoint for your service, including the key for your service in the header. Both of these values are obtained from the configuration file. + - The process consists of two REST requests: One to initiate the image-generation request, and another to retrieve the results. + The initial request includes the following data: + - The user-provided prompt that describes the image to be generated + - The number of images to be generated (in this case, 1) + - The resolution (size) of the image to be generated. + - The response header from the initial request includes an **operation-location** value that is used for the subsequent callback to get the results. + - The code polls the callback URL until the status of the image-generation task is *succeeded*, and then extracts and displays a URL for the generated image. + +### Run the app + +Now that you've reviewed the code, it's time to run it and generate some images. + +1. In the console prompt pane, enter the appropriate command to run your application: + + **Python** + + ```bash + python generate-image.py + ``` + + **C#** + + ```bash + dotnet run + ``` + +2. When prompted, enter a description for an image. For example, *A giraffe flying a kite*. + +3. Wait for the image to be generated - a hyperlink will be displayed in the console pane. Then select the hyperlink to open a new browser tab and review the image that was generated. + +4. Close the tab containing the generated image and re-run the app to generate a new image with a different prompt. + +## Clean up + +When you're done with your Azure OpenAI resource, remember to delete the resource in the [Azure portal](https://portal.azure.com/?azure-portal=true). diff --git a/Instructions/Exercises/06-use-own-data.md b/Instructions/Exercises/06-use-own-data.md new file mode 100644 index 00000000..182b3e09 --- /dev/null +++ b/Instructions/Exercises/06-use-own-data.md @@ -0,0 +1,117 @@ +--- +lab: + title: 'Use your own data with Azure OpenAI' +--- + +# Use your own data with Azure OpenAI + +The Azure OpenAI Service enables you to use your own data with the intelligence of the underlying LLM. You can limit the model to only use your data for pertinent topics, or blend it with results from the pre-trained model. + +This exercise will take approximately **20** minutes. + +## Before you start + +You will need an Azure subscription that has been approved for access to the Azure OpenAI service. + +- To sign up for a free Azure subscription, visit [https://azure.microsoft.com/free](https://azure.microsoft.com/free). +- To request access to the Azure OpenAI service, visit [https://aka.ms/oaiapply](https://aka.ms/oaiapply). + +## Provision an Azure OpenAI resource + +Before you can use Azure OpenAI models, you must provision an Azure OpenAI resource in your Azure subscription. + +1. Sign into the [Azure portal](https://portal.azure.com?azure-portal=true). +2. Create an **Azure OpenAI** resource with the following settings: + - **Subscription**: An Azure subscription that has been approved for access to the Azure OpenAI service. + - **Resource group**: Choose an existing resource group, or create a new one with a name of your choice. + - **Region**: Choose any available region. + - **Name**: A unique name of your choice. + - **Pricing tier**: Standard S0 +3. Wait for deployment to complete. Then go to the deployed Azure OpenAI resource in the Azure portal. + +## Deploy a model + +To chat with the Azure OpenAI, you must first deploy a model to use through the **Azure OpenAI Studio**. Once deployed, we will use the model with the playground and use our data to ground its responses. + +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. +2. In Azure OpenAI Studio, create a new deployment with the following settings: + - **Model name**: gpt-35-turbo + - **Model version**: *Use the default version* + - **Deployment name**: text-turbo + +## Observe normal chat behavior without adding your own data + +Before connecting Azure OpenAI to your data, first observe how the base model responds to queries without any grounding data. + +1. Navigate to the **Chat** playground, and make sure the `gpt-35-turbo` model you deployed is selected in the **Configuration** pane (this should be the default, if you only have one deployed model). +1. Enter the following prompts, and observe the output. + + ```code + I'd like to take a trip to New York. Where should I stay? + ``` + + ```code + What are some facts about New York? + ``` + +1. Try similar questions about tourism and places to stay for other locations that will be included in our grounding data, such as London, or San Francisco. You'll likely get complete responses about areas or neighborhoods, and some general facts about the city. + +## Connect your data in the chat playground + +Next, add your data in the chat playground to see how it responds with your data as grounding + +1. [Download the data](https://aka.ms/own-data-brochures) that you will use from GitHub. Extract the PDFs in the `.zip` provided. +1. Navigate to the **Chat** playground, and select *Add your data* in the Assistant setup pane. +1. Select **Add a data source** and choose *Upload files* from the dropdown. +1. You'll need to create a storage account and Azure Cognitive Search resource. Under the dropdown for the storage resource, select **Create a new Azure Blob storage resource**, and create a storage account with the following settings. Anything not specified leave as the default. + + - **Subscription**: *Same subscription as your Azure OpenAI resource* + - **Resource group**: *Same resource group as your Azure OpenAI resource* + - **Storage account name**: *Enter globally unique name* + - **Region**: *Same region as your Azure OpenAI resource* + - **Redundancy**: Locally-redundant storage (LRS) + +1. Once the resource is being created, come back to Azure OpenAI Studio and select **Create a new Azure Cognitive Search resource** with the following settings. Anything not specified leave as the default. + + - **Subscription**: *Same subscription as your Azure OpenAI resource* + - **Resource group**: *Same resource group as your Azure OpenAI resource* + - **Service name**: *Enter globally unique name* + - **Location**: *Same location as your Azure OpenAI resource* + - **Pricing tier**: Basic + +1. Wait until your search resource has been deployed, then switch back to the Azure AI Studio and refresh the page. +1. In the **Add data**, enter the following values for your data source, then select **Next**. + + - **Select data source**: Upload files + - **Select Azure Blob storage resouce**: *Choose the storage resource you created* + - Turn on CORS when prompted + - **Select Azure Cognitive Search resource**: *Choose the search resource you created* + - **Enter the index name**: margiestravel + - **Add vector search to this search resource**: unchecked + - **I acknowledge that connecting to an Azure Cognitive Search account will incur usage to my account** : checked + +1. On the **Upload files** page, upload the PDFs you downloaded, and then select **Next**. +1. On the **Data management** page select the **Keyword** search type from the drop-down, and then select **Next**. +1. On the **Review and finish** page select **Save and close**, which will add your data. This may take a few minutes, during which you need to leave your window open. Once complete, you'll see the data source, search resource, and index specified in the **Assistant setup** pane. + +## Chat with a model grounded in your data + +Now that you've added your data, ask the same questions as you did previously, and see how the response differs. + +``` +I'd like to take a trip to New York. Where should I stay? +``` + +``` +What are some facts about New York? +``` + +You'll notice a very different response this time, with specifics about certain hotels and a mention of Margie's Travel, as well as references to where the information provided came from. If you open the PDF reference listed in the response, you'll see the same hotels as the model provided. + +Try asking it about other cities included in the grounding data, which are Dubai, Las Vegas, London, and San Francisco. + +> **Note**: **Add your data** is still in preview and might not always behave as expected for this feature, such as giving the incorrect reference for a city not included in the grounding data. + +## Clean up + +When you're done with your Azure OpenAI resource, remember to delete the resource in the [Azure portal](https://portal.azure.com/?azure-portal=true). Be sure to also include the storage account and search resource, as those can incur a relatively large cost. diff --git a/Instructions/Labs/01-get-started-azure-openai.md b/Instructions/Labs/01-get-started-azure-openai.md index ccc8e775..4865bb69 100644 --- a/Instructions/Labs/01-get-started-azure-openai.md +++ b/Instructions/Labs/01-get-started-azure-openai.md @@ -23,7 +23,7 @@ Before you can use Azure OpenAI models, you must provision an Azure OpenAI resou 1. Sign into the [Azure portal](https://portal.azure.com). 2. Create an **Azure OpenAI** resource with the following settings: - **Subscription**: An Azure subscription that has been approved for access to the Azure OpenAI service. - - **Resource group**: Create a new resource group with a name of your choice. + - **Resource group**: Choose an existing resource group, or create a new one with a name of your choice. - **Region**: Choose any available region. - **Name**: A unique name of your choice. - **Pricing tier**: Standard S0 @@ -33,10 +33,10 @@ Before you can use Azure OpenAI models, you must provision an Azure OpenAI resou Azure OpenAI provides a web-based portal named **Azure OpenAI Studio**, that you can use to deploy, manage, and explore models. You'll start your exploration of Azure OpenAI by using Azure OpenAI Studio to deploy a model. -1. On the **Overview** page for your Azure OpenAI resource, use the **Explore** button to open Azure OpenAI Studio in a new browser tab. +1. On the **Overview** page for your Azure OpenAI resource, use the **Go to Azure OpenAI Studio** button to open Azure OpenAI Studio in a new browser tab. 2. In Azure OpenAI Studio, create a new deployment with the following settings: - **Model**: gpt-35-turbo - - **Modev version**: Auto-update to default + - **Model version**: Auto-update to default - **Deployment name**: my-gpt-model > **Note**: Azure OpenAI includes multiple models, each optimized for a different balance of capabilities and performance. In this exercise, you'll use the **GPT-35-Turbo** model, which is a good general model for summarizing and generating natural language and code. For more information about the available models in Azure OpenAI, see [Models](https://learn.microsoft.com/azure/cognitive-services/openai/concepts/models) in the Azure OpenAI documentation. diff --git a/Instructions/Labs/02-natural-language-azure-openai.md b/Instructions/Labs/02-natural-language-azure-openai.md index 9ea6be77..e254b206 100644 --- a/Instructions/Labs/02-natural-language-azure-openai.md +++ b/Instructions/Labs/02-natural-language-azure-openai.md @@ -23,7 +23,7 @@ Before you can use Azure OpenAI models, you must provision an Azure OpenAI resou 1. Sign into the [Azure portal](https://portal.azure.com). 2. Create an **Azure OpenAI** resource with the following settings: - **Subscription**: An Azure subscription that has been approved for access to the Azure OpenAI service. - - **Resource group**: Create a new resource group with a name of your choice. + - **Resource group**: Choose an existing resource group, or create a new one with a name of your choice. - **Region**: Choose any available region. - **Name**: A unique name of your choice. - **Pricing tier**: Standard S0 @@ -52,9 +52,19 @@ To show how to integrate with an Azure OpenAI model, we'll use a short command-l 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. -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. +3. If you're prompted to create storage for your Cloud Shell, select **Show advanced settings** and select the following settings: + - **Subscription**: Your subscription + - **Cloud shell regions**: Choose any available region + - **Show VNET isolation setings** Unselected + - **Resource group**: Use the existing resource group where you provisioned your Azure OpenAI resource + - **Storage account**: Create a new storage account with a unique name + - **File share**: Create a new file share with a unique name -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. + Then wait a minute or so for the storage to be created. + + > **Note**: If you already have a cloud shell set up in your Azure subscription, you may need to use the **Reset user settings** option in the ⚙️ menu to ensure the latest versions of Python and the .NET Framework are installed. + +4. Make sure the type of shell indicated on the top left of the Cloud Shell pane is *Bash*. If it's *PowerShell*, switch to *Bash* by using the drop-down menu. 5. Once the terminal starts, enter the following command to download the sample application and save it to a folder called `azure-openai`. @@ -96,7 +106,7 @@ For this exercise, you'll complete some key parts of the application to enable u ```bash cd CSharp - dotnet add package Azure.AI.OpenAI --prerelease + dotnet add package Azure.AI.OpenAI --version 1.0.0-beta.5 ``` **Python** diff --git a/Instructions/Labs/03-prompt-engineering.md b/Instructions/Labs/03-prompt-engineering.md index 819b20c6..4b726f5f 100644 --- a/Instructions/Labs/03-prompt-engineering.md +++ b/Instructions/Labs/03-prompt-engineering.md @@ -9,8 +9,6 @@ With the Azure OpenAI Service, developers can create chatbots, language models, When working with the Azure OpenAI Service, how developers shape their prompt greatly impacts how the generative AI model will respond. Azure OpenAI models are able to tailor and format content, if requested in a clear and concise way. In this exercise, you'll learn how different prompts for similar content help shape the AI model's response to better satisfy your requirements. -Imagine you are trying to send out information for a new wildlife rescue, and want to get assistance from a generative AI model. - This exercise will take approximately **25** minutes. ## Before you start @@ -27,7 +25,7 @@ Before you can use Azure OpenAI models, you must provision an Azure OpenAI resou 1. Sign into the [Azure portal](https://portal.azure.com). 2. Create an **Azure OpenAI** resource with the following settings: - **Subscription**: An Azure subscription that has been approved for access to the Azure OpenAI service. - - **Resource group**: Create a new resource group with a name of your choice. + - **Resource group**: Choose an existing resource group, or create a new one with a name of your choice. - **Region**: Choose any available region. - **Name**: A unique name of your choice. - **Pricing tier**: Standard S0 @@ -150,9 +148,19 @@ To show how to integrate with an Azure OpenAI model, we'll use a short command-l 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. -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. +3. If you're prompted to create storage for your Cloud Shell, select **Show advanced settings** and select the following settings: + - **Subscription**: Your subscription + - **Cloud shell regions**: Choose any available region + - **Show VNET isolation setings** Unselected + - **Resource group**: Use the existing resource group where you provisioned your Azure OpenAI resource + - **Storage account**: Create a new storage account with a unique name + - **File share**: Create a new file share with a unique name + + Then wait a minute or so for the storage to be created. + + > **Note**: If you already have a cloud shell set up in your Azure subscription, you may need to use the **Reset user settings** option in the ⚙️ menu to ensure the latest versions of Python and the .NET Framework are installed. -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. +4. Make sure the type of shell indicated on the top left of the Cloud Shell pane is *Bash*. If it's *PowerShell*, switch to *Bash* by using the drop-down menu. 5. Once the terminal starts, enter the following command to download the sample application and save it to a folder called `azure-openai`. @@ -304,7 +312,7 @@ Each prompt is displayed in the console as it sends for you to see how differenc 1. Next, choose option **3** to ask for an email similar to above, but with a formatted table with additional animals included. 1. Observe the prompt input, and generated output. This time, you'll likely see a similar email with text formatted in a specific way (in this case, a table near the end) demonstrating how the generative AI models can format output when requested. 1. Next, choose option **4** to ask for a similar email, but this time specifying different tone in the system message. -1. Observe the prompt input, and generated output. This time you'll likely see the email in a similar format, but with a much less informal tone. You'll likely even see jokes included! +1. Observe the prompt input, and generated output. This time you'll likely see the email in a similar format, but with a much more informal tone. You'll likely even see jokes included! Increasing the temperature often causes the response to vary, even when provided the same prompt, due to the increased randomness. You can run it multiple times with different temperature or top_p values to see how that impacts the response to the same prompt. diff --git a/Instructions/Labs/04-code-generation.md b/Instructions/Labs/04-code-generation.md index e5086c4b..fe56279d 100644 --- a/Instructions/Labs/04-code-generation.md +++ b/Instructions/Labs/04-code-generation.md @@ -23,7 +23,7 @@ Before you can use Azure OpenAI models, you must provision an Azure OpenAI resou 1. Sign into the [Azure portal](https://portal.azure.com). 2. Create an **Azure OpenAI** resource with the following settings: - **Subscription**: An Azure subscription that has been approved for access to the Azure OpenAI service. - - **Resource group**: Create a new resource group with a name of your choice. + - **Resource group**: Choose an existing resource group, or create a new one with a name of your choice. - **Region**: Choose any available region. - **Name**: A unique name of your choice. - **Pricing tier**: Standard S0 @@ -85,9 +85,19 @@ To show how to integrate with an Azure OpenAI model, we'll use a short command-l 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. -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. +3. If you're prompted to create storage for your Cloud Shell, select **Show advanced settings** and select the following settings: + - **Subscription**: Your subscription + - **Cloud shell regions**: Choose any available region + - **Show VNET isolation setings** Unselected + - **Resource group**: Use the existing resource group where you provisioned your Azure OpenAI resource + - **Storage account**: Create a new storage account with a unique name + - **File share**: Create a new file share with a unique name -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. + Then wait a minute or so for the storage to be created. + + > **Note**: If you already have a cloud shell set up in your Azure subscription, you may need to use the **Reset user settings** option in the ⚙️ menu to ensure the latest versions of Python and the .NET Framework are installed. + +4. Make sure the type of shell indicated on the top left of the Cloud Shell pane is *Bash*. If it's *PowerShell*, switch to *Bash* by using the drop-down menu. 5. Once the terminal starts, enter the following command to download the sample application and save it to a folder called `azure-openai`. diff --git a/Instructions/Labs/05-generate-images.md b/Instructions/Labs/05-generate-images.md index 4cb6a43d..ba7b40e7 100644 --- a/Instructions/Labs/05-generate-images.md +++ b/Instructions/Labs/05-generate-images.md @@ -23,8 +23,8 @@ Before you can use Azure OpenAI models, you must provision an Azure OpenAI resou 1. Sign into the [Azure portal](https://portal.azure.com). 2. Create an **Azure OpenAI** resource with the following settings: - **Subscription**: An Azure subscription that has been approved for access to the Azure OpenAI service. - - **Resource group**: Create a new resource group with a name of your choice. - - **Region**: Choose any available region. + - **Resource group**: Choose an existing resource group, or create a new one with a name of your choice. + - **Region**: Choose **EastUS** as region - **Name**: A unique name of your choice. - **Pricing tier**: Standard S0 3. Wait for deployment to complete. Then go to the deployed Azure OpenAI resource in the Azure portal. @@ -58,11 +58,19 @@ In this exercise, you'll use a simple Python or Microsoft C# app to generate ima 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. -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. +3. If you're prompted to create storage for your Cloud Shell, select **Show advanced settings** and select the following settings: + - **Subscription**: Your subscription + - **Cloud shell regions**: Choose any available region + - **Show VNET isolation setings** Unselected + - **Resource group**: Use the existing resource group where you provisioned your Azure OpenAI resource + - **Storage account**: Create a new storage account with a unique name + - **File share**: Create a new file share with a unique name + + Then wait a minute or so for the storage to be created. > **Note**: If you already have a cloud shell set up in your Azure subscription, you may need to use the **Reset user settings** option in the ⚙️ menu to ensure the latest versions of Python and the .NET Framework are installed. -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. +4. Make sure the type of shell indicated on the top left of the Cloud Shell pane is *Bash*. If it's *PowerShell*, switch to *Bash* by using the drop-down menu. 5. Once the terminal starts, enter the following command to download the application code you are going to work with. diff --git a/Instructions/Labs/06-use-own-data.md b/Instructions/Labs/06-use-own-data.md index 8a9d46db..182b3e09 100644 --- a/Instructions/Labs/06-use-own-data.md +++ b/Instructions/Labs/06-use-own-data.md @@ -23,7 +23,7 @@ Before you can use Azure OpenAI models, you must provision an Azure OpenAI resou 1. Sign into the [Azure portal](https://portal.azure.com?azure-portal=true). 2. Create an **Azure OpenAI** resource with the following settings: - **Subscription**: An Azure subscription that has been approved for access to the Azure OpenAI service. - - **Resource group**: Create a new resource group with a name of your choice. + - **Resource group**: Choose an existing resource group, or create a new one with a name of your choice. - **Region**: Choose any available region. - **Name**: A unique name of your choice. - **Pricing tier**: Standard S0 @@ -80,27 +80,29 @@ Next, add your data in the chat playground to see how it responds with your data - **Pricing tier**: Basic 1. Wait until your search resource has been deployed, then switch back to the Azure AI Studio and refresh the page. -1. In the **Add data**, enter the following values for your data source. +1. In the **Add data**, enter the following values for your data source, then select **Next**. - **Select data source**: Upload files - **Select Azure Blob storage resouce**: *Choose the storage resource you created* - Turn on CORS when prompted - **Select Azure Cognitive Search resource**: *Choose the search resource you created* - **Enter the index name**: margiestravel - - Select the check box + - **Add vector search to this search resource**: unchecked + - **I acknowledge that connecting to an Azure Cognitive Search account will incur usage to my account** : checked -1. On the **Upload files** page, upload the PDFs you downloaded. -1. Select **Save and close**, which will add your data. This may take a few minutes, during which you need to leave your window open. Once complete, you'll see the data source, search resource, and index specified in the **Assistant setup** pane. +1. On the **Upload files** page, upload the PDFs you downloaded, and then select **Next**. +1. On the **Data management** page select the **Keyword** search type from the drop-down, and then select **Next**. +1. On the **Review and finish** page select **Save and close**, which will add your data. This may take a few minutes, during which you need to leave your window open. Once complete, you'll see the data source, search resource, and index specified in the **Assistant setup** pane. ## Chat with a model grounded in your data Now that you've added your data, ask the same questions as you did previously, and see how the response differs. -```code +``` I'd like to take a trip to New York. Where should I stay? ``` -```code +``` What are some facts about New York? ```