Skip to content

Latest commit

 

History

History
112 lines (85 loc) · 3.98 KB

technical_guide.md

File metadata and controls

112 lines (85 loc) · 3.98 KB

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


What is DialogFlow?

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.

The way it can be used: alt text

Implementation

1. In DialogFlow

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:

alt text

Adding intents in Dialogflow

alt text

2. In Text Editor

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

3. In Gemfile

gem 'google-cloud-dialogflow', '~> 0.2.2'

4. In .env file

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.

5. In Terminal

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