Skip to content

Commit 23a6d1e

Browse files
authored
Refresh Labs (March) (MicrosoftLearning#53)
* 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
1 parent bd5d1d4 commit 23a6d1e

File tree

26 files changed

+579
-352
lines changed

26 files changed

+579
-352
lines changed

Instructions/Exercises/01-get-started-azure-openai.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ lab:
77

88
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.
99

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.
1111

1212
This exercise takes approximately **30** minutes.
1313

@@ -70,7 +70,7 @@ Now that you've deployed a model, you can use it to generate responses based on
7070
- **Chat session** - used to submit chat messages and view responses.
7171
- **Configuration** - used to configure settings for the model deployment.
7272
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.
7474
1. In the **Chat session** panel, enter the user query `How can I use generative AI to help me market a new product?`
7575

7676
> **Note**: You may receive a response that the API deployment is not yet ready. If so, wait for a few minutes and try again.

Instructions/Exercises/02-natural-language-azure-openai.md

Lines changed: 172 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
---
22
lab:
3-
title: 'Integrate Azure OpenAI into your app'
3+
title: 'Use Azure OpenAI SDKs in your app'
44
---
55

6-
# Integrate Azure OpenAI into your app
6+
# Use Azure OpenAI APIs in your app
77

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 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.
911

1012
This exercise will take approximately **30** minutes.
1113

@@ -70,21 +72,21 @@ You'll develop your Azure OpenAI app using Visual Studio Code. The code files fo
7072
7173
## Configure your application
7274

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.
7476

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.
7678
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:
7779

7880
**C#**:
7981

8082
```
81-
dotnet add package Azure.AI.OpenAI --version 1.0.0-beta.9
83+
dotnet add package Azure.AI.OpenAI --version 1.0.0-beta.14
8284
```
8385
8486
**Python**:
8587
8688
```
87-
pip install openai==1.2.0
89+
pip install openai==1.13.3
8890
```
8991
9092
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.
106108
**C#**: Program.cs
107109
108110
```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.";
111132
```
112133
113134
**Python**: test-openai-model.py
114135
115136
```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+
"""
118150
```
119151
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`.
121153
122154
**C#**: Program.cs
123155
124156
```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+
{
131161
Messages =
132162
{
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),
163+
new ChatRequestSystemMessage(systemMessage),
164+
new ChatRequestUserMessage(inputText),
135165
},
136-
MaxTokens = 120,
166+
MaxTokens = 400,
137167
Temperature = 0.7f,
138168
DeploymentName = oaiDeploymentName
139-
};
169+
};
140170
141-
// Send request to Azure OpenAI model
142-
ChatCompletions response = client.GetChatCompletions(chatCompletionsOptions);
143-
string completion = response.Choices[0].Message.Content;
171+
// Send request to Azure OpenAI model
172+
ChatCompletions response = client.GetChatCompletions(chatCompletionsOptions);
144173
145-
Console.WriteLine("Summary: " + completion + "\n");
174+
// Print the response
175+
string completion = response.Choices[0].Message.Content;
176+
Console.WriteLine("Response: " + completion + "\n");
146177
```
147178
148179
**Python**: test-openai-model.py
149180
150181
```python
151-
# Initialize the Azure OpenAI client
152-
client = AzureOpenAI(
153-
azure_endpoint = azure_oai_endpoint,
154-
api_key=azure_oai_key,
155-
api_version="2023-05-15"
156-
)
157-
158-
# Send request to Azure OpenAI model
159-
response = client.chat.completions.create(
160-
model=azure_oai_deployment,
161-
temperature=0.7,
162-
max_tokens=120,
163-
messages=[
164-
{"role": "system", "content": "You are a helpful assistant."},
165-
{"role": "user", "content": "Summarize the following text in 20 words or less:\n" + text}
166-
]
182+
# Add code to send request...
183+
# Send request to Azure OpenAI model
184+
response = client.chat.completions.create(
185+
model=azure_oai_deployment,
186+
temperature=0.7,
187+
max_tokens=400,
188+
messages=[
189+
{"role": "system", "content": system_message},
190+
{"role": "user", "content": input_text}
191+
]
167192
)
168-
169-
print("Summary: " + response.choices[0].message.content + "\n")
193+
generated_text = response.choices[0].message.content
194+
195+
# Print the response
196+
print("Response: " + generated_text + "\n")
170197
```
171198
172-
3. Save the changes to your code file.
199+
1. Save the changes to your code file.
173200
174201
## Test your application
175202
176203
Now that your app has been configured, run it to send your request to your model and observe the response.
177204
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.
181206
182207
- **C#**: `dotnet run`
183208
- **Python**: `python test-openai-model.py`
184209
185210
> **Tip**: You can use the **Maximize panel size** (**^**) icon in the terminal toolbar to see more of the console text.
186211
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.
228+
229+
**C#**: Program.cs
230+
231+
```csharp
232+
// Initialize messages list
233+
var messagesList = new List<ChatRequestMessage>()
234+
{
235+
new ChatRequestSystemMessage(systemMessage),
236+
};
237+
```
238+
239+
**Python**: test-openai-model.py
240+
241+
```python
242+
# Initialize messages array
243+
messages_array = [{"role": "system", "content": system_message}]
244+
```
245+
246+
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.
247+
248+
**C#**: Program.cs
249+
250+
```csharp
251+
// Add code to send request...
252+
// Build completion options object
253+
messagesList.Add(new ChatRequestUserMessage(inputText));
254+
255+
ChatCompletionsOptions chatCompletionsOptions = new ChatCompletionsOptions()
256+
{
257+
MaxTokens = 1200,
258+
Temperature = 0.7f,
259+
DeploymentName = oaiDeploymentName
260+
};
261+
262+
// Add messages to the completion options
263+
foreach (ChatRequestMessage chatMessage in messagesList)
264+
{
265+
chatCompletionsOptions.Messages.Add(chatMessage);
266+
}
267+
268+
// Send request to Azure OpenAI model
269+
ChatCompletions response = client.GetChatCompletions(chatCompletionsOptions);
270+
271+
// Return the response
272+
string completion = response.Choices[0].Message.Content;
273+
274+
// Add generated text to messages list
275+
messagesList.Add(new ChatRequestAssistantMessage(completion));
276+
277+
Console.WriteLine("Response: " + completion + "\n");
278+
```
279+
280+
**Python**: test-openai-model.py
281+
282+
```python
283+
# Add code to send request...
284+
# Send request to Azure OpenAI model
285+
messages_array.append({"role": "user", "content": input_text})
286+
287+
response = client.chat.completions.create(
288+
model=azure_oai_deployment,
289+
temperature=0.7,
290+
max_tokens=1200,
291+
messages=messages_array
292+
)
293+
generated_text = response.choices[0].message.content
294+
# Add generated text to messages array
295+
messages_array.append({"role": "system", "content": generated_text})
296+
297+
# Print generated text
298+
print("Summary: " + generated_text + "\n")
299+
```
300+
301+
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.
191310
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.
193312
194313
## Clean up
195314

0 commit comments

Comments
 (0)