Building a Chat bot with RASA NLU

Meet tracy-bot, an order tracking bot that uses RASA NLU to understand user chats and provide relevant data from DB.

Imagine you run an e-commerce store, and customers frequently reach out to inquire about the status of their orders. Manually responding to each query can be time-consuming and error-prone. Tracy, our order tracking bot, will automate this process, allowing customers to easily check the status of their orders through a conversational interface.

Setting up RASA NLU

pip install rasa


Project setup

Create a new directory for your project and navigate to it:

mkdir tracy-bot
cd tracy-bot

Training Data

Create a file called nlu.md and define intents and entities along with their examples:


## intent:check_order_status
- What's the status of my order?
- Can you tell me the status of order #12345?
- Check my order status

## intent:track_order
- Track my order
- I want to track my order
- Where is my package?

Configuration: Create a file called config.yml and configure the NLU pipeline:

language: en
pipeline: supervised_embeddings

Training the Model: Run the following command to train the NLU model

rasa train nlu

Testing the Model: Evaluate the trained model by testing it with sample user queries

from rasa.nlu.model import Interpreter

interpreter = Interpreter.load("models/nlu")
result = interpreter.parse("Can you check the status of my order?")
print(result)

Building Tracy's Conversational Flow and Integration with Database:

  1. Building the Core:Create a file called domain.yml and define the actions and intents:
intents:
  - check_order_status
  - track_order

actions:
  - utter_greet
  - utter_checking_status

2. Designing the Stories: Create a file called stories.md and define the conversational flow using stories:

## happy path
* greet
  - utter_greet
* check_order_status
  - utter_checking_status

3. Implementing Custom Actions: Create a file called actions.py and define a custom action to fetch order status from a database:

from typing import Any, Text, Dict, List
from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher

class ActionCheckOrderStatus(Action):
    def name(self) -> Text:
        return "action_check_order_status"

    def run(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
        order_number = tracker.get_slot("order_number")
        # Implement database integration logic here
        status = get_order_status_from_database(order_number)
        dispatcher.utter_message(text=f"The status of your order #{order_number} is {status}.")
        return []

4. Training the Core Model: Run the following command to train the core model:

rasa train

5. Running Tracy: Start the bot's action server:

Below are some examples that showcase the back-and-forth interaction between the user and the order tracking bot, Tracy. Each user query is followed by Tracy's response, providing the necessary information based on the order number provided by the user.

rasa run actions