Create custom policy

Hello everyone! I hope all of you doing well.

I installed Rasa 3.6.8 on my machine and tried to build a custom policy. After investigating the documentation, YouTube, and GitHub examples. I found out that there are no examples of creating a custom policy (hope to be fixed soon)

So the policy that I’m trying to create is simple, I want to execute specific actions depending on specific numbers

I registered the policy in the model configuration and built the following policy

@DefaultV1Recipe.register(
    [DefaultV1Recipe.ComponentType.POLICY_WITHOUT_END_TO_END_SUPPORT], is_trainable=False
)
class NumberPolicy(Policy):
    """Policy which maps numbers to actions.
    """

    @staticmethod
    def get_default_config() -> Dict[Text, Any]:
        return {"priority": 6}

    def train(
            self,
            training_trackers: List[TrackerWithCachedStates],
            domain: Domain,
            **kwargs: Any,
    ) -> Resource:
        return Resource(
            'NumberPolicy',
            output_fingerprint='NumberPolicyFingerprint',
        )

    def predict_action_probabilities(
        self,
        tracker: DialogueStateTracker,
        domain: Domain,
        rule_only_data: Optional[Dict[Text, Any]] = None,
        **kwargs: Any,
    ) -> List[float]:
        """Predicts an action."""
        result = self._default_predictions(domain)
        if tracker.latest_message.text is None:
            return result
        user_msg = tracker.latest_message.text.strip()
         if user_msg.isnumeric():
            idx = domain.index_for_action(NAME_OF_THE_ACTION)
            result[idx] = 1.0
            return result
        return result
    def _metadata(self) -> Dict[Text, Any]:
        return {"priority": self.priority}

    @classmethod
    def _metadata_filename(cls) -> Text:
        return "number_policy.json"

so my questions are;

  • what do I miss to not work?
  • do I really need to implement the train method or is it enough to write a pass or not mention it at all?

I would appreciate any help here and would appreciate it if Rasa team added some examples of a simple custom NLU component and Policies.

Thanks & Regards