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
{{ message }}
This repository was archived by the owner on Jun 24, 2025. It is now read-only.
In this exercise, you'll get started with Azure AI Services by creating an **Azure AI Services** resource in your Azure subscription and using it from a client application. The goal of the exercise is not to gain expertise in any particular service, but rather to become familiar with a general pattern for provisioning and working with Azure AI services as a developer.
10
10
11
-
## Clone the repository in Visual Studio Code
12
-
13
-
You'll develop your code using Visual Studio Code. The code files for your app have been provided in a GitHub repo.
14
-
15
-
> **Tip**: If you have already cloned the **mslearn-ai-services** repo, open it in Visual Studio code. Otherwise, follow these steps to clone it to your development environment.
16
-
17
-
1. Start Visual Studio Code.
18
-
2. Open the palette (SHIFT+CTRL+P) and run a **Git: Clone** command to clone the `https://github.com/MicrosoftLearning/mslearn-ai-services` repository to a local folder (it doesn't matter which folder).
19
-
3. When the repository has been cloned, open the folder in Visual Studio Code.
20
-
4. Wait while additional files are installed to support the C# code projects in the repo, if necessary
21
-
22
-
> **Note**: If you are prompted to add required assets to build and debug, select **Not Now**.
23
-
24
-
5. Expand the `Labfiles/01-use-azure-ai-services` folder.
25
-
26
-
Code for both C# and Python has been provided. Expand the folder of your preferred language.
27
-
28
11
## Provision an Azure AI Services resource
29
12
30
13
Azure AI Services are cloud-based services that encapsulate artificial intelligence capabilities you can incorporate into your applications. You can provision individual Azure AI services resources for specific APIs (for example, **Language** or **Vision**), or you can provision a single **Azure AI Services** resource that provides access to multiple Azure AI services APIs through a single endpoint and key. In this case, you'll use a single **Azure AI Services** resource.
31
14
32
15
1. Open the Azure portal at `https://portal.azure.com`, and sign in using the Microsoft account associated with your Azure subscription.
33
-
2. In the top search bar, search for *Azure AI services*, select **Azure AI Services**, and create an Azure AI services multi-service account resource with the following settings:
16
+
2. In the top search bar, search for *Azure AI services*, select **Azure AI services multi-service account**, and create a resource with the following settings:
34
17
-**Subscription**: *Your Azure subscription*
35
18
-**Resource group**: *Choose or create a resource group (if you are using a restricted subscription, you may not have permission to create a new resource group - use the one provided)*
36
19
-**Region**: *Choose any available region*
@@ -43,104 +26,187 @@ Azure AI Services are cloud-based services that encapsulate artificial intellige
43
26
- Two *keys* that can be used for authentication (client applications can use either key to authenticate).
44
27
- The *location* where the resource is hosted. This is required for requests to some (but not all) APIs.
45
28
29
+
## Clone the repository in Cloud Shell
30
+
31
+
You'll develop your code using Cloud Shell from the Azure Portal. The code files for your app have been provided in a GitHub repo.
32
+
33
+
> **Tip**: If you have already cloned the **mslearn-ai-services** repo recently, you can skip this task. Otherwise, follow these steps to clone it to your development environment.
34
+
35
+
1. In the Azure Portal, use the **[\>_]** button to the right of the search bar at the top of the page to create a new Cloud Shell in the Azure portal, selecting a ***PowerShell*** environment. The cloud shell provides a command line interface in a pane at the bottom of the Azure portal.
36
+
37
+
> **Note**: If you have previously created a cloud shell that uses a *Bash* environment, switch it to ***PowerShell***.
38
+
39
+
1. In the cloud shell toolbar, in the **Settings** menu, select **Go to Classic version** (this is required to use the code editor).
40
+
41
+
> **Tip**: As you paste commands into the cloudshell, the ouput may take up a large amount of the screen buffer. You can clear the screen by entering the `cls` command to make it easier to focus on each task.
42
+
43
+
1. In the PowerShell pane, enter the following commands to clone the GitHub repo for this exercise:
1. After the repo has been cloned, navigate to the folder containing the application code files:
51
+
52
+
```
53
+
cd mslearn-ai-services/Labfiles/01-use-azure-ai-services
54
+
```
55
+
46
56
## Use a REST Interface
47
57
48
58
The Azure AI services APIs are REST-based, so you can consume them by submitting JSON requests over HTTP. In this example, you'll explore a console application that uses the **Language** REST API to perform language detection; but the basic principle is the same for all of the APIs supported by the Azure AI Services resource.
49
59
50
60
> **Note**: In this exercise, you can choose to use the REST API from either **C#** or **Python**. In the steps below, perform the actions appropriate for your preferred language.
51
61
52
-
1. In Visual Studio Code, expand the **C-Sharp** or **Python** folder depending on your language preference.
53
-
2. View the contents of the **rest-client** folder, and note that it contains a file for configuration settings:
62
+
1. Navigate to the folder containing the application code files for your preferred language:
63
+
64
+
**Python**
65
+
66
+
```
67
+
cd Python/rest-client
68
+
```
69
+
70
+
**C#**
71
+
72
+
```
73
+
cd C-Sharp/rest-client
74
+
```
75
+
76
+
1. Using the `ls` command, you can view the contents of the **rest-client** folder. Note that it contains a file for configuration settings:
54
77
55
-
-**C#**: appsettings.json
56
78
- **Python**: .env
79
+
- **C#**: appsettings.json
80
+
81
+
1. Enter the following command to edit the configuration file that has been provided:
82
+
83
+
**Python**
84
+
85
+
```
86
+
code .env
87
+
```
88
+
89
+
**C#**
90
+
91
+
```
92
+
code appsettings.json
93
+
```
94
+
95
+
The file is opened in a code editor.
57
96
58
-
Open the configuration file and update the configuration values it contains to reflect the **endpoint** and an authentication **key** for your Azure AI services resource. Save your changes.
97
+
1. In the code file, update the configuration values it contains to reflect the **endpoint** and an authentication **key** for your Azure AI services resource.
98
+
1. After you've replaced the placeholders, use the **CTRL+S** command to save your changes and then use the **CTRL+Q** command to close the code editor while keeping the cloud shell command line open.
99
+
1. Enter the following command to open the code file for the client application:
59
100
60
-
3. Note that the **rest-client** folder contains a code file for the client application:
101
+
**Python**
102
+
103
+
```
104
+
code rest-client.py
105
+
```
106
+
107
+
**C#**
61
108
62
-
-**C#**: Program.cs
63
-
-**Python**: rest-client.py
109
+
```
110
+
code Program.cs
111
+
```
64
112
65
-
Open the code file and review the code it contains, noting the following details:
113
+
1. Review the code it contains, noting the following details:
66
114
- Various namespaces are imported to enable HTTP communication
67
115
- Code in the **Main** function retrieves the endpoint and key for your Azure AI services resource - these will be used to send REST requests to the Text Analytics service.
68
116
- The program accepts user input, and uses the **GetLanguage** function to call the Text Analytics language detection REST API for your Azure AI services endpoint to detect the language of the text that was entered.
69
117
- The request sent to the API consists of a JSON object containing the input data - in this case, a collection of **document** objects, each of which has an **id** and **text**.
70
118
- The key for your service is included in the request header to authenticate your client application.
71
119
- The response from the service is a JSON object, which the client application can parse.
72
120
73
-
4. Right click on the **rest-client**folder, select *Open in Integrated Terminal* and run the following command:
121
+
1. Use the **CTRL+Q** command to close the code editor while keeping the cloud shell command line open and run the following command:
74
122
75
-
**C#**
123
+
**Python**
76
124
77
125
```
78
-
dotnet run
126
+
pip install python-dotenv
127
+
python rest-client.py
79
128
```
80
129
81
-
**Python**
130
+
**C#**
82
131
83
132
```
84
-
pip install python-dotenv
85
-
python rest-client.py
133
+
dotnet run
86
134
```
87
135
88
-
5. When prompted, enter some text and review the language that is detected by the service, which is returned in the JSON response. For example, try entering "Hello", "Bonjour", and "Gracias".
89
-
6. When you have finished testing the application, enter "quit" to stop the program.
136
+
1. When prompted, enter some text and review the language that is detected by the service, which is returned in the JSON response. For example, try entering "Hello", "Bonjour", and "Gracias".
137
+
1. When you have finished testing the application, enter "quit" to stop the program.
90
138
91
139
## Use an SDK
92
140
93
141
You can write code that consumes Azure AI services REST APIs directly, but there are software development kits (SDKs) for many popular programming languages, including Microsoft C#, Python, Java, and Node.js. Using an SDK can greatly simplify development of applications that consume Azure AI services.
94
142
95
-
1. In Visual Studio Code, expand the **sdk-client** folder under the **C-Sharp** or **Python** folder, depending on your language preference. Then run `cd ../sdk-client` to change into the relevant **sdk-client** folder.
143
+
1. Navigate to the folder containing the SDK application code files by running `cd ../sdk-client`.
144
+
145
+
1. Install the Text Analytics SDK package by running the appropriate command for your language preference:
96
146
97
-
2. Install the Text Analytics SDK package by running the appropriate command for your language preference:
1. Enter the following command to edit the configuration file that has been provided:
160
+
105
161
**Python**
106
162
107
163
```
108
-
pip install azure-ai-textanalytics==5.3.0
164
+
code .env
109
165
```
110
166
111
-
3. View the contents of the **sdk-client** folder, and note that it contains a file for configuration settings:
167
+
**C#**
112
168
113
-
- **C#**: appsettings.json
114
-
- **Python**: .env
169
+
```
170
+
code appsettings.json
171
+
```
115
172
116
-
Open the configuration file and update the configuration values it contains to reflect the **endpoint** and an authentication **key** for your Azure AI services resource. Save your changes.
117
-
118
-
4. Note that the **sdk-client** folder contains a code file for the client application:
173
+
1. In the code file, update the configuration values it contains to reflect the **endpoint** and an authentication **key** for your Azure AI services resource.
174
+
1. After you've replaced the placeholders, use the **CTRL+S** command to save your changes and then use the **CTRL+Q** command to close the code editor while keeping the cloud shell command line open.
175
+
1. Enter the following command to open the code file for the client application:
119
176
120
-
- **C#**: Program.cs
121
-
- **Python**: sdk-client.py
177
+
**Python**
178
+
179
+
```
180
+
code sdk-client.py
181
+
```
182
+
183
+
**C#**
122
184
123
-
Open the code file and review the code it contains, noting the following details:
185
+
```
186
+
code Program.cs
187
+
```
188
+
189
+
1. Review the code it contains, noting the following details:
124
190
- The namespace for the SDK you installed is imported
125
191
- Code in the **Main** function retrieves the endpoint and key for your Azure AI services resource - these will be used with the SDK to create a client for the Text Analytics service.
126
192
- The **GetLanguage** function uses the SDK to create a client for the service, and then uses the client to detect the language of the text that was entered.
127
193
128
-
5. Return to the terminal, ensure you are in the **sdk-client** folder, and enter the following command to run the program:
194
+
1. Use the **CTRL+Q** command to close the code editor and run the following command:
129
195
130
-
**C#**
196
+
**Python**
131
197
132
198
```
133
-
dotnet run
199
+
python sdk-client.py
134
200
```
135
201
136
-
**Python**
202
+
**C#**
137
203
138
204
```
139
-
python sdk-client.py
205
+
dotnet run
140
206
```
141
207
142
-
6. When prompted, enter some text and review the language that is detected by the service. For example, try entering "Goodbye", "Au revoir", and "Hasta la vista".
143
-
7. When you have finished testing the application, enter "quit" to stop the program.
208
+
7. When prompted, enter some text and review the language that is detected by the service. For example, try entering "Goodbye", "Au revoir", and "Hasta la vista".
209
+
8. When you have finished testing the application, enter "quit" to stop the program.
144
210
145
211
> **Note**: Some languages that require Unicode character sets may not be recognized in this simple console application.
146
212
@@ -154,4 +220,4 @@ If you're not using the Azure resources created in this lab for other training m
154
220
155
221
## More information
156
222
157
-
For more information about Azure AI Services, see the [Azure AI Services documentation](https://docs.microsoft.com/azure/ai-services/what-are-ai-services).
223
+
For more information about Azure AI Services, see the [Azure AI Services documentation](https://docs.microsoft.com/azure/ai-services/what-are-ai-services).
0 commit comments