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
- Search for "Language Service" in the search bar.
- Click on create and provide all the required details to create your language service.
- Note the key and endpoint URL of the language service available under the resource overview page.
Use the MS Azure Language Studio
- 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.
- Fill in the required details to connect your language studio to the language resource you just created earlier.
Creating a Language Project
- In the language studio, click on create a new project
- Fill in all the required details.
Creating Intents, Entities, and Learned Entities
- On the schema definition page of your language model, click on add a new intent and accordingly create intents to return desired outputs.
- 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.
Training Our Model
- Go to the "Training jobs" tab in the left hand side panel.
- Click on Create a "Start a training job".
- Fill in the required details.
- Wait till the model training gets completed (this may take some while).
Deploying the Model
- Go to the "Deploying a model" tab in the left hand side section.
- Click on "Add a deployment".
Testing Deployments
- Go to the "testing deployments" tab in the left hand side section.
- Enter your query in the "enter text textbox" and the result will be displayed in "JSON" format.
Testing Our Service in VS Code
Note: we will be using Python language for the entirety of our tutorial
- Open VS Code in your device.
- Clone the following Git repository using the following URL: "https://github.com/MicrosoftLearning/AI-102-AIEngineer"
- Open the 10b-clu-client(review)
- Open python-->clock-client---->.env file
- Paste the endpoint URL and the primary key in the .env file.
- 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
- 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
- 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))
- 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"]))
- 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.')
- 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
- Go to deploying a model tab.
- Select your currently deployed model.
- 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)
- 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