Custom graph component for sentiment analysis

Hi, im using rasa 3 and i’m trying to implement a custom graph component for sentiment analysis. I’m using a pretrained model with vadersentiment.

My code :

from __future__ import annotations

from rasa.engine.graph import GraphComponent, ExecutionContext

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

from import DefaultV1Recipe

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

from rasa.engine.graph import ExecutionContext

from import Resource

from import ModelStorage

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

from rasa.shared.nlu.constants import TEXT

from rasa.nlu.extractors.extractor import EntityExtractorMixin

from nltk.sentiment.vader import SentimentIntensityAnalyzer

    DefaultV1Recipe.ComponentType.ENTITY_EXTRACTOR, is_trainable=False

   class SentimentAnalyzer(GraphComponent, EntityExtractorMixin):
    """A pre-trained sentiment component"""

    name = "sentiment"
    provides = ["entities"]
    requires = []
    defaults = {}
    language_list = ["en"]

    def __init__(self, component_config: Dict[Text, Any]) -> None:

        self.component_config = component_config

    def create(
            config: Dict[Text, Any],
            model_storage: ModelStorage,
            resource: Resource,
            execution_context: ExecutionContext,
    ) -> GraphComponent:
        return cls(config)

    def train(self, training_data: TrainingData) -> Resource:


    def convert_to_rasa(self, value, confidence):
        """Convert model output into the Rasa NLU compatible output format."""

        entity = {"value": value,
                  "confidence": confidence,
                  "entity": "sentiment",
                  "extractor": "sentiment_extractor"}

        return entity

    def process(self, messages: List[Message]) -> List[Message]:
        """Retrieve the text message, pass it to the classifier and append the prediction results 
        to the message class."""
        sid = SentimentIntensityAnalyzer()
        for message in messages:
            res = sid.polarity_scores(message.get(TEXT))
            key, value = max(res.items(), key=lambda x: x[1])
            entity = self.convert_to_rasa(key, value)
            message.set("entities", [entity], add_to_output=True)

        return messages

    def persist(self, file_name, model_dir):
        """Pass because a pre-trained model is already persisted"""

I have this errors :

2022-01-14 15:05:34 ERROR    rasa.server  - An unexpected error occurred. Error: Error running graph component for node run_sentiment.SentimentAnalyzer9.

2022-01-14 15:05:34 ERROR  - An exception occurred while recording messages.

Traceback (most recent call last):

  File "c:\users\hp\pycharmprojects\newpr\venv\lib\site-packages\rasa\core\training\", line 1498, in record_messages
    await _enter_user_message(conversation_id, endpoint)

  File "c:\users\hp\pycharmprojects\newpr\venv\lib\site-packages\rasa\core\training\", line 1341, in _enter_user_message
    await send_message(endpoint, conversation_id, message)

  File "c:\users\hp\pycharmprojects\newpr\venv\lib\site-packages\rasa\core\training\", line 161, in send_message
    return await endpoint.request(

  File "c:\users\hp\pycharmprojects\newpr\venv\lib\site-packages\rasa\utils\", line 173, in request
    raise ClientResponseError(

rasa.utils.endpoints.ClientResponseError: 500, Internal Server Error, 
body='b'{"version":"3.0.4","status":"failure","message":"An unexpected error occurred. Error: Error running graph component for node run_sentiment.SentimentAnalyzer9.","reason":"ConversationError","details":{},"help":null,"code":500}''
ror running graph component for node run_sentiment.SentimentAnalyzer9.","reason":"ConversationError","details {},"help":null,"code":500}''

Hello and Welcome to the forum @Alaa.

Can you confirm that you are running rasa action server at the back end? Can you even share me the rasa --version whilst activating the environment?

If you can share me the tutorial link which you are following that will be great for me to comment or suggest. Thanks.

Hi, nik202 and Thank you for the answer. I’m using rasa 3. Yes, i’m running rasa action server at the back end. I followed this tutorial “How to Enhance Rasa NLU Models with Custom Components | Rasa Blog | The Rasa Blog | Rasa”, but i needed to modify the code, because the tutorial is too old and don’t work for rasa 3

@Alaa Yeps, I’m aware of this blog post.

@Alaa I’d recommend to please use rasa 2.x as the original blog post was initially developed on 2. x or please check the Juste repo on GitHub or even you can tag her here.

I can only help you here on this itself as I didn’t work on this personally. But, I tried my level best to help you. Good Luck!

pinging @Fares for the suggestion and help, please :slight_smile: I hope you don’t mind?

@nik202 you are welcome bro

I’d recommend to use rasa 2.8 and try this Pretrained sentiment component nltk and if you have any error please let me know

@Fares Thank you for the prompt reply Ben :slight_smile: and wish you a very awesome 2022 :bouquet::pray:

1 Like

thanks Nik , thanks Fares. I will change my Rasa version from 3 to 2.8 and try the code that Fares sent me

@Alaa great, please do remember to close this thread as solution and good luck!