CHATBOT (Dialogflow) used from Ruby

1.First of all

I will introduce how to use CHATBOT that can be used from ** Ruby **.

Use ** Dialogflow ** for CHATBOT. It is a service provided by google, and it seems easy to use once you get used to it. I have explained the settings of Dialogflow, but I have omitted the explanation of the meaning of items.

It seems that the demand for CHATBOT is increasing due to the corona virus, so I hope it will be helpful for those who are considering introducing CHATBOT to their services.

1-1. What is Dialogflow?

dialogflow https://cloud.google.com/dialogflow/docs > Dialogflow is a natural language understanding platform that facilitates the design of conversational user interfaces and integration into mobile apps, web applications, devices, bots, interactive voice response systems, and more. Dialogflow can be used to provide users with a new way of working.

plan

https://cloud.google.com/dialogflow/pricing The free plan has a frame of 180 requests / minute for text, and there is enough volume for verification.

upper limit

https://cloud.google.com/dialogflow/quotas Since the number of intents (conversations) is up to 2,000, it may not be enough for a large service, but this is also sufficient.

2. Create a CHATBOT with Dialogflow

First, create a CHATBOT with Dialogflow. If you have already made it, please skip it.

2-1. Creating an agent

Create an agent. An agent is a unit of CHATBOT. If you want to create another CHATBOT, it's like creating another agent.

Access DialogFlow

--Access the URL below and log in.

https://dialogflow.cloud.google.com/ dialogflow

Creating an agent

--Click ** [Create new agent] ** from the menu. --Enter the agent information. -Enter ** agent name **, ** LANGUAGE **, ** TIME ZONE . - GOOGLE PROJECT ** is automatically generated. A project for this agent will be generated in ** GCP (GoogleCloudPlatform) **, so be careful not to forget to delete the GCP project when deleting the agent.

dialogflow

-** Click the [CREATE] ** button to generate the agent.

Creating an intent

An intent is a conversation in a CHATBOT. When you create an agent, "** Default Welcome Intent **" is already automatically generated, so this time we will use it. I will omit the detailed explanation about the intent, so please google.

** About Default Welcome Intent ** Upon receipt of the conversation, such as "long time", "Hello", "Hi", is the intent to return to the "Hello!".

3. Get credentials

Generate a ** key file ** to access Dialogflow from a Ruby program.

3-1. Open Google Project

Visit Google Project

--Click the ** [Gears] ** button from the Dialogflow menu

dialogflow

-Click the link ** under the ** Project ID

dialogflow

3-2. Create a key

Access service account

--When the Google Project screen appears, click ** [IAM]> [Service Account] ** from the menu.

dialogflow

Create a service account

-** Click Create Service Account **. --Enter the service account details. -Enter the ** service account name ** and ** service account ID **.

dialogflow

-** Click the [Create] ** button. --Enter the permissions of the service account. -For ** Role **, select ** Dialogflow API Administrator **.

dialogflow

-** Click the [Continue] ** button. --On the next screen, click the ** [Finish] ** button without entering anything.

Create a key

--From the service account list screen, click ** [Create Key] ** from ** Operation ** of the created service account. --Select ** [JSON] ** on the Create Private Key screen and click ** [Create] **.

dialogflow

Then the JSON file will be downloaded. This JSON file is used when executing a Ruby program.

Let's call the downloaded file ** milky-agent-xxxxx.json **.

4. Create a Ruby program

It's finally about Ruby. The execution environment of Ruby is written on the assumption that it already exists.

First prepare the environment required for execution, and then execute the program.

4-1. Package installation

Setting environment variables

--Place the JSON file in an appropriate directory and register it in the environment variable. By registering in ** GOOGLE_APPLICATION_CREDENTIALS **, it will be automatically read when the program is executed.

/// milky-agent-xxxxx.json~/Suppose you put it in the tmp directory.
$ export GOOGLE_APPLICATION_CREDENTIALS=~/tmp/milky-agent-xxxxx.json

Package installation

--Install the gems needed to access Dialogflow.

$ gem install google-cloud-core
$ gem install google-cloud-dialogflow

4-2. Program

The program to be executed is written below. Copy ** project_id ** and ** session_id ** from your GCP project.

milkybot.rb


project_id = "[GCP project ID]"
session_id = "[Service account key ID]"
text = "Hello"
language_code = "jp"

require "google/cloud/dialogflow"

session_client = Google::Cloud::Dialogflow.sessions
session = session_client.session_path project: project_id,
                                      session: session_id

query_input = { text: { text: text, language_code: language_code } }
response = session_client.detect_intent session:     session,
                                        query_input: 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"

5. Run

--Run the program.

$ ruby milkybot.rb 
Query text:Hello
Intent detected:   Default Welcome Intent
Intent confidence: 1.0
Fulfillment text:Hello!

If you see the above, you are successful! Try changing ** text ** in the program to something like "after a long time" or "hi". I think you will get the same answer.

After that, if you register various conversations on Dialogflow, you can create your own favorite CHATBOT. The investigation was difficult, but when I tried it, it was surprisingly easy.

Recommended Posts

CHATBOT (Dialogflow) used from Ruby
From Java to Ruby !!
Try using Cocoa from Ruby
Use C program from Ruby
Use Face API from Ruby
[Ruby] What is `!!` used for?
[Ruby] Receive input from console
[Ruby] Escape from multiple loops [Nest]
Introduction to Ruby (from other languages)
Ruby methods often used in Rails