Countdown to Christmas Makes Challenge 2017 – Day 7

Project:

Ask an Elf / ElfBot
(Using IBM Watson and Python!)

To recreate this project you need to have a IBM Cloud Account (aka IBM Bluemix). Don’t worry if you haven’t got an account you can create one for free here. https://www.ibm.com/watson/services/conversation/

Once you’ve logged in your now ready to start. Under your “MyIBM” page, navigate under “Products and Services” and activate “IBM Bluemix”. It will ask you again to log in, so enter your username and password. Now you should be logged into “IBM Cloud”. Click on “Conversation-yn”, which will take you to the “Conversation-yn” page. From there just click “Launch Tool”. Now you should be on the “Workspaces” page. From here you just need to import the ElfBot JSON file. To do this just lick the “Import” button to the right of the blue “Create” Button. That’s it, ElfBot has been imported, and is now ready to use!

Download: ElfBot JSON file for Watson Conversation

Workspaces:

Credentials:

This page contains the API details required for connecting to the ElfBot. You will need to transfer the “Username”, “Password” and “Workspace ID” to the python script below.

Intents:

The intents are the list of questions that you want the chatbot to answer. They are grouped into tags – containing questions that all require the same answer. When the chatbot goes to respond it will look at the question and fit it into one of these tags to give the correct answer. Here is a look at my intents for the ElfBot.

Entities:

The only entity that I use for this chatbot is the built in system name entity “@sys-person”. This can be activated from the, “System entites” tab on the entities page and then used for the dialogue. Note: “@sys-person” should be switched “ON”.

Dialog:

The dialogue page is where the answer to the questions are written and stored. You will create a node for each question group and link these up to the intent tag, containing the question you want to answer. You can then write the correct answers to the questions within these nodes. Once this is done the chatbot will then fit each question asked to an intent, take the corresponding answer from the dialogue and then output the response. Here is a look at the nodes that I used.

Code:
Python code to interface with Watson Conversation.
Note: Before you run the code you will need to install the Watson Developer Cloud library.
pip install --upgrade watson-developer-cloud

# Ask-an-Elf / ElfBot by @KariLawler (Twitter), 07/Dec/17, v1.0
# Written in Python 3

import json
import watson_developer_cloud

# set up API credentials
conversation = watson_developer_cloud.ConversationV1(
	username = '<username_goes_here>',
	password = '<password_goes_here>',
	version = '2017-05-26'
)

while True:
	# get question from user
	question = input("Question? ")
	# send question to Watson and get answer
	response = conversation.message(
		workspace_id = '<workspace_id_goes_here>',
		input = {'text': question }
	)
	# print answer
	answer = response['output']['text']
	print(*answer)

Leave a Reply