AI 100T01A ENU TrainerHandbook
AI 100T01A ENU TrainerHandbook
AI 100T01A ENU TrainerHandbook
AI-100T01
Designing and
Implementing an Azure AI
Solution
MCT USE ONLY. STUDENT USE PROHIBITED
AI-100T01
Designing and Implementing an
Azure AI Solution
MCT USE ONLY. STUDENT USE PROHIBITED
II
MCT USE ONLY. STUDENT USE PROHIBITED
III
MCT USE ONLY. STUDENT USE PROHIBITED
Contents
ly there are a set of services that are available in release versions and some that are currently in preview.
In this course, we will describe each of the services in general but will pick a couple of key services to drill
down in further. Covering every cognitive service that is available would take a considerable course but
once you understand the mechanics of accessing the services in one area, it is easy to transition to using
any of the others.
If you want to create an application that can process images and return information about what is in the
images, you should consider the advanced algorithms that are part of the Computer Vision API. The
services available in Computer Vision allow you to upload images for analysis, or provide a URL that links
to an image that will be analyzed by the service.
The Computer Vision API provides algorithms to process images and return insights. For example, you
can find out if an image has mature content, or can use it to find all the faces in an image. It also has
other features like estimating dominant and accent colors, categorizing the content of images, and
describing an image with complete English sentences. Additionally, it can also intelligently generate
image thumbnails for displaying large images effectively.
Some of the key aspects that the computer vision API can analyze in images are listed here:
●● Tag visual features - Identify and tag visual features in an image, from a set of thousands of recog-
nizable objects, living things, scenery, and actions. When the tags are ambiguous or not common
knowledge, the API response provides ‘hints’ to clarify the meaning of the tag in the context of a
known setting. Tagging isn't limited to the main subject, such as a person in the foreground, but also
includes the setting (indoor or outdoor), furniture, tools, plants, animals, accessories, gadgets, and so
on.
●● Detect objects - Object detection is similar to tagging, but the API returns the bounding box coordi-
nates for each tag applied. For example, if an image contains a dog, cat and person, the Detect
operation will list those objects together with their coordinates in the image. You can use this func-
tionality to process further relationships between the objects in an image. It also lets you know when
there are multiple instances of the same tag in an image.
●● Detect brands - Identify commercial brands in images or videos from a database of thousands of
global logos. You can use this feature, for example, to discover which brands are most popular on
social media or most prevalent in media product placement.
●● Categorize an image - Identify and categorize an entire image, using a category taxonomy with
parent/child hereditary hierarchies. Categories can be used alone, or with our new tagging models.
Currently, English is the only supported language for tagging and categorizing images.
●● Describe an image - Generate a description of an entire image in human-readable language, using
complete sentences. Computer Vision's algorithms generate various descriptions based on the objects
identified in the image. The descriptions are each evaluated and a confidence score generated. A list is
then returned ordered from highest confidence score to lowest.
●● Detect faces - Detect faces in an image and provide information about each detected face. Computer
Vision returns the coordinates, rectangle, gender, and age for each detected face.
Computer Vision provides a subset of the functionality that can be found in Face, and you can use the
Face service for more detailed analysis, such as facial identification and pose detection.
●● Detect image types - Detect characteristics about an image, such as whether an image is a line
drawing or the likelihood of whether an image is clip art.
●● Detect domain-specific content - Use domain models to detect and identify domain-specific
content in an image, such as celebrities and landmarks. For example, if an image contains people,
Computer Vision can use a domain model for celebrities included with the service to determine if the
people detected in the image match known celebrities.
MCT USE ONLY. STUDENT USE PROHIBITED
Overview of Azure Cognitive Services 3
●● Detect the color scheme - Analyze color usage within an image. Computer Vision can determine
whether an image is black & white or color and, for color images, identify the dominant and accent
colors.
●● Generate a thumbnail - Analyze the contents of an image to generate an appropriate thumbnail for
that image. Computer Vision first generates a high-quality thumbnail and then analyzes the objects
within the image to determine the area of interest. Computer Vision then crops the image to fit the
requirements of the area of interest. The generated thumbnail can be presented using an aspect ratio
that is different from the aspect ratio of the original image, depending on your needs.
●● Get the area of interest - Analyze the contents of an image to return the coordinates of the area of
interest. This is the same function that is used to generate a thumbnail, but instead of cropping the
image, Computer Vision returns the bounding box coordinates of the region, so the calling applica-
tion can modify the original image as desired.
The Computer Vision API is available in many regions across the globe. To find the region nearest you,
see the Products available by region1.
You can use the Computer Vision API to:
●● Analyze images for insight
●● Extract printed text from images using optical character recognition (OCR).
●● Recognize printed and handwritten text from images
●● Recognize celebrities and landmarks
●● Analyze video
●● Generate a thumbnail of an image
1 https://azure.microsoft.com/en-us/global-infrastructure/services/?products=cognitive-services
MCT USE ONLY. STUDENT USE PROHIBITED 4 Module 0 Introducing Azure Cognitive Services
"metadata": {
"height": 200,
"width": 300,
"format": "Jpeg"
}
}
Detecting Objects
Object detection is similar to tagging, but the API returns the bounding box coordinates for each tag
applied. For example, if an image contains a dog, cat and person, the Detect operation will list those
objects together with their coordinates in the image. You can use this functionality to process further
relationships between the objects in an image. It also lets you know when there are multiple instances of
the same tag in an image. This API will apply tags according to the object or living thing found in the
image supplied. An example of a returned JSON might look like this:
{
"objects":[
{
"rectangle":{
"x":730,
"y":66,
"w":135,
"h":85
},
"object":"kitchen appliance",
"confidence":0.501
},
{
"rectangle":{
"x":523,
"y":377,
"w":185,
"h":46
},
"object":"computer keyboard",
"confidence":0.51
},
{
"rectangle":{
"x":471,
"y":218,
"w":289,
"h":226
},
"object":"Laptop",
"confidence":0.85,
"parent":{
"object":"computer",
MCT USE ONLY. STUDENT USE PROHIBITED 6 Module 0 Introducing Azure Cognitive Services
"confidence":0.851
}
},
{
"rectangle":{
"x":654,
"y":0,
"w":584,
"h":473
},
"object":"person",
"confidence":0.855
}
],
"requestId":"a7fde8fd-cc18-4f5f-99d3-897dcd07b308",
"metadata":{
"width":1260,
"height":473,
"format":"Jpeg"
}
}
Detect Brands
Identify commercial brands in images or videos from a database of thousands of global logos. You can
use this feature, for example, to discover which brands are most popular on social media or most preva-
lent in media product placement. Microsoft considers this feature to be a specialized mode of object
detection. It is based on a database containing thousands of globally recognizable logos that may be
found in your images or videos. IF recognized brands are detected, the results will list the brand name, a
confidence score between 0 and 1, and the coordinates of a bounding box where the logo was detected.
If you find that a brand is present in an image but not detected by this API, you might consider creating a
Custom Vision service to address the issue.
An example of a result in JSON, where an image contains a Microsoft logo and brand name, would look
like this:
{
"brands":[
{
"name":"Microsoft",
"confidence":0.657,
"rectangle":{
"x":436,
"y":473,
"w":568,
"h":267
}
},
{
"name":"Microsoft",
MCT USE ONLY. STUDENT USE PROHIBITED
Overview of Azure Cognitive Services 7
"confidence":0.85,
"rectangle":{
"x":101,
"y":561,
"w":273,
"h":263
}
}
],
"requestId":"10dcd2d6-0cf6-4a5e-9733-dc2e4b08ac8d",
"metadata":{
"width":1286,
"height":1715,
"format":"Jpeg"
}
}
Categorize an Image
Identify and categorize an entire image, using a category taxonomy with parent/child hereditary hierar-
chies. Categories can be used alone, or with our new tagging models.
Currently, English is the only supported language for tagging and categorizing images.
Describe an Image
Generate a description of an entire image in human-readable language, using complete sentences.
Computer Vision's algorithms generate various descriptions based on the objects identified in the image.
The descriptions are each evaluated and a confidence score generated. A list is then returned ordered
from highest confidence score to lowest. An example JSON result is shown here:
{
"description": {
"tags": ["outdoor", "building", "photo", "city", "white", "black",
"large", "sitting", "old", "water", "skyscraper", "many", "boat", "river",
"group", "street", "people", "field", "tall", "bird", "standing"],
"captions": [
{
"text": "a black and white photo of a city",
"confidence": 0.95301952483304808
},
{
"text": "a black and white photo of a large city",
"confidence": 0.94085190563213816
},
{
"text": "a large white building in a city",
"confidence": 0.93108362931954824
}
MCT USE ONLY. STUDENT USE PROHIBITED 8 Module 0 Introducing Azure Cognitive Services
]
},
"requestId": "b20bfc83-fb25-4b8d-a3f8-b2a1f084b159",
"metadata": {
"height": 300,
"width": 239,
"format": "Jpeg"
}
}
Detect Faces
Detect faces in an image and provide information about each detected face. Computer Vision returns the
coordinates, rectangle, gender, and age for each detected face.
Computer Vision provides a subset of the functionality that can be found in Face, and you can use the
Face service for more detailed analysis, such as facial identification and pose detection.
Generate a Thumbnail
Analyze the contents of an image to generate an appropriate thumbnail for that image. Computer Vision
first generates a high-quality thumbnail and then analyzes the objects within the image to determine the
area of interest. Computer Vision then crops the image to fit the requirements of the area of interest. The
generated thumbnail can be presented using an aspect ratio that is different from the aspect ratio of the
original image, depending on your needs.
MCT USE ONLY. STUDENT USE PROHIBITED
Overview of Azure Cognitive Services 9
2 https://docs.microsoft.com/en-us/azure/cognitive-services/computer-vision/language-support#text-recognition
3 https://docs.microsoft.com/azure/cognitive-services/content-moderator/overview
MCT USE ONLY. STUDENT USE PROHIBITED 10 Module 0 Introducing Azure Cognitive Services
●● Provides neural text-to-speech voices nearly indistinguishable from human speech (English).
●● Convert text to natural-sounding speech.
●● Offer multiple genders and/or dialects for many supported languages.
●● Support plain text input or Speech Synthesis Markup Language (SSML).
●● Speech translation services that can:
Speech-to-Text Translation
Speech-to-text from Azure Speech Services, also known as speech-to-text, enables real-time transcription
of audio streams into text that your applications, tools, or devices can consume, display, and take action
on as command input.
4 https://azure.microsoft.com/en-us/pricing/details/cognitive-services/speech-services/
MCT USE ONLY. STUDENT USE PROHIBITED
Overview of Azure Cognitive Services 11
Speech translation services are exposed through platform-independent REST-based APIs, or an SDK, that
allow them to be integrated into any solution requiring multi-language speech translation.
Microsoft has a set of trained data that resides in the cloud and is the source used in what Microsoft calls
the Universal language model. Speech-to-text uses this language model to perform the translation.
You can use different input sources for the audio such as:
●● microphones
●● streaming audio
●● locally or cloud-stored audio file (using the Speech SDK and REST APIs)
Note The Speech SDK has support for audio in 16-bit WAV, 16-bit PCM formats as well as 16KHz/8KHz
single-channel audio for speech recognition as in the input. Additionl formats are supported with the
REST endpoint for speech-to-text or the batch transcription service.
Core features
Features available via the Speech SDK and REST APIs:
* LUIS intents and entities can be derived using a separate LUIS subscription. With this subscription, the
SDK can call LUIS for you and provide entity and intent results. With the REST API, you can call LUIS
yourself to derive intents and entities with your LUIS subscription.
** These services are available using the cris.ai endpoint. See Swagger5 reference.
print("Say something...")
# Checks result.
if result.reason == speechsdk.ResultReason.RecognizedSpeech:
print("Recognized: {}".format(result.text))
elif result.reason == speechsdk.ResultReason.NoMatch:
print("No speech could be recognized: {}".format(result.no_match_de-
tails))
elif result.reason == speechsdk.ResultReason.Canceled:
5 https://westus.cris.ai/swagger/ui/index
MCT USE ONLY. STUDENT USE PROHIBITED
Overview of Azure Cognitive Services 13
cancellation_details = result.cancellation_details
print("Speech Recognition canceled: {}".format(cancellation_details.
reason))
if cancellation_details.reason == speechsdk.CancellationReason.Error:
print("Error details: {}".format(cancellation_details.error_de-
tails))
namespace helloworld
{
class Program
{
public static async Task RecognizeSpeechAsync()
{
// Creates an instance of a speech config with specified sub-
scription key and service region.
// Replace with your own subscription key and service region
(e.g., "westus").
var config = SpeechConfig.FromSubscription("YourSubscriptionKey",
"YourServiceRegion");
// Checks result.
if (result.Reason == ResultReason.RecognizedSpeech)
{
Console.WriteLine($"We recognized: {result.Text}");
}
else if (result.Reason == ResultReason.NoMatch)
MCT USE ONLY. STUDENT USE PROHIBITED 14 Module 0 Introducing Azure Cognitive Services
{
Console.WriteLine($"NOMATCH: Speech could not be recog-
nized.");
}
else if (result.Reason == ResultReason.Canceled)
{
var cancellation = CancellationDetails.FromResult(re-
sult);
Console.WriteLine($"CANCELED: Reason={cancellation.
Reason}");
if (cancellation.Reason == CancellationReason.Error)
{
Console.WriteLine($"CANCELED: ErrorCode={cancella-
tion.ErrorCode}");
Console.WriteLine($"CANCELED: ErrorDetails={cancel-
lation.ErrorDetails}");
Console.WriteLine($"CANCELED: Did you update the
subscription info?");
}
}
}
}
IMPORTANT
The Python and C# code snippets above have been reduced for brevity and would require more code to
function properly in real scenarios.
Speech to Text
This API gives you the functionality necessary to perform real-time transcription of audio streams in
textual representation. A prime example might be a scenario where you want to have closed captions for
a speaker at an event or during a presentation.
Speech-to-text uses the Universal language model that was trained using Microsoft-owned data and is
deployed in the cloud. It's optimal for conversational and dictation scenarios. The audio input can come
from a connected microphone, streaming audio, or from audio that is stored as a file. If the Universal
language model does not meet your needs, you can create custom models to meet your needs. Micro-
soft supports the following options for customization:
MCT USE ONLY. STUDENT USE PROHIBITED
Overview of Azure Cognitive Services 15
Model Description
Acoustic Model Creating a custom acoustic model is helpful if your
application, tools, or devices are used in a particu-
lar environment, like in a car or factory with
specific recording conditions. Examples involve
accented speech, specific background noises, or
using a specific microphone for recording.
Language Model Create a custom language model to improve
transcription of industry-specific vocabulary and
grammar, such as medical terminology, or IT
jargon.
Pronunciation Model With a custom pronunciation model, you can
define the phonetic form and display of a word or
term. It's useful for handling customized terms,
such as product names or acronyms. All you need
to get started is a pronunciation file – a simple .txt
file.
6 https://portal.azure.com?azure-portal=true
MCT USE ONLY. STUDENT USE PROHIBITED 16 Module 0 Introducing Azure Cognitive Services
7 https://azure.microsoft.com/en-us/services/cognitive-services/
MCT USE ONLY. STUDENT USE PROHIBITED
Overview of Azure Cognitive Services 17
●● Language Understanding (LUIS) - Language Understanding (LUIS) is a cloud-based API service that
applies custom machine-learning intelligence to a user's conversational, natural language text to
predict overall meaning, and pull out relevant, detailed information.
A client application for LUIS is any conversational application that communicates with a user in natural
language to complete a task. Examples of client applications include social media apps, chat bots, and
speech-enabled desktop applications.
Language understanding is a concept that even humans get wrong from time to time. A good
example is the use of slang terms or localized phrases. If you are in Indonesia at a public place,
perhaps a mall or in a restaurant, and you're searching for the restroom. Indonesia language lessons
might teach you to ask where the restroom is with the phrase, "Di mana kamar kecil?". While this is
technically correct, it applies mainly to seeking the restroom in someone's house because kamar kecil
literally means, small (kecil) room (kamar). In public, it's more correct to ask, "Di mana WC?", or "Di
mana toilette?". However, almost all Indonesians will know what you are asking. What happens if you
attempt to have a computer perform that translation to understand what you asked? Will it get the
correct answer or will it try to direct you to a “small room” somewhere that isn't actually a restroom?
Likewise, in the English language, there are many scenarios where a human “understands” the mean-
ing of a phrase or statement, where the subtle similarities aren't apparent to a non-native English
speaker. How many would understand the phrase "swing the door to"? This is the same as “shut the
door” or "close the door", but not everyone would understand these equivalents. For AI to understand
language, specific aspects are critical to aid the algorithm in making comparisons and distinctions.
This is where the Language Understanding Intelligent Service, LUIS, comes into play.
LUIS makes use of three key aspects for understanding language:
Intent - An intent represents a task or action the user wants to perform.
It is a purpose or goal expressed in a user's utterance.
Utterance - Utterances are input from the user that your app needs to
interpret.
Entities - The entity represents a word or phrase inside the utterance
that you want extracted.
In Preview
●● Immersive Reader - The language service also has an immersive reader feature in preview as of this
writing. Immersive Reader can help users read and understand text and supports a feature set that
aids readers of all abilities.
MCT USE ONLY. STUDENT USE PROHIBITED 18 Module 0 Introducing Azure Cognitive Services
https://[location].dev.cognitive.microsoft.com/docs/services/TextAnalytics.V2.0
The landing page displays a menu on the left and content to the right. The menu lists the POST methods
you can call on the Text Analytics API. These endpoints are Detect Language, Entities, Key Phrases, and
Sentiment. To call one of these operations, we need to do a few things.
●● Select the method we want to call.
●● Add the access key that we saved earlier in the lesson to each call.
2. From the left menu, select Sentiment. This selection opens the Sentiment documentation to the right.
As the documentation shows, we'll be making a REST call in the following format.
https://[location].api.cognitive.microsoft.com/text/analytics/v2.0/sentiment
[location] is replaced with the location that you selected when you created the Text Analytics account.
We'll pass in our subscription key, or access key, in the ocp-Apim-Subscription-Key header.
Next, we have the request body section, which holds a documents array in JSON format. Each docu-
ment in the array has three properties. The properties are “language”, "id", and “text”. The "id" is a
number in this example, but it can be anything you want as long as it's unique in the documents array.
In this example, we're also passing in documents written in three different languages. Over 15 lan-
guages are supported in the Sentiment feature of the Text Analytics API. For more information, check
out Supported languages in the Text Analytics API. The maximum size of a single document is 5,000
characters, and one request can have up to 1,000 documents.
The complete request, including the headers and the request URL are displayed in the next section.
MCT USE ONLY. STUDENT USE PROHIBITED 20 Module 0 Introducing Azure Cognitive Services
The last portion of the page shows the information about the response. The response holds the
insight the Text Analytics API had about our documents. An array of documents is returned to us,
without the original text. We get back an “id” and "score" for each document. The API returns a
numeric score between 0 and 1. Scores close to 1 indicate positive sentiment, while scores close to 0
indicate negative sentiment. A score of 0.5 indicates the lack of sentiment, a neutral statement. In this
example, we have two pretty positive documents and one negative document.
Introducing Search
The Search aspect of Cognitive Services, at a high level, utilizes Bing as the search engine. Having said
that, you can focus your aspects around Search on specific areas. Note that this topic is rather large and
this course will not be covering these search APIs in depth but merely providing an introduction and leav-
ing it to the reader to explore any of the APIs that are of interest.
Workflow
A common workflow for using these services would be:
1. Create a Cognitive Services API account with access to the Bing Search APIs. If you don't have an
Azure subscription, you can create an account for free.
2. Send a request to the API, with a valid search query.
3. Process the API response by parsing the returned JSON message.
The current Search APIs support the following separate areas:
●● Crop, resize, and display thumbnails - Edit and display thumbnail previews for the videos returned by
Bing Video Search API.
●● Get trending videos - Search for trending videos from around the world.
●● Get video insights - Customize a search for trending videos from around the world.
Bing Autosuggest
Bing Autosuggest API lets you send a partial search query term to Bing and get back a list of suggested
queries that other users have searched on. For example, as the user enters each character of their search
term, you'd call this API and populate the search box's drop-down list with the suggested query strings.
The workflow for this service is just a bit different due to the way it works. You still create the Cognitive
Services API account, as in the workflow at the opening of this topic, but during step two, you should
send a request to the API each time a users types a new character in the search box. Then you
process the returned JSON.
The rationale as to why you call this API each time the user types a new character in your application's
search box is because, as more characters are entered, the API will return more relevant suggested search
queries. For example, the suggestions the API might return for a single s are likely to be less relevant than
ones for sail.
The user can then select a suggestion from the drop-down list, you can use it to begin searching with one
of the Bing Search APIs, or directly go to the Bing search results page.
demote specific content to create a custom view of the web and help your users quickly find relevant
search results. It contains the following features:
●● Custom real-time search suggestions - Provide search suggestions that can be displayed as a drop-
down list as your users type.
●● Custom image search experiences - Enable your users to search for images from the domains and
websites specified in your custom search instance.
●● Custom video search experiences - Enable your users to search for videos from the domains and sites
specified in your custom search instance.
●● Share your custom search instance - Collaboratively edit and test your search instance by sharing it
with members of your team.
●● Configure a UI for your applications and websites - Collaboratively edit and test your search instance
by sharing it with members of your team.
3. URL encode your search terms for the q="" query parameter. For example, q=nearby+restaurant or
q=nearby%20restaurant. Set pagination as well, if needed.
4. Send a request to the Bing Local Business Search API
5. Parse the JSON response
Introducing Decision
The Decision component of the Azure Cognitive Services suite is a relatively new addition to the family. It
is intended to help you enable informed and efficient decision making. Decision has brought over the
Content Moderator service that previously sat in other areas
8 https://azure.microsoft.com/en-us/services/cognitive-services/autosuggest/
9 https://azure.microsoft.com/en-us/services/cognitive-services/bing-image-search-api/
MCT USE ONLY. STUDENT USE PROHIBITED
Overview of Azure Cognitive Services 25
of Cognitive Services and also adds to new features in preview. Decision consists of the following
services:
●● Content Moderator - Use this service to detect potentially offensive content such as unwanted
images, filter text for profanity or other unwanted text, and moderate content that might be consid-
ered adult or racy content.
●● Anomaly Detector (Preview) - This feature set can help you to monitor business health in real-time,
leverage interactive data analytics for your business, and even help to conduct Internet of Things (IoT)
remote monitoring.
●● Personalizer (Preview) - Personalizer helps to deliver richer personalized experiences in the applica-
tions that you develop. It can understand and manage the reinforcement learning loop and can be
deployed anywhere from the cloud to the edge.
Because the Personalizer and Anomaly Detector features are still in preview, they are not covered in this
course content.
Content Moderator
If your organization publishes content that will be viewed or consumed by the public, or even internally,
and you want to ensure that offensive or adult-related content is not included, you can use the various
features available in the content moderator services. Content moderator has the ability to evaluate text,
images, and video content for offensive material. To help ensure that the service doesn't inadvertently
include or exclude content accidentally, you can also rely on the human review tool aspect of content
moderator.
Text Moderation
The service can evaluate written text to determine if it contains the following:
●● Profanity - capable of detecting for profanity in more than 100 languages
●● Inappropriate text - this is context dependent and also supports the use of custom lists for evaluation
●● Personally Identifiable Information (PII) - scan text and identify the potential of exposing PII informa-
tion
●● Information in custom term lists - block or allow content according to your own content policies
Image Moderation
●● Use machine-assisted image moderation and human-in-the-loop Review tool to moderate images for
adult and racy content.
●● Scan images for text content and extract the text.
●● Detect faces to aid in detecting personal data that may be contained in the images due to the
recognition of faces in the images. You can match images against custom lists, and take further action.
●● The Optical Character Recognition (OCR) operation predicts the presence of text content in an image
and extracts it for text moderation.
●● The Match operation allows fuzzy matching of incoming images against any of your custom lists,
created and managed using the List operations. If a match is found, the operation returns the identifi-
er and the moderation tags of the matched image.
MCT USE ONLY. STUDENT USE PROHIBITED 26 Module 0 Introducing Azure Cognitive Services
Video Moderation
Use Content Moderator's machine-assisted video moderation and human review tool to moderate videos
and transcripts for adult (explicit) and racy (suggestive) content.
●● Video-trained classifier (preview) - Microsoft's adult and racy video classifier is trained with videos
●● Shot detection - rather than just outputting frames, the service also provides shot-level data so you
can review the video using shot-level or frame-level data
●● Key frame detection - the service identifies and outputs only potentially complete (good) frames. The
feature makes frame-level adult and racy analysis easier and more efficient.
●● Visualization for human review - using the review tool, users can evaluate the insights of the video,
change tags related to the video and perform many features associated with reviewing and evaluating
video.
●● Transcript moderation - video files that will have transcripts or closed captioning require moderation
to search for offensive speech. This option requires the use of the Azure Media Indexer to convert the
speech to text, if the transcript files are not available, and then use the Content Moderator review API
to scan the text in the review tool
Review Tool
The Review tool, when used in conjunction with the machine-assisted moderation APIs, allows you to
accomplish the following tasks in the content moderation process:
●● A common set of tools to moderate text, image, and video.
●● Automate the creation of human reviews when moderation API results come in.
●● Assign or escalate content reviews to multiple review teams, organized by content category or
experience level.
●● Use default or custom logic filters (workflows) to sort and track content, without writing any code.
●● Use connectors to process content with Microsoft PhotoDNA, Text Analytics, and Face APIs in addition
to the Content Moderator APIs.
●● Build your own connector to create workflows for any API or business process.
●● Get key performance metrics on your content moderation processes.
10 https://azure.microsoft.com/en-us/services/cognitive-services/content-moderator/
MCT USE ONLY. STUDENT USE PROHIBITED
Overview of Azure Cognitive Services 27
8. You should be taken to your Azure portal with the resource being deployed
9. Once the resource is deployed and you are at the Quick Start page, select the Content Moderator
Review Tool link in section 2
10. This will open a new browser tab where you can create your review team
11. Complete the fields for Region, Team Name, and Team ID
12. Agree to the terms and select Create Team
13. In a few seconds, the Content Moderator dashboard opens
14. Select the Try drop down and choose Text
15. Select the Click here link to use default sample text
16. Point out to the students the contents of the text that will be evaluated and then select Submit
17. After a few seconds, the process completes and there is a Click her to review link at the of the page.
Select that to see the result
18. Discuss with the students the highlighted components in the left pane
19. Discuss the resulting output in the right pane showing that HasProfanity is True as is hasPII. Point out
the places in the text where those values are present.
20. You can also choose to test this with images or video files if you have any on the local computer.
MCT USE ONLY. STUDENT USE PROHIBITED 28 Module 0 Introducing Azure Cognitive Services
4. Select Face from the list and then Click or Select Create
5. You would next complete the details in the Create blade
6. Click or Select Create to create the single-service subscription for the Face API
Using the Overview blade, you can ascertain basic information about your service such as the resource
group it is part of, its current status, the region it is located, assigned subscription, pricing tier, and of
course, the endpoint that would be used to access the service. The other core areas from the top left
options of the service permit you to view the activity log for the service, handle access control, manage
the tags for the service and to run some diagnostics.
Activity Log blade will display a history of access to the service. Options permit you to filter on manage-
ment groups, Azure subscriptions, timespan, event severity, resource group, or add your own filter.
Access Control is the blade you will use to manage role-based access control (RBAC). We don't cover
RBAC here as it is covered in the Azure Administrator tracks, both online and classroom-based.
Tags is another area that was covered in the Azure Administrator training and pertains to tagging
resources for identification in billing invoice investigations.
Diagnose and solve problems will open a blade that will display resource health along with guidance on
determining issues with the service and some troubleshooting steps.
Resource Management
Under the Resource Management section, you will find the previously mentioned Quick Start but at the
top of the section is the option to view the Keys. Managing the keys for your service is critical to security.
MCT USE ONLY. STUDENT USE PROHIBITED
Creating Cognitive Services using Azure Portal 31
Applications that want to connect to and use this service, must use one of the keys listed on this page.
You can use the Copy button at the far right of each key to copy it to the clipboard for use in your
applications. If you think that your keys have been compromised, you should use the regenerate options
at the top of this blade to create new keys for the service.
Selecting the Pricing Tier option opens a blade where you can choose a new pricing tier that is different
from that originally chosen when you created the service. Pricing tiers can be added or removed in Azure
so you should review this blade periodically to not only see what changes may have been made, but to
also ensure that the tier you have chosen is correct for your usage model.
The Billing by Subscription opens a blade that provides you with options to select an Azure subscription,
resource and resource group and time span to view the current costs associated with the services. Note
that there is a delay in the reporting that may affect the timeliness of the information displayed on this
blade.
If you want to view basic properties for the service, you will find that information on the Properties blade.
Essentially these are read only fields that show associated subscription name and ID, the resource group
(RG) this service is assigned to and the ID for the RG.
You can add Locks to the service by using the Locks blade. We will not cover locks in this course.
Many Azure administrators will look to manage services and resources through the use of Azure Resource
Manager (ARM) templates. To aid in using ARM with your service, selecting the Automation Script blade
results in Azure generating a template for you automatically. The Azure Administrator training, AZ-1xx
set of courses, covers ARM templates and their usage.
MCT USE ONLY. STUDENT USE PROHIBITED 32 Module 0 Introducing Azure Cognitive Services
Alerts
Alerts proactively notify you when important conditions are found in your monitoring data. They allow
you to identify and address issues before the users of your system notice them. Alerts have a specific
flow that you should understand to effectively use them.
When you create a rule, the Azure portal opens the Create rule blade where you specify the following
information:
Resource - the resource to which the alert rule will apply. If you do this from the service blade, the
service is automatically selected but you can select another resource. This is actually indicative of the fact
that Alerts and Alert rules extend across many Azure resources
Condition - this is a signal logic that the alert rule will use. Signals are emitted by the target resource and
can be of several types. Metric, Activity log, Application Insights, and Log. The list is fairly extensive with
some signals being specific a type of resource
Action Group - who, or what, should be notified of the alert for action. Notify your team via email and
text messages or automate actions using webhooks, runbooks, functions, logic apps
Alert Details - provide a name for the alert rule and a description to explain the purpose for the alert
rule.
The default option is to enable the new rule as soon as it is created but you can turn that off if you don't
want the rule to enabled immediately.
Metrics
Selecting the Metrics monitoring option opens the Metrics blade that consists of a chart. Initially this
chart will be blank until you add a metric that you want to display. The resource should be auto-populat-
ed in the resource entry and a default metric namespace. Click or Selecting the arrow in the Metric drop
down will allow you to select a metric to add to the chart. Only once you have selected a metric, will the
aggregation options be available. If you think about it, the aggregations will change depending on the
metric chosen anyway.
You can also change the chart type from the default line type to an area, bar, or scatter or grid options.
You can also choose to create a new alert rule directly from this blade if you wanted to chart a different
alert rule. Once you have created your metric chart, you can pin it to the dashboard for easy monitoring.
Diagnostic Settings
This blade allows you to select ab Azure subscription, resource group, resource types, and resources to
use in a diagnostic scenario. You can turn on diagnostics to collect:
●● Audit data
●● RequestResponse information for the service
●● All Metrics that are configured for the service
1. You can also access the API key(s) from the Keys page and the Endpoint form the Overview page.
3. Click or Select or Select the Overview option and review the information on this page
4. Select the Access control (IAM) option. Azure uses Role Based Access Control (RBAC) and this is
where you control the RBAC for your Cognitive Services account. You can also check currently
assigned access levels on this page
5. Select the Tags option in the left nav pane. This is where you create tags for categorizing resources.
Azure fundamentals training discusses the importance of tags for billing and other identificaiton
needs for your resources
6. If you wish to change the pricing tier for this service or account, you can select Pricing tier under the
Resource Management group in the left nav pane
7. Under Resource Management, select the Billing by Subscription option to view the costs associated
with each resource you have created, filtered by subscription. Note that this will show all resources
and not just the current Cognitive Services account
MCT USE ONLY. STUDENT USE PROHIBITED
Creating Cognitive Services using Azure Portal 35
8. The Properties option, under Resource Management, displays basic information about the service and
allows you to change things such as the subscription and resource group assignments.
9. You can choose the type of lock to apply to this resource on the Locks page. You can choose to lock
the entire subscription, the resource group, or the resource itself. THe type of locks supported are
Read-only and Delete and have been covered in Azure Fundamentals training.
10. Finally, if you decide that you want to generate a template to make creation of this resource easier in
the future, or to script it for later uses, choose the Export template option, under Resource Manage-
ment. Templates are covered in other Azure training.
21. Now you can select an action to take. Click or Select or Select or move to the Action Type drop down
and choose Email/SMS/Push/Voice as the option
22. Select the SMS check box and enter a phone number to receive SMS messages.
23. Leave the remaining options at their default and Click or Select or Select OK
24. Select OK again on the Add Action Group page
25. Back on the Configured Actions panel, select Done to apply the actions and close the pane
26. Provide an Alert rule name in the Alert Details section
27. Select Sev 1 for the Severity
28. Leave Yes selected under enable rule upon creation
29. Select or Click or Select or Select the Create alert rule button
30. Explore the remaining monitoring options such as Metrics, Diagnotic Settings and Logs to gain a
familiarity with the monitoring options available. Because these are common across resources in
Azure, refer to the Azure training options for more details on the usage.
MCT USE ONLY. STUDENT USE PROHIBITED
Testing Cognitive Services using API Testing Console 37
11 https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236
MCT USE ONLY. STUDENT USE PROHIBITED 38 Module 0 Introducing Azure Cognitive Services
In this particular testing console the service will detect faces in an image however, rather than uploading
an image directly from this page or from an application that you are using, you supply a URL in the JSON
body for the request. This URL could represent the location of images in a web page or even from a
source control repository like GitHub.
Note also that you will need the Key from your service before this API test will succeed. That assumes
that you have a face detection service available and the keys are available to you. You would paste the
key into the Ocp-Apim-Subscription-Key. This is apparent because that field is highlighted in red indicat-
ing a value is required.
After the key is provided, an image URL is entered in the Request body JSON, you can Click or Select the
Send button which activates the service and performs a face detection on the image that was available in
the URL. The response body will be displayed at the bottom of the page when the action takes place.
This could be a successful completion or there may be errors generated. Errors will be displayed with the
standard HTTP error codes such as 401 or 404, but if it succeeds, you should see a 200 HTTP success
code and a resultant JSON response as shown in this sample. Not the format of the response and the
data that is returned. You see coordinates for any detected faces.
MCT USE ONLY. STUDENT USE PROHIBITED
Testing Cognitive Services using API Testing Console 39
MCT USE ONLY. STUDENT USE PROHIBITED 40 Module 0 Introducing Azure Cognitive Services
Lab
Lab Environment
The hands-on experiences in this course require a computer with the following software installed:
●● Visual Studio (latest version)
●● .NET Framework 4.0 (or higher) SDK
●● Lab Setup Instructions and content are in GitHub repo for this course12.
The Azure Portal requires a modern browser that's compatible with your operating system:
●● Microsoft Edge (latest version)
●● Internet Explorer 11
●● Safari (latest version, Mac only)
●● Chrome (latest version)
●● Firefox (latest version)
Note that the labs can be completed with a local computer configured with the above software or, if your
Azure subscription permits, you can also make use of the Microsoft Azure Data Science Virtual Machine
(DSVM).
12 https://github.com/MicrosoftLearning/AI-100-Design-Implement-Azure-AISol
13 https://github.com/MicrosoftLearning/AI-100-Design-Implement-Azure-AISol
MCT USE ONLY. STUDENT USE PROHIBITED
Lab 41
This hands-on lab guides you through creating an intelligent console application from end-to-end using
Cognitive Services (specifically the Computer Vision API). We use the ImageProcessing portable class
library (PCL), discussing its contents and how to use it in your own applications.
MCT USE ONLY. STUDENT USE PROHIBITED
Module 1 Creating Bots
Building a bot should follow a consistent approach from planning to finished product as outlined in this
diagram.
Microsoft helps you to develop intelligent bots using an open-source SDK and set of tools that allow you
to:
●● Create bots that can interact with users in a natural way by integrating Azure Cognitive Services into
your bot (speech, search, language understanding, vision, QnA Maker)
●● Use the open-source SDK and tools for building, testing, and publishing your bot to the Azure
platform
●● Integrate popular channels to reach your customers in more ways (some examples include Skype,
Cortana, Microsoft Office, Microsoft Teams, Facebook Messenger, Kik, and Slack)
●● Create your own branded virtual assistant through the use of the Microsoft solution accelerators
Pricing for bot services will vary depending on usage and channels selected. Be sure to visit the pricing
page1 for up-to-date details on how much a bot may cost.
1 https://azure.microsoft.com/en-us/pricing/details/bot-service/
2 https://aka.ms/AA3m39t
MCT USE ONLY. STUDENT USE PROHIBITED
Introducing the Bot Service 45
●● Test - You should always test your bot before you release it. There are currently two recommended
testing mechanisms:
●● Test your bot locally with the emulator. The Bot Framework Emulator is a stand-alone app that not
only provides a chat interface, but also debugging and interrogation tools to help understand how
and why your bot does what it does. The emulator can be run on a locally alongside your in-devel-
opment bot application.
●● Test your bot on the web. Once configured through the Azure portal your bot can also be reached
through a web chat interface. The web chat interface is a great way to grant access to your bot to
testers and other people who do not have direct access to the bot's running code.
●● Publish - Once testing is complete, you can publish your bot to Microsoft Azure or host in on your
own web service or data center
●● Connect - You can connect your bot to many different channels for users to gain access to the bot
such as:
●● Facebook
●● Messenger
●● Skype
●● Slack
●● Microsoft Teams
●● SMS/Text
●● Cortana
●● Evaluate - Once your bot is released, you should monitor the bot for sustained engineering. You may
find users' interactions may be different than your initial plan so you might find it necessary to
improve the bot over time.
Compliance Considerations
The Azure Bot Service can address you compliance concerns as well because the service is now compliant
with the following:
●● ISO 27001:2013 and ISO 27018:2014
●● PCI DSS
●● Microsoft's HIPAA BAA
●● SOC 1 and SOC 2
In this diagram, you see two activity types, a ConversationUpdate and a Message. The Bot Framework
Service may send a conversation update when a party joins the conversation. For example, on starting a
conversation with the Bot Framework Emulator, you will see two conversation update activities (one for
the user joining the conversation and one for the bot joining). To distinguish these conversation update
activities, check whether the members added property includes a member other than the bot.
The message activity carries conversation information between the parties. In an echo bot example, the
message activities are carrying simple text and the channel will render this text. Alternatively, the message
activity might carry text to be spoken, suggested actions or cards to be displayed.
It's also important to note that channels may include some additional information to be included with an
activity. This may be supporting information or metadata as necessary for the specific channel.
These activities will be sent using HTTP POST requests. The diagram also depicts that the POST requests
are also paired with HTTP status responses. In our case, the responses are all HTTP 200 OK status codes.
You can also experience 404 not found or 401 authorization codes as well depending on the specifics of
the request. For example, if your endpoint URL is not valid, you may experience an HTTP 404 error code.
You will gain more exposure to the service and framework as you begin working with the labs in the
course but one final diagram will help you understand the activity processing stack.
MCT USE ONLY. STUDENT USE PROHIBITED
Introducing the Bot Service 47
In the example above, the bot replied to the message activity with another message activity containing
the same text message. Processing starts with the HTTP POST request, with the activity information
carried as a JSON payload, arriving at the web server. In C# this will typically be an ASP.NET project, in a
JavaScript Node.js project this is likely to be one of the popular frameworks such as Express or Restify.
The adapter, an integrated component of the SDK, is the core of the SDK runtime. The activity is carried
as JSON in the HTTP POST body. This JSON is deserialized to create the Activity object that is then
handed to the adapter with a call to process activity method. On receiving the activity, the adapter
creates a turn context and calls the middleware.
MCT USE ONLY. STUDENT USE PROHIBITED 48 Module 1 Creating Bots
●● FormFlow for automatically generating a bot (from a C# class) that guides the user through the
conversation, providing help, navigation, clarification, and confirmation as needed
If your development team is familiar with JavaScript and Node.js, then you can opt to use the Bot Frame-
work SDK for Node.js. This is a powerful, easy-to-use framework that provides a familiar way for Node.js
developers to write bots. You can use it to build a wide variety of conversational user interfaces, from
simple prompts to free-form conversations.
The conversational logic for your bot is hosted as a web service. The Bot Framework SDK uses restify, a
popular framework for building web services, to create the bot's web server. The SDK is also compatible
with Express and the use of other web app frameworks is possible with some adaption.
Using the SDK, you can take advantage of the following SDK features:
●● Powerful system for building dialogs to encapsulate conversational logic.
●● Built-in prompts for simple things such as Yes/No, strings, numbers, and enumerations, as well as
support for messages containing images and attachments, and rich cards containing buttons.
●● Built-in support for powerful AI frameworks such as LUIS.
●● Built-in recognizers and event handlers that guide the user through the conversation, providing help,
navigation, clarification, and confirmation as needed.
Additional Environments
The Bot Framework SDK is open source and you can find up-to-date information on the GitHub repo3
for the framework. On the repo, you will find additional environment and language support as it be-
comes available.
Currently, there are preview versions available for Python4 and for Java5. Keep in mind that these are
preview and updates, additions, or deletions are possible.
3 https://github.com/microsoft/botframework-sdk
4 https://github.com/Microsoft/botbuilder-python#packages
5 https://github.com/Microsoft/botbuilder-java#packages
MCT USE ONLY. STUDENT USE PROHIBITED 50 Module 1 Creating Bots
5. A new blade will open where you will enter information about the Web App Bot.
6. In the Bot Service blade, provide the requested information about your bot using the following as
guidance.
●● Bot name - As with other resources in Azure, this must be unique. Provide a short, descriptive
name for your bot.
●● Subscription - Select the Azure subscription you want to use.
●● Resource Group - Creating a new resource group permits you the ability to delete all resources
you create in that group, by deleting the resource group itself. This is a great way to clean up
resources after completing tutorials. You can also choose to use an existing resource group that
you may have already created
●● Location - Select the geographic location for your resource group. Your location choice can be any
location listed, though it's often best to choose a location closest to your customer. The location
cannot be changed once the bot is created.
●● Pricing tier - Leave the default option here as it allows for a maximum of 1000 premium message,
which is adequate for this tutorial and for quick testing.
●● App name - The unique URL name of the bot. For example, if you name your bot myawesomebot,
then your bot's URL will be http://myawesomebot.azurewebsites.net. The name must use alphanu-
meric and underscore characters only. There is a 35 character limit to this field. The App name
cannot be changed once the bot is created.
MCT USE ONLY. STUDENT USE PROHIBITED
Create a Basic Chat Bot 51
●● Bot template - Leave the default set as Basic Bot (C#). You won't be writing or modifying any code
so don't worry about the language choice at this point.
●● LUIS App location - because we are choosing the Basic Bot, the template is looking for a location
for the Language Understanding Service, or LUIS. Select an appropriate location here. While it
doesn't have to reconcile with the bot location, it's typically a good idea to match the two, if
possible.
●● App service plan/Location - Create a new app service plan here, or choose an existing on if you
have already created an app service plan. Creating a new one will require you to choose a loca-
tion, a plan name, and a default pricing tier will be selected.
●● Application Insights - turn this off as we will not make use of insights for this sample bot
●● Microsoft App ID and password - leave the default for auto creating these, unless you want to
secure this for later usage.
7. Click or Select the Create button. Azure will go through a validation process and then begin a
deployment process
8. Once the bot has been deployed, you will receive a notification which you can use to go to the new
resource. If you miss the notification, you can simply Click or Select on the notification icon in the top
nav bar to open the notification and then Click or Select Go to Resource
9. Click or Select the Test in Web Chat link in the left nave pane, under Bot management.
10. A new chat window opens in the main area of the Azure dashboard.
11. Your bot displays a welcome message and then prompts you to select a choice of options or to enter
some text about how it can help you.
12. Interact with the bot by typing in Book a flight.
13. The bot responds by asking where you would like to travel.
14. You may notice that the bot doesn't have a lot of interactive options which is due to the fact that you
haven't written any real code to make the bot “intelligent”
15. Before we close out this task list, explore some more of the bot aspects by Click or Selecting on the
Channels option under Bot management.
16. Note that the Web Chat channel is the one that you used initially to interact with the bot. Take not of
the other channels available to be used with the bot.
17. Click or Select the Settings option under Bot management and note the configuration settings that
are applied to your bot. If you are creating external channels, the endpoint is an important piece of
information to know.
18. Feel free to explore the additional potions and settings for your bot if you wish or, go back to the
Overview page and Click or Select Delete to remove the bot and prevent future charges against your
Azure account.
MCT USE ONLY. STUDENT USE PROHIBITED 52 Module 1 Creating Bots
Local Bots
To connect to a local Bot for testing with the console, you would perform the following steps:
1. Start the bot on the local computer
2. Open the Bot Framework Emulator
3. Open the .bot configuration file
6 https://github.com/Microsoft/BotFramework-Emulator/blob/master/README.md#download
MCT USE ONLY. STUDENT USE PROHIBITED
Testing with Bot Emulator 53
4. Once your bot configuration file is open, you can interact with the bot through the emulator and
evaluate how the bot behaves
5. When opening a .bot file, you may be required to enter your bot file secret key. You can find this, in a
local bot, in the appsettings.json filee included with the Visual Studio project for the bot solution.
You can also connect to a local bot using a URL as opposed to the .bot file. In this case, you will need to
grab the port number from the running bot. Simply use the URL http://localhost:port number/api/
messages and replace ‘port number’ in the URL with your bot's port. Typically this is 3978.
Remote Bots
If you are using Windows and you are running the Bot Framework Emulator behind a firewall or other
network boundary and want to connect to a bot that is hosted remotely, you must install and configure
ngrok tunneling software. The Bot Framework Emulator integrates tightly with ngrok tunnelling software
(developed by inconshreveable7), and can launch it automatically when it is needed.
1. Open the Emulator Settings
2. Enter the path to ngrok
3. Select whether or not to bypass ngrok for local addresses
4. Click or Select Save
7 https://inconshreveable.com/
MCT USE ONLY. STUDENT USE PROHIBITED 54 Module 1 Creating Bots
You will have a chance to run through this during the lab exercises.
MCT USE ONLY. STUDENT USE PROHIBITED
Lab 55
Lab
Lab 3: Basic Filter Bot
In this lab, we will be setting up an intelligent bot from end-to-end that can respond to a user's chat
window text prompt. We will be building on what we have already learned about building bots within
Azure, but adding in a layer of custom logic to give our bot more bespoke functionality.
This bot will be built in the Microsoft Bot Framework. We will evoke the architecture that enables the bot
interface to receive and respond with textual messages, and we will build logic that enables our bot to
respond to inquiries containing specific text.
We will also be testing our bot in the Bot Emulator, and addressing the middleware that enables us to
perform specialized tasks on the message that the bot receives from the user.
We will evoke some concepts pertaining to Azure Search, and Microsoft's Language Understanding
Intelligent Service (LUIS), but will not implement them in this lab.
Lab Objectives
●● Create a Basic Bot in the Azure Portal
●● Download the Bot Source Code
●● Modify the Bot Code
●● Test with the Bot Emulator
Now, we wish to log bot chats to enable our customer service team to follow up to inquiries, determine if
the bot is performing in the expected manner, and to analyze customer data. This hands-on lab guides
you through enabling various logging scenarios for your bot solutions.
In the advanced analytics space, there are plenty of uses for storing log conversations. Having a corpus of
chat conversations can allow developers to:
●● Build question and answer engines specific to a domain.
●● Determine if a bot is responding in the expected manner.
●● Perform analysis on specific topics or products to identify trends.
Lab Objectives
●● Understand how to intercept and log message activities between bots and users
●● Log utterances to file storage
MCT USE ONLY. STUDENT USE PROHIBITED
Module 2 Enhancing Bots with QnA Maker
Architecture
To understand how the QnA Maker provides the services it does, we can look at the architecture behind
it. As mentioned in the introduction on this topic, you use a web-based portal to create your knowledge
base. This portal is part of the QnA Maker management services. The management services include the
portal but also provide facilities for updating, training, and publishing your knowledge base for consump-
tion. Although the portal is a big part of this, you can also make use of management APIs that can be
accessed through a set of request URLs, comprised of request headers, the subscription key, and a
request body. We will only cover the use of the portal here.
Once you have created the knowledge base and published it, the QnA Maker data and runtime are
responsible for making your service available. It will be accessible from your Microsoft Azure subscription
in the region you chose during the creation process. The content that you have added into your knowl-
edge base will be stored in Azure Search while the endpoint for access by client applications, including
your Bot, are provided as a deployed App service. Microsoft Azure also provides the option of integrat-
ing application insights into your service for analytics.
1 https://www.qnamaker.ai
MCT USE ONLY. STUDENT USE PROHIBITED
Introducing QnA Maker 59
To get started with the service, you need to select Create a knowledge base. This will require a sign in,
using a Microsoft Account login. Once you sign in, chances are that you do not have any knowledge
bases existing and the display will indicate this.
If you Click or Select the Create a knowledge base menu again, a new page loads allowing you to create
your QnA Service.
MCT USE ONLY. STUDENT USE PROHIBITED 60 Module 2 Enhancing Bots with QnA Maker
Once you Click or Select the Create QnA Service, you are directed to your Azure portal, if you are already
signed into the Azure portal. If you are not currently signed in, you will be required to sign into Microsoft
Azure with your existing Azure account. This is required because the service will be hosted on Azure.
The service requires you to provide the following information:
●● Name - like all Azure services, this name must be unique
●● Subscription - the Azure subscription that will be used to host the service
●● Pricing Tier - this is based on number of transactions per second. You will investigate this further in
the walkthrough and labs but note that pricing tiers can change
●● Location - you should select a geographic location that is closest to you or where you want to host
the service
●● Resource Group - like all Azure objects, you should assign this service to an existing resource group,
or create a new one
●● Search Pricing Tier - select the lowest cost search tier that will serve your needs. Searching is required
in order for the service to function correctly. There is currently one free option that includes 3
indexes, 50MB of storage, and no scaling options
●● App Name - an app name is automatically chosen based on the service name you created at the top.
You can change this app name to something different if you wish. It will be the host portion of the
URL that will be generated for the service
●● Website location - determines where the web site will be hosted. The default is to host it in the same
location as the service
●● App Insights - you can opt to include insights for the service to help you diagnose and evaluate the
app and service for various performance considerations. If you choose to enable App Insights, you
will also need to select a location for the app insights data. Once again, the default is in the same
region.
Once you create the service, it will be hosted on Azure and accessible to Bots through the use of the
name you created and will require authorization through the use of a key. You can find the key (2
provided) on the Keys blade under the Resource Management option.
MCT USE ONLY. STUDENT USE PROHIBITED
Introducing QnA Maker 61
2 https://azure.microsoft.com/free/?WT.mc_id=A261C142F
3 https://www.qnamaker.ai/
MCT USE ONLY. STUDENT USE PROHIBITED 62 Module 2 Enhancing Bots with QnA Maker
When creating your knowledge base for QnA Maker, you should follow the life cycle recommended by
Microsoft. This life cycle involves an iterative model that encompasses the following steps:
●● Create/Update - manage your knowledge base by creating and updating the content for the QnA
session
●● Publish - deploy the knowledge base to the endpoint
●● Analyze - use analytics to monitor and gather feedback
●● Test - a constant process that verifies matching and ranking while the KB is in use
MCT USE ONLY. STUDENT USE PROHIBITED
Implementing a Knowledge Base with QnA Maker 63
QnA Maker can extract the question/answer pairs from a variety of data sources. There is a fairly broad
support for different data sources that can be used.
●● URLs - can contain the following types of information
●● FAQs
●● (Flat, with sections or with a topics homepage)
●● Support pages
●● (Single page how-to articles, troubleshooting articles etc.)
●● PDF
●● FAQs
●● Product Manual
●● Brochures
●● Paper
●● Flyer Policy
●● Support guide
●● Structured QnA
●● DOC
●● FAQs
●● Product Manual
●● Brochures
●● Paper
●● Flyer Policy
●● Support guide
MCT USE ONLY. STUDENT USE PROHIBITED 64 Module 2 Enhancing Bots with QnA Maker
●● Structured QnA
●● Excel - Structured QnA file (including RTF, HTML support)
●● TXT/TSV Files - Structured QnA file
If you do not have pre-existing content to populate the knowledge base, you can add QnAs editorially in
QnA Maker Knowledge base. QnA Maker allows you to manage the content of your knowledge base by
providing an easy-to-use editing experience. Select your knowledge base in your list of existing knowl-
edge bases in the QnA Maker interface, then edit the items.
Once a knowledge base is created, it is recommended that you make edits to the knowledge base text in
the QnA Maker portal, rather than exporting and reimporting through local files. However, there may be
times that you need to edit a knowledge base locally. When creating your knowledge base for QnA
Maker, be sure to follow the best practices4 described by Microsoft.
4 https://docs.microsoft.com/en-us/azure/cognitive-services/qnamaker/concepts/best-practices
MCT USE ONLY. STUDENT USE PROHIBITED
Implementing a Knowledge Base with QnA Maker 65
the scenes. When you begin looking at step 2, you will be able to select the Azure Directory ID, Subscrip-
tion, and QnA Service name, as shown here.
Once you have connected the service to this knowledge base, you can move on the next step which
allows you to name the KB and then begin to populate the KB with the necessary questions and answers.
You should ensure that you name your KB with a descriptive name that makes it easy to understand its
purpose when you work with it.
5 https://github.com/MicrosoftDocs/mslearn-work-with-vision-cognitive-services/blob/master/Microsoft%20Bot%20FAQ.zip?raw=true
MCT USE ONLY. STUDENT USE PROHIBITED 66 Module 2 Enhancing Bots with QnA Maker
You should review and make any edits to this page prior to publishing your knowledge base. You can
perform the following editing options:
●● Add questions related to an answer - immediately to the right if each question is an X that you can
use to delete a question but also a plus (+) symbol that you can use to add additional questions,
related to the answer found in the answer column. Recall that you can have the same answer related
to multiple questions in an effort to ensure coverage for anticipated questions that are similar
●● Delete an answer - you can Click or Select the trash can icon next to an answer to delete a question/
answer pair
●● Add QnA Pair - Click or Selecting this button, found above the page navigation arrows, allows you to
add a new question and answer pair to knowledge base. You would do this if your source document
didn't contain all the QnA pairs you need, or to augment the existing list with some new data.
●● Show Metadata Tags - initially your QnA pairs may not have any metadata associated with them, Click
or Selecting the Show metadata tags icon, will display any metadata tags associated with each QnA
pair
Once you are satisfied with your knowledge base, you can then publish it so it becomes available to your
Bot. It is recommended that you test your KB with the web Bot that is available on the same page.
Once you are satisfied with your testing, you can also save and train the knowledge base to help better
refine the answers returned when users ask questions. If you are satisfied and ready to make the KB
available as an accessible endpoint, Click or Selecting the Publish button will start the publishing process.
You will be presented with a notification that the KB will move from a test index to a production index.
This is important to understand that there may be cost changes associated with the service once it is
published, that are not present during testing.
Once you Click or Select Publish on this page, your KB will have an endpoint that is accessible to the
service. You will also be presented with another web page that outlines some important information for
accessing your KB and service using HTTP. You will find information for both Postman and Curl usage.
You will visit the information on this page later when integrating the bot with the KB and service so you
should copy the information down or leave the page open until you have completed the integration
component.
MCT USE ONLY. STUDENT USE PROHIBITED
Implementing a Knowledge Base with QnA Maker 67
Integrating QnA with a bot in this manner, is time consuming and error-prone. As a result, the recom-
mended method is to actually use the QnA Maker portal to make the connection. Your instructor will
walk you through this procedure in the next topic.
4. The Web App Bot configuration is pretty much completed for you already with information generated
from the Qn Maker configurations
5. For this walkthrough, leave the entries as they appear with the exception of Application Insights
option. Turn that off
6. Click or select the Create button
7. Azure will perform some validation and then begin the process of creating and deploying the bot
8. Once the bot is deployed, go to the resource
9. Under the Bot management section, select Test in Web Chat
10. Once the test window opens, interact with the bot to see the responses returned from the knowledge
base
11. Under App Service Settings, select All App Service Settings
12. A new window opens detailing some statistics for the bot
13. Select the Configuration option, under Settings
14. Note that from the previous topic, we discussed the connection information around the QnA Auth
Key, Endpoint, and knowledge base ID
15. We find those settings located on this panel
16. Select the Show values option to see the information used to connect the bot to the QnA Maker
knowledge base
17. If you look at the Postman or Curl sample requests on the service deployment page in the QnA Maker
site, you will note these values in the request headers
You have now successfully created a QnA Maker service, published it on Azure, created a Web chat Bot,
and integrated the Bot with the QnA Maker service to provide a chat-based experience for users to
interact with the Microsoft Bot Framework FAQ.
MCT USE ONLY. STUDENT USE PROHIBITED 70 Module 2 Enhancing Bots with QnA Maker
Lab
Lab 5: Integrate QnA Maker with a Bot
AdventureWorks wants to use a Bot to allow their customer support FAQ to drive conversations on a
Customer Support Bot. A document already exists that contains some questions and answers taken from
the FAQ engagements with customers. This will serve as a starting point but may need to be augmented
with additional questions and answers.
Lab Objectives
●● Create a QnA Service
●● Generate a Knowledge Base using a PDF Document (FAQ)
●● Connect and Publish the Knowledge Base
●● Connect the QnA Service to a Bot
MCT USE ONLY. STUDENT USE PROHIBITED
Module 3 Learn How to Create Language Un-
derstanding with LUIS
flight to New York for New Year's Eve celebrations.” If we evaluate this utterance for key aspects, we can
determine the user's intent. They want to book a flight. We can state the intent is "BookFlight".
Entities, as indicated above, are words or phrases, but also simply data, that help to provide specific
context for the utterance and aid in more accurately identifying the intent. Not every utterance will
contain entities. In our utterance for booking the new York flight, we can identify entities such as:
●● New York - we can classify this entity as Location.Destination.
●● New Year's Eve - we might classify this entity as Event.
●● The number 2 - this is mapped to a built-in entity in LUIS known as prebuilt entity, specifically a
prebuilt number.
Natural Language
Designed to identify valuable information in conversations, LUIS interprets user goals (intents) and distills
valuable information from sentences (entities), for a high quality, nuanced language model. LUIS inte-
grates seamlessly with the Azure Bot Service, making it easy to create a sophisticated bot.
What this means for you is that LUIS takes a lot of the “hard work” out of your hands and provides the
services necessary to help you integrate natural language understanding and processing into your bots.
Using the LUIS interface, you can easily create intents, utterances, and entities that represent example
questions or comments a user might make while interacting with your bot, and have LUIS automatically
recognize those, and similar statements, to allow your bot the ability to "understand" what a user is
asking.
Integrated Learning
Powerful developer tools are combined with customizable pre-built apps and entity dictionaries, such as
Calendar, Music, and Devices, so you can build and deploy a solution more quickly. Dictionaries are
mined from the collective knowledge of the web and supply billions of entries, helping your model to
correctly identify valuable information from user conversations.
MCT USE ONLY. STUDENT USE PROHIBITED
Introducing Language Understanding 73
Ongoing Learning
Active learning is used to continuously improve the quality of the natural language models. Once the
model starts processing input, LUIS begins active learning, allowing you to constantly update and
improve the model. You can also review the series of statements or questions that users have provided to
the bot, and LUIS has interpreted. If you find matches that don't meet your needs, or perhaps require
some tweaking, you can do that easily in the LUIS tools and then train the models again to improve
results.
different from the previous utterance. A good variety in the utterances includes overall word count, word
choice, verb tense, and punctuation.
1 https://portal.azure.com
2 https://www.luis.ai/
3 https://eu.luis.ai/
4 https://au.luis.ai/
MCT USE ONLY. STUDENT USE PROHIBITED 76 Module 3 Learn How to Create Language Understanding with LUIS
When a user interacts with the LUIS app, through your chat bot as an example, and they enter an utter-
ance (a phrase) into the chat bot, the LUIS service will return the single top intent that most closely
matches that utterance. However, for testing purposes, you can also choose to return scores for all
intents that LUIS matches to the utterance. You would need to provide the verbose=true flag in the
query string for the API call, but it can help you understand how LUIS is performing in the matching of
utterances to intents and helps you tweak your sample utterances and retrain your model.
This is important to note for limitations on the service. For more information on the limits, you can view
the Model Boundaries5 page on the docs site.
Introducing Utterances
In a LUIS app, the concept of an utterance is best explained as being a phrase or question that a user
might utilize to interact with your app. You map utterances to intents to help determine the users'
intentions. Let's consider a scenario where you are creating a LUIS app that will allow users to search for
images. You would start by creating an intent called SearchPics. Following that, you would want to
create a set of utterances that are sample phrases or questions a user might ask in relation to searching
for images. The following list is just an example of what you might enter.
●● Find outdoor pics
●● Are there pictures of a train?
●● Find pictures of food.
●● Search for photos of boys playing
●● Give me pics of business women
●● Show me beach pics
●● I want to find dog photos
●● Search for pictures of men indoors
●● Show me pictures of men wearing glasses
●● I want to see pics of sad boys
●● Show me happy baby pics
Careful review of these utterances can also reveal a common theme. Words like pics, pictures, and
photos are examples of the common theme in the utterances. They all map to the concept of images of
pictures or photos. You will also see, in the next topic, how these relate to the concept of an entity, which
is another aspect you will work with in a LUIS app.
Each intent that you create in your LUIS app, should have a number of utterances that related to it. You
may not be able to think of every possible combination of phrase or question that a user might enter, but
that's where the training aspect of the model comes into play. Through the entry of utterances, training,
testing, and evaluating the accuracy, you can create better models.
Introducing Entities
The entity represents a word or phrase inside the utterance that you want extracted. An utterance can
include many entities or none at all. An entity represents a class including a collection of similar objects
(places, things, people, events or concepts). Entities describe information relevant to the intent, and
sometimes they are essential for your app to perform its task. For example, a News Search app may
include entities such as “topic”, “source”, “keyword” and “publishing date”, which are key data to search
for news. In a travel booking app, the “location”, “date”, “airline”, "travel class" and “tickets” are key
information for flight booking (relevant to the "Book flight" intent). By comparison, the intent represents
the prediction of the entire utterance.
In the topic on utterances, we mentioned that there were words in the utterances that shared a common
theme. These words, such as pics, images, photos, etc. make great candidates for entities. While intents
5 https://docs.microsoft.com/en-us/azure/cognitive-services/luis/luis-boundaries#model-boundaries
MCT USE ONLY. STUDENT USE PROHIBITED
Build Intents and Utterances 79
are required, entities are optional. You do not need to create entities for every concept in your app, but
only for those required for the client application to take action. You label or mark entities for the purpose
of entity extraction only, it does not help with intent prediction.
LUIS offers many types of entities. Choose the entity based on how the data should be extracted and how
it should be represented after it is extracted.
Composite Entity
A composite entity is made up of other entities, such as prebuilt entities, simple, regular expression, list,
and hierarchical entities. The separate entities form a whole entity.
Hierarchical entity
A hierarchical entity is a category of contextually learned simple entities called children.
List Entity
Fixed and closed set of related words, including synonyms.
Pattern.any entity
Pattern.any is a variable-length placeholder used only in a pattern's template utterance to mark where
the entity begins and ends.
Example
Given a client application that searches for books based on title, the pattern.any extracts the complete
title. A template utterance using pattern.any for this book search is Was {BookTitle} written by an Ameri-
can this year[?].
MCT USE ONLY. STUDENT USE PROHIBITED 80 Module 3 Learn How to Create Language Understanding with LUIS
Prebuilt entity
Prebuilt entities are built-in types that represent common concepts such as email, URL, and phone
number. Prebuilt entity names are reserved. All prebuilt entities that are added to the application are
returned in the endpoint prediction query if they are found in the utterance.
Simple entity
A simple entity is a generic entity that describes a single concept and is learned from the ma-
chine-learned context. Because simple entities are generally names such as company names, product
names, or other categories of names, add a phrase list when using a simple entity to boost the signal of
the names used.
Create entities
Next, let's create the entities we need. In this case, we'll create an entity that can capture a specific ask by
a user, for example, when the user requests to search the pictures, they may specify what they are looking
for.
1. Click or Select on Entities in the left-hand column and then Click or Select Create new entity.
2. Give it an entity name “facet” and entity type Simple. Then Click or Select Done.
3. Create a new Intent called “SearchPics”. Use the same steps as above.
MCT USE ONLY. STUDENT USE PROHIBITED 82 Module 3 Learn How to Create Language Understanding with LUIS
3. Add two more Intents with related utterances according to this list:
●● SharePic - “Share this pic”, "Can you tweet that?", “post to Twitter”
●● OrderPic - “Print this picture”, "I would like to order prints", "Can I get an 8x10 of that one?",
“Order wallets”
MCT USE ONLY. STUDENT USE PROHIBITED
Build Intents and Utterances 83
4. To finish out the Intents and entities exercise, add some utterances to the existing None intent, that
don't match the context of this LUIS app. Examples are:
●● “I'm hungry for pizza”
●● “Search videos”
●● “Show me how to drive a car”
MCT USE ONLY. STUDENT USE PROHIBITED 84 Module 3 Learn How to Create Language Understanding with LUIS
Lab
Lab 6: Implement the LUIS Model
In this lab you will build the logic into a LUIS Service that helps your Bot, which will be integrated later, to
search for images in the system.
Lab Objectives
●● Create a LUIS Service
●● Add Intelligence to your LUIS Service
●● Train and Test the LUIS Service
MCT USE ONLY. STUDENT USE PROHIBITED
Module 4 Enhance Your Bot with LUIS
Once you select your environment and Click or Select Publish, you should see a green header show up
indicated that publishing is complete. There is also a hyperlink that when Click or Selected, will take you
to the list of endpoints for the LUIS app. This is where you get the necessary endpoint keys and URL for
accessing that particular LUIS app.
The endpoint is the URL in the lower right that you will need to provide to your application in order to
locate the application service on the Internet. You will also need to have the authoring key to be able to
authorize access to the service endpoint and use the application. If you want to make this LUIS app
public so that anyone with the valid key can query the service, you simply open the Application Informa-
tion page and slide the Not Public button so that it indicates Public.
MCT USE ONLY. STUDENT USE PROHIBITED
Overview of Language Understanding in AI Applications 87
Selecting the Keys and Endpoints option displays the authoring key and the endpoint URL that will serve
as the host name. Note that the key you see on this screen is indicated as an Authoring Key. The
Authoring Key permits you to do testing in the free tier but you will need to change this to your endpoint
key when your traffic exceeds the free tier quota.
You can paste these values into the Application Settings for the bot service by opening the Application
Settings blade for the bot service and locating the three entries in the App settings section.
MCT USE ONLY. STUDENT USE PROHIBITED
Overview of Language Understanding in AI Applications 89
If you have created a bot using the Azure Web App Bot template and either downloaded the code for use
in Visual Studio, you can connect LUIS and the bot automatically because the source code that is generat-
ed, automatically contains the necessary information to work with your LUIS service that was created as a
part of the bot creation steps. The only aspect you need to configure in the downloaded source code is
in the appsettings.json file. You put the botFileSecret and botFilePath values in this file.
{
"botFileSecret": "",
"botFilePath": ""
You get these values from the Application Setting blade for your bot service.
MCT USE ONLY. STUDENT USE PROHIBITED 90 Module 4 Enhance Your Bot with LUIS
MCT USE ONLY. STUDENT USE PROHIBITED
Integrating LUIS and Bots 91
6. Leave this browser tab open. For any steps with the LUIS portal, open a new browser tab. Continue to
the next section when the new bot service is deployed.
/// <summary>
/// Run every turn of the conversation. Handles orchestration of messages.
/// </summary>
/// <param name="turnContext">Bot Turn Context.</param>
/// <param name="cancellationToken">Task CancellationToken.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous opera-
tion.</returns>
public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken
cancellationToken)
{
var activity = turnContext.Activity;
if (activity.Type == ActivityTypes.Message)
{
// Perform a call to LUIS to retrieve results for the current
activity message.
var luisResults = await _services.LuisServices[LuisConfiguration].
RecognizeAsync(turnContext, cancellationToken).ConfigureAwait(false);
The bot sends the user's utterance to LUIS and gets the results. The top intent determines the conversa-
tion flow.
Copy
{
"botFileSecret": "",
"botFilePath": ""
}
2. Set the values of the variables to the values you copied from the Azure bot service's Application
Settings in Step 1 of the Download the web app bot section.
Note: Your json file may have placeholder text between the double quotes, simple replace that placehold-
er text with the values
3. In Visual Studio, start the bot. A browser window opens with the web app bot's web site at http://
localhost:3978/.
MCT USE ONLY. STUDENT USE PROHIBITED
Integrating LUIS and Bots 95
Notice that the period, ., between the domain and the intent from the LUIS portal's app is replaced with
an underscore, _.
2. Find the OnTurnAsync method that receives the LUIS prediction of the utterance. Add code in the
switch statement to return the LUIS response for the two HomeAutomation intents.
MCT USE ONLY. STUDENT USE PROHIBITED 96 Module 4 Enhance Your Bot with LUIS
case TurnOnIntent:
await turnContext.SendActivityAsync("TurnOn intent found, JSON re-
sponse: " + luisResults?.Entities.ToString());
break;
case TurnOffIntent:
await turnContext.SendActivityAsync("TurnOff intent found, JSON re-
sponse: " + luisResults?.Entities.ToString());
break;
The bot doesn't have the exact same response as a LUIS REST API request so it is important to learn the
differences by looking at the response JSON. The text and intents properties are the same but the entities
property values have been modified.
{
"$instance": {
"HomeAutomation_Device": [
{
"startIndex": 23,
"endIndex": 29,
"score": 0.9776345,
"text": "lights",
"type": "HomeAutomation.Device"
}
],
"HomeAutomation_Room": [
{
"startIndex": 12,
"endIndex": 22,
"score": 0.9079433,
"text": "livingroom",
"type": "HomeAutomation.Room"
}
]
},
"HomeAutomation_Device": [
"lights"
],
"HomeAutomation_Room": [
"livingroom"
]
}
Lab
Lab 7: Integrate LUIS into Bot Dialogs
Our bot is now capable of taking in a user's input and responding based on the user's input. Unfortunate-
ly, our bot's communication skills are brittle. One typo, or a rephrasing of words, and the bot will not
understand. This can cause frustration for the user. We can greatly increase the bot's conversational
abilities by enabling it to understand natural language with the LUIS model we built
Lab Objectives
●● Add LUIS to your C# Code
●● Add LUIS to the PictureBot Dialog
●● Test the LUIS Service
MCT USE ONLY. STUDENT USE PROHIBITED
Module 5 Integrate Cognitive Services with
Bots and Agents
●● Language Detection - If your business is a global one, and you are doing business in regions where
languages other than English are spoken, your bot may not work at all if you only code it to work with
English or any single language. If a user types an initial greeting into your bot, it would be a good
idea to check the language the user is utilizing. You can take a different approach depending on
how to intend to support your users. For example, you could simply make use of the language
detection services in Azure Cognitive Services to detect any language not supported by your bot and
to respond to the user in that language, simply indicating that the bot only supports a single lan-
guage. Alternatively, you could elect to provide language translation in a more complex bot scenario
that uses language detection and then translation services to communicate with the user in their
native language.
●● Language Understanding - integrate your bot with LUIS, as you have learned in previous lessons, to
help your bot understand a user's intent through the natural language processing features available in
LUIS.
Another scenario might present it self where a user is interacting with a bot in a conversational manner
and is using the camera on their computer. The bot can interact with the camera and take snapshots of
the user, sending the images to the emotion detection APIs to determine if the user is getting upset, or
experiencing some other emotion, allowing the bot to respond in a more appropriate manner. Perhaps
they user is getting agitated with the bot and you should pass them off to a human customer-service
representative.
You can likely think of many more scenarios where the use of images can greatly improve the experience
with bots. The integration of the computer vision APIs can help you achieve this.
MCT USE ONLY. STUDENT USE PROHIBITED 102 Module 5 Integrate Cognitive Services with Bots and Agents
{
"documents": [
{
"language": "en",
"id": "1",
"text": "We love this trail and make the trip every year.
The views are breathtaking and well worth the hike!"
},
{
"language": "en",
"id": "2",
"text": "Poorly marked trails! I thought we were goners.
Worst hike ever."
},
{
"language": "en",
"id": "3",
"text": "Everyone in my family liked the trail but thought
it was too challenging for the less athletic among us. Not necessarily
recommended for small children."
},
{
"language": "en",
"id": "4",
"text": "It was foggy so we missed the spectacular views,
but the trail was ok. Worth checking out if you are in the area."
},
{
"language": "en",
MCT USE ONLY. STUDENT USE PROHIBITED
Sentiment for Bots with Text Analytics 103
"id": "5",
"text": "This is my favorite trail. It has beautiful views
and many places to stop and rest"
}
]
}
Note that the language elements is indicated as en for US English. If you expect to support multiple
languages in your bot, and you want to use sentiment analysis in the supported languages, you should
perform language detection first, and use the result to ensure you provide the proper language code.
Language detection is covered in the next lesson.
5. Once the service is created, your will require one of the keys that are available on the keys blade for
resource management. This is used to authenticate a request to the service, just like many of the
other Azure Cognitive Services. Copy one of the keys to the clipboard.
6. Head over the API testing console for Text Analytics1.
7. On that page, select the region where you created the service. In our example, we created the service
in the West US 2 location so we would need to also select that region for the testing console.
1 https://westus.dev.cognitive.microsoft.com/docs/services/TextAnalytics.V2.0/operations/56f30ceeeda5650db055a3c9
MCT USE ONLY. STUDENT USE PROHIBITED
Sentiment for Bots with Text Analytics 105
8. Once the API Testing Console opens, ensure that you have one of your keys copied and then paste it
into the Ocp-Apim-Subscription-Key location on this page. This will be used to authenticate the
request to the service. Also, remember that the key is appropriate only for the region specified in
your service. If you create the service in a region that is different from that used for the API Testing
Console, authentication will fail.
9. There is already some sample text in the request body so you can use that for your initial testing to
see what the responses look like. Simply Click or Selecting Send on the page will execute the test for
you and you should receive a 200 OK response along with a JSON response at the bottom of the
page. As an example, the request body consisted of the following JSON that was sent.
{
"documents": [
{
"language": "en",
"id": "1",
"text": "Hello world. This is some input text that I love."
},
{
"language": "fr",
"id": "2",
"text": "Bonjour tout le monde"
},
{
"language": "es",
"id": "3",
"text": "La carretera estaba atascada. Había mucho tráfico el día de
ayer."
}
]
}
MCT USE ONLY. STUDENT USE PROHIBITED 106 Module 5 Integrate Cognitive Services with Bots and Agents
10. Note that there three samples in this body. One in English, one in French, and one in Spanish. This
demonstrates the ability of the sentiment analysis to work across these languages. Remember that
the service cannot detect the language itself, you must provide the language code in the JSON body.
Once the Send button is Click or Selected, the result is returned and looks like this.
Transfer-Encoding: chunked
x-ms-transaction-count: 3
CSP-Billing-Usage: CognitiveServices.TextAnalytics.BatchScoring|3
x-aml-ta-request-id: 576a9ece-5721-4ed2-8baf-29dd5477320b
X-Content-Type-Options: nosniff
apim-request-id: c46ca189-3047-42fc-bc37-242026a5d170
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
Date: Wed, 06 Mar 2019 17:11:20 GMT
Content-Type: application/json; charset=utf-8
{
"documents": [{
"id": "1",
"score": 0.98690706491470337
}, {
"id": "2",
"score": 0.84012651443481445
}, {
"id": "3",
"score": 0.334433376789093
}],
"errors": []
}
The important aspects are found in the JSON formated area of the response. You can see that the first
sentence returned a value of 0.98 which indicates a positive sentiment. The second, 0.84 is still on the
positive side as it is closer to the value 1 but the last sentence is only 0.33 which indicates a less positive
sentiment.
Also note that the response uses the same IDs that you provided in the request body, to help you align
the responses. You will make use of the ID values in your code to determine the sentiment to sentence
mapping, in the event your submitted multiple sentences as we did in this test.
If you want to test the service with an application, you can find some code samples on the API landing
page if you scroll to the bottom. An example of C# code that you could use in a .NET console-based
application would be:
using System;
using System.Net.Http.Headers;
using System.Text;
using System.Net.Http;
using System.Web;
namespace CSHttpClientSample
{
MCT USE ONLY. STUDENT USE PROHIBITED
Sentiment for Bots with Text Analytics 107
// Request headers
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key",
"{subscription key}");
HttpResponseMessage response;
// Request body
byte[] byteData = Encoding.UTF8.GetBytes("{body}");
}
}
}
All you need to do is replace the {subscription key} value with your copied key from the service and
replace the {body} value with your text that you want to send to the service. Note that the service is
called in an asynchronous fashion.
MCT USE ONLY. STUDENT USE PROHIBITED 108 Module 5 Integrate Cognitive Services with Bots and Agents
{
"documents": [
{
"id": "1",
"text": "This document is in English."
},
{
"id": "2",
"text": "Este documento está en inglés."
},
{
"id": "3",
"text": "Ce document est en anglais."
},
{
"id": "4",
"text": "本文件为英文"
},
{
"id": "5",
"text": "Этот документ на английском языке."
}
]
}
The service will return a JSON response that contains the IDs provided in the request body along with a
value indicating the confidence level of the detected language. The confidence level is a value ranging
from 0 to 1 with values closer to 1 being a higher confidence level. The JSON response is also formatted
a little differently that what you saw in the sentiment analysis. The reason is that your document may
contain a mix of languages. Let's evaluate that with a standard JSON response that maps to the above
request JSON.
{
"documents": [
MCT USE ONLY. STUDENT USE PROHIBITED
Detect Language in a Bot 109
{
"id": "1",
"detectedLanguages": [
{
"name": "English",
"iso6391Name": "en",
"score": 1
}
]
},
{
"id": "2",
"detectedLanguages": [
{
"name": "Spanish",
"iso6391Name": "es",
"score": 1
}
]
},
{
"id": "3",
"detectedLanguages": [
{
"name": "French",
"iso6391Name": "fr",
"score": 1
}
]
},
{
"id": "4",
"detectedLanguages": [
{
"name": "Chinese_Simplified",
"iso6391Name": "zh_chs",
"score": 1
}
]
},
{
"id": "5",
"detectedLanguages": [
{
"name": "Russian",
"iso6391Name": "ru",
"score": 1
}
]
}
MCT USE ONLY. STUDENT USE PROHIBITED 110 Module 5 Integrate Cognitive Services with Bots and Agents
],
Note how the response if formulated in this JSON file. There is a documents array that contains the IDs
of the documents that were sent in the request but also note that there are nested arrays of “detected
languages” for each ID. In this case, there is only one language in the detectedLanguages array. Each
detected language is identified using a name, and ISO code with the letter designator, and a confidence
score. In our sample, all of the languages show a confidence of 1, mostly because the text is relatively
simple and easy to identify the language for.
If we were to pass in a document that had mixed content, from a language perspective, the service will
behave a bit differently. Mixed language content within the same document returns the language with
the largest representation in the content, but with a lower positive rating, reflecting the marginal strength
of that assessment. In the following example, input is a blend of English, Spanish, and French. The
analyzer counts characters in each segment to determine the predominant language.
{
"documents": [
{
"id": "1",
"text": "Hello, I would like to take a class at your University. ¿Se
ofrecen clases en español? Es mi primera lengua y más fácil para escribir.
Que diriez-vous des cours en français?"
}
]
}
You can likely determine for yourself what the outcome will be for this evaluation. If the service counts
the number of characters in each segment, we can deduce that Spanish will have the most in terms of
characters and as a result, it would be the “predominant” language in the text. As you might expect, the
following sample shows a returned result for this multi-language example.
{
"documents": [
{
"id": "1",
"detectedLanguages": [
{
"name": "Spanish",
"iso6391Name": "es",
"score": 0.9375
}
]
}
],
"errors": []
}
The last condition to consider is when there is ambiguity as to the language content. This might happen
if you submit textual content that the analyzer is not able to parse. As a result, the response for the
language name and ISO code will indicate (unknown) and the score value will be returned as NaN, or Not
a Number. The following example shows how the response would look.
MCT USE ONLY. STUDENT USE PROHIBITED
Detect Language in a Bot 111
{
"id": "5",
"detectedLanguages": [
{
"name": "(Unknown)",
"iso6391Name": "(Unknown)",
"score": "NaN"
}
]
2 https://westus.dev.cognitive.microsoft.com/docs/services/TextAnalytics.V2.0/operations/56f30ceeeda5650db055a3c7
MCT USE ONLY. STUDENT USE PROHIBITED 112 Module 5 Integrate Cognitive Services with Bots and Agents
1. In this sample we are passing in some simple text again. The first is English text, the next is a simple
greeting in Indonesian, number 3 is Spanish text, but the last item is actually a series of emoticons.
2. Click or Select Send to send this to the service and see what the results are.
{
"documents": [{
"id": "1",
"detectedLanguages": [{
"name": "English",
"iso6391Name": "en",
"score": 1.0
}]
}, {
"id": "2",
"detectedLanguages": [{
"name": "Indonesian",
"iso6391Name": "id",
"score": 1.0
}]
}, {
"id": "3",
"detectedLanguages": [{
"name": "Spanish",
"iso6391Name": "es",
"score": 1.0
}]
}, {
"id": "4",
"detectedLanguages": [{
"name": "English",
"iso6391Name": "en",
"score": 1.0
}]
}],
"errors": []
}
As we expected, the first entry was in English, the second is Indonesian, the third Spanish, but the fourth
might not be what you expected. It is returned as English with a confidence score of 1.0, which indicates
the analyzer is positive it is English. In the current iteration of the analyzer, emoticons will be detected as
English rather than unknown or some other language.
Similar to the Sentiment Analysis service, you can find sample code for different application types at the
bottom of the API landing page. The samples are available for Curl, C#, Java, JavaScript, Objective-C,
PHP, Python, and Ruby, which helps to cover a broad range of programming languages and environ-
ments.
Remember that the returned JSON is what your application will need to parse to take action on the
results from the service.
MCT USE ONLY. STUDENT USE PROHIBITED
Lab 113
Lab
Lab 8: Language Detection in a Bot
Users may interact with your bot in their native language. Your bot should provide for this scenario and
react by either changing the language used for responses or simply indicate that their entered language
is not supported. Either way, you will need to determine which language the user is entering initially.
Lab Objectives
●● Implement Language Detection with a Bot