Why rasa dont support Policy based ont FSM? Is that possible by developing custom policy?

Usually fsm can be uesed in dialoge system ,i dont see why rasa abandon it ?

@ akelad@ chkosscan i get your help?

Hello!

You can model simple FSM using RulePolicy

  1. create default rasa project via rasa init command

  2. add this slot mappings to model state transitions

intents:
  - greet
  - goodbye

actions:
  - action_state_transition

slots:
  state:
    type: categorical
    initial_value: greet
    influence_conversation: true
    values:
      - greet
      - goodbye
    mappings:
    - type: custom
  new_state:
    type: categorical
    initial_value: greet
    influence_conversation: false
    values:
      - greet
      - goodbye
    mappings:
    - type: from_intent
      intent: goodbye
      value: goodbye
      condition:
      - slot_was_set:
        - state: greet
    - type: from_intent
      intent: greet
      value: greet
      condition:
      - slot_was_set:
        - state: goodbye

responses:
  utter_greet:
  - text: "Hey! How are you?"
  utter_happy:
  - text: "Great, carry on!"
  utter_goodbye:
  - text: "Bye"
  utter_iamabot:
  - text: "I am a bot, powered by Rasa."
  1. define state handlers as follows:
rules:

# GREET

- rule: Entrypoint
  condition: 
  - slot_was_set:
    - state: greet
  steps:
  - action: action_state_transition
  - action: utter_greet

- rule: Bye
  condition: 
  - slot_was_set:
    - state: greet
  steps:
  - intent: goodbye
  - action: utter_iamabot
  - action: action_state_transition
  wait_for_user_input: false

- rule: Greet
  condition:
  - slot_was_set:
    - state: greet
  steps:
  - intent: greet
  - action: utter_happy

# GOODBYE

- rule: Entrypoint
  condition: 
  - slot_was_set:
    - state: goodbye
  steps:
  - action: action_state_transition
  - action: utter_goodbye

- rule: Bye
  condition: 
  - slot_was_set:
    - state: goodbye
  steps:
  - intent: goodbye
  - action: utter_happy

- rule: Greet
  condition: 
  - slot_was_set:
    - state: goodbye
  steps:
  - intent: greet
  - action: utter_iamabot
  - action: action_state_transition
  wait_for_user_input: false

  1. add custom action:
from typing import Any, Text, Dict, List

from rasa_sdk import Action, Tracker
from rasa_sdk.events import SlotSet
from rasa_sdk.executor import CollectingDispatcher

class ActionStateTransition(Action):
    def name(self) -> Text:
        return "action_state_transition"

    def run(
        self, 
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any]
    ) -> List[Dict[Text, Any]]:
        return [SlotSet('state', tracker.get_slot('new_state'))]

State Machine is:

  • 2 states greet and goodbye
  • on intent goodbye being in state greet go to state goodbye
  • on intent greet being in state goodbye go to state greet
  • on intent greet being in state greet no transition
  • on intent goodbye being in state goodbye no transition

You are also able to use mapping conditions to model simple constrain of transitions and custom mappings to model complex transition logic

Thank you!

I am really thankful to your reply , but can you post the whole context of domain.yml, that will be a great help for me! Best for you ,Bro!

1 Like

I’ve edited previous reply. If it works for you, please mark post as a solution to help others find it quickly.

It works, thank you !

1 Like