Skip to content

Instantly share code, notes, and snippets.

@wochinge
Last active September 15, 2022 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wochinge/ace2130f5031258fce65c86428247910 to your computer and use it in GitHub Desktop.
Save wochinge/ace2130f5031258fce65c86428247910 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