Hi. I am trying to set a slot via a custom action based on a recognized emotional state. the custom action works, but the SetSlot does somehow not. Has anyone a hint?
Custom Action:
from typing import Any, Text, Dict, List
from rasa_sdk import Action, Tracker
from rasa_sdk.events import SlotSet
from rasa_sdk.executor import CollectingDispatcher
from transformers import pipeline
classifier = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base", return_all_scores=True)
class ActionEmotion(Action):
def name(self) -> Text:
return "action_emotion"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
text = str(tracker.latest_message["text"])
emotion = classifier(text)
output = emotion[0]
keys = []
values = []
for i in output:
for j in i.values():
if type(j) == str:
keys.append(j)
else:
values.append(j)
newdict = dict(zip(keys, values))
max_key = max(newdict, key=newdict.get)
dispatcher.utter_message(text="top emotion is: {}".format(max_key))
return [SlotSet("emotion", max_key)]
Domain:
slots:
emotion:
type: categorical
values:
- anger
- disgust
- fear
- joy
- neutral
- sadness
- surprise
mappings:
- type: custom
action: action_emotion
Output from rasa --debug:
2022-10-22 15:19:59 DEBUG rasa.core.processor - Action ‘action_emotion’ ended with events ‘[BotUttered(‘top emotion is: fear’, {“elements”: null, “quick_replies”: null, “buttons”: null, “attachment”: null, “image”: null, “custom”: null}, {}, 1666444799.731138)]’. 2022-10-22 15:19:59 DEBUG rasa.core.processor - Current slot values: firstname: None emotion: None audience: None sex: None session_started_metadata: None