Dialogflow v2

The dialogflow service enables the use of the Google Dialogflow platform within your application.

Dialogflow is used to translate human speech into intents (intent classification). In other words, not only does it (try to) convert an audio stream into readable text, it also classifies this text into an intent and extracts additional parameters called entities from the text, if specified. For example, an audio stream can be transcribed to the string "I am 15 years old", and classified as the intent 'answer_age' with entity 'age=15'.

In order to create a Dialogflow agent, visit https://dialogflow.cloud.google.com and log-in with a Google account of choice. Use the 'Create Agent' button in the top left to start your first project. For our framework to be able to communicate with this agent, the project ID and a keyfile are required. To get a keyfile read the instructions Getting a google dialogflow key. Once you have a keyfile, place it inside the conf/dialog-flow folder in your local repository.

In Dialogflow, the main items of interest are the Intents and the Entities. An intent is something you want to recognize from an end-user; here we will show you an example of an intent that is aimed at recognizing someone’s name.

When creating an intent you can name it anything you like; In this example we go with 'answer_name' (seen at the very top). Below 'Action and parameters', you should give the name of the intent that will actually be used in your program. Here, we also set that to 'answer_name'.

Moreover, it is useful to set a context for the intent. Contexts allow you to define specific states that a conversation must be in for an intent to match. You can also have intents activate contexts to help direct the conversation in future exchanges. Usually though, in a social robotics application, the context is already known. So in this example we match the name of the (input)context with the name of the intent, and thus make it 'answer_name' as well. By default, Dialogflow keeps the context active for 5 exchanges; but we can fix this by changing the 5 (at the start of the output context) to a 0.

Now we arrive at the most important aspect of the intent: the training phrases. Here, you can give the kinds of input strings you would expect; from these, Dialogflow learns the model it will eventually use. You can identify a part of the phrase as a parameter by double-clicking on the relevant word and selecting the appropriate entity from the list. It will then automatically appear below ‘Action and parameters' as well; the ‘parameter name’ there will be passed in the result (we use ‘name’ here). The system has many built-in entities (like 'sys.person'), but you can define your own entities as well (even through importing CSV files). Our complete intent example thus looks like this (note: using sys.given-name is usually preferred):

Using the Dialogflow component with Nao microphone

Before running demo_nao_diaglogflow.py, make sure to start the SIC service by running ‘run-dialogflow’ in another terminal. You may have to run 'pip install social-interaction-cloud[dialogflow]' beforehand.

Let's create a simple demo that prints the transcript and the intent detected by Dialogflow.

Start by importing the necessary SIC functions:

from sic_framework.devices import Nao from sic_framework.devices.nao import NaoqiTextToSpeechRequest from sic_framework.services.dialogflow.dialogflow import (DialogflowConf, GetIntentRequest, RecognitionResult, QueryResult, Dialogflow)

First, we create a connection to the Nao robot to be able to access its microphone. In order to use this intent we created earlier in an application, we need to set the keyfile and the sample rate. To do this, we create the Dialogflow configuration object. Make sure you read the documentation at the start of this page to obtain the key file for your Dialogflow project.

# connect to the robot nao = Nao(ip='192.168.178.45') # load the key json file keyfile_json = json.load(open("../../conf/dialogflow/dialogflow-tutorial.json")) # set up the config conf = DialogflowConf(keyfile_json=keyfile_json, sample_rate_hertz=16000)

Having done this setup, we can initiate the Dialogflow object and connect the output of NaoqiMicrophone as the input of Dialogflow. The parameters ip='localhost' and conf=conf pass the ip address of the device the DialogflowComponent is running on and our configuration to be able to authenticate to Dialogflow.

# initiate Dialogflow object dialogflow = Dialogflow(ip='localhost', conf=conf) # connect the output of NaoqiMicrophone as the input of DialogflowComponent dialogflow.connect(nao.mic)

Finally, we need to register a callback function to act whenever Dialogflow output is available. Whenever Dialogflow detects a new word, we will receive a RecognitionResult message. Then, on_dialog function simply prints the detected speech when it’s considered final.

Now we can start actually getting intents from the user! We need to set a chat ID, with which Dialogflow identifies the conversation. This can be a random number (or the same one if you want to continue a conversation). Then, we request Dialogflow to get an intent. It will start sending the Nao’s microphone audio to Dialogflow. As you start talking, the SIC on_dialog function should print the transcript in the terminal.

Here is the definition for on_dialog

And that's it! You should now be able to talk to your robot. See also this demo script for a more complete example. Make sure to set the proper keyfile path!