diff --git a/docs/docs/tutorial.mdx b/docs/docs/tutorial.mdx index 17abc598f169..96756f9a15b0 100644 --- a/docs/docs/tutorial.mdx +++ b/docs/docs/tutorial.mdx @@ -11,7 +11,7 @@ import TutorialActionLabel from "@theme/TutorialActionLabel"; You will build an assistant in this tutorial for helping people transfer money. This tutorial does not assume and existing knowledge of Rasa or chatbots. -The techniques you will learn in this tutorial are fundamental to building any Rasa bot, +The techniques you will learn in this tutorial are fundamental to building any Rasa assistant, and understanding it will bring you quite far along to mastering Rasa. ## What are you building? @@ -24,38 +24,57 @@ Here are some of the conversations your assistant will be able to handle: - I'd like to transfer some money - Sure - who are we sending money to? - Jen + I want to send money + Who would you like to send money to? + to Jen How much would you like to send? $50 - Just to confirm: you want to send $50 to Jen? + Please confirm: you want to transfer $50.0 to Jen? yes - Ok, it's done! + All done. $50.0 has been sent to Jen. + Is there anything else I can help you with?! - I want to send $100 to Joe + I want to send 100 dollars to Joe - Just to confirm, you want to send $100.00 to Joe Smith? + Please confirm: you want to transfer $100.0 to Joe? yes. - All done. $100.00 has been sent to Joe Smith. + All done. $100.00 has been sent to Joe. + Is there anything else I can help you with? + + + + + I need to send $50 + Who would you like to send money to? + Jen + Please confirm: you want to transfer $50.0 to Jen? + actually no 60 + Ok, I corrected the amount. + Please confirm: you want to transfer $60.0 to Jen? + yes. + All done. $60.00 has been sent to Jen. Is there anything else I can help you with? +## Following This Tutorial + +Whenever there is something for you to do, you'll see this label: + + ## Setup @@ -103,6 +122,8 @@ In this tutorial you will primarily work with the following files: ## Testing your money transfer flow + + Train your assistant by running: ```bash @@ -115,7 +136,7 @@ And start talking to it in the shell by running: rasa shell ``` -Tell the bot that you'd like to transfer some money to a friend. +Tell the assistant that you'd like to transfer some money to a friend. ## Understanding your money transfer flow. @@ -125,18 +146,12 @@ Let's look at this definition to see what is going on: ```yaml-rasa title="flows.yml" flows: transfer_money: - description: | - This flow lets users send money to friends - and family, in US Dollars. + description: This flow lets users send money to friends and family. steps: - - id: "ask_recipient" - collect_information: recipient - next: "ask_amount" - - id: "ask_amount" - collect_information: amount - next: "transfer_successful" - - id: "transfer_successful" - action: utter_transfer_complete + - collect: recipient + - collect: amount + description: the number of US dollars to send + - action: utter_transfer_complete ``` The two key attributes of the `transfer_money` flow are the `description` and the `steps`. @@ -145,43 +160,37 @@ But it is also helpful for anyone who inspects your code to understand what is g If a user says "I need to transfer some money", the description helps Rasa understand that this is the relevant flow. The `steps` describe the business logic required to do what the user asked for. -There are three steps defined, each with an `id`. -The `id` can be anything you like, but it's a good idea to use names that make it clear what is going on. -The first step is a `collect_information` step, which is used to fill a `slot`. -A `collect_information` step sends a message to the user requesting information, and waits for an answer. +The first step in your flow is a `collect` step, which is used to fill a `slot`. +A `collect` step sends a message to the user requesting information, and waits for an answer. + ## Collecting Information in Slots -`Slots` are a key-value memory store used during a conversation. +`Slots` are variables that your assistant can read and write throughout a conversation. Slots are defined in your `domain.yml` file. For example, the definition of your `recipient` slot looks like this: ```yaml-rasa title="domain.yml" slots: recipient: type: text - mappings: - - type: custom # ... ``` -They can be used to store information provided to you by the user or fetched via an API call. +Slots can be used to store information that users provide during the conversation, +or information that has been fetched via an API call. First, you're going to see how to store information provided by the end user in a slot. -To do this, you define a `collect_information` step like the first step in your flow above. +To do this, you define a `collect` step like the first step in your flow above. ```yaml-rasa title="flows.yml" flows: transfer_money: description: This flow lets users send money to friends and family. steps: - - id: "ask_recipient" - # highlight-next-line - collect_information: recipient - next: "ask_amount" - - id: "ask_amount" - collect_information: amount - next: "transfer_successful" - - id: "transfer_successful" - action: utter_transfer_complete + # highlight-next-line + - collect: recipient + - collect: amount + description: the number of US dollars to send + - action: utter_transfer_complete ``` Rasa will look for a `response` called `utter_ask_recipient` in your domain file and use this to @@ -195,7 +204,8 @@ responses: After sending this message, Rasa will wait for a response from the user. When the user responds, Rasa will try to use their answer to fill the slot `recipient`. -Read about slot validation (link) to learn more about how to run additional checks on slot values. +Read about [slot validation](./concepts/slot-validation-actions.mdx) to learn how you +can run extra checks on the slot values Rasa has extracted. The diagram below summarizes how slot values are used to collect and store information, and how they can be used to create branching logic. @@ -205,9 +215,27 @@ and how they can be used to create branching logic. src={useBaseUrl("/img/slots_in_flows.png")} /> +### Descriptions in collect steps + +The second `collect` step includes a description of the information your assistant +will request from the user. +Descriptions are optional, but can help Rasa extract slot values more reliably. + +```yaml-rasa title="flows.yml" +flows: + transfer_money: + description: This flow lets users send money to friends and family. + steps: + - collect: recipient + - collect: amount + # highlight-next-line + description: the number of US dollars to send + - action: utter_transfer_complete +``` + ## Action Steps -The third `step` in your `transfer_money` flow is not a `collect_information` step but an `action` step. +The third `step` in your `transfer_money` flow is not a `collect` step but an `action` step. When you reach an action step in a flow, your assistant will execute the corresponding action and then proceed to the next step. It will not stop to wait for the user's next message. @@ -218,21 +246,19 @@ flows: transfer_money: description: This flow lets users send money to friends and family. steps: - - id: "ask_recipient" - collect_information: recipient - next: "ask_amount" - - id: "ask_amount" - collect_information: amount - next: "transfer_successful" - - id: "transfer_successful" - # highlight-next-line - action: utter_transfer_complete + - collect: recipient + - collect: amount + description: the number of US dollars to send + # highlight-next-line + - action: utter_transfer_complete ``` ## Branching Logic Slots are also used to build branching logic in flows. + + You're going to introduce an extra step to your flow, asking the user to confirm the amount and the recipient before sending the transfer. Since you are asking a yes/no question, you can store the result in a boolean `slot` @@ -240,19 +266,16 @@ which you will call `final_confirmation`. In your domain file, add the definition of the `final_confirmation` slot and the corresponding response: `utter_ask_final_confirmation`. +Also add a response to confirm the transfer has been cancelled. ```yaml-rasa title="domain.yml" slots: recipient: type: Text - mappings: - - type: custom # ... # highlight-start final_confirmation: type: bool - mappings: - - type: custom # highlight-end ``` @@ -264,46 +287,53 @@ responses: # highlight-start utter_ask_final_confirmation: - text: "Please confirm: you want to transfer {amount} to {recipient}?" + utter_transfer_cancelled: + - text: "Your transfer has been cancelled." # highlight-end ``` Notice that your confirmation question uses curly brackets `{}` to include slot values in your response. -Add a `collect_information` step to your flow and give it the id `confirm_transfer`. -Also change the `next` attribute of the `ask_amount` step to match the `id` of your newly created step: `confirm_transfer`. +Add a `collect` step to your flow for the slot `final_confirmation`. +This step includes a `next` attribute with your branching logic. +The expression after the `if` key will be evaluated to true or false to determine +the next step in your flow. +The `then` and `else` keys can contain either a list of steps or the `id` of a step +to jump to. +In this case, the `then` key contains an `action` step to inform the user their transfer +was cancelled. The `else` key contains the id `transfer_successful`. +Notice that you've added this `id` to the final step in your flow. + ```yaml-rasa title="flows.yml" flows: transfer_money: description: This flow lets users send money to friends and family. steps: - - id: "ask_recipient" - collect_information: recipient - next: "ask_amount" - - id: "ask_amount" - collect_information: amount - # highlight-start - next: "confirm_transfer" - - id: "confirm_transfer" - collect_information: final_confirmation + - collect: recipient + - collect: amount + description: the number of US dollars to send + # highlight-start + - collect: final_confirmation next: - - if: final_confirmation - then: "transfer_successful" - # highlight-end - - id: "transfer_successful" - action: utter_transfer_complete + - if: not final_confirmation + then: + - action: utter_transfer_cancelled + next: END + - else: transfer_successful + # highlight-end + - action: utter_transfer_complete + # highlight-next-line + id: transfer_successful + ``` -The `next` attribute of your `confirm_transfer` step includes a `condition`. -The expression after the `if:` key will be evaluated and return `True` or `False`. -In this case, we're using the value of the boolean slot `final_confirmation`. -Conditions can be used to create branching logic, including logical operators like `AND`, `OR`, etc. -You can learn more about conditions and advanced logic here (link). -To try out the updated version of your assistant, run `rasa train`, and then `rasa shell` to talk to your bot. +To try out the updated version of your assistant, run `rasa train`, and then `rasa shell` to talk to your assistant. It should now ask you to confirm before completing the transfer. + ## Integrating an API call An `action` step in a flow can describe two types of actions. @@ -311,15 +341,15 @@ If the name of the action starts with `utter_`, then this action sends a message The name of the action has to match the name of one of the `responses` defined in your domain. The final step in your flow contains the action `utter_transfer_complete`, and this response is also defined in your domain. Responses can contain buttons, images, and custom payloads. -You can learn more about everything you can do with responses here (link). +You can learn more about everything you can do with responses [here](./concepts/responses.mdx). The second type of `action` is a custom action. The name of a custom action starts with `action_`. -You are going to create a custom action, `action_check_balance`, to check whether the user has enough money -to make the transfer, and then add logic to your flow to handle both cases. +You are going to create a custom action, `action_check_sufficient_funds`, to check whether the +user has enough money to make the transfer, and then add logic to your flow to handle both cases. Your custom action is defined in the file `actions.py`. -To learn more about custom actions, go here (link). +To learn more about custom actions, go [here](./concepts/custom-actions.mdx). Your `actions.py` file should look like this: @@ -329,9 +359,9 @@ from rasa_sdk import Action, Tracker from rasa_sdk.executor import CollectingDispatcher from rasa_sdk.events import SlotSet -class ActionSufficientFunds(Action): +class ActionCheckSufficientFunds(Action): def name(self) -> Text: - return "action_sufficient_funds" + return "action_check_sufficient_funds" def run(self, dispatcher: CollectingDispatcher, tracker: Tracker, @@ -348,10 +378,9 @@ Slots are the primary way to pass information to and from custom actions. In the `run()` method above, you access the value of the `amount` slot that was set during the conversation, and you pass information back to the conversation by returning a `SlotSet` event to update the `has_sufficient_funds` slot. -diagram of how slots are used with custom actions + +diagram of how slots are used with custom actions + Now you are going to make three additions to your `domain.yml`. You will add a top-level section listing your custom actions. @@ -361,7 +390,7 @@ add a new response to send to the user in case they do not have sufficient funds ```yaml-rasa title="domain.yml" # highlight-start actions: - - action_sufficient_funds + - action_check_sufficient_funds # highlight-end slots: @@ -384,38 +413,43 @@ responses: Now you are going to update your flow logic to handle the cases where the user does or does not have enough money in their account to make the transfer. +Notice that your `collect: final_confirmation` step now also has an id so that your branching logic +can jump to it. + ```yaml-rasa title="flows.yml" flows: transfer_money: description: This flow lets users send money to friends and family. steps: - - id: "ask_recipient" - collect_information: recipient - next: "ask_amount" - - id: "ask_amount" - collect_information: amount - # highlight-start - next: "check_funds" - - id: "check_funds" - action: action_sufficient_funds + - collect: recipient + - collect: amount + description: the number of US dollars to send + # highlight-start + - action: action_check_sufficient_funds next: - - if: has_sufficient_funds - then: "confirm_transfer" - - else: "insufficient_funds" - - id: "insufficient_funds" - action: utter_insufficient_funds - # highlight-end - - id: "confirm_transfer" - collect_information: final_confirmation + - if: not has_sufficient_funds + then: + - action: utter_insufficient_funds + next: END + - else: final_confirmation + # highlight-end + - collect: final_confirmation + # highlight-next-line + id: final_confirmation next: - - if: final_confirmation - then: "transfer_successful" - - id: "transfer_successful" - action: utter_transfer_complete + - if: not final_confirmation + then: + - action: utter_transfer_cancelled + next: END + - else: transfer_successful + - action: utter_transfer_complete + id: transfer_successful ``` ## Testing your Custom Action + + Custom actions are run as a separate server to the main Rasa assistant. To start your custom action server, create a new terminal tab and run: @@ -430,8 +464,8 @@ action_endpoint: url: "http://localhost:5055/webhook" ``` -Then re-start your bot by running `rasa shell`. -When you reach the `"check_funds"` step in your flow, Rasa will call the custom action `action_sufficient_funds`. +Then re-start your assistant by running `rasa shell`. +When you reach the `"check_funds"` step in your flow, Rasa will call the custom action `action_check_sufficient_funds`. We have hardcoded the user's balance to be `1000`, so if you try to send more, the assistant will tell you that you don't have enough funds in your account.