Insert slot value without return[SlotSet()]

Hi, I want to insert slot value without using return[SlotSet(' ',..)]. How can I do that between the code?

Is there something like tracker.set_slot()

1 Like

you should do it with returning events as you mentioned

yeah I know it is in good practice but for setting that slot value I have to create a new custom action which don’t have any other work except setting slot value. That’s why I am searching for new method to setting slot.

Resolved!!! tracker.slots[update_slot] = None

Checked with print slot is set but having an issue that it does not save slot in stories, stories have old slot values. Will try to resolve later.

It should be return [SlotSet(update_slot, None)]

@Ghostvv Yeah that’s the another and popular method but there are some people who need to fix the slots using tracker.slots[] method because they want to update slot in the middle of custom_action. I am trying to help those people.

P.S. Resolved my issue by using SlotSet method. Saw other posts so trying to help them.

I am trying to update the slots in the middle of custom_action, i have tried as you mentions above but i am facing the below error “TypeError: Object of type SlotSet is not JSON serializable” . Please help to resolve this issue. complete error as follows:

ERROR:flask.app:Exception on /webhook [POST] Traceback (most recent call last): File “C:\ProgramData\Miniconda2\envs\advchatbot\lib\site-packages\flask\app.py”, line 2292, in wsgi_app response = self.full_dispatch_request() File “C:\ProgramData\Miniconda2\envs\advchatbot\lib\site-packages\flask\app.py”, line 1815, in full_dispatch_request rv = self.handle_user_exception(e) File “C:\ProgramData\Miniconda2\envs\advchatbot\lib\site-packages\flask_cors\extension.py”, line 161, in wrapped_function return cors_after_request(app.make_response(f(*args, **kwargs))) File “C:\ProgramData\Miniconda2\envs\advchatbot\lib\site-packages\flask\app.py”, line 1718, in handle_user_exception reraise(exc_type, exc_value, tb) File “C:\ProgramData\Miniconda2\envs\advchatbot\lib\site-packages\flask_compat.py”, line 35, in reraise raise value File “C:\ProgramData\Miniconda2\envs\advchatbot\lib\site-packages\flask\app.py”, line 1813, in full_dispatch_request rv = self.dispatch_request() File “C:\ProgramData\Miniconda2\envs\advchatbot\lib\site-packages\flask\app.py”, line 1799, in dispatch_request return self.view_functionsrule.endpoint File “C:\ProgramData\Miniconda2\envs\advchatbot\lib\site-packages\flask_cors\decorator.py”, line 128, in wrapped_function resp = make_response(f(*args, **kwargs)) File “C:\ProgramData\Miniconda2\envs\advchatbot\lib\site-packages\rasa_core_sdk\endpoint.py”, line 91, in webhook return jsonify(response) File “C:\ProgramData\Miniconda2\envs\advchatbot\lib\site-packages\flask\json_init_.py”, line 321, in jsonify dumps(data, indent=indent, separators=separators) + ‘\n’, File “C:\ProgramData\Miniconda2\envs\advchatbot\lib\site-packages\flask\json_init_.py”, line 179, in dumps rv = json.dumps(obj, **kwargs) File "C:\ProgramData\Miniconda2\envs\advchatbot\lib\site-packages\simplejson_init.py", line 399, in dumps **kw).encode(obj) File “C:\ProgramData\Miniconda2\envs\advchatbot\lib\site-packages\simplejson\encoder.py”, line 296, in encode chunks = self.iterencode(o, _one_shot=True) File “C:\ProgramData\Miniconda2\envs\advchatbot\lib\site-packages\simplejson\encoder.py”, line 378, in iterencode return iterencode(o, 0) File "C:\ProgramData\Miniconda2\envs\advchatbot\lib\site-packages\flask\json_init.py", line 81, in default return _json.JSONEncoder.default(self, o) File “C:\ProgramData\Miniconda2\envs\advchatbot\lib\site-packages\simplejson\encoder.py”, line 273, in default o.class.name) TypeError: Object of type SlotSet is not JSON serializable.

Please help me, Thanks in advance.

Can you send me your code line like what you typed?

