Time between testing two questions

Hi,

Is there a way to determine the time elapsed between two consecutive test questions for a rasa chatbot?

Let’s say, I have a trained model and I am testing it out and I need to find how much time it took for the bot to give the response for 5th question (subtracting the timestamp of 4th from that of the 5th question).

Yes, Rasa Action server

Welcome to the forum! :slight_smile:

As @InnoOmnia mentioned, you should use Custom Actions. This lets you write custom Python code as a response to intents.

In these actions, you can set a Slot representing the timestamp of the last question by returning [SlotSet('time_slot': timestamp_now)] (see Events).

And you can use tracker.get_slot(‘time_slot’) to retrieve the value of the slot.

1 Like

Hi Chris and @InnoOmnia,

Thanks for your inputs. Based on your recommendations we tried the custom actions at our end and we have a hunch we are definitely close to achieving our aim. Below is how our custom action class looks like :

But this throws an error

File “/home/intel/Desktop/chatbot_tgl/venv/rasa_chatbot/WW40.3/actions/actions.py”, line 49 event = SlotSet(‘time_slot’, timestamp: Optional[float] = None) ^ SyntaxError: invalid syntax

Our understanding is we are missing on initialization of the timestamp parameter. We would appreciate if you can help us out to correct it.

Please, don’t use pictures but block quote formatting

anyway this

event = SlotSet('time_slot, timestamp: Optional[float] = none)

should be something like this

evt = [SlotSet(‘time_slot’: timestamp_now)]

1 Like

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
```