[Solved]Retrieving conversation from Mongotrackerstore/Redistrackerstore and continuing with the previous conversation

I have saved user bot conversations in mongotrackerstore which is saving the user-bot conversation. suppose the user logs off and starts the conversation after some time with different device(say phone). Is there any way to get the past conversations and continue our next conversation as based on the previous ones.

I have tried a method

from rasa_core.trackers import DialogueStateTracker tracker1=DialogueStateTracker(sender_id,domain.slots) events=db.retrieve(sender_id).as_dialogue().event

for event in events:

print(event.as_story_string())

tracker1.update(event)

@akelad I got a solution where I am able to update the tracker as per past events for the same user so that user can continue the conversation from where he left the conversation.Also,i am able to print the USER-BOT conversation from the past events. from rasa_core.trackers import DialogueStateTracker; tracker1=DialogueStateTracker(sender_id,domain.slots); events=db.retrieve(sender_id).as_dialogue().event;

for event in events:

tracker1.update(event);

event_dict=event.as_dict();

if event_dict['event']=='bot' or event_dict['event']=='user': print(event_dict['text'])

As long as the sender id is the same when the user logs in, it should automatically retrieve the history of the conversation i believe

@akelad I tried with just retrieving the conversation and see whether it continues with the previous conversation. But it was not happening since tracker was not updated. We have to update the tracker with previous events to continue with the same conversation.

when i am using above code, i am getting send_id not define. from where are u getting this sender_id.

My code is below for the reference-

action_endpoint = EndpointConfig(url=“http://0.0.0.0:5055/webhook”)

db = MongoTrackerStore(domain=“d.yml”,host=‘host ip’, db=‘xyz’, username=“x”,password=“x”,collection=“x”,event_broker=None)

    tracker1=DialogueStateTracker(sender_id,domain.slots) 
    events=db.retrieve(sender_id).as_dialogue().event
    for event in events:
            print(event.as_story_string())
            tracker1.update(event)

    agent = Agent.load('models/dialogue', interpreter='models/current/nlu',action_endpoint = action_endpoint,tracker_store=db)

I am getting send_id not define,from where will i get sender_id?

Is there a solution? This is really tricky, I hope rasa docs can explain more about tracker store and event broker

Hi @JialiuXu,

If you have given the details of tracker_store database in endpoints.yml, metadata of your conversation will get stored against the sender property that you use while sending messages to Rasa server.

{
  "message": "Hi there!",
  "sender": "JialiuXu"
}

This sender or rather sender_id is actually the conversation_id.

If you restart your server and send another message with the same sender id, the tracker will fetch all the metadata stored in your tracker store database and replay all those messages in the background to reach the previous state. You don’t have to manually retrieve/fiddle with any message.

Here the previous state refers to the state of tracker after the last message with the same sender_id was sent to Rasa server before restart.

That is what @akelad is referring to in her message.

Hie, where do you insert this piece of code. inside actions.py ? could you show the sample of your code

@ashukrishna100

hey @akelad from the above solution where do insert the code. would really appreciate your help on this.

The gentlemen that started this thread said that the solution was this peace of code from rasa_core.trackers import DialogueStateTracker; tracker1=DialogueStateTracker(sender_id,domain.slots); events=db.retrieve(sender_id).as_dialogue().event;

for event in events:

tracker1.update(event);

event_dict=event.as_dict();

if event_dict['event']=='bot' or event_dict['event']=='user': print(event_dict['text'])

So i’m just trying to find out, where do i place this solution. does it go inside my actions.py or any of the backend files(trackers.py or tracker_store.py)

@jacobdanielfcs Did you finally figureout where to put that piece of Code? @akelad can you please help with this matter as to where do we place the above code? do we put it in the actions.py?

This piece of code shouldn’t be necessary. If your tracker is persisted somewhere (e.g. MongoDB), the conversation should continue where the user left off if they have the same sender ID (depending on your sessions settings). You can also use the api to get the tracker of a conversation.

Thank you @akelad for your clarification, I got your point, do you know where do we tweak the session settings? can you provide your inputs on that ?

In the domain file :slight_smile:

one of the good source, to store your data.

YouTube