How to create a custom Component that returns a message instead of an Intent?

Hi, I want to create based on the intent of the DIETClassifier and the last n messages my own response selection algorithm. I also looked at the code of ResponseSelector but I didn’t get the clue how it selects the message which is presented to the user. It would be very helpful if someone could provide me an example where there is a custom component which returns in the process method always “Hello world” as bot response. The way I want to do this is the following:

@DefaultV1Recipe.register(
    DefaultV1Recipe.ComponentType.INTENT_CLASSIFIER, is_trainable=False
)
class CustomComponent(GraphComponent):
    // initialization etc
    ....
    def process(self, messages: List[Message]) -> List[Message]:
        //here I want to return "Hello World" as a bot message to the user

Is the way I want to do it feasable or is there another (better) way?

Why do you want to do this as an NLU component? Can you explain the use case in more detail?

Hi @stephens, our main goal is to create a bot that represents a client in a counselling session. Now I want to integrate a Coherence Measurement into rasa, which measures based on the chat history and the possible responses for an intent a score about which output fits best to the input. I will do an example:

Chat history: [Bot]: Hi, I am Martin and I have a problem with my son.

[User]: Hello Martin, I am your counsellor. What problem do you have with your son?

Recognized Intent: demand_for_problem

  • rule: rule_demand_for_problem

     steps: 
    
        -- intent: demand_for_problem
        -- action: utter_demand_for_problem
    

doain.yml

responses:

utter_demand_for_problem
   -- text: My son takes drugs
   -- text: My dad takes drugs

Now my coherenceMeasureComponent should measure for each text in utter_demand_for_problem a score with the possible responses in utter_demand_for_problem and the chat history and should return the response with the highest score to the user.

Is this possible with customComponents or should I use something else? Like custom actions?

This doesn’t sound like a good use case for a custom response selector.

The existing AugmentedMemoizationPolicy use this type of approach so you can include this in your policies.

I would start by creating your bot using Rasa best practices and existing nlu components and policies.

Thank you for response! I thought a policy just chooses the next action for instance (utter_demand_for_problem). What my Custom Component should do is after the prediction of the action (like utter_demand_for_problem) to predict a score for “My son takes drugs” and “My dad takes drugs” and returns the one to the user which has a higher score. I tried to write it down in pseudocode:

history; //history is a list containing all past messages in the on-going conversation

recognized_action = utter_demand_for_problem

scores = [ ]

// text is either “My son takes drugs” or “My dad takes drugs”

for text in recognized_action.responses:

    scores.append(score_model.predict(history, text)) 

return recognized_action.responses[argmax(scores)]

Here’s an architecture diagram

I thought a policy just chooses the next action

Policies control the dialogue. You can read more about them here. As I mentioned, memoization already does what you want so you don’t need something custom.

Custom Component should do is after the prediction of the action

No, the main output of the NLU is the intent and entity prediction (not dialogue - action/utterance prediction). The response selector is a special case for FAQ’s but you’re use case is not for FAQ’s because you want to look back in the conversation to decide the next step of the dialogue.

Thanks for response again. But if I look into the documentation of rasa I see a definition of Policies that says: “Your assistant uses policies to decide which action to take at each step in a conversation.” In my opinion that means, that a policy chooses an action not a response. In case of the MemoizationPolicy the documentation says:

“The MemoizationPolicy remembers the stories from your training data.” → I don’t have stories defined and I don’t want to access training data.

I just want to predict the coherence based on the context and the relevant responses. and return the best response. My model for this already works and does not has to be trained. Maybe I could build a custom action and call the custom action for this from a customPolicy, that every time an intent is recognized calls the custom action.