Thank you for your response, when I am trying to update slots in middle of functions I got the above issue, now issue is resolved when I replace the return step as follows - return[SlotSet(’ ',…)]. Thank You.

1 Like

so, when you update tracker.slots, you change the local copy of tracker, that is passed to the custom action, in order to notify rasa_core that the slot has changed, you need to return SlotSet event

2 Likes

Yeah, Thanx @Ghostvv for clarifying it.

Resolved!!!

@abhishakskilrock need to store some information as slots which are not actually part of required_slots for my form. That’s why i was trying to update those slots using tracker.slots[‘slot_name’]=slot_value . I was doing it inside a slot validation method that needs to return the value of that particular slot. So, i can’t return anything but that slot inside that validation method. As you said i can see those slots being set just after using tracker.slots(). But if i call tracker.get_slot() for those slots from a different method than it shows that the slots were not set. How did you resolve this? P.S : i can’t return SlotSet() event as the validation method should only return the particular slot the validation method was created for. Here is my code:

def validate_bank_account_number(
        self,
        value: Text,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any],
    ) -> Dict[Text, Any]:
        """Validate cuisine value."""
        intent_found=tracker.get_intent_of_latest_message()
        #print(intent_found)
        if intent_found=="last_two_transaction":
            #raise ActionExecutionRejection(self.name(),f"Failed to extract slot with action {self.name()}",deactivate_form=True)
            #raise ActionExecutionRejection(self.name(),f"Failed to extract slot with action {self.name()}")
            #return self._deactivate()
            #dispatcher.utter_message(template="utter_wrong_bank_account_number")
            FollowupAction(name="validate_last_two_transaction_form") 
        list_valid=["জিরো","ওয়ান","টু","থ্রি","ফোর","ফাইভ","সিক্স","সেভেন","এইট","নাইন"]
        dict_word_to_digit={"জিরো":'0',"ওয়ান":'1',"টু":'2',"থ্রি":'3',"ফোর":'4',"ফাইভ":'5',"সিক্স":'6',"সেভেন":'7',"এইট":'8',"নাইন":'9'}
        print("i am from bank account section and my value is ",value)
        if type(value)==str:
            if len(value.split())==13:
                 return {"bank_account_number": value}
        if type(value)!=list:
            dispatcher.utter_message(template="utter_wrong_bank_account_number")
            return {"bank_account_number": None}
        if len(value)!=3:
            dispatcher.utter_message(template="utter_wrong_bank_account_number")
            return {"bank_account_number": None}
        else:
            if len(value[0].split())!=13 and len(value[1].split())!=13 and len(value[2].split())!=13:
                dispatcher.utter_message(template="utter_wrong_bank_account_number")
                return {"bank_account_number": None}
            else:
                get_value=value[2]
            bank_id=''
            for i in get_value.split():
                bank_id=bank_id+dict_word_to_digit[i]
            show_df=pd.read_csv('/home/zaowad/Desktop/rasa/bank_bot_v6/actions/demo_dataset.csv')
            try:
                val_collection=list(show_df[show_df['Bank ID']==int(bank_id)].values[0])
            except:
                dispatcher.utter_message(template="utter_wrong_bank_account_number")
                return {"bank_account_number": None}
            print(bank_id)
            print(val_collection)
            tracker.slots["temp_name"]=val_collection[3]  
            tracker.slots["temp_date_of_birth"]=val_collection[2]
            tracker.slots["temp_ballance"]=val_collection[4]    
            tracker.slots["temp_transaction"]= val_collection[5]
            print(tracker.get_slot("temp_name"))
            print(tracker.get_slot("temp_date_of_birth"))
            print(tracker.get_slot("temp_ballance"))
            print(tracker.get_slot("temp_transaction"))  
        return {"bank_account_number": bank_id}

@akelad can you suggest any shorts of workaround?

I know it’s a bit late, However, you can try something

return [SlotSet("temp_name", val_collection[3]),
        SlotSet(["temp_date_of_birth", val_collection[2]),
        SlotSet("temp_ballance", val_collection[4]),
        SlotSet("temp_transaction", val_collection[5]),
        SlotSet("bank_account_number", bank_id),
]

should work. try

Still looking for a way to set a slot without using return [SlotSet(slot_name, slot_value)] statement.

I am trying to set the slot from another slot’s validation method and hence I can’t use the return statement method

1 Like

Hello, I have the same problem, that is, I want to save the slot value without returning it. How do you finally solve this problem?

Looking forward to your reply, thank you very much!