How to get the value of previous slot in current form action?

Hi, I am building a contextual bot. In order to answer the current question, bot needs the previous contextual data. So, how can I get the value of previously set slot in the current form action?

PS: Bot has only 1 slot, so I want the previous slot value to proceed.

Your help will be appreciated. Thanks!!!

I’m not sure if I understood how your conversation goes.

  1. User asks a question
  2. Bot responds to the question based on what entities are in the user message(or some conditions are met)?

I don’t think you should use a form for this. I would use just a basic custom action.


Inside the custom action you can get current slot value using: tracker.get_slot('slotnamehere')

Latest intent: tracker.latest_message['intent'].get('name')

But if you feel like form works for you. You can use same methods inside your form. Use them when you validate a slot.

When you validate, users message is in a variable called value.

@k1m Conversation flow you mentioned is correct. Even if I don’t use forms, how will I get the previous value of slot(as it will be holding the current extracted value)?. I need this because my current conversation depends on previous slot value.

Example story:

user: Can I get a version?

bot : Sure, which version you want? - ver1, ver2, ver3?

user: ver2

bot: 2.2.3

user: show me rollback version

bot: ver2 doesn’t have rollback only ver1 has

So here bot needs to remember which version user has asked first only then it can give answer for rollback version.

If you know any alternate way to do this, kindly suggest that too. I am new to rasa and doesn’t know how to train bot for contextual data


@IgNoRaNt23 Off-course we use this method to get slot value, but here ver1, ver2, ver3, rollback version belongs to same slot named “version_type”. So in this case, how can we get the value of previous value of slot.

Maybe its me, but that example story doesn’t help to understand your problem at all. Where does the slot get set the first time? Where does it get set to something else?

After all the whole conversation is on the tracker, so you might retrieve it, but that gets ugly. And it’s probably not the way its supposed to be done.

Slot gets first set when user asks for version.

1st time slot is filled: [ver1] (version_type)

2nd time : [rollback version] (version_type)

So when user asks for rollback version, bot should check slot value(1st) and then respond

This is how I would do it:

Use a form for this part:

user : Can I get a version?

bot : Sure, which version you want? - ver1, ver2, ver3?

user : ver2

bot : 2.2.3

Make two slots. for example version and userVersion

Can I get a version? triggers the form. form will ask for a version and validate it. If it ok, set the value to userVersion and clear version

Use custom action for this:

user : show me rollback version

bot : ver2 doesn’t have rollback only ver1 has

triggers custom action.

userVersion = tracker.get_slot('userVersion')

# logic to find the rollback version for userVersion.

# dispatcher.utter_message('text here')

Make sure to check if userVersion == None. If it is trigger the form to get the version.

okay. So basically you want to say that I need to use 2 slots here and it won’t work with single slot, right?

And in general how to do the contextual training? Like do we need to use forms every time or use different slots and then maintain the context ?

The first time it would work using only one slot, but the second time it would skip the form because a slot is already set.

Here you can’t clear the slot at the end of the form because you need the slot if the user asks for a rollback version. If everything would happen inside the form you could clear all slots at the end and use only one slot.

Why to use form to get the version? You can validate user input.

If the user would ask for a weather, you could just take the entities(city/location) in the user message and run a custom action.

1 Like

Ok, got it. Thanks a lot for clarifying the concepts. :slightly_smiling_face::blush:

1 Like