Technical Guide:
How to use dialogueflow to extract intents from user inputs, map it to our code and give users output
by: Harshika Jain, MIIPS Adv 2018
Course: 49714-A1
Previously known as API.AI, this became known as DialogFlow after being acquired by Google. It offers NLP for building into a chatbot interface such as keyword matching, understanding human speech to derive intent and meaning etc.
For setting up Dialogflow, an account needs to be set up and an agent needs to be created. Here are the tutorials for the same:
- Create a dialogflow account: (https://dialogflow.com/docs/getting-started/create-account)
- Create an agent: (https://dialogflow.com/docs/getting-started/first-agent)
- Where to find your project_id (will be used later in .env file)
- A helpful video: (https://www.youtube.com/watch?v=2CVP6xe2ssk)
- How to make an intent screenshot
Example intent name: EventIntent
Training phrases: “upcoming events”, “events”, “upcoming birthdays”
- Also refer to the following code repo which has examples of adding dialogflow to ruby:
https://github.com/GoogleCloudPlatform/ruby-docs-samples/blob/master/dialogflow/detect_intent_texts.rb
Require "securerandom"
def detect_intet_texts project_id:, session_id:, texts:, language_code:
# [START dialogflow_detect_intent_text]
# project_id = "Your Google Cloud project ID"
# session_id = "mysession"
# texts = "hello", "book a meeting room"]
# language_code = "en-US"
require "google/cloud/dialogflow"
session_client = Google::Cloud::Dialogflow::Sessions.new
session = session_client.class.session_path project_id, session_id
puts "Session path: #{session}"
texts.each do |text|
query_input = { text: { text: text, language_code: language_code } }
response = session_client.detect_intent session, query_input
query_result = response.query_result
puts "Query text: #{query_result.query_text}"
puts "Intent detected: #{query_result.intent.display_name}"
puts "Intent confidence: #{query_result.intent_detection_confidence}"
puts "Fulfillment text: #{query_result.fulfillment_text}\n"
return query_result.intent.display_name
end
# [END dialogflow_detect_intent_text]
End
def determine_response (body)
body = body.downcase.strip
no = "i didnt quite understand what you mean"
project_id = ENV["GOOGLE_CLOUD_PROJECT"]
intent = detect_intent_texts project_id: project_id,
session_id: SecureRandom.uuid,
texts: [body],
language_code:"en-US"
if intent == "EventIntent"
response = "no upcoming events"
end
return response
gem 'google-cloud-dialogflow', '~> 0.2.2'
GOOGLE_CLOUD_PROJECT = "your_project_id"
The api can do the rest of the work of finding your unique project id in the text editor.
gem install google-cloud-dialogflow
How to set up google cloud api in the Heroku environment variables:
- Works after cd and the file path
- This is done to ensure that intents from dialogflow can be received by heroku, sent to our code and the intent be mapped to the output
#check the existing config vars
heroku config
#set up a new variable, i.e. google cloud api
Heroku config:set GOOGLE_CLOUD_PROJECT="project_id"
#remember to take care of not having any space before and after "="
fin