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 :
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 :
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}''