{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Amazon Lex Create CoffeeBot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "Copyright [2017]-[2017] Amazon.com, Inc. or its affiliates. All Rights Reserved.\n", "\n", "Licensed under the Apache License, Version 2.0 (the \"License\"). You may not use this file except in compliance with the License. A copy of the License is located at\n", "\n", "http://aws.amazon.com/apache2.0/\n", "\n", "or in the \"license\" file accompanying this file. This file is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.\n", "***\n", "\n", "### Prerequisites:\n", "\n", "#### Identity and Acces Management\n", "\n", "The user or role that executes the commands must have permissions in AWS Identity and Access Management (IAM) to perform those actions. AWS provides a set of managed policies that help you get started quickly. For our example, you should apply the following managed policy to your user or role:\n", "\n", " AmazonLexFullAccess\n", "\n", "Be aware that we recommend you follow AWS IAM best practices for production implementations, which is out of scope for this workshop.\n", "\n", "#### Coffee Bot\n", "\n", "Use the code examples provided in this notebook to create the fictional Coffee Bot as documented in [this Github repository](https://github.com/awslabs/amz-ai-building-better-bots/blob/master/README.md).\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import boto3\n", "import IPython\n", "import base64\n", "import time\n", "from pprint import pprint\n", "\n", "aws_region = 'eu-west-1'\n", "\n", "bot = boto3.client('lex-models', region_name=aws_region)\n", "OUTPUT_BLACKLIST = (\"ResponseMetadata\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "slot_types = {\n", " 'cafeBeverageType':{'values': {'mocha','latte machiato','cappucino','hot chocolate','frappucino'},\n", " 'resolution': 'TOP_RESOLUTION'},\n", " 'cafeBeverageSize': {'values': {'small','medium','large'},\n", " 'resolution': 'TOP_RESOLUTION'},\n", " 'cafeBeverageStrength':{'values': {'single','double','triple','quadruple'},\n", " 'resolution': 'TOP_RESOLUTION'},\n", " 'cafeCreamerType': {'values': {'skim','half and half','almond','whole'},\n", " 'resolution': 'TOP_RESOLUTION'}}\n", "\n", "intent_name = 'cafeOrderBeverage'\n", "bot_name = 'CoffeeBot'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### create custom slot types" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for type_name in slot_types:\n", " all_types = bot.get_slot_types(\n", " nameContains=type_name\n", " )\n", " # only proceed if slot type doesn't exist yet \n", " if len(all_types['slotTypes'])== 0:\n", "\n", " enumerations = []\n", " values = slot_types[type_name]['values']\n", " for val in values:\n", " enumerations.append({'value': val})\n", "\n", " response = bot.put_slot_type(\n", " name=type_name,\n", " enumerationValues=enumerations,\n", " valueSelectionStrategy=slot_types[type_name]['resolution']\n", " )\n", " if response['ResponseMetadata']['HTTPStatusCode'] ==200:\n", " print (\"%s slot type created, checksum: %s\" %(type_name,response['checksum']))\n", " else:\n", " pprint (response)\n", " else:\n", " print (\"%s already exists, skipping slot type\" %type_name)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### create order beverage intent" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "response = bot.put_intent(\n", " name=intent_name,\n", " description='Handles coffee order requests',\n", " slots=[\n", " {\n", " 'name': 'BeverageType',\n", " 'slotConstraint': 'Required',\n", " 'slotType': 'cafeBeverageType',\n", " 'slotTypeVersion': '$LATEST',\n", " 'valueElicitationPrompt': {\n", " 'messages': [\n", " {\n", " 'contentType': 'PlainText',\n", " 'content': 'What type of coffee would you like'\n", " },\n", " {\n", " 'contentType': 'PlainText',\n", " 'content': 'What type of coffee do you fancy?'\n", " },\n", " ],\n", " 'maxAttempts': 3\n", " },\n", " 'priority': 1,\n", " 'sampleUtterances': [\n", " 'I would like to have a {BeverageType} please.','A {BeverageType} please.'\n", " ]\n", " },\n", " {\n", " 'name': 'BeverageSize',\n", " 'slotConstraint': 'Required',\n", " 'slotType': 'cafeBeverageSize',\n", " 'slotTypeVersion': '$LATEST',\n", " 'valueElicitationPrompt': {\n", " 'messages': [\n", " {\n", " 'contentType': 'PlainText',\n", " 'content': 'What size would you like, small, medium or large?'\n", " }\n", " ],\n", " 'maxAttempts': 3\n", " },\n", " 'priority': 2,\n", " 'sampleUtterances': [\n", " 'I have a {BeverageSize} one please.','{BeverageSize} please.'\n", " ]\n", " },\n", " {\n", " 'name': 'BeverageStrength',\n", " 'slotConstraint': 'Optional',\n", " 'slotType': 'cafeBeverageStrength',\n", " 'slotTypeVersion': '$LATEST',\n", " 'valueElicitationPrompt': {\n", " 'messages': [\n", " {\n", " 'contentType': 'PlainText',\n", " 'content': 'How many shots do you want?'\n", " }\n", " ],\n", " 'maxAttempts': 3\n", " },\n", " 'priority': 3,\n", " 'sampleUtterances': [\n", " '{BeverageStrength} shot please.'\n", " ]\n", " },\n", " {\n", " 'name': 'Creamer',\n", " 'slotConstraint': 'Optional',\n", " 'slotType': 'cafeCreamerType',\n", " 'slotTypeVersion': '$LATEST',\n", " 'valueElicitationPrompt': {\n", " 'messages': [\n", " {\n", " 'contentType': 'PlainText',\n", " 'content': 'What milk do you like?'\n", " }\n", " ],\n", " 'maxAttempts': 3\n", " },\n", " 'priority': 4\n", " },\n", " ],\n", " sampleUtterances=[\n", " 'I would like to order a coffee',\n", " 'I would like to order a {BeverageType}',\n", " 'Can I order a {BeverageType} please',\n", " 'Can I get a {BeverageSize} {Creamer} {BeverageStrength} {BeverageType}',\n", " 'Can I get a {BeverageType}',\n", " 'I would like a {BeverageSize} {BeverageType}'\n", " ],\n", " followUpPrompt={\n", " 'prompt': {\n", " 'messages': [\n", " {\n", " 'contentType': 'PlainText',\n", " 'content': 'Sure. Is there anything else I can do for you?'\n", " }\n", " ],\n", " 'maxAttempts': 2\n", " },\n", " 'rejectionStatement': {\n", " 'messages': [\n", " {\n", " 'contentType': 'PlainText',\n", " 'content': 'Your {BeverageSize} {BeverageType} will be ready for pick-up soon.'\n", " },\n", " {\n", " 'contentType': 'PlainText',\n", " 'content': 'We will have your {BeverageSize} {BeverageType} ready for pick-up soon.'\n", " },\n", " ]\n", " }\n", " },\n", " fulfillmentActivity={\n", " 'type': 'ReturnIntent'\n", " }\n", ")\n", "\n", "if response['ResponseMetadata']['HTTPStatusCode'] ==200:\n", " print ('Intent created, checksum: %s' %response['checksum'])\n", "else:\n", " pprint(response)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### create coffee bot" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "response = bot.put_bot(\n", " name=bot_name,\n", " description='Coffee order bot for Amazon Lex demo',\n", " intents=[\n", " {\n", " 'intentName': intent_name,\n", " 'intentVersion': '$LATEST'\n", " },\n", " ],\n", " clarificationPrompt={\n", " 'messages': [\n", " {\n", " 'contentType': 'PlainText',\n", " 'content': \"Sorry, can you please repeat that?\"\n", " },\n", " {\n", " 'contentType': 'PlainText',\n", " 'content': \"Sorry, but I didn't understand that. Could you try again, please?\"\n", " },\n", " ],\n", " 'maxAttempts': 2\n", " },\n", " abortStatement={\n", " 'messages': [\n", " {\n", " 'contentType': 'PlainText',\n", " 'content': 'Sorry, I could not understand. Goodbye.'\n", " },\n", " ]\n", " },\n", " idleSessionTTLInSeconds=300,\n", " voiceId='Salli',\n", " processBehavior='BUILD',\n", " locale='en-US',\n", " childDirected=False\n", ")\n", "\n", "if response['ResponseMetadata']['HTTPStatusCode'] ==200:\n", " print ('Bot created, checksum: %s' %response['checksum'])\n", " \n", " print ('Console URL: https://eu-west-1.console.aws.amazon.com/lex/home?region=eu-west-1#bot-editor:bot=%s' %bot_name)\n", "else:\n", " pprint (response)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### open coffee bot in the AWS console\n", "\n", "Open [Amazon Lex console](https://eu-west-1.console.aws.amazon.com/lex/home?region=eu-west-1#bots:) and wait for the build to complete before testing. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### backup code for debugging and clean-up" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# get checksums for resource updated\n", "response = bot.get_intent(\n", " name=intent_name,\n", " version='$LATEST')\n", "pprint ('Intent cheksum: %s' %response['checksum'])\n", "response = bot.get_bot(\n", " name=bot_name,\n", " versionOrAlias='$LATEST')\n", "pprint ('Bot cheksum: %s' %response['checksum'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# delete resources\n", "\n", "def delete_bot():\n", " response = bot.delete_bot(\n", " name=bot_name)\n", " if response['ResponseMetadata']['HTTPStatusCode'] ==204:\n", " print (\"%s deleted\" %bot_name)\n", " else:\n", " pprint (response)\n", "\n", " time.sleep(3) \n", "\n", " response = bot.delete_intent(\n", " name=intent_name)\n", " if response['ResponseMetadata']['HTTPStatusCode'] ==204:\n", " print (\"%s intent deleted\" %intent_name)\n", " else:\n", " pprint (response)\n", "\n", " for type_name in slot_types:\n", " time.sleep(3) \n", " response = bot.delete_slot_type(\n", " name=type_name\n", " )\n", " if response['ResponseMetadata']['HTTPStatusCode'] ==204:\n", " print (\"%s slot type deleted\" %type_name)\n", " else:\n", " pprint (response)\n", " \n", "#delete_bot() " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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 }