...
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 at in the top left top to start your first project. For our framework to be able to communicate with this agent, the project ID and a keyfile are required. Press the settings icon next to your agent's name at the left top to see the Project ID. Click on the Project ID itself in order to generate the project, and then, in order to get the corresponding JSON keyfile, follow the steps given here.The 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' here(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. A context is set by the requester in order to indicate that we only want to recognize this specific intent, and not another one. UsuallyContexts 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 kind of answer we want to get is known. We 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 makes keeps the context 'stick' active for 5 answersexchanges; 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 'given namesys.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
Info |
---|
Before running |
Let's create a simple service demo that prints the transcript and the intent detected by Dialogflow.
Start by importing the necessary SIC functionality.functions:
Code Block |
---|
from sic_framework.devices import Nao from sic_framework.devices.nao import NaoNaoqiTextToSpeechRequest from sic_framework.services.dialogflow.dialogflow_service import DialogflowService(DialogflowConf, DialogflowConfGetIntentRequest, GetIntentRequest, \RecognitionResult, RecognitionResultQueryResult, QueryResult |
...
Dialogflow) |
First, we create a connection to the Nao robot to be able to access its microphone (using nao. mic after it is initialized). In order to use this intent we created earlier in an application, we need to set the language, project ID (agent name), keyfile and the keyfilesample 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 files and ID key file for your Dialogflow project.
Code Block |
---|
class DemoDialogflow(SICApplication): def run(self) -> None: # connect to the robot nao = Nao(device_idip='nao', application=self192.168.178.45') # load the key json conffile keyfile_json = DialogflowConf(keyfile="dialogflow-keyjson.load(open("../../conf/dialogflow/dialogflow-tutorial.json",)) # set up the config conf project_id='dialogflow-test-project-376814', = DialogflowConf(keyfile_json=keyfile_json, sample_rate_hertz=16000) |
Having done this setup, we can self.connect
to the Dialogflow service (make sure it is up and running, or you will get a timeout). The parameters inputs_to_service=[nao.mic]
initiate the Dialogflow object and connect the output of NaoqiMicrophone
as the input of Dialogflow. The parameters ip='localhost'
and conf=conf
pass the Nao microphone as an input the ip address of the device the DialogflowComponent
is running on and our configuration to be able to authenticate to Dialogflow.
Code Block |
---|
# initiate Dialogflow object dialogflow = self.connect(DialogflowService, device_idDialogflow(ip='locallocalhost', conf=conf) # connect the output of NaoqiMicrophone as the input of inputs_to_service=[DialogflowComponent dialogflow.connect(nao.mic], conf=conf)) |
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. TheThen, on_recognition_resultdialog
function simply prints the detected speech when it’s considered final
.
Code Block |
---|
# register a callback function to act upon arrival of recognition_result dialogflow.register_callback(on_recognition_resultdialog) |
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_recognition_result
should print interim transcripts. Whenever you are done, and if Dialogflow successfully detected your intent, it should print it on screen! The Dialogflow agent’s response should also be printeddialog
function should print the transcript in the terminal.
Code Block |
---|
chat_idx = np.random.randint(10000) for i in range(25): print(" ->---- Conversation turn", i) reply = dialogflow.request(GetIntentRequest(chat_idx)) print(reply.intent) if reply.fulfillment_message: iftext = reply.fulfillment_message: print("Reply:", reply.fulfillment_messagetext) if reply.response.query_result.intent: print("Intent:", reply.response.query_result.intent.display_namenao.tts.request(NaoqiTextToSpeechRequest(text)) |
Here is the definition for on_recognition_resultdialog
Code Block |
---|
def on_recognition_resultdialog(message): if message.response: if print(message.response.recognition_result.transcript) |
To start your application, we need to include
Code Block |
---|
if __name__ == '__main__':is_final: test_app = DemoDialogflow() test_app.run(print("Transcript:", message.response.recognition_result.transcript) |
And that's it! You should now be able to talk to your robot. See also https://bitbucket.org/socialroboticshub/docker/src/v3/sic/sic_framework/tests/demo_dialogflow.py this demo script for a more complex complete example. Make sure to set your own agent name and the proper keyfile path!