How to adjust chatbot responses based on sentiment

Hello,

I integrated a sentiment analysis component into my chatbot. User messages are classified with a neutral, positive or negative sentiment provided as an entity. I am now thinking about ways to adjust the chatbot responses based on the extracted sentiment. I already implemented a custom action which cheers up the user if he is in a bad mood similar to the moodbot (rasa/examples/moodbot at master · RasaHQ/rasa · GitHub). However, I would prefer to make changes with more impact. I would suggest that the bot uses different styles of language based on the sentiment (e.g. adding emoticons based on the sentiment of the messages). What would be the best approach to do so?

Thanks!

@juste_petr May be you can help.I have also integrated Sentiment analyser.How do I make my bot response based on that

Hi lniklas1 @vaidehi , i know is a old post have anyone of you manage to do it ?

Hi @BELIEVEIT ,

I think a intuitive way to solve this is to use a combination of:

  1. Using the sentiment for setting a custom slot e.g. (setting it with a custom action)
slots:
  sentiment:
    type: categorical
    values:
      - positive
      - neutral
      - negative
  1. Use conditional response variationslink
responses:
  utter_based_on_sentiment:
    - condition:
        - type: slot
          name: sentiment
          value: positive
      text: "Hey, {name}. Nice to see you again! How are you?"

    - condition:
        - type: slot
          name: sentiment
          value: neutral
      text: "Hey, {name}. How are you?"

What do you think?

Kind regards
Julian

1 Like

@JulianGerhard Thanks You , this is what i’m looking for , i will try out later .

Edit: btw for this part : using sentiment for setting a custom slot e.g. (setting it with a custom action)

I think you are refering to Forms this doc.

I’m confuse,what should i do with the custom action , possible to show me an example too ? Thanks

Hi @BELIEVEIT ,

what I meant is that you don’t need to use a Form to set a slot. This can also be achieved by using a custom action, e.g. like this:

class ActionCheckSentiment(Action):
    def name(self) -> Text:
        return "action_check_sentiment"

    def extract_sentiment_from_tracker(self, tracker: Tracker) -> Dict:
        """Tries to check for present sentiment data. May affect the story."""
            return tracker.sentiment # keep in mind that you need to use your implementation here

    def run(self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
        sentiment = self.extract_sentiment_from_tracker(tracker=tracker)
        slot_sets = []
        slot_sets.append(SlotSet("sentiment", sentiment))
        return slot_sets

Actually you are returning a SlotSet Event. This avoids having the overhead of a Form.

Kind regards
Julian

1 Like

Thanks Julian Your solution is great but i can’t mark it as Solution as i’m not the post starter

HI Julian , I would like to ask more the custom action part (btw I test on RASA 2.6 it works on the part using the experimental features )

Currently I using raspberry pi with RASA 1.10.26 ver , so I might need to use this approach using Cust Action for the extract_sentiment_from_tracker function at the return part what implementation I need to do here and also how do I use it for my stories , I think a example might help me better to understand . Thanks :blush: