In [1]:
import json
import sys
import os
sys.path.append(os.path.join(os.getcwd(),'..','..'))
import watson_developer_cloud
In [2]:
USERNAME = os.environ.get('CONVERSATION_USERNAME','<USERNAME>')
PASSWORD = os.environ.get('CONVERSATION_PASSWORD','<PASSWORD>')
In [4]:
conversation = watson_developer_cloud.ConversationV1(username=USERNAME,
password=PASSWORD,
version='2017-04-21')
Let's create and chat with a simple pizza bot. We'll start by defining the bot's workspace, then add intents and examples to recognize pizza orders. Once the chatbot is configured, we'll send a message to converse with our pizza bot.
In [5]:
# define the dialog for our example workspace
dialog_nodes = [{'conditions': '#order_pizza',
'context': None,
'description': None,
'dialog_node': 'YesYouCan',
'go_to': None,
'metadata': None,
'output': {'text': {'selection_policy': 'random',
'values': ['Yes You can!', 'Of course!']}},
'parent': None,
'previous_sibling': None,}]
# create an example workspace
workspace = conversation.create_workspace(name='example_workspace',
description='An example workspace.',
language='en',
dialog_nodes=dialog_nodes)
# print response and save workspace_id
print(json.dumps(workspace, indent=2))
workspace_id=workspace['workspace_id']
In [6]:
# add an intent to the workspace
intent = conversation.create_intent(workspace_id=workspace_id,
intent='order_pizza',
description='A pizza order.')
print(json.dumps(intent, indent=2))
In [7]:
# add examples to the intent
example1 = conversation.create_example(workspace_id=workspace_id,
intent='order_pizza',
text='Can I order a pizza?')
example2 = conversation.create_example(workspace_id=workspace_id,
intent='order_pizza',
text='I want to order a pizza.')
example3 = conversation.create_example(workspace_id=workspace_id,
intent='order_pizza',
text='pizza order')
example4 = conversation.create_example(workspace_id=workspace_id,
intent='order_pizza',
text='pizza to go')
print(json.dumps(example1, indent=2))
print(json.dumps(example2, indent=2))
print(json.dumps(example3, indent=2))
print(json.dumps(example4, indent=2))
In [8]:
# check workspace status (wait for training to complete)
workspace = conversation.get_workspace(workspace_id=workspace_id)
print('The workspace status is: {0}'.format(workspace['status']))
if workspace['status'] == 'Available':
print('Ready to chat!')
else:
print('The workspace should be available shortly. Please try again in 30s.')
print('(You can send messages, but not all functionality will be supported yet.)')
In [9]:
# start a chat with the pizza bot
message_input = {'text': 'Can I order a pizza?'}
response = conversation.message(workspace_id=workspace_id,
message_input=message_input)
print(json.dumps(response, indent=2))
In [10]:
# continue a chat with the pizza bot
# (when you send multiple requests for the same conversation,
# then include the context object from the previous response)
message_input = {'text': 'medium'}
response = conversation.message(workspace_id=workspace_id,
message_input=message_input,
context=response['context'])
print(json.dumps(response, indent=2))
The following examples demonstrate each operation of the Conversation service. They use the pizza chatbot workspace configured above.
In [11]:
message_input = {'text': 'Can I order a pizza?'}
response = conversation.message(workspace_id=workspace_id,
message_input=message_input)
print(json.dumps(response, indent=2))
In [12]:
response = conversation.create_workspace(name='test_workspace',
description='Test workspace.',
language='en',
metadata={})
print(json.dumps(response, indent=2))
test_workspace_id = response['workspace_id']
In [13]:
response = conversation.delete_workspace(workspace_id=test_workspace_id)
print(json.dumps(response, indent=2))
In [14]:
response = conversation.get_workspace(workspace_id=workspace_id, export=True)
print(json.dumps(response, indent=2))
In [15]:
response = conversation.list_workspaces()
print(json.dumps(response, indent=2))
In [16]:
response = conversation.update_workspace(workspace_id=workspace_id,
description='An example workspace for ordering pizza.')
print(json.dumps(response, indent=2))
In [17]:
response = conversation.create_intent(workspace_id=workspace_id,
intent='test_intent',
description='Test intent.')
print(json.dumps(response, indent=2))
In [18]:
response = conversation.delete_intent(workspace_id=workspace_id,
intent='test_intent')
print(json.dumps(response, indent=2))
In [19]:
response = conversation.get_intent(workspace_id=workspace_id,
intent='order_pizza',
export=True)
print(json.dumps(response, indent=2))
In [20]:
response = conversation.list_intents(workspace_id=workspace_id,
export=True)
print(json.dumps(response, indent=2))
In [21]:
response = conversation.update_intent(workspace_id=workspace_id,
intent='order_pizza',
new_intent='order_pizza',
new_description='Order a pizza.')
print(json.dumps(response, indent=2))
In [22]:
response = conversation.create_example(workspace_id=workspace_id,
intent='order_pizza',
text='Gimme a pizza with pepperoni')
print(json.dumps(response, indent=2))
In [23]:
response = conversation.delete_example(workspace_id=workspace_id,
intent='order_pizza',
text='Gimme a pizza with pepperoni')
print(json.dumps(response, indent=2))
In [24]:
response = conversation.get_example(workspace_id=workspace_id,
intent='order_pizza',
text='I want to order a pizza.')
print(json.dumps(response, indent=2))
In [25]:
response = conversation.list_examples(workspace_id=workspace_id,
intent='order_pizza')
print(json.dumps(response, indent=2))
In [26]:
response = conversation.update_example(workspace_id=workspace_id,
intent='order_pizza',
text='I want to order a pizza.',
new_text='I want to order a pizza with pepperoni.')
print(json.dumps(response, indent=2))
In [27]:
response = conversation.create_counterexample(workspace_id=workspace_id,
text='I want financial advice today.')
print(json.dumps(response, indent=2))
In [28]:
response = conversation.get_counterexample(workspace_id=workspace_id,
text='I want financial advice today.')
print(json.dumps(response, indent=2))
In [29]:
response = conversation.list_counterexamples(workspace_id=workspace_id)
print(json.dumps(response, indent=2))
In [30]:
response = conversation.update_counterexample(workspace_id=workspace_id,
text='I want financial advice today.',
new_text='I want financial advice for tomorrow.')
print(json.dumps(response, indent=2))
In [31]:
response = conversation.delete_counterexample(workspace_id=workspace_id,
text='I want financial advice for tomorrow.')
print(json.dumps(response, indent=2))
In [32]:
values = [{"value": "juice"}]
In [33]:
response = conversation.create_entity(workspace_id=workspace_id,
entity='test_entity',
description='A test entity.',
values=values)
print(json.dumps(response, indent=2))
In [34]:
response = conversation.get_entity(workspace_id=workspace_id,
entity='test_entity',
export=True)
print(json.dumps(response, indent=2))
In [35]:
response = conversation.list_entities(workspace_id=workspace_id)
print(json.dumps(response, indent=2))
In [36]:
response = conversation.update_entity(workspace_id=workspace_id,
entity='test_entity',
new_description='An updated test entity.')
print(json.dumps(response, indent=2))
In [37]:
response = conversation.delete_entity(workspace_id=workspace_id,
entity='test_entity')
print(json.dumps(response, indent=2))
In [38]:
values = [{"value": "orange juice"}]
conversation.create_entity(workspace_id, 'beverage', values=values)
Out[38]:
In [39]:
response = conversation.create_synonym(workspace_id, 'beverage', 'orange juice', 'oj')
print(json.dumps(response, indent=2))
In [40]:
response = conversation.get_synonym(workspace_id, 'beverage', 'orange juice', 'oj')
print(json.dumps(response, indent=2))
In [41]:
response = conversation.list_synonyms(workspace_id, 'beverage', 'orange juice')
print(json.dumps(response, indent=2))
In [42]:
response = conversation.update_synonym(workspace_id, 'beverage', 'orange juice', 'oj', 'OJ')
print(json.dumps(response, indent=2))
In [43]:
response = conversation.delete_synonym(workspace_id, 'beverage', 'orange juice', 'OJ')
print(json.dumps(response, indent=2))
In [44]:
conversation.delete_entity(workspace_id, 'beverage')
Out[44]:
In [45]:
conversation.create_entity(workspace_id, 'test_entity')
Out[45]:
In [46]:
response = conversation.create_value(workspace_id, 'test_entity', 'test')
print(json.dumps(response, indent=2))
In [47]:
response = conversation.get_value(workspace_id, 'test_entity', 'test')
print(json.dumps(response, indent=2))
In [48]:
response = conversation.list_values(workspace_id, 'test_entity')
print(json.dumps(response, indent=2))
In [49]:
response = conversation.update_value(workspace_id, 'test_entity', 'test', 'example')
print(json.dumps(response, indent=2))
In [50]:
response = conversation.delete_value(workspace_id, 'test_entity', 'example')
print(json.dumps(response, indent=2))
In [51]:
conversation.delete_entity(workspace_id, 'test_entity')
Out[51]:
In [52]:
response = conversation.list_logs(workspace_id=workspace_id)
print(json.dumps(response, indent=2))
Let's cleanup by deleting the pizza chatbot, since it is no longer needed.
In [53]:
# clean-up by deleting the workspace
conversation.delete_workspace(workspace_id=workspace_id)
Out[53]: