Get Started with Azure AI Services
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.
Clone the repository in Visual Studio Code
You’ll develop your code using Visual Studio Code. The code files for your app have been provided in a GitHub repo.
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.
- Start Visual Studio Code.
- 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). - When the repository has been cloned, open the folder in Visual Studio Code.
-
Wait while additional files are installed to support the C# code projects in the repo, if necessary
Note: If you are prompted to add required assets to build and debug, select Not Now.
- Expand the
Labfiles/01-use-azure-ai-services
folder.
Code for both C# and Python has been provided. Expand the folder of your preferred language.
Provision an Azure AI Services resource
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.
- Open the Azure portal at
https://portal.azure.com
, and sign in using the Microsoft account associated with your Azure subscription. - 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:
- Subscription: Your Azure subscription
- 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)
- Region: Choose any available region
- Name: Enter a unique name
- Pricing tier: Standard S0
- Select the required checkboxes and create the resource.
- Wait for deployment to complete, and then view the deployment details.
- Go to the resource and view its Keys and Endpoint page. This page contains the information that you will need to connect to your resource and use it from applications you develop. Specifically:
- An HTTP endpoint to which client applications can send requests.
- Two keys that can be used for authentication (client applications can use either key to authenticate).
- The location where the resource is hosted. This is required for requests to some (but not all) APIs.
Use a REST Interface
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.
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.
- In Visual Studio Code, expand the C-Sharp or Python folder depending on your language preference.
-
View the contents of the rest-client folder, and note that it contains a file for configuration settings:
- C#: appsettings.json
- Python: .env
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.
-
Note that the rest-client folder contains a code file for the client application:
- C#: Program.cs
- Python: rest-client.py
Open the code file and review the code it contains, noting the following details:
- Various namespaces are imported to enable HTTP communication
- 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.
- 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.
- 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.
- The key for your service is included in the request header to authenticate your client application.
- The response from the service is a JSON object, which the client application can parse.
-
Right click on the rest-client folder, select Open in Integrated Terminal and run the following command:
C#
dotnet run
Python
pip install python-dotenv python rest-client.py
- 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”.
- When you have finished testing the application, enter “quit” to stop the program.
Use an SDK
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.
-
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. -
Install the Text Analytics SDK package by running the appropriate command for your language preference:
C#
dotnet add package Azure.AI.TextAnalytics --version 5.3.0
Python
pip install azure-ai-textanalytics==5.3.0
-
View the contents of the sdk-client folder, and note that it contains a file for configuration settings:
- C#: appsettings.json
- Python: .env
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.
-
Note that the sdk-client folder contains a code file for the client application:
- C#: Program.cs
- Python: sdk-client.py
Open the code file and review the code it contains, noting the following details:
- The namespace for the SDK you installed is imported
- 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.
- 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.
-
Return to the terminal, ensure you are in the sdk-client folder, and enter the following command to run the program:
C#
dotnet run
Python
python sdk-client.py
- 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”.
- When you have finished testing the application, enter “quit” to stop the program.
Note: Some languages that require Unicode character sets may not be recognized in this simple console application.
Clean up resources
If you’re not using the Azure resources created in this lab for other training modules, you can delete them to avoid incurring further charges.
-
Open the Azure portal at
https://portal.azure.com
, and in the top search bar, search for the resources you created in this lab. -
On the resource page, select Delete and follow the instructions to delete the resource. Alternatively, you can delete the entire resource group to clean up all resources at the same time.
More information
For more information about Azure AI Services, see the Azure AI Services documentation.