Overwrite user message (custom component)

Hello!

I am trying to create a custom component, which overwrite the user input message. I am working on rasa version 2.3.4. I have read the documentation, the forum discussions, and have considered the different suggestions, but I can’t get it to work!

Your input → Hee llo!!! my name is: DÁÑIEL, 35=YEARS old! someone on the other side?? rasa.core.processor - Received user message ‘Hee llo!!! my name is: DÁÑIEL, 35=YEARS old! someone on the other side??’

My custom component not overwrite the user input message.

my config.yml looks like this:

language: es

pipeline:

  • name: edit_message.Preprocessor
  • name: SpacyNLP
    • model: es_core_news_sm

and my custom component: import typing

from typing import Any, Optional, Text, Dict, List, Type

import re

import unidecode

from rasa.nlu.components import Component

from rasa.nlu.config import RasaNLUModelConfig

from rasa.shared.nlu.training_data.training_data import TrainingData

from rasa.shared.nlu.training_data.message import Message

if typing.TYPE_CHECKING:

from rasa.nlu.model import Metadata

class edit_message(Component):

provides = ["text"]

#requires = []

defaults = {}

language_list = None

def __init__(self, component_config=None):

    super(edit_message, self).__init__(component_config)

def train(self, training_data, cfg, **kwargs):

    pass

def process(self, message: Message, **kwargs: Any) -> None:

    mt = message.get("text")

    if mt!=None:

        mt = unidecode.unidecode(mt)

        mt = mt.lower()  

        mt = re.sub(r'(\!)', " ! ", mt)

        mt = re.sub(r'(\:)', " : ", mt)

        mt = re.sub('=', ' = ', mt)

        mt = re.sub(r'(\?)', " ? ", mt)

        mt = re.sub(r'\s{2,}', ' ', mt)

        message.set('text', mt, add_to_output = True)

    pass

Can anybody help me?

Hi @rosodelu

What exactly is the component doing? Do you see any errors in the logs. Also, if you formated your post better it would be easier to help.

I want to add a custom component, to replace certain characters. In order to improve the recognition of entities separated by these.

Example user input message:

He llooo! my name is: Dáñiel! 15=years old

It would be overwritten as:

he lloo ! my name is : daniel ! 15 = years old

Then I could put as an example of training data:

he lloo ! my name is : [daniel](name) ! [15](years) = years old

I’ve been trying to change this line:

message.set (‘text’, mt, add_to_output = True)

to

message.set (‘text’, ‘hello’, add_to_output = True)

and now i am sure my component is working, and is working as i want, as in that case all input messages will be overwritten as hello.

Thanks @jjuzl !!

1 Like

Thanks for sharing your solution. I’ve been wondering whether it was safe or not to override the TEXT key in message. Seems like it’s safe.

Hey I am trying to achieve similar thing But what happens is my bot doesnt give a reply back I am not sure what mistake i am doing What should i do Also the message.set,.get where is this documentation

Maybe it’s because of the version you’re using. For me it works in the version of RASA 2.8.13.

So you mean to say that rasa custom components doesnt work in rasa 2.1.2?

No, what I mean is that the way to get the message text in another version MAY BE slightly different. Have you checked the documentation for the version you’re using?

Hey So i couldnt find the documentation for message.set or like that I checked the custom aomponents category and there is no mention of how to get the message