Rasa Admin Intents

Convenient Custom Actions

Posted by Greg Stephens on April 05, 2020 · 7 mins read

As you develop Rasa chatbots, you find yourself checking the Rasa logs and running docker commands. Instead of ssh’ing into your server to look at the log, config files or run a command, why not ask your bot.

Let’s create some custom actions and intents that allow’s the Rasa admin to ask the bot some common questions.

  • What’s your version
  • What are the current slot values
  • What are the f1/confidence scores for that last utterance

You can choose to set this up as undocumented features of the bot or even limit access to these actions to selected users based on their sender_id. You can find a working example of these in my demo Jokebot.

Chatbot Version

The admin intent I use the most is to ask the bot it’s version. Originally, I set a version variable in my actions.py and created an utterance with another version id which I updated when there were changes in the training data or bot config. I’ve since added Rasa API calls to get the Rasa & Rasa X version info.

Here’s the custom action for action_version. Note that this uses the Rasa X version API endpoint to get both the Rasa & Rasa X version and it hard codes the Rasa X hostname to rasa-x. If you’re not using Rasa X, you could change this to call the Rasa version endpoint.

class ActionVersion(Action):
    def name(self):
        logger.info("ActionVersion self called")
        # define the name of the action which can then be included in training stories
        return "action_version"

    def run(self, dispatcher, tracker, domain):
        #logger.info(">>> responding with version: {}".format(vers))
        #dispatcher.utter_message(vers) #send the message back to the user
        try:
            request = json.loads(requests.get('http://rasa-x:5002/api/version').text)
        except:
            request = { "rasa-x": "", "rasa": { "production": "" }}
        logger.info(">> rasa x version response: {}".format(request['rasa-x']))
        logger.info(">> rasa version response: {}".format(request['rasa']['production']))
        dispatcher.utter_message(f"Rasa X: {request['rasa-x']}\nRasa:  {request['rasa']['production']}\nActions: {vers}")
        return []

Next, you would add an intent, something like this:

## intent:version
- version
- show version
- what version
- what's your version
- show build date
- what is your build date
- what's your build
- show model version

And your story:

## story_version
* version
  - utter_version
  - action_version

And don’t forget to add the action_version to the actions: section of your domain.yml and version to the intents: section as well as the utter_version utterance for your model/training data version to the responses: section:

responses:
  utter_version:
  - text: "Model 1.0.1 - Mar 9, 2020"

Show All Slots

The next custom action will show all of the current slot values.

class ActionShowSlots(Action):
    def name(self):
        logger.info("ActionVersion self called")
        # define the name of the action which can then be included in training stories
        return "action_show_slots"

    def run(self, dispatcher, tracker, domain):
        msg = "Slots:\n"
        for k, v in tracker.slots.items():
            msg += f" {k} | {v}\n"
        dispatcher.utter_message(msg)
        return []

Setup the intent:

## intent:show_slots
- slots
- show slots
- view slots
- let's see the slots

Create the story:

## show_slots
* show_slots
  - action_show_slots

Finally, add the action_show_slots to the actions: section of your domain.yml and show_slots to the intents: section:

F1/Confidence Scores

This custom action looks back one iteration in the tracker and returns the NLU f1 or confidence scores for the most recent utterance.

class ActionLastIntent(Action):
    def name(self):
        return "action_f1_score"

    def run(self, dispatcher, tracker, domain):
        msg = intentHistoryStr(tracker, 1, 4)
        dispatcher.utter_message(msg) #send the message back to the user
        return []

Add some NLU examples:

## intent:f1_score
- show last intent
- debug last intent
- debug intent info
- show f1 score
- last f1 score
- last intent f1 score
- what did you say that
- f1 score
- f1
- confidence scores
- show confidence
- intent confidence
- nlu confidence

And a story:

## f1_score
* f1_score
  - action_f1_score

Don’t forget to add the action_f1_score to the actions: section of your domain.yml and f1_score to the intents: section: