<commit_before>{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "import json\n",
    "import sys\n",
    "import os\n",
    "sys.path.append(os.path.join(os.getcwd(),'..','..'))\n",
    "import watson_developer_cloud"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "USERNAME = os.environ.get('CONVERSATION_USERNAME','<USERNAME>')\n",
    "PASSWORD = <PASSWORD>('CONVERSATION_PASSWORD','<PASSWORD>')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "conversation = watson_developer_cloud.ConversationV1(username=USERNAME,\n",
    "                                                     password=PASSWORD,\n",
    "                                                     version='2016-09-20')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'pagination': {'refresh_url': '/v1/workspaces?version=2016-09-20'},\n",
       " 'workspaces': [{'created': '2017-02-09T19:46:26.792Z',\n",
       "   'description': 'yo dawg',\n",
       "   'language': 'en',\n",
       "   'metadata': None,\n",
       "   'name': 'development',\n",
       "   'updated': '2017-02-09T20:00:38.558Z',\n",
       "   'workspace_id': '8c8ee3b3-6149-4bc0-ba66-6517fd6c976a'}]}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conversation.list_workspaces()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'my experimental workspace', 'created': '2017-02-09T21:48:35.245Z', 'updated': '2017-02-09T21:48:35.245Z', 'language': 'en', 'metadata': None, 'description': 'an experimental workspace', 'workspace_id': 'dbd1211f-3abb-4165-8a67-8bfa95410aa9'}\n"
     ]
    }
   ],
   "source": [
    "new_workspace = conversation.create_workspace(name='my experimental workspace',\n",
    "                                              description='an experimental workspace',\n",
    "                                              language='en',\n",
    "                                              intents=[{\n",
    "                                                      \"intent\": \"orderpizza\",\n",
    "                                                      \"examples\": [\n",
    "                                                            {\n",
    "                                                              \"text\": \"can I order a pizza?\"\n",
    "                                                            },\n",
    "                                                            {\n",
    "                                                              \"text\": \"I want to order a pizza\"\n",
    "                                                            },\n",
    "                                                            {\n",
    "                                                              \"text\": \"pizza order\"\n",
    "                                                            },\n",
    "                                                            {\n",
    "                                                              \"text\": \"pizza to go\"\n",
    "                                                            }\n",
    "                                                          ],\n",
    "                                                          \"description\": \"pizza intents\"\n",
    "                                                        }\n",
    "                                               ],\n",
    "                                              dialog_nodes=[{'conditions': '#orderpizza',\n",
    "                                                             'context': None,\n",
    "                                                             'description': None,\n",
    "                                                             'dialog_node': 'YesYouCan',\n",
    "                                                             'go_to': None,\n",
    "                                                             'metadata': None,\n",
    "                                                             'output':  {'text': {'selection_policy': 'random',\n",
    "                                                                                   'values': \n",
    "                                                                                   ['Yes You can!', 'Of course!']}},\n",
    "                                                             'parent': None,\n",
    "                                                             'previous_sibling': None,}])\n",
    "print(new_workspace)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'created': '2017-02-09T21:48:35.245Z',\n",
       " 'description': 'an experimental workspace',\n",
       " 'language': 'en',\n",
       " 'metadata': None,\n",
       " 'name': 'my experimental workspace',\n",
       " 'status': 'Training',\n",
       " 'updated': '2017-02-09T21:48:35.245Z',\n",
       " 'workspace_id': 'dbd1211f-3abb-4165-8a67-8bfa95410aa9'}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conversation.get_workspace(workspace_id=new_workspace['workspace_id'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'created': '2017-02-09T21:39:30.390Z',\n",
       " 'description': 'an experimental workspace',\n",
       " 'language': 'en',\n",
       " 'metadata': None,\n",
       " 'name': 'changed name',\n",
       " 'updated': '2017-02-09T21:39:59.124Z',\n",
       " 'workspace_id': '66e78807-13fa-47ae-9251-dd8c89c8fd19'}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_workspace['name'] = 'changed name'\n",
    "conversation.update_workspace(new_workspace['workspace_id'], name=new_workspace['name'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'counterexamples': [],\n",
       " 'created': '2017-02-09T21:48:35.245Z',\n",
       " 'description': 'an experimental workspace',\n",
       " 'dialog_nodes': [{'conditions': '#orderpizza',\n",
       "   'context': None,\n",
       "   'created': '2017-02-09T21:48:35.245Z',\n",
       "   'description': None,\n",
       "   'dialog_node': 'YesYouCan',\n",
       "   'go_to': None,\n",
       "   'metadata': None,\n",
       "   'output': {'text': {'selection_policy': 'random',\n",
       "     'values': ['Yes You can!', 'Of course!']}},\n",
       "   'parent': None,\n",
       "   'previous_sibling': None,\n",
       "   'updated': '2017-02-09T21:48:35.245Z'}],\n",
       " 'entities': [],\n",
       " 'intents': [{'created': '2017-02-09T21:48:35.245Z',\n",
       "   'description': 'pizza intents',\n",
       "   'examples': [{'created': '2017-02-09T21:48:35.245Z',\n",
       "     'text': 'can I order a pizza?',\n",
       "     'updated': '2017-02-09T21:48:35.245Z'},\n",
       "    {'created': '2017-02-09T21:48:35.245Z',\n",
       "     'text': 'I want to order a pizza',\n",
       "     'updated': '2017-02-09T21:48:35.245Z'},\n",
       "    {'created': '2017-02-09T21:48:35.245Z',\n",
       "     'text': 'pizza order',\n",
       "     'updated': '2017-02-09T21:48:35.245Z'},\n",
       "    {'created': '2017-02-09T21:48:35.245Z',\n",
       "     'text': 'pizza to go',\n",
       "     'updated': '2017-02-09T21:48:35.245Z'}],\n",
       "   'intent': 'orderpizza',\n",
       "   'updated': '2017-02-09T21:48:35.245Z'}],\n",
       " 'language': 'en',\n",
       " 'metadata': None,\n",
       " 'name': 'my experimental workspace',\n",
       " 'status': 'Available',\n",
       " 'updated': '2017-02-09T21:48:35.245Z',\n",
       " 'workspace_id': 'dbd1211f-3abb-4165-8a67-8bfa95410aa9'}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conversation.get_workspace(new_workspace['workspace_id'], export=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"intents\": [\n",
      "    {\n",
      "      \"intent\": \"orderpizza\",\n",
      "      \"confidence\": 1\n",
      "    }\n",
      "  ],\n",
      "  \"entities\": [],\n",
      "  \"input\": {\n",
      "    \"text\": \"can I order a pizza?\"\n",
      "  },\n",
      "  \"output\": {\n",
      "    \"log_messages\": [],\n",
      "    \"text\": [\n",
      "      \"Of course!\"\n",
      "    ],\n",
      "    \"nodes_visited\": [\n",
      "      \"YesYouCan\"\n",
      "    ]\n",
      "  },\n",
      "  \"context\": {\n",
      "    \"conversation_id\": \"972f212d-be0f-47fd-a82d-a6f917e16cfd\",\n",
      "    \"system\": {\n",
      "      \"dialog_stack\": [\n",
      "        {\n",
      "          \"dialog_node\": \"root\"\n",
      "        }\n",
      "      ],\n",
      "      \"dialog_turn_counter\": 1,\n",
      "      \"dialog_request_counter\": 1,\n",
      "      \"_node_output_map\": {\n",
      "        \"YesYouCan\": [\n",
      "          0,\n",
      "          1,\n",
      "          0\n",
      "        ]\n",
      "      }\n",
      "    }\n",
      "  },\n",
      "  \"alternate_intents\": false\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "response = conversation.message(workspace_id=new_workspace['workspace_id'],\n",
    "                                message_input={'text': 'can I order a pizza?'})\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conversation.delete_workspace(new_workspace['workspace_id'])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
<commit_msg>Add notebook examples for new conversation endpoints<commit_after>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Conversation Service"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import sys\n",
    "import os\n",
    "sys.path.append(os.path.join(os.getcwd(),'..','..'))\n",
    "import watson_developer_cloud"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "USERNAME = os.environ.get('CONVERSATION_USERNAME','<USERNAME>')\n",
    "PASSWORD = os.environ.get('CONVERSATION_PASSWORD','<PASSWORD>')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "conversation = watson_developer_cloud.ConversationV1(username=USERNAME,\n",
    "                                                     password=PASSWORD,\n",
    "                                                     version='2016-09-20')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Pizza Chatbot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "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."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"name\": \"example_workspace\",\n",
      "  \"created\": \"2017-04-17T18:55:39.740Z\",\n",
      "  \"updated\": \"2017-04-17T18:55:39.740Z\",\n",
      "  \"language\": \"en\",\n",
      "  \"metadata\": null,\n",
      "  \"description\": \"An example workspace.\",\n",
      "  \"workspace_id\": \"0247c2f5-4155-4be5-bd51-167de32f2f39\"\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "# define the dialog for our example workspace\n",
    "dialog_nodes = [{'conditions': '#order_pizza',\n",
    "                 'context': None,\n",
    "                 'description': None,\n",
    "                 'dialog_node': 'YesYouCan',\n",
    "                 'go_to': None,\n",
    "                 'metadata': None,\n",
    "                 'output': {'text': {'selection_policy': 'random',\n",
    "                                     'values': ['Yes You can!', 'Of course!']}},\n",
    "                 'parent': None,\n",
    "                 'previous_sibling': None,}]\n",
    "\n",
    "# create an example workspace\n",
    "workspace = conversation.create_workspace(name='example_workspace',\n",
    "                                          description='An example workspace.',\n",
    "                                          language='en',\n",
    "                                          dialog_nodes=dialog_nodes)\n",
    "\n",
    "# print response and save workspace_id\n",
    "print(json.dumps(workspace, indent=2))\n",
    "workspace_id=workspace['workspace_id']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"intent\": \"order_pizza\",\n",
      "  \"created\": \"2017-04-17T18:55:40.899Z\",\n",
      "  \"updated\": \"2017-04-17T18:55:40.899Z\",\n",
      "  \"description\": \"A pizza order.\"\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "# add an intent to the workspace\n",
    "intent = conversation.create_intent(workspace_id=workspace_id,\n",
    "                                    intent='order_pizza',\n",
    "                                    description='A pizza order.')\n",
    "print(json.dumps(intent, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"text\": \"Can I order a pizza?\",\n",
      "  \"created\": \"2017-04-17T18:55:41.755Z\",\n",
      "  \"updated\": \"2017-04-17T18:55:41.755Z\"\n",
      "}\n",
      "{\n",
      "  \"text\": \"I want to order a pizza.\",\n",
      "  \"created\": \"2017-04-17T18:55:42.172Z\",\n",
      "  \"updated\": \"2017-04-17T18:55:42.172Z\"\n",
      "}\n",
      "{\n",
      "  \"text\": \"pizza order\",\n",
      "  \"created\": \"2017-04-17T18:55:42.606Z\",\n",
      "  \"updated\": \"2017-04-17T18:55:42.606Z\"\n",
      "}\n",
      "{\n",
      "  \"text\": \"pizza to go\",\n",
      "  \"created\": \"2017-04-17T18:55:43.018Z\",\n",
      "  \"updated\": \"2017-04-17T18:55:43.018Z\"\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "# add examples to the intent\n",
    "example1 = conversation.create_example(workspace_id=workspace_id,\n",
    "                                       intent='order_pizza',\n",
    "                                       text='Can I order a pizza?')\n",
    "example2 = conversation.create_example(workspace_id=workspace_id,\n",
    "                                       intent='order_pizza',\n",
    "                                       text='I want to order a pizza.')\n",
    "example3 = conversation.create_example(workspace_id=workspace_id,\n",
    "                                       intent='order_pizza',\n",
    "                                       text='pizza order')\n",
    "example4 = conversation.create_example(workspace_id=workspace_id,\n",
    "                                       intent='order_pizza',\n",
    "                                       text='pizza to go')\n",
    "\n",
    "print(json.dumps(example1, indent=2))\n",
    "print(json.dumps(example2, indent=2))\n",
    "print(json.dumps(example3, indent=2))\n",
    "print(json.dumps(example4, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The workspace status is: Available\n",
      "Ready to chat!\n"
     ]
    }
   ],
   "source": [
    "# check workspace status (wait for training to complete)\n",
    "workspace = conversation.get_workspace(workspace_id=workspace_id)\n",
    "print('The workspace status is: {0}'.format(workspace['status']))\n",
    "if workspace['status'] == 'Available':\n",
    "    print('Ready to chat!')\n",
    "else:\n",
    "    print('The workspace should be available shortly. Please try again in 30s.')\n",
    "    print('(You can send messages, but not all functionality will be supported yet.)')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"intents\": [\n",
      "    {\n",
      "      \"intent\": \"order_pizza\",\n",
      "      \"confidence\": 1\n",
      "    }\n",
      "  ],\n",
      "  \"entities\": [],\n",
      "  \"input\": {\n",
      "    \"text\": \"Can I order a pizza?\"\n",
      "  },\n",
      "  \"output\": {\n",
      "    \"log_messages\": [],\n",
      "    \"text\": [\n",
      "      \"Yes You can!\"\n",
      "    ],\n",
      "    \"nodes_visited\": [\n",
      "      \"YesYouCan\"\n",
      "    ]\n",
      "  },\n",
      "  \"context\": {\n",
      "    \"conversation_id\": \"a84cbf65-d41d-4092-8450-e03c986a3340\",\n",
      "    \"system\": {\n",
      "      \"dialog_stack\": [\n",
      "        {\n",
      "          \"dialog_node\": \"root\"\n",
      "        }\n",
      "      ],\n",
      "      \"dialog_turn_counter\": 1,\n",
      "      \"dialog_request_counter\": 1,\n",
      "      \"_node_output_map\": {\n",
      "        \"YesYouCan\": [\n",
      "          0,\n",
      "          0,\n",
      "          1\n",
      "        ]\n",
      "      },\n",
      "      \"branch_exited\": true,\n",
      "      \"branch_exited_reason\": \"completed\"\n",
      "    }\n",
      "  }\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "# start a chat with the pizza bot\n",
    "message_input = {'text': 'Can I order a pizza?'}\n",
    "response = conversation.message(workspace_id=workspace_id,\n",
    "                                message_input=message_input)\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"intents\": [\n",
      "    {\n",
      "      \"intent\": \"order_pizza\",\n",
      "      \"confidence\": 1\n",
      "    }\n",
      "  ],\n",
      "  \"entities\": [],\n",
      "  \"input\": {\n",
      "    \"text\": \"medium\"\n",
      "  },\n",
      "  \"output\": {\n",
      "    \"log_messages\": [],\n",
      "    \"text\": [\n",
      "      \"Of course!\"\n",
      "    ],\n",
      "    \"nodes_visited\": [\n",
      "      \"YesYouCan\"\n",
      "    ]\n",
      "  },\n",
      "  \"context\": {\n",
      "    \"conversation_id\": \"a84cbf65-d41d-4092-8450-e03c986a3340\",\n",
      "    \"system\": {\n",
      "      \"dialog_stack\": [\n",
      "        {\n",
      "          \"dialog_node\": \"root\"\n",
      "        }\n",
      "      ],\n",
      "      \"dialog_turn_counter\": 2,\n",
      "      \"dialog_request_counter\": 2,\n",
      "      \"_node_output_map\": {\n",
      "        \"YesYouCan\": [\n",
      "          1,\n",
      "          0,\n",
      "          1\n",
      "        ]\n",
      "      },\n",
      "      \"branch_exited\": true,\n",
      "      \"branch_exited_reason\": \"completed\"\n",
      "    }\n",
      "  }\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "# continue a chat with the pizza bot\n",
    "# (when you send multiple requests for the same conversation,\n",
    "#  then include the context object from the previous response)\n",
    "message_input = {'text': 'medium'}\n",
    "response = conversation.message(workspace_id=workspace_id,\n",
    "                                message_input=message_input,\n",
    "                                context=response['context'])\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Operation Examples"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The following examples demonstrate each operation of the Conversation service. They use the pizza chatbot workspace configured above."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Message"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"intents\": [\n",
      "    {\n",
      "      \"intent\": \"order_pizza\",\n",
      "      \"confidence\": 1\n",
      "    }\n",
      "  ],\n",
      "  \"entities\": [],\n",
      "  \"input\": {\n",
      "    \"text\": \"Can I order a pizza?\"\n",
      "  },\n",
      "  \"output\": {\n",
      "    \"log_messages\": [],\n",
      "    \"text\": [\n",
      "      \"Of course!\"\n",
      "    ],\n",
      "    \"nodes_visited\": [\n",
      "      \"YesYouCan\"\n",
      "    ]\n",
      "  },\n",
      "  \"context\": {\n",
      "    \"conversation_id\": \"bceff6eb-f89b-48d3-9190-99e9cca6a3e7\",\n",
      "    \"system\": {\n",
      "      \"dialog_stack\": [\n",
      "        {\n",
      "          \"dialog_node\": \"root\"\n",
      "        }\n",
      "      ],\n",
      "      \"dialog_turn_counter\": 1,\n",
      "      \"dialog_request_counter\": 1,\n",
      "      \"_node_output_map\": {\n",
      "        \"YesYouCan\": [\n",
      "          0,\n",
      "          1,\n",
      "          0\n",
      "        ]\n",
      "      },\n",
      "      \"branch_exited\": true,\n",
      "      \"branch_exited_reason\": \"completed\"\n",
      "    }\n",
      "  }\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "message_input = {'text': 'Can I order a pizza?'}\n",
    "response = conversation.message(workspace_id=workspace_id,\n",
    "                                message_input=message_input)\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Workspaces"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"name\": \"test_workspace\",\n",
      "  \"created\": \"2017-04-17T18:57:41.168Z\",\n",
      "  \"updated\": \"2017-04-17T18:57:41.168Z\",\n",
      "  \"language\": \"en\",\n",
      "  \"metadata\": {},\n",
      "  \"description\": \"Test workspace.\",\n",
      "  \"workspace_id\": \"d8cc32e4-5fb4-47ec-944c-0d7fcd59651c\"\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "response = conversation.create_workspace(name='test_workspace',\n",
    "                                         description='Test workspace.',\n",
    "                                         language='en',\n",
    "                                         metadata={})\n",
    "print(json.dumps(response, indent=2))\n",
    "test_workspace_id = response['workspace_id']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n"
     ]
    }
   ],
   "source": [
    "response = conversation.delete_workspace(workspace_id=test_workspace_id)\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"name\": \"example_workspace\",\n",
      "  \"created\": \"2017-04-17T18:55:39.740Z\",\n",
      "  \"intents\": [\n",
      "    {\n",
      "      \"intent\": \"order_pizza\",\n",
      "      \"created\": \"2017-04-17T18:55:40.899Z\",\n",
      "      \"updated\": \"2017-04-17T18:55:43.018Z\",\n",
      "      \"examples\": [\n",
      "        {\n",
      "          \"text\": \"Can I order a pizza?\",\n",
      "          \"created\": \"2017-04-17T18:55:41.755Z\",\n",
      "          \"updated\": \"2017-04-17T18:55:41.755Z\"\n",
      "        },\n",
      "        {\n",
      "          \"text\": \"I want to order a pizza.\",\n",
      "          \"created\": \"2017-04-17T18:55:42.172Z\",\n",
      "          \"updated\": \"2017-04-17T18:55:42.172Z\"\n",
      "        },\n",
      "        {\n",
      "          \"text\": \"pizza order\",\n",
      "          \"created\": \"2017-04-17T18:55:42.606Z\",\n",
      "          \"updated\": \"2017-04-17T18:55:42.606Z\"\n",
      "        },\n",
      "        {\n",
      "          \"text\": \"pizza to go\",\n",
      "          \"created\": \"2017-04-17T18:55:43.018Z\",\n",
      "          \"updated\": \"2017-04-17T18:55:43.018Z\"\n",
      "        }\n",
      "      ],\n",
      "      \"description\": \"A pizza order.\"\n",
      "    }\n",
      "  ],\n",
      "  \"updated\": \"2017-04-17T18:55:43.018Z\",\n",
      "  \"entities\": [],\n",
      "  \"language\": \"en\",\n",
      "  \"metadata\": null,\n",
      "  \"description\": \"An example workspace.\",\n",
      "  \"dialog_nodes\": [\n",
      "    {\n",
      "      \"go_to\": null,\n",
      "      \"output\": {\n",
      "        \"text\": {\n",
      "          \"values\": [\n",
      "            \"Yes You can!\",\n",
      "            \"Of course!\"\n",
      "          ],\n",
      "          \"selection_policy\": \"random\"\n",
      "        }\n",
      "      },\n",
      "      \"parent\": null,\n",
      "      \"context\": null,\n",
      "      \"created\": \"2017-04-17T18:55:39.740Z\",\n",
      "      \"updated\": \"2017-04-17T18:55:39.740Z\",\n",
      "      \"metadata\": null,\n",
      "      \"conditions\": \"#order_pizza\",\n",
      "      \"description\": null,\n",
      "      \"dialog_node\": \"YesYouCan\",\n",
      "      \"previous_sibling\": null\n",
      "    }\n",
      "  ],\n",
      "  \"workspace_id\": \"0247c2f5-4155-4be5-bd51-167de32f2f39\",\n",
      "  \"counterexamples\": [],\n",
      "  \"status\": \"Available\"\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "response = conversation.get_workspace(workspace_id=workspace_id, export=True)\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"workspaces\": [\n",
      "    {\n",
      "      \"name\": \"Car_Dashboard\",\n",
      "      \"created\": \"2016-07-19T16:31:17.236Z\",\n",
      "      \"updated\": \"2017-04-11T02:29:46.290Z\",\n",
      "      \"language\": \"en\",\n",
      "      \"metadata\": {\n",
      "        \"runtime_version\": \"2016-09-20\"\n",
      "      },\n",
      "      \"description\": \"Cognitive Car workspace which allows multi-turn conversations to perform tasks in the car.\",\n",
      "      \"workspace_id\": \"8d869397-411b-4f0a-864d-a2ba419bb249\"\n",
      "    },\n",
      "    {\n",
      "      \"name\": \"example_workspace\",\n",
      "      \"created\": \"2017-04-17T18:07:47.842Z\",\n",
      "      \"updated\": \"2017-04-17T18:07:47.842Z\",\n",
      "      \"language\": \"en\",\n",
      "      \"metadata\": null,\n",
      "      \"description\": \"An example workspace.\",\n",
      "      \"workspace_id\": \"a7196fc9-c458-4b75-963c-dc4a3dc32ef7\"\n",
      "    },\n",
      "    {\n",
      "      \"name\": \"example_workspace\",\n",
      "      \"created\": \"2017-04-17T18:42:40.986Z\",\n",
      "      \"updated\": \"2017-04-17T18:54:21.407Z\",\n",
      "      \"language\": \"en\",\n",
      "      \"metadata\": null,\n",
      "      \"description\": \"An example workspace for ordering pizza.\",\n",
      "      \"workspace_id\": \"88a4327b-421c-436f-b54a-5e00df6b8e8b\"\n",
      "    },\n",
      "    {\n",
      "      \"name\": \"example_workspace\",\n",
      "      \"created\": \"2017-04-17T18:55:39.740Z\",\n",
      "      \"updated\": \"2017-04-17T18:55:43.018Z\",\n",
      "      \"language\": \"en\",\n",
      "      \"metadata\": null,\n",
      "      \"description\": \"An example workspace.\",\n",
      "      \"workspace_id\": \"0247c2f5-4155-4be5-bd51-167de32f2f39\"\n",
      "    },\n",
      "    {\n",
      "      \"name\": \"LaForge POC\",\n",
      "      \"created\": \"2016-09-07T19:01:46.271Z\",\n",
      "      \"updated\": \"2016-11-29T21:46:38.969Z\",\n",
      "      \"language\": \"en\",\n",
      "      \"metadata\": {\n",
      "        \"runtime_version\": \"2016-09-20\"\n",
      "      },\n",
      "      \"description\": null,\n",
      "      \"workspace_id\": \"4f4046a6-50c5-4a52-9247-b2538f0fe7ac\"\n",
      "    }\n",
      "  ],\n",
      "  \"pagination\": {\n",
      "    \"refresh_url\": \"/v1/workspaces?version=2016-09-20\"\n",
      "  }\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "response = conversation.list_workspaces()\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"name\": \"example_workspace\",\n",
      "  \"created\": \"2017-04-17T18:55:39.740Z\",\n",
      "  \"updated\": \"2017-04-17T18:57:45.626Z\",\n",
      "  \"language\": \"en\",\n",
      "  \"metadata\": null,\n",
      "  \"description\": \"An example workspace for ordering pizza.\",\n",
      "  \"workspace_id\": \"0247c2f5-4155-4be5-bd51-167de32f2f39\"\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "response = conversation.update_workspace(workspace_id=workspace_id,\n",
    "                                         description='An example workspace for ordering pizza.')\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Intents"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"intent\": \"test_intent\",\n",
      "  \"created\": \"2017-04-17T18:57:47.513Z\",\n",
      "  \"updated\": \"2017-04-17T18:57:47.513Z\",\n",
      "  \"description\": \"Test intent.\"\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "response = conversation.create_intent(workspace_id=workspace_id,\n",
    "                                      intent='test_intent',\n",
    "                                      description='Test intent.')\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n"
     ]
    }
   ],
   "source": [
    "response = conversation.delete_intent(workspace_id=workspace_id,\n",
    "                                      intent='test_intent')\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"intent\": \"order_pizza\",\n",
      "  \"created\": \"2017-04-17T18:55:40.899Z\",\n",
      "  \"updated\": \"2017-04-17T18:55:43.018Z\",\n",
      "  \"examples\": [\n",
      "    {\n",
      "      \"text\": \"Can I order a pizza?\",\n",
      "      \"created\": \"2017-04-17T18:55:41.755Z\",\n",
      "      \"updated\": \"2017-04-17T18:55:41.755Z\"\n",
      "    },\n",
      "    {\n",
      "      \"text\": \"I want to order a pizza.\",\n",
      "      \"created\": \"2017-04-17T18:55:42.172Z\",\n",
      "      \"updated\": \"2017-04-17T18:55:42.172Z\"\n",
      "    },\n",
      "    {\n",
      "      \"text\": \"pizza order\",\n",
      "      \"created\": \"2017-04-17T18:55:42.606Z\",\n",
      "      \"updated\": \"2017-04-17T18:55:42.606Z\"\n",
      "    },\n",
      "    {\n",
      "      \"text\": \"pizza to go\",\n",
      "      \"created\": \"2017-04-17T18:55:43.018Z\",\n",
      "      \"updated\": \"2017-04-17T18:55:43.018Z\"\n",
      "    }\n",
      "  ],\n",
      "  \"description\": \"A pizza order.\"\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "response = conversation.get_intent(workspace_id=workspace_id,\n",
    "                                   intent='order_pizza',\n",
    "                                   export=True)\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"intents\": [\n",
      "    {\n",
      "      \"intent\": \"order_pizza\",\n",
      "      \"created\": \"2017-04-17T18:55:40.899Z\",\n",
      "      \"updated\": \"2017-04-17T18:55:43.018Z\",\n",
      "      \"examples\": [\n",
      "        {\n",
      "          \"text\": \"Can I order a pizza?\",\n",
      "          \"created\": \"2017-04-17T18:55:41.755Z\",\n",
      "          \"updated\": \"2017-04-17T18:55:41.755Z\"\n",
      "        },\n",
      "        {\n",
      "          \"text\": \"I want to order a pizza.\",\n",
      "          \"created\": \"2017-04-17T18:55:42.172Z\",\n",
      "          \"updated\": \"2017-04-17T18:55:42.172Z\"\n",
      "        },\n",
      "        {\n",
      "          \"text\": \"pizza order\",\n",
      "          \"created\": \"2017-04-17T18:55:42.606Z\",\n",
      "          \"updated\": \"2017-04-17T18:55:42.606Z\"\n",
      "        },\n",
      "        {\n",
      "          \"text\": \"pizza to go\",\n",
      "          \"created\": \"2017-04-17T18:55:43.018Z\",\n",
      "          \"updated\": \"2017-04-17T18:55:43.018Z\"\n",
      "        }\n",
      "      ],\n",
      "      \"description\": \"A pizza order.\"\n",
      "    }\n",
      "  ],\n",
      "  \"pagination\": {\n",
      "    \"refresh_url\": \"/v1/workspaces/0247c2f5-4155-4be5-bd51-167de32f2f39/intents?version=2016-09-20&export=true\"\n",
      "  }\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "response = conversation.list_intents(workspace_id=workspace_id,\n",
    "                                     export=True)\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"intent\": \"order_pizza\",\n",
      "  \"created\": \"2017-04-17T18:55:40.899Z\",\n",
      "  \"updated\": \"2017-04-17T18:57:52.129Z\",\n",
      "  \"description\": \"Order a pizza.\"\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "response = conversation.update_intent(workspace_id=workspace_id,\n",
    "                                      intent='order_pizza',\n",
    "                                      new_intent='order_pizza',\n",
    "                                      new_description='Order a pizza.')\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Examples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"text\": \"Gimme a pizza with pepperoni\",\n",
      "  \"created\": \"2017-04-17T18:57:54.817Z\",\n",
      "  \"updated\": \"2017-04-17T18:57:54.817Z\"\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "response = conversation.create_example(workspace_id=workspace_id,\n",
    "                                       intent='order_pizza',\n",
    "                                       text='Gimme a pizza with pepperoni')\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n"
     ]
    }
   ],
   "source": [
    "response = conversation.delete_example(workspace_id=workspace_id,\n",
    "                                       intent='order_pizza',\n",
    "                                       text='Gimme a pizza with pepperoni')\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"text\": \"I want to order a pizza.\",\n",
      "  \"created\": \"2017-04-17T18:55:42.172Z\",\n",
      "  \"updated\": \"2017-04-17T18:55:42.172Z\"\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "response = conversation.get_example(workspace_id=workspace_id,\n",
    "                                    intent='order_pizza',\n",
    "                                    text='I want to order a pizza.')\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"examples\": [\n",
      "    {\n",
      "      \"text\": \"Can I order a pizza?\",\n",
      "      \"created\": \"2017-04-17T18:55:41.755Z\",\n",
      "      \"updated\": \"2017-04-17T18:55:41.755Z\"\n",
      "    },\n",
      "    {\n",
      "      \"text\": \"I want to order a pizza.\",\n",
      "      \"created\": \"2017-04-17T18:55:42.172Z\",\n",
      "      \"updated\": \"2017-04-17T18:55:42.172Z\"\n",
      "    },\n",
      "    {\n",
      "      \"text\": \"pizza order\",\n",
      "      \"created\": \"2017-04-17T18:55:42.606Z\",\n",
      "      \"updated\": \"2017-04-17T18:55:42.606Z\"\n",
      "    },\n",
      "    {\n",
      "      \"text\": \"pizza to go\",\n",
      "      \"created\": \"2017-04-17T18:55:43.018Z\",\n",
      "      \"updated\": \"2017-04-17T18:55:43.018Z\"\n",
      "    }\n",
      "  ],\n",
      "  \"pagination\": {\n",
      "    \"refresh_url\": \"/v1/workspaces/0247c2f5-4155-4be5-bd51-167de32f2f39/intents/order_pizza/examples?version=2016-09-20\"\n",
      "  }\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "response = conversation.list_examples(workspace_id=workspace_id,\n",
    "                                      intent='order_pizza')\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"text\": \"I want to order a pizza with pepperoni.\",\n",
      "  \"created\": \"2017-04-17T18:55:42.172Z\",\n",
      "  \"updated\": \"2017-04-17T18:58:00.647Z\"\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "response = conversation.update_example(workspace_id=workspace_id,\n",
    "                                       intent='order_pizza',\n",
    "                                       text='I want to order a pizza.',\n",
    "                                       new_text='I want to order a pizza with pepperoni.')\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Counterexamples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"text\": \"I want financial advice today.\",\n",
      "  \"created\": \"2017-04-17T18:58:03.340Z\",\n",
      "  \"updated\": \"2017-04-17T18:58:03.340Z\"\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "response = conversation.create_counterexample(workspace_id=workspace_id,\n",
    "                                              text='I want financial advice today.')\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"text\": \"I want financial advice today.\",\n",
      "  \"created\": \"2017-04-17T18:58:03.340Z\",\n",
      "  \"updated\": \"2017-04-17T18:58:03.340Z\"\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "response = conversation.get_counterexample(workspace_id=workspace_id,\n",
    "                                           text='I want financial advice today.')\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"counterexamples\": [\n",
      "    {\n",
      "      \"text\": \"I want financial advice today.\",\n",
      "      \"created\": \"2017-04-17T18:58:03.340Z\",\n",
      "      \"updated\": \"2017-04-17T18:58:03.340Z\"\n",
      "    }\n",
      "  ],\n",
      "  \"pagination\": {\n",
      "    \"refresh_url\": \"/v1/workspaces/0247c2f5-4155-4be5-bd51-167de32f2f39/counterexamples?version=2016-09-20\"\n",
      "  }\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "response = conversation.list_counterexamples(workspace_id=workspace_id)\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"text\": \"I want financial advice for tomorrow.\",\n",
      "  \"created\": \"2017-04-17T18:58:03.340Z\",\n",
      "  \"updated\": \"2017-04-17T18:58:09.403Z\"\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "response = conversation.update_counterexample(workspace_id=workspace_id,\n",
    "                                              text='I want financial advice today.',\n",
    "                                              new_text='I want financial advice for tomorrow.')\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n"
     ]
    }
   ],
   "source": [
    "response = conversation.delete_counterexample(workspace_id=workspace_id,\n",
    "                                              text='I want financial advice for tomorrow.')\n",
    "print(json.dumps(response, indent=2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Cleanup (Delete Pizza Chatbot)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's cleanup by deleting the pizza chatbot, since it is no longer needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# clean-up by deleting the workspace\n",
    "conversation.delete_workspace(workspace_id=workspace_id)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}

{'fraction_non_alphanumeric': 0.22680703277621012, 'fraction_numerical': 0.05463425222487519, 'mean_word_length': 1.4962613784135241, 'pattern_counts': {'":': 719, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 163, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3299479', 'n_tokens_mistral': 17690, 'n_tokens_neox': 16011, 'n_words': 2382}