# Dynamic KnowledgeBase : update inmemoryknowledgebase using custom action

**URL:** <https://forum.rasa.com/t/dynamic-knowledgebase-update-inmemoryknowledgebase-using-custom-action/52736>\
**Category:** Rasa Open Source\
**Created:** [April 21, 2022, 9:43am UTC](https://forum.rasa.com/t/dynamic-knowledgebase-update-inmemoryknowledgebase-using-custom-action/52736 "2022-04-21T09:43:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![yashjn72](https://avatars.discourse-cdn.com/v4/letter/y/bb73d2/32.png) [@yashjn72](https://forum.rasa.com/u/yashjn72)\
**Post date:** [April 21, 2022, 9:43am UTC](https://forum.rasa.com/t/dynamic-knowledgebase-update-inmemoryknowledgebase-using-custom-action/52736/1 "2022-04-21T09:43:47Z")

</div>

Hi Rasa Members,

I am working on building a chatbot where my requirement is to update InMemoryKnowledgeBase data based on requirement.

Currently, whenever I start action server InMemoryKnowledgeBase gets updated using below class.

```auto
class MyKnowledgeBaseAction(ActionQueryKnowledgeBase):
    def __init__ (self):
        knowledge_base = InMemoryKnowledgeBase("userdata.json")
        super(). __init__ (knowledge_base)

```

Above class is reading data from productlist.json file. I have a custom actions which updates “userdata.json” based on user’s ask.

Now, I am not able to reload data in InMemoryKnowledgeBase whenever that file is updated. Can someone share on how to update InMemoryKnowledgeBase using any other custom action or code snippet.

---

<div class="post-metadata">

**Author:** ![anca](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/anca/32/14540_2.png) [@anca](https://forum.rasa.com/u/anca)\
**Post date:** [April 22, 2022, 10:36am UTC](https://forum.rasa.com/t/dynamic-knowledgebase-update-inmemoryknowledgebase-using-custom-action/52736/2 "2022-04-22T10:36:05Z")

</div>

@yashjn72 Could you please share your `actions.py` file to understand how you are updating the `userdata.json` file currently, in addition to your training data files?

Looking through the codebase, I can see `InMemoryKnowledgeBase` has an [instance method](https://github.com/RasaHQ/rasa-sdk/blob/e58dfaa52ebbc3fa44c5e3b5771e5866fcd1176c/rasa_sdk/knowledge_base/storage.py#L128) `self.load()` that loads the json file given to its constructor and stores the data in a dictionary `self.data`. You might need to override the implementation of `run` method in your custom action extending `ActionQueryKnowledgeBase` by first calling `self.knowledge_base.load()` and then copying the [rest of the code](https://github.com/RasaHQ/rasa-sdk/blob/e58dfaa52ebbc3fa44c5e3b5771e5866fcd1176c/rasa_sdk/knowledge_base/actions.py#L103) currently in the `run` method to maintain existing functionality.

---

<div class="post-metadata">

**Author:** ![yashjn72](https://avatars.discourse-cdn.com/v4/letter/y/bb73d2/32.png) [@yashjn72](https://forum.rasa.com/u/yashjn72)\
**Post date:** [April 22, 2022, 10:56am UTC](https://forum.rasa.com/t/dynamic-knowledgebase-update-inmemoryknowledgebase-using-custom-action/52736/3 "2022-04-22T10:56:00Z")

</div>

Hi @anca ,

Please follow below code snippet for actions.py

```auto
class MyKnowledgeBaseAction(ActionQueryKnowledgeBase):
    def __init__ (self):
        knowledge_base = InMemoryKnowledgeBase("userdata.json")
        super(). __init__ (knowledge_base)

class somecustomjob(Action):
    def name(self) -> Text:
        return "action_somecustomaction"

    def run(self, dispatcher: CollectingDispatcher,
                tracker: Tracker,
                domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
        ## some code to generate userdata.json file 
        with open('userdata.json', 'w') as convert_file: 
            convert_file.write(json.dumps({"user_list":userdata}))
        
        knowledge_base = InMemoryKnowledgeBase("productlist.json")
        ActionQueryKnowledgeBase(knowledge_base)
       
        return [SlotSet(SLOT_OBJECT_TYPE, "user_list")]

```

I am using above method to reload inMemoryKnowledgeBase. But is is not working.

---

<div class="post-metadata">

**Author:** ![anca](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/anca/32/14540_2.png) [@anca](https://forum.rasa.com/u/anca)\
**Post date:** [April 22, 2022, 11:00am UTC](https://forum.rasa.com/t/dynamic-knowledgebase-update-inmemoryknowledgebase-using-custom-action/52736/4 "2022-04-22T11:00:16Z")

</div>

Yup, this probably doesn’t work because the main knowledge base action used by the bot is `MyKnowledgeBaseAction` whose `userdata.json` file never gets updated. Please try the suggestion I recommended.

---

<div class="post-metadata">

**Author:** ![yashjn72](https://avatars.discourse-cdn.com/v4/letter/y/bb73d2/32.png) [@yashjn72](https://forum.rasa.com/u/yashjn72)\
**Post date:** [April 22, 2022, 12:16pm UTC](https://forum.rasa.com/t/dynamic-knowledgebase-update-inmemoryknowledgebase-using-custom-action/52736/5 "2022-04-22T12:16:52Z")

</div>

Could you please share a sample snippet.

---

<div class="post-metadata">

**Author:** ![yashjn72](https://avatars.discourse-cdn.com/v4/letter/y/bb73d2/32.png) [@yashjn72](https://forum.rasa.com/u/yashjn72)\
**Post date:** [April 27, 2022, 12:24pm UTC](https://forum.rasa.com/t/dynamic-knowledgebase-update-inmemoryknowledgebase-using-custom-action/52736/6 "2022-04-27T12:24:59Z")

</div>

Well I am not able to implement what you have suggested. It will be very helpful if you can share a snippet.

My custom action is already extended by Action call, how can I extend it with another class.

userdata.json file is same. As it is created by previous rasa session before restart.

---

<div class="post-metadata">

**Author:** ![anca](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/anca/32/14540_2.png) [@anca](https://forum.rasa.com/u/anca)\
**Post date:** [April 29, 2022, 1:06pm UTC](https://forum.rasa.com/t/dynamic-knowledgebase-update-inmemoryknowledgebase-using-custom-action/52736/7 "2022-04-29T13:06:39Z")

</div>

@yashjn72 Please try this but beware that this is an untested recommendation:

```auto
class MyKnowledgeBaseAction(ActionQueryKnowledgeBase):
    def __init__ (self):
        knowledge_base = InMemoryKnowledgeBase("userdata.json")
        super(). __init__ (knowledge_base)

     async def run(
        self,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: "DomainDict",
      ) -> List[Dict[Text, Any]]:
        # this is the only line you need to introduce
        self.knowledge_base.load()
        
        # this is copied from current Rasa SDK Python implementation
        object_type = tracker.get_slot(SLOT_OBJECT_TYPE)
        last_object_type = tracker.get_slot(SLOT_LAST_OBJECT_TYPE)
        attribute = tracker.get_slot(SLOT_ATTRIBUTE)

        new_request = object_type != last_object_type

        if not object_type:
            dispatcher.utter_message(response="utter_ask_rephrase")
            return []

        if not attribute or new_request:
            return await self._query_objects(dispatcher, object_type, tracker)
        elif attribute:
            return await self._query_attribute(
                dispatcher, object_type, attribute, tracker
            )

        dispatcher.utter_message(response="utter_ask_rephrase")
        return []

```
