I am new to rasa and need a bit of help. I would like to be asked random multiple choice questions, and then the possible answers to appear as buttons. Using buttons seem to be quite simple, but how would you get a random question(from a list). Would I use a external database? Thanks
Hi,
I think you could define a custom action like this:
class RandomQuestionAction(Action):
def name(self) -> Text:
return "action_random_question"
def run(
self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict
) -> List[EventType]:
questions = {
'question1': ['answer1', 'answer2'],
'question2': ['answers']
}
question, answers = random.choice(list(questions.items()))
dispatcher.utter_message(
text=question,
buttons=[{"title": p, "payload": p} for p in answers]
)
return []
The dictionary questions
can contain the questions as key and the possible answers in a list as the values.
Muchas gracias!
Where would you write the correct answer out of the possible answers though, as a form of verification to tell the user if it is correct or not.
I think I would define the intents correct
and incorrect
, and change the action like this:
class RandomQuestionAction(Action):
def name(self) -> Text:
return "action_random_question"
def run(
self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict
) -> List[EventType]:
questions = {
'question1': [('answer1', '/correct'), ('answer2', '/incorrect')],
'question2': [('answers', '/intent')]
}
question, answers = random.choice(list(questions.items()))
dispatcher.utter_message(
text=question,
buttons=[{"title": a, "payload": i} for (a, i) in answers]
)
return []
Thus, when the user presses the correct button, the intent correct
is activated and in case of choosing the wrong answer, the intent incorrect
will be activated.
Then you can define the responses utter_correct
and utter_incorrect
and add the rules:
- rule: Correct answer
steps:
- intent: correct
- action: utter_correct
- rule: Incorrect answer
steps:
- intent: incorrect
- action: utter_incorrect
That really helps a lot, thanks!