You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* initial refresh
* fixes to lab 2
* refresh of lab 2 and 3
* updates for lab 2 and base for lab 3
* final of lab 3
* Add base level lab 4
* final lab 4
* updates to lab 6
* delete lab 3 system boilerplates
Copy file name to clipboardExpand all lines: Instructions/Exercises/01-get-started-azure-openai.md
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ lab:
7
7
8
8
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 generative AI models.
9
9
10
-
In scenario for this exercise, you will perform the role of a software developer who has been tasked to implement an AI agent that can use generative AI to help a marketing organization improve its effectiveness at reaching customers and advertising new products. The techniques used in the exercise can be applied to any scenario where an organization wants to use generative AI models to help employees be more effective and productive.
10
+
In the scenario for this exercise, you will perform the role of a software developer who has been tasked to implement an AI agent that can use generative AI to help a marketing organization improve its effectiveness at reaching customers and advertising new products. The techniques used in the exercise can be applied to any scenario where an organization wants to use generative AI models to help employees be more effective and productive.
11
11
12
12
This exercise takes approximately **30** minutes.
13
13
@@ -70,7 +70,7 @@ Now that you've deployed a model, you can use it to generate responses based on
70
70
-**Chat session** - used to submit chat messages and view responses.
71
71
-**Configuration** - used to configure settings for the model deployment.
72
72
1. In the **Configuration** panel, ensure that your gpt-35-turbo-16k model deployment is selected.
73
-
1. In the **Assistant setup** panel, review the default **System message**, which should be *You are an AI assistant that helps people find information.* The system message is included in prompts submitted to the model, and provides context for the model's responses; setting expectations about how an AI agent based on the model should interact with the user.
73
+
1. In the **Setup** panel, review the default **System message**, which should be *You are an AI assistant that helps people find information.* The system message is included in prompts submitted to the model, and provides context for the model's responses; setting expectations about how an AI agent based on the model should interact with the user.
74
74
1. In the **Chat session** panel, enter the user query `How can I use generative AI to help me market a new product?`
75
75
76
76
> **Note**: You may receive a response that the API deployment is not yet ready. If so, wait for a few minutes and try again.
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.
8
+
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.
9
+
10
+
In the scenario for this exercise, you will perform the role of a software developer who has been tasked to implement an app that can use generative AI to help provide hiking recommendations. The techniques used in the exercise can be applied to any app that wants to use Azure OpenAI APIs.
9
11
10
12
This exercise will take approximately **30** minutes.
11
13
@@ -70,21 +72,21 @@ You'll develop your Azure OpenAI app using Visual Studio Code. The code files fo
70
72
71
73
## Configure your application
72
74
73
-
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. First, you'll complete some key parts of the application to enable using your Azure OpenAI resource.
75
+
Applications for both C# and Python have been provided. Both apps feature the same functionality. First, you'll complete some key parts of the application to enable using your Azure OpenAI resource.
74
76
75
-
1. In Visual Studio Code, in the **Explorer** pane, browse to the **Labfiles/02-nlp-azure-openai** folder and expand the **CSharp** or **Python** folder depending on your language preference. Each folder contains the language-specific files for an app into which you're you're going to integrate Azure OpenAI functionality.
77
+
1. In Visual Studio Code, in the **Explorer** pane, browse to the **Labfiles/02-azure-openai-api** folder and expand the **CSharp** or **Python** folder depending on your language preference. Each folder contains the language-specific files for an app into which you're going to integrate Azure OpenAI functionality.
76
78
2. Right-click the **CSharp** or **Python** folder containing your code files and open an integrated terminal. Then install the Azure OpenAI SDK package by running the appropriate command for your language preference:
3. In the **Explorer** pane, in the **CSharp** or **Python** folder, open the configuration file for your preferred language
@@ -106,90 +108,207 @@ Now you're ready to use the Azure OpenAI SDK to consume your deployed model.
106
108
**C#**: Program.cs
107
109
108
110
```csharp
109
-
// Add Azure OpenAI package
110
-
using Azure.AI.OpenAI;
111
+
// Add Azure OpenAI package
112
+
using Azure.AI.OpenAI;
113
+
```
114
+
115
+
**Python**: test-openai-model.py
116
+
117
+
```python
118
+
# Add Azure OpenAI package
119
+
from openai import AzureOpenAI
120
+
```
121
+
122
+
1. In the application code for your language, replace the comment ***Initialize the Azure OpenAI client...*** with the following code to initialize the client and define our system message.
123
+
124
+
**C#**: Program.cs
125
+
126
+
```csharp
127
+
// Initialize the Azure OpenAI client
128
+
OpenAIClient client = new OpenAIClient(new Uri(oaiEndpoint), new AzureKeyCredential(oaiKey));
129
+
130
+
// System message to provide context to the model
131
+
string systemMessage = "I am a hiking enthusiast named Forest who helps people discover hikes in their area. If no area is specified, I will default to near Rainier National Park. I will then provide three suggestions for nearby hikes that vary in length. I will also share an interesting fact about the local nature on the hikes when making a recommendation.";
111
132
```
112
133
113
134
**Python**: test-openai-model.py
114
135
115
136
```python
116
-
# Add Azure OpenAI package
117
-
from openai import AzureOpenAI
137
+
# Initialize the Azure OpenAI client
138
+
client = AzureOpenAI(
139
+
azure_endpoint = azure_oai_endpoint,
140
+
api_key=azure_oai_key,
141
+
api_version="2024-02-15-preview"
142
+
)
143
+
144
+
# Create a system message
145
+
system_message = """I am a hiking enthusiast named Forest who helps people discover hikes in their area.
146
+
If no area is specified, I will default to near Rainier National Park.
147
+
I will then provide three suggestions for nearby hikes that vary in length.
148
+
I will also share an interesting fact about the local nature on the hikes when making a recommendation.
149
+
"""
118
150
```
119
151
120
-
2. In the application code for your language, replace the comment ***Add code to build request...*** with the necessary code for building the request; specifying the various parameters for your model such as `prompt` and `temperature`.
152
+
1. Replace the comment ***Add code to send request...*** with the necessary code for building the request; specifying the various parameters for your model such as `messages` and `temperature`.
121
153
122
154
**C#**: Program.cs
123
155
124
156
```csharp
125
-
// Initialize the Azure OpenAI client
126
-
OpenAIClient client = new OpenAIClient(new Uri(oaiEndpoint), new AzureKeyCredential(oaiKey));
127
-
128
-
// Build completion options object
129
-
ChatCompletionsOptions chatCompletionsOptions = new ChatCompletionsOptions()
130
-
{
157
+
// Add code to send request...
158
+
// Build completion options object
159
+
ChatCompletionsOptions chatCompletionsOptions = new ChatCompletionsOptions()
160
+
{
131
161
Messages =
132
162
{
133
-
new ChatMessage(ChatRole.System, "You are a helpful assistant."),
134
-
new ChatMessage(ChatRole.User, "Summarize the following text in 20 words or less:\n" + text),
Now that your app has been configured, run it to send your request to your model and observe the response.
177
204
178
-
1. In the **Explorer** pane, expand the **Labfiles/02-nlp-azure-openai/text-files** folder and open the **sample-text.txt** file. This text file contains the text you will submit to the model to be summarized.
179
-
180
-
2. In the interactive terminal pane, ensure the folder context is the folder for your preferred language. Then enter the following command to run the application.
205
+
1. In the interactive terminal pane, ensure the folder context is the folder for your preferred language. Then enter the following command to run the application.
181
206
182
207
- **C#**: `dotnet run`
183
208
- **Python**: `python test-openai-model.py`
184
209
185
210
> **Tip**: You can use the **Maximize panel size** (**^**) icon in the terminal toolbar to see more of the console text.
186
211
187
-
3. Observe the summarization of the sample text file.
188
-
4. In the code file for your preferred language, and change the *temperature* parameter value in your request to **1.0** and save the file.
189
-
5. Run the application again, and observe the output.
190
-
6. Re-run the app a few times, noting the output each time - it may vary.
212
+
1. When prompted, enter the text `What hike should I do near Rainier?`.
213
+
1. Observe the output, taking note that the response follows the guidelines provided in the system message you added to the *messages* array.
214
+
1. Provide the prompt `Where should I hike near Boise? I'm looking for something of easy difficulty, between 2 to 3 miles, with moderate elevation gain.` and observe the output.
215
+
1. In the code file for your preferred language, and change the *temperature* parameter value in your request to **1.0** and save the file.
216
+
1. Run the application again using the prompts above, and observe the output.
217
+
218
+
Increasing the temperature often causes the response 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.
219
+
220
+
## Maintain conversation history
221
+
222
+
In most real-world applications, the ability to reference previous parts of the conversation allows for a more realistic interaction with an AI agent. The Azure OpenAI API is stateless by design, but by providing a history of the conversation in your prompt you enable the AI model to reference past messages.
223
+
224
+
1. Run the app again and provide the prompt `Where is a good hike near Boise?`.
225
+
1. Observe the output, and then prompt `How difficult is the second hike you suggested?`.
226
+
1. The response from the model will likely indicate can't understand the hike you're referring to. To fix that, we can enable the model to have the past conversation messages for reference.
227
+
1. In your application, we need to add the previous prompt and response to the future prompt we are sending. Below the definition of the **system message**, add the following code.
1. Under the comment ***Add code to send request...***, replace all the code from the comment to the end of the **while** loop with the following code then save the file. The code is mostly the same, but now using the messages array to store the conversation history.
1. Save the file. In the code you added, notice we now append the previous input and response to the prompt array which allows the model to understand the history of our conversation.
302
+
1. In the terminal pane, enter the following command to run the application.
303
+
304
+
- **C#**: `dotnet run`
305
+
- **Python**: `python test-openai-model.py`
306
+
307
+
1. Run the app again and provide the prompt `Where is a good hike near Boise?`.
308
+
1. Observe the output, and then prompt `How difficult is the second hike you suggested?`.
309
+
1. You'll likely get a response about the second hike the model suggested, which provides a much more realistic conversation. You can ask additional follow up questions referencing previous answers, and each time the history provides context for the model to answer.
191
310
192
-
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.
311
+
> **Tip**: The token count is only set to 1200, so if the conversation continues too long the application will run out of available tokens, resulting in an incomplete prompt. In production uses, limiting the length of the history to the most recent inputs and responses will help control the number of required tokens.
0 commit comments