Enhacing Rasa NLU models with Custom Components

@Juste Hi I am facing an issue in implementing custom components in rasa_nlu. In config I have put name: “sentiment.SentimentAnalyzer”

The error that I am getting during training

Can you please help me on this? I am using rasa_nlu 0.15.0

Hi @rideep. You shouldn’t put the custom components code inside the rasa_nlu package. The file should sit in your project (assistant’s) directory. Do you get the same error if you structure your project files that way?

Anyone, please answer this question. How to load a model one single time and use it everytime?

Hi @Juste. I was implementing the sentiment analysis component but when training keep getting the error: AttributeError: module ‘rasa.nlu.utils’ has no attribute ‘json_pickle’. Running on rasa version 1.10.0 Tried to use python pickle module as well but no luck yet. Any specific thing that I am missing? For starters I just used the code in your blog post.

AttributeError: module ‘rasa.nlu.utils’ has no attribute ‘json_pickle’

Hi! @samgpt I read this right now, and I had the same issue trying to following the tutorial Enhancing Rasa NLU models with Custom Components… In rasa version 1.10.x try using this:

import rasa.utils.io as io_utils

and:

io_utils.json_pickle()

io_utils.json_unpickle()

This work for me. I’m using rasa 1.10.2

I write this here in case someone else needs. Regards!

Oops! I realized that there was a problem when the model is saved or loaded, and the SentimentAnalyser always gived to me the same answer! Finally I used pickle like suggest Collen here Sentiment analysis issue ! Need help please!

Hey can a add 2 custom components my config file will look like this

  • name: WhitespaceTokenizer

  • name: component 1

  • name : component 2

  • name: RegexFeaturizer … `

whenever I do this only component 2 is used. what is this happening.

Also, the form stops working when I use the custom sentiment component.

1 Like

With the custom components, make sure if it not is overwriting the object you want to spit out(tokens, featurizers or entities) it is possible that the second components initializes a new object and overrides the list object you want to append to.

happened to me when i introduced a custom entity extractor

an example would be

message.set(
            ENTITIES, message.get(ENTITIES, []) + extracted_entities, add_to_output=True
        )
1 Like

Hi @Juste I am using rasa 2.0 i create a python package inside the rasa nlu folder with the name of customcomponent inside of it I create a file sentiment.py . in the registr.py i do an import : from rasa.nlu.customcomponent import SentimentAnalyzer , I add the name of the class SentimentAnalyzer, in the component_classes list and I add this component in the config.yml : - name: sentiment.SentimentAnalyzer but It gives me this error : ModuleNotFoundError: No module named ‘sentiment’ can you help me please ?

I’m trying to make custom component for my bot, I’m getting an error tokens = [list(map(lambda x: x.text, t.get('tokens'))) for t in training_data] TypeError: 'NoneType' object is not iterable

can anyone help me out, what I understood is each object in training data does not have anything like tokens

@hemanthyernagula it sounds like training_data is none… Can you check this?

I am getting a similar error but when I check training_data it is not None.

@abhi are you following the tutorial? If not, could you make a new post for this question, so that we can keep this one from getting (even more) cluttered? You can feel free to tag me on the new post :slight_smile: Otherwise, could you share what you have modified from the tutorial?

hello @Juste So this what i did

  1. create the sentiments.py

  2. update my pipeline in config.yml

  3. create the labels.txt

I have trained my bot but when i run it (rasa shell --debug) i get nothing and i can’t print the sentiment and the confidence? I’m very confused

Hello @Juste

  1. Can you please confirm that the sentiment.py would go in the main project directory and not inside the actions folder?

  2. Can you also tell me how can I check the rasa nlu output? I usually use the tracker.events. But if I have to check if my custom component has been called or giving me the right results, how can I check where the convert_to_rasa() is being added?

  3. Could you also give more insight on setting the PYTHONPATH for my custom component to be picked up by rasa? by that I mean more step by step instruction on setting the PYTHONPATH… Does that mean i just do ‘which python’ and add that to my PATHONPATH and then add this to ~/.bash_profile ’ export PYTHONPATH=/path_to_your_project_dir/:$PYTHONPATH’ ?

  4. The last question would be using huggingface models with ‘import pipeline’ inside custom components. Do you see any issues with that approach?

Looking forward to hearing from you. Thanks

Here is the latest blog regarding implementing custom components in rasa 3.0

1 Like

@Baval in this blog it’s not describing how to train a model in custom component. He makes his is trained = False., I have implemented a custom component of sentiment analyzer, but I am confused that in which category I should mentioned it like in Tokenizer or classifier where should I put this component in config.yml pipeline.

Use

replace your training_data to training_data.intent_examples

with open(‘labels.txt’, ‘r’) as f: labels = f.read().splitlines() texts = [e.get(TEXT) for e in training_data.intent_examples if e.get(TEXT)]

Is your custom component build in Rasa 3.0? If so, can you share your sentiment analyzer?

Yeah I have build custom component in Rasa 3.0 only