Conversational Understanding Model with MS Azure

Introduction

We, in this project, aim to create a conversational understanding model with MS Azure. A conversational understanding model must aim to predict the intent the of the user typing in the input and then accordingly perform the desired function/print the desired output.

The overall workflow of the model would look something like:

  • Take input from the user
  • Determine the intent of the user.
  • Print the result/ perform the desired action

Creating an Azure AI language resource

  1. Search for "Language Service" in the search bar.
  2. Click on create and provide all the required details to create your language service.
  3. Note the key and endpoint URL of the language service available under the resource overview page.

Use the MS Azure Language Studio

  1. In a new browser tab, open the language studio by using the URL: "https://language.cognitive.azure.com/" and then sign in using your Azure credentials.
  2. Fill in the required details to connect your language studio to the language resource you just created earlier.

Select an Azure Resource

 

Creating a Language Project

  1. In the language studio, click on create a new project
  2. Fill in all the required details.

Creating a Language Project

 

Creating Intents, Entities, and Learned Entities

  1. On the schema definition page of your language model, click on add a new intent and accordingly create intents to return desired outputs.
  2. Similarly, create desired entities and learned entities.

Note: For a more detailed discussion on the above topic, refer to the video attached to this article.

Schema Definition  

Schema Definition

Training Our Model

  1. Go to the "Training jobs" tab in the left hand side panel.
  2. Click on Create a "Start a training job".
  3. Fill in the required details.
  4. Wait till the model training gets completed (this may take some while).

Start a training job    

Training Jobs

 

Deploying the Model

  1. Go to the "Deploying a model" tab in the left hand side section.
  2. Click on "Add a deployment".

Deploying Model Deploying model

Testing Deployments

  1. Go to the "testing deployments" tab in the left hand side section.
  2. Enter your query in the "enter text textbox" and the result will be displayed in "JSON" format.

Testing Deployments

Testing model  

Testing Our Service in VS Code

Note: we will be using Python language for the entirety of our tutorial

  1. Open VS Code in your device.
  2. Clone the following Git repository using the following URL: "https://github.com/MicrosoftLearning/AI-102-AIEngineer"
  3. Open the 10b-clu-client(review)
  4. Open python-->clock-client---->.env file
  5. Paste the endpoint URL and the primary key in the .env file.
  6. Pen the clock-client folder in an integrated terminal and paste the following code to install the required SDK:
     pip install azure-ai-language-conversations --pre
     python -m pip install python-dotenv
     python -m pip install python-dateutil
  7. Open the clock-client.py file and add the following code under the "namespaces" comment:
     # Import namespaces
     from azure.core.credentials import AzureKeyCredential
     from azure.ai.language.conversations import ConversationAnalysisClient
  8. Add the following code to create a client for your project:
     # Create a client for the Language service model
     client = ConversationAnalysisClient(
         ls_prediction_endpoint, AzureKeyCredential(ls_prediction_key))
  9. Paste this code to call the language service model entities and intents:
     # Call the Language service model to get intent and entities
     cls_project = 'LanguageProject' #put the name of your own project here 
     deployment_slot = 'newDeployment' #put you model deployment name here
    
     with client:
         query = userText
         result = client.analyze_conversation(
             task={
                 "kind": "Conversation",
                 "analysisInput": {
                     "conversationItem": {
                         "participantId": "1",
                         "id": "1",
                         "modality": "text",
                         "language": "en",
                         "text": query
                     },
                     "isLoggingEnabled": False
                 },
                 "parameters": {
                     "projectName": cls_project,
                     "deploymentName": deployment_slot,
                     "verbose": True
                 }
             }
         )
    
     top_intent = result["result"]["prediction"]["topIntent"]
     entities = result["result"]["prediction"]["entities"]
    
     print("view top intent:")
     print("\ttop intent: {}".format(result["result"]["prediction"]["topIntent"]))
     print("\tcategory: {}".format(result["result"]["prediction"]["intents"][0]["category"]))
     print("\tconfidence score: {}\n".format(result["result"]["prediction"]["intents"][0]["confidenceScore"]))
    
     print("view entities:")
     for entity in entities:
         print("\tcategory: {}".format(entity["category"]))
         print("\ttext: {}".format(entity["text"]))
         print("\tconfidence score: {}".format(entity["confidenceScore"]))
    
     print("query: {}".format(result["result"]["query"]))
  10. Now we want the model to detect entities and intents and if it is true in the input query, then the model should perform the appropriate action. To achieve this, paste the following code:
     # Apply the appropriate action
     if top_intent == 'GetTime':
         location = 'local'
         # Check for entities
         if len(entities) > 0:
             # Check for a location entity
             for entity in entities:
                 if 'Location' == entity["category"]:
                     # ML entities are strings, get the first one
                     location = entity["text"]
         # Get the time for the specified location
         print(GetTime(location))
    
     elif top_intent == 'GetDay':
         date_string = date.today().strftime("%m/%d/%Y")
         # Check for entities
         if len(entities) > 0:
             # Check for a Date entity
             for entity in entities:
                 if 'Date' == entity["category"]:
                     # Regex entities are strings, get the first one
                     date_string = entity["text"]
         # Get the day for the specified date
         print(GetDay(date_string))
    
     elif top_intent == 'GetDate':
         day = 'today'
         # Check for entities
         if len(entities) > 0:
             # Check for a Weekday entity
             for entity in entities:
                 if 'Weekday' == entity["category"]:
                 # List entities are lists
                     day = entity["text"]
         # Get the date for the specified day
         print(GetDate(day))
    
     else:
         # Some other intent (for example, "None") was predicted
         print('Try asking me for the time, the day, or the date.')
  11. Save changes to the whole project/file and run the following command in the terminal to run your clock-client.py file.
python clock-client.py

Alternative Method: Using Curl POST method in Command Prompt

  1. Go to deploying a model tab.
  2. Select your currently deployed model.
  3. Click on "get prediction URL" and paste the text in cmd by entering the appropriate details (See the reference video in this article for a detailed explanation)
  4. The output is returned in JSON format
curl -X POST "YOUR ENDPOINT URL HERE" -H "Ocp-Apim-Subscription-Key: YOUR KEY HERE"  -H "Apim-Request-Id: YOUR REQUEST ID HERE" -H "Content-Type: application/json" -d "{\"kind\":\"Conversation\",\"analysisInput\":{\"conversationItem\":{\"id\":\"1\",\"text\":\"what time is it?\",\"modality\":\"text\",\"language\":\"en\",\"participantId\":\"1\"}},\"parameters\":{\"projectName\":\"LanguageProject\",\"verbose\":true,\"deploymentName\":\"newDeployment\",\"stringIndexType\":\"TextElement_V8\"}}"

Attaching the reference video