First, Python syntax issue:
You cannot use type hinting (: Optional[float]
) and default arguments (= None
) when passing arguments. SlotSet is not a function definition, it’s a constructor for an existing class.
- Wrong:
SlotSet('time_slot', timestamp: Optional[float] = None)
- Correct:
SlotSet('time_slot', timestamp)
Second, Rasa usage issue:
You should return a list of events at the end of the run()
method as I mentioned above
The slot will be set after the whole actions runs.
- Wrong:
event = SlotSet('time_slot', timestamp)
return []
- Correct:
event = SlotSet('time_slot', timestamp)
return [event]
Third, Python issue again:
You want to set the variable timestamp
inside the time_slot
, but you haven’t defined it anywhere.
- Wrong:
event = SlotSet('time_slot', timestamp)
return [event]
- Correct:
timestamp = datetime.datetime.now()
event = SlotSet('time_slot', timestamp)
return [event]
Fourth, logic issue:
You are trying to set the slot, then retrieve the value back right after. Even if SlotSet
worked inside a run()
method, that would be like doing b = a
then c = b
. Just do c = a
.
-
Wrong:
timestamp = datetime.datetime.now()
event = SlotSet('time_slot', timestamp)
time = tracker.get_slot('time_slot')
return [event]
-
Correct:
time = datetime.datetime.now()
return []
Finally, if you want to use the time inside the run()
method AND save it for future use:
time = datetime.datetime.now()
dispatcher.utter_message(time)
event = SlotSet('time_slot', time)
return [event]
Then, in the next action, your retrieve that slot using tracker.get_slot()
:
previous_time = tracker.get_slot('time_slot')
Or, if you want to retrieve the slot and set it again both in every action:
previous_time = tracker.get_slot('time_slot')
dispatcher.utter_message(f'The time of the previous action was {previous_time}')
new_time = datetime.datetime.now()
return [SlotSet('time_slot', new_time)]
And as @InnoOmnia said, please don’t paste pictures to help those who help you copy your code. Instead, use block quote formatting by placing three backticks (```) and not apostrophes (’’’) a line before and a line after the code
```
like this
```