# Custom graph component for sentiment analysis

**URL:** <https://forum.rasa.com/t/custom-graph-component-for-sentiment-analysis/50599>\
**Category:** Rasa Open Source\
**Created:** [January 14, 2022, 3:17pm UTC](https://forum.rasa.com/t/custom-graph-component-for-sentiment-analysis/50599 "2022-01-14T15:17:21Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alaa](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@Alaa](https://forum.rasa.com/u/Alaa)\
**Post date:** [January 14, 2022, 3:17pm UTC](https://forum.rasa.com/t/custom-graph-component-for-sentiment-analysis/50599/1 "2022-01-14T15:17:21Z")

</div>

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 sentiment.py code :

```auto
from __future__ import annotations

from rasa.engine.graph import GraphComponent, ExecutionContext

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

from rasa.engine.recipes.default_recipe import DefaultV1Recipe

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

from rasa.engine.graph import ExecutionContext

from rasa.engine.storage.resource import Resource

from rasa.engine.storage.storage 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.register(
    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

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

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

        pass

    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"""
        pass

```

I have this errors :

```auto
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 rasa.core.training.interactive - An exception occurred while recording messages.

Traceback (most recent call last):

  File "c:\users\hp\pycharmprojects\newpr\venv\lib\site-packages\rasa\core\training\interactive.py", 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\interactive.py", 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\interactive.py", line 161, in send_message
    return await endpoint.request(

  File "c:\users\hp\pycharmprojects\newpr\venv\lib\site-packages\rasa\utils\endpoints.py", 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}''

```

---

<div class="post-metadata">

**Author:** ![nik202](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/nik202/32/16098_2.png) [@nik202](https://forum.rasa.com/u/nik202)\
**Post date:** [January 14, 2022, 4:36pm UTC](https://forum.rasa.com/t/custom-graph-component-for-sentiment-analysis/50599/2 "2022-01-14T16:36:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Alaa](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@Alaa](https://forum.rasa.com/u/Alaa)\
**Post date:** [January 14, 2022, 5:13pm UTC](https://forum.rasa.com/t/custom-graph-component-for-sentiment-analysis/50599/3 "2022-01-14T17:13:23Z")

</div>

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](https://rasa.com/blog/enhancing-rasa-nlu-with-custom-components/)”, but i needed to modify the code, because the tutorial is too old and don’t work for rasa 3

---

<div class="post-metadata">

**Author:** ![nik202](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/nik202/32/16098_2.png) [@nik202](https://forum.rasa.com/u/nik202)\
**Post date:** [January 14, 2022, 6:43pm UTC](https://forum.rasa.com/t/custom-graph-component-for-sentiment-analysis/50599/4 "2022-01-14T18:43:00Z")

</div>

@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!

---

<div class="post-metadata">

**Author:** ![nik202](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/nik202/32/16098_2.png) [@nik202](https://forum.rasa.com/u/nik202)\
**Post date:** [January 14, 2022, 6:44pm UTC](https://forum.rasa.com/t/custom-graph-component-for-sentiment-analysis/50599/5 "2022-01-14T18:44:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Fares](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/fares/32/15478_2.png) [@Fares](https://forum.rasa.com/u/Fares)\
**Post date:** [January 14, 2022, 9:15pm UTC](https://forum.rasa.com/t/custom-graph-component-for-sentiment-analysis/50599/6 "2022-01-14T21:15:02Z")

</div>

@nik202 you are welcome bro

---

<div class="post-metadata">

**Author:** ![Fares](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/fares/32/15478_2.png) [@Fares](https://forum.rasa.com/u/Fares)\
**Post date:** [January 14, 2022, 9:17pm UTC](https://forum.rasa.com/t/custom-graph-component-for-sentiment-analysis/50599/7 "2022-01-14T21:17:24Z")

</div>

I’d recommend to use rasa 2.8 and try this [Pretrained sentiment component nltk](http://forum.rasa.com/t/pretrained-sentiment-component-nltk/47912) and if you have any error please let me know

---

<div class="post-metadata">

**Author:** ![nik202](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/nik202/32/16098_2.png) [@nik202](https://forum.rasa.com/u/nik202)\
**Post date:** [January 14, 2022, 10:59pm UTC](https://forum.rasa.com/t/custom-graph-component-for-sentiment-analysis/50599/8 "2022-01-14T22:59:15Z")

</div>

@Fares Thank you for the prompt reply Ben 🙂 and wish you a very awesome 2022 💐🙏

---

<div class="post-metadata">

**Author:** ![Alaa](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@Alaa](https://forum.rasa.com/u/Alaa)\
**Post date:** [January 15, 2022, 6:29am UTC](https://forum.rasa.com/t/custom-graph-component-for-sentiment-analysis/50599/9 "2022-01-15T06:29:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nik202](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/nik202/32/16098_2.png) [@nik202](https://forum.rasa.com/u/nik202)\
**Post date:** [January 15, 2022, 11:43pm UTC](https://forum.rasa.com/t/custom-graph-component-for-sentiment-analysis/50599/10 "2022-01-15T23:43:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![LindaA](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/lindaa/32/18445_2.png) [@LindaA](https://forum.rasa.com/u/LindaA)\
**Post date:** [July 1, 2022, 1:23pm UTC](https://forum.rasa.com/t/custom-graph-component-for-sentiment-analysis/50599/11 "2022-07-01T13:23:30Z")

</div>

Hi all,

I was just wondering whether downgrading to RASA 2.8 is the only solution or whether there is a way to run sentiment analysis also with RASA 3.0?

Thank you!

---

<div class="post-metadata">

**Author:** ![milsol](https://avatars.discourse-cdn.com/v4/letter/m/f19dbf/32.png) [@milsol](https://forum.rasa.com/u/milsol)\
**Post date:** [September 7, 2023, 12:29pm UTC](https://forum.rasa.com/t/custom-graph-component-for-sentiment-analysis/50599/12 "2023-09-07T12:29:17Z")

</div>

Hi all,

Is downgrading to RASA 2.x still the only solution to have sentiment analysis in RASA 3.x? I hope there is a better solution at the moment, these posts here are pretty old I guess…
