Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alinasrullayev/fc08c73728cbd3ec59b59ccba3c19500 to your computer and use it in GitHub Desktop.
Save alinasrullayev/fc08c73728cbd3ec59b59ccba3c19500 to your computer and use it in GitHub Desktop.
ActionDefaultAskAffirmation
from rasa_core_sdk import Action
import csv
class ActionDefaultAskAffirmation(Action):
"""Asks for an affirmation of the intent if NLU threshold is not met."""
def name(self):
return "action_default_ask_affirmation"
def __init__(self):
self.intent_mappings = {}
# read the mapping from a csv and store it in a dictionary
with open('intent_mapping.csv', newline='', encoding='utf-8') as file:
csv_reader = csv.reader(file)
for row in csv_reader:
self.intent_mappings[row[0]] = row[1]
def run(self, dispatcher, tracker, domain):
# get the most likely intent
last_intent_name = tracker.latest_message['intent']['name']
# get the prompt for the intent
intent_prompt = self.intent_mappings[last_intent_name]
# Create the affirmation message and add two buttons to it.
# Use '/<intent_name>' as payload to directly trigger '<intent_name>'
# when the button is clicked.
message = "Did you mean '{}'?".format(intent_prompt)
buttons = [{'title': 'Yes',
'payload': '/{}'.format(last_intent_name)},
{'title': 'No',
'payload': '/out_of_scope'}]
dispatcher.utter_button_message(message, buttons=buttons)
return []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment