Add custom ML model in Rasa custom actions

Hello, I have created a ‘sentence-similarity’ model using ‘sentence_transformers’. I wanted to include that in rasa custom action. Sentence similarity is one of the functionality my chatbot perform. But I am getting an error 'ModuleNotFoundError: No module named ‘sentence_transformers’ whenever I try to run ‘rasa run actions’ command.

sentence_transformers package is installed in the virtual environment I am using.

Note: I have also read, it is doable with REST API call. But I don’t want to do that.

Could you share the custom action code that is using the similarity feature? In essense you should merely need to import it, but I’m wondering if you’re running rasa run actions from the correct virtual environment. Could you confirm the venv by running rasa --version just before running rasa run actions?

Could you explain the use-case for sentence similarity a bit more? You want your custom action to tell the user if their message is similar to something it saw before?

Thank you very much for your reply. Yes the virtual environment is same for both the rasa and the transformer model. I created a separate class for the model and it is running fine independently but when I call it in custom action class it was causing the error.

I was able to find the way around to solve the issue by using hugging face and downloading the whole model and mean_pooling method: sentence-transformers/all-mpnet-base-v2 · Hugging Face The sentence similarity model I am using to score the similarity of two sentences (the similarity of user utterance and the statements in my knowledge base)

I am still unable to understand why the class is working independently but when called in custom action it create error. Here is the code for reference

class FunctionsSimilarity():
        def __init__(self):
            super(FunctionsSimilarity, self).__init__()
            self.tokenizer = AutoTokenizer.from_pretrained('sentence-transformers/all-mpnet-base-v2')
            self.model = AutoModel.from_pretrained('sentence-transformers/all-mpnet-base-v2')

        def similarity(self, sentencesList, sentenceToCompare):
            sentenceList_embeddings = self.transformerModel.encode(sentencesList, convert_to_tensor=True)
            sentence_embeddings = self.transformerModel.encode(sentenceToCompare, convert_to_tensor=True)
            similarity = util.pytorch_cos_sim(sentenceList_embeddings, sentence_embeddings)
            return similarity

class ActionAddFunctionality(Action):

     def name(self) -> Text:
         return "action_add_functionality"

     def run(self, dispatcher: "CollectingDispatcher", tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
         sentenceSimilarity = FunctionsSimilarity()
         sentenceSimilarity.similarity(['this is sentence 1', 'this is sentence 02'], tracker.latest_message.text)
         return dispatcher.utter_message("Found it!!")

Could you share the full trace?