{"id":3881,"date":"2023-11-04T23:13:54","date_gmt":"2023-11-04T23:13:54","guid":{"rendered":"http:\/\/localhost:10003\/how-to-create-a-chatbot-with-rasa-and-python\/"},"modified":"2023-11-05T05:48:29","modified_gmt":"2023-11-05T05:48:29","slug":"how-to-create-a-chatbot-with-rasa-and-python","status":"publish","type":"post","link":"http:\/\/localhost:10003\/how-to-create-a-chatbot-with-rasa-and-python\/","title":{"rendered":"How to Create a Chatbot with Rasa and Python"},"content":{"rendered":"
With the increasing popularity of chatbots, more and more businesses are turning to the technology to automate customer support and provide personalized experiences. Rasa is an open-source machine learning framework that allows developers to build, improve, and deploy their own AI-powered chatbots. In this tutorial, we will walk through the process of creating a chatbot with Rasa and Python.<\/p>\n
Note<\/strong>: This tutorial assumes that you have basic knowledge of Python and the command line.<\/p>\n Rasa is an open-source framework for building AI-powered chatbots. It provides two main components: Rasa NLU<\/strong> for natural language understanding and Rasa Core<\/strong> for handling dialogue flows. With these tools, you can design conversational AI agents that can understand user inputs and generate appropriate responses.<\/p>\n Rasa NLU allows you to define intents, which represent the goal or meaning behind a user’s message, and entities, which are specific pieces of user information. On the other hand, Rasa Core handles the sequential aspect of conversation, allowing you to define a dialogue flow through a set of stories and design custom actions to perform specific tasks in response to user inputs.<\/p>\n To get started with Rasa, you’ll need to set up your development environment. Follow these steps to install the required dependencies:<\/p>\n Create a Virtual Environment<\/strong>: It’s always a good practice to work within a virtual environment to prevent conflicts with other Python projects. Open a command prompt or terminal and run the following command to create and activate a virtual environment:<\/p>\n You should see the version number of Rasa, indicating a successful installation.<\/p>\n<\/li>\n<\/ol>\n Now that your development environment is ready, let’s move on to designing conversations using stories.<\/p>\n Stories are a key component of Rasa Core. They represent sample conversations between a user and a chatbot and define the flow of the conversation. Each story consists of a series of user inputs (intents) and the corresponding bot responses.<\/p>\n To design a conversation, create a new file called In this example, the story starts with the intent With Rasa, you can define complex dialogues by using slots, which allow the bot to store and retrieve information during the conversation. Here’s an example of a story with slots:<\/p>\n In this example, the bot starts by greeting the user. Then, it prompts the user to order a pizza by asking for the pizza type. The answer to this question is stored in the By designing conversations with stories, you can define dialogue flows that have multiple paths based on user inputs. Now let’s move on to defining intents and entities.<\/p>\n Intents represent the goal or meaning behind a user’s message, while entities are specific pieces of user information. To define intents and entities in Rasa, you can create two separate files called The In this example, we have defined three intents: The In this example, we define two entities, Note that this is a simple example and you can define more complex intents, entities, and responses as needed for your chatbot. Now let’s move on to creating NLU training data.<\/p>\n NLU training data is used to train a machine learning model to understand user inputs. In Rasa, the training data is defined in the To create the training data, we can use Rasa’s Run the following command to start the interactive training session:<\/p>\n This will launch the interactive shell where you can enter example user inputs and annotate them with the correct intents and entities.<\/p>\n<\/li>\n Enter an example user input and press Enter. For example, you can enter “Hello, I want to order a pizza.”<\/p>\n<\/li>\n The shell will display the detected intents and entities, along with the confidence scores. If the detected intents and entities are correct, you can press Enter to continue. Otherwise, you can manually correct them.<\/p>\n<\/li>\n Continue entering example user inputs until you have a sufficient amount of training data.<\/p>\n<\/li>\n After you finish annotating the training data, press Ctrl + C to exit the interactive shell.<\/p>\n<\/li>\n<\/ol>\n The annotated training data will be automatically saved in the The dialogue model is responsible for managing the flow of the conversation and generating appropriate responses based on user inputs. In Rasa, the dialogue model is defined using stories and custom actions.<\/p>\n To build the dialogue model, create a new file called To train the dialogue model, use Rasa’s Here’s the command to train the model:<\/p>\n After the training is complete, the trained model will be saved in the Custom actions allow you to define custom behavior in response to user inputs. Actions can perform a wide range of tasks, such as calling external APIs, querying databases, or executing business logic.<\/p>\n To define a custom action, create a new Python file and subclass In this example, we define a custom action called The Inside the Now that we have defined the custom action, we can update the After defining the stories, NLU training data, and the dialogue model, it’s time to train the chatbot.<\/p>\n To train the chatbot, run the following command:<\/p>\n This command will read the stories, NLU training data, and the dialogue model file, and train a machine learning model using the Rasa framework.<\/p>\n Training the chatbot might take some time, depending on the complexity of your training data and the size of your model. After the training is complete, the trained model will be saved in the To test the chatbot, use Rasa’s To test the chatbot, follow these steps:<\/p>\n This file contains the sample conversations that you want to test.<\/p>\n<\/li>\n Run the following command to test the chatbot:<\/p>\n This command will evaluate the bot’s performance by comparing the predicted actions and responses with the expected ones from the test data.<\/p>\n<\/li>\n<\/ol>\n After running the test, Rasa will display the evaluation results, including the accuracy, precision, and F1-score.<\/p>\n Once you are satisfied with the performance of your chatbot, it’s time to deploy it to a production environment.<\/p>\n Rasa provides several deployment options, including running the chatbot as a web server, integrating it into a messaging platform like Facebook Messenger or Slack, or deploying it as a REST API.<\/p>\n You can find detailed instructions on how to deploy your chatbot in the Rasa Documentation<\/a>.<\/p>\n In this tutorial, we have learned how to create a chatbot with Rasa and Python. We started by setting up the development environment and defining conversations using stories. Then, we defined intents and entities, created NLU training data, and built the dialogue model. We also learned how to handle custom actions, train and test the chatbot, and deploy it to a production environment.<\/p>\n Rasa provides a powerful set of tools and libraries to create intelligent chatbots that can understand user inputs, generate contextual responses, and perform complex tasks. With Rasa, you have full control over the behavior of your chatbot and can easily customize it to fit your specific needs.<\/p>\n I hope this tutorial has given you a good understanding of how to create a chatbot with Rasa and Python. Now it’s time to unleash your creativity and build your own AI-powered chatbot!<\/p>\n","protected":false},"excerpt":{"rendered":" With the increasing popularity of chatbots, more and more businesses are turning to the technology to automate customer support and provide personalized experiences. Rasa is an open-source machine learning framework that allows developers to build, improve, and deploy their own AI-powered chatbots. In this tutorial, we will walk through the Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[20,83,76,77,79,85,75,82,78,81,80,84,74],"yoast_head":"\nIntroduction to Rasa<\/h2>\n
Setting up the Development Environment<\/h2>\n
\n
python3 -m venv myenv\nsource myenv\/bin\/activate # For Mac\/Linux\n.myenvScriptsactivate # For Windows\n<\/code><\/pre>\n<\/li>\n
pip install rasa\n<\/code><\/pre>\n<\/li>\n
rasa --version\n<\/code><\/pre>\n
Designing Conversations using Stories<\/h2>\n
stories.md<\/code>. Here’s an example of a simple story:<\/p>\n
## greeting\n* greet\n - utter_greet\n\n## goodbye\n* goodbye\n - utter_goodbye\n<\/code><\/pre>\n
greet<\/code>. When the user sends a message with the intent
greet<\/code>, the bot responds with the template
utter_greet<\/code>. Similarly, when the intent
goodbye<\/code> is detected, the bot responds with
utter_goodbye<\/code>.<\/p>\n
## order_pizza\n* greet\n - utter_greet\n* order_pizza\n - slot{\"pizza_type\": \"unknown\"}\n - utter_ask_pizza_type\n* inform{\"pizza_type\": \"margherita\"}\n - slot{\"pizza_type\": \"margherita\"}\n - utter_ask_pizza_size\n* inform{\"pizza_size\": \"large\"}\n - slot{\"pizza_size\": \"large\"}\n - utter_confirm_order\n* affirm\n - action_submit_order\n - utter_order_confirmation\n* goodbye\n - utter_goodbye\n<\/code><\/pre>\n
pizza_type<\/code> slot. The bot then asks for the size of the pizza and stores it in the
pizza_size<\/code> slot. Finally, it confirms the order and takes action to submit the order information.<\/p>\n
Defining Intents and Entities<\/h2>\n
nlu.md<\/code> and
domain.yml<\/code>.<\/p>\n
nlu.md<\/code> file contains examples of user inputs, along with the associated intents and entities. Here’s an example:<\/p>\n
## intent:greet\n- hey\n- hello\n- hi\n- good morning\n\n## intent:goodbye\n- bye\n- goodbye\n- see you later\n\n## intent:order_pizza\n- i want to order a pizza\n- can i get a pizza?\n- let's order pizza\n<\/code><\/pre>\n
greet<\/code>,
goodbye<\/code>, and
order_pizza<\/code>. Each intent consists of several examples of user inputs that represent the same meaning.<\/p>\n
domain.yml<\/code> file defines the entities, slots, and responses for the chatbot. Here’s an example:<\/p>\n
intents:\n - greet\n - goodbye\n - order_pizza\n\nentities:\n - pizza_type\n - pizza_size\n\nslots:\n pizza_type:\n type: unfeaturized\n pizza_size:\n type: unfeaturized\n\nresponses:\n utter_greet:\n - text: \"Hello! Welcome to our pizza bot.\"\n utter_goodbye:\n - text: \"Goodbye! Have a great day.\"\n utter_ask_pizza_type:\n - text: \"What type of pizza would you like to order?\"\n utter_ask_pizza_size:\n - text: \"What size would you like (small, medium, or large)?\"\n utter_confirm_order:\n - text: \"You ordered a {pizza_size} {pizza_type} pizza. Is that correct?\"\n utter_order_confirmation:\n - text: \"Your order has been received. Enjoy your pizza!\"\n<\/code><\/pre>\n
pizza_type<\/code> and
pizza_size<\/code>. We also define two slots for storing the values of these entities. The responses are predefined templates that the bot uses to generate responses during the conversation.<\/p>\n
Creating NLU Training Data<\/h2>\n
nlu.md<\/code> file that we created earlier.<\/p>\n
rasa shell nlu<\/code> command. This command allows you to interactively test and annotate your training data.<\/p>\n
\n
nlu.md<\/code> file.<\/p>\n<\/li>\n
rasa shell nlu\n<\/code><\/pre>\n
nlu.md<\/code> file. You can now move on to building the dialogue model.<\/p>\n
Building the Dialogue Model<\/h2>\n
domain.yml<\/code>. This file contains the definitions for intents, entities, slots, and responses, as well as the stories.<\/p>\n
rasa train<\/code> command. This command reads the stories and training data, and trains a machine learning model to understand user inputs and generate responses.<\/p>\n
rasa train\n<\/code><\/pre>\n
models<\/code> directory.<\/p>\n
Handling Custom Actions<\/h2>\n
rasa_sdk.Action<\/code>. Here’s an example:<\/p>\n
from rasa_sdk import Action\nfrom rasa_sdk.events import SlotSet\n\nclass SubmitOrderAction(Action):\n def name(self):\n return \"action_submit_order\"\n\n def run(self, dispatcher, tracker, domain):\n pizza_type = tracker.get_slot(\"pizza_type\")\n pizza_size = tracker.get_slot(\"pizza_size\")\n\n # Code to submit the order and get the confirmation number\n confirmation_number = \"123456\"\n\n dispatcher.utter_message(\"Your order has been received. Confirmation number: {}\".format(confirmation_number))\n\n return [SlotSet(\"pizza_type\", None), SlotSet(\"pizza_size\", None)]\n<\/code><\/pre>\n
SubmitOrderAction<\/code>. The
name<\/code> method returns the name of the action, which will be used when defining the action in the stories.<\/p>\n
run<\/code> method is called when the action is triggered. It receives three parameters:
dispatcher<\/code>,
tracker<\/code>, and
domain<\/code>. The
dispatcher<\/code> is used to send messages to the user, while the
tracker<\/code> gives you access to the conversation history and slot values. The
domain<\/code> provides access to the domain definitions.<\/p>\n
run<\/code> method, you can perform any task needed to handle the action. In this example, we retrieve the values of the
pizza_type<\/code> and
pizza_size<\/code> slots and use them to submit the order. We then send a message to the user with the confirmation number and reset the slots to their initial values.<\/p>\n
domain.yml<\/code> file to include it in the response templates.<\/p>\n
Training the Chatbot<\/h2>\n
rasa train\n<\/code><\/pre>\n
models<\/code> directory.<\/p>\n
Testing the Chatbot<\/h2>\n
rasa test<\/code> command. This command reads a test data file with sample conversations and evaluates the bot’s performance.<\/p>\n
\n
test.md<\/code>. Here’s an example:\n
## order_pizza\n* greet\n - utter_greet\n* order_pizza\n - slot{\"pizza_type\": \"unknown\"}\n - utter_ask_pizza_type\n* inform{\"pizza_type\": \"margherita\"}\n - slot{\"pizza_type\": \"margherita\"}\n - utter_ask_pizza_size\n* inform{\"pizza_size\": \"large\"}\n - slot{\"pizza_size\": \"large\"}\n - utter_confirm_order\n* affirm\n - action_submit_order\n - utter_order_confirmation\n* goodbye\n - utter_goodbye\n<\/code><\/pre>\n
rasa test\n<\/code><\/pre>\n
Deploying the Chatbot<\/h2>\n
Conclusion<\/h2>\n