Implementing multiple session support in a chatbot

250 views
Skip to first unread message

Parth Sharma

unread,
May 18, 2019, 9:47:24 AM5/18/19
to Django users

I am currently implementing a Chatbot purely in python.

In my current implementation, each time the user starts a new chat from a session, another Chatbot instance is launched and hence the Chatbot starts from the initial state.

I wish to change that behaviour and make it similar to let’s say chat on Facebook/Messenger, in which you can seamlessly move between sessions while having a chat without inconsistencies. Namely, I want these attributes:

  1. If the user enters anything from let’s say session A it should be immediately visible in all ongoing sessions. Similarly, the Chatbot reply should be visible in all the devices immediately.
  2. Have all sessions show the same chat history

To implement the first point, I used this example from the django-channels docs and modified it by creating a single group/chatroom for each user. All the sessions from the same user get connected to the same group/chatroom and hence receive all the messages in the group/chatroom regardless of where they were sent from.

However, this implementation currently has a bug. Each time that a user is connected, it initializes a Chatbot instance which starts from the initial state again while the older connections have Chatbot instances that are currently at a different state.

This leads to inconsistent replies which are different based on which window the user typed something in.

Basically instead of having two sessions talk to the same Chatbot instance, we have two sessions talking to two different Chatbot instances and messages from all these four sources are getting added to the same chatroom.

Moreover, we are wasting resources by keeping multiple Chatbot instances per user which increases with the number of currently active sessions.

I want all of the user windows to interact with the same Chatbot instance. What would be the best way to implement that?

Currently I can think of three solutions:

  1. Creating another Django project the Chatbot and make requests to that HTTP server. The Chatbot state is maintained in that server and any request from the user will go to the same Chatbot instance.
    • This is straightforward to implement for me (simply spin up another server)
    • This naturally solves all the problems regarding state as all the instances will query the same Chatbot object
  2. Creating a Master channel thread which will hold the actual Chatbot instance(a python object) and any new channels will will defer to it for the reply from the Chatbot.
    • This will be complicated to implement
    • I will have to maintain which thread is master and which ones are slaves
    • In situations where a user closes the master thread connection I will somehow have to change one of the slave connections to a master connection and pass the entire object(?!) or atleast pass the state variable and re-create the chatbot instance.
  3. Spawn an independent thread/process in python for the chatbot instance and have all the channel connections talk to that thread/process .
    • This will be hard for me to implement as I don’t currently know how to do IPC in python

Are there any other solutions possible? What would be the ideal solution?

I am using the following technologies:

  1. Django as the main backend, with Django Channels for WebSockets
  2. RASA NLU for the NLU component of the chatbot and a finite state machine model implemented using pytransitions for the dialog management in python

Chetan Ganji

unread,
May 18, 2019, 5:29:30 PM5/18/19
to django...@googlegroups.com
What you need is to implement 2 design patterns in your django app. Which ones?

1. Singleton Design Pattern for the Chatbot. All the users and all of their sessions are using the same chatbot instance.

Why?

Because when a new session is created for a user, he will be referring to the same chatbot instance and all of previous history would be accessible.
You need to add a timestamp to each message. So that when a new session is created,
all of previous messages can be to sent to this session by filtering on the timestamp of the messages.

This instance can be used to store all of the sessions of all of the users. Something like below.
That way when the observer getters and setters are called, they get a list of active session of the current user. 
So the actual message is sent by iterating over the active sessions. Hence all the sessions get the same message.


class Chatbot(object):

_sessions = {
"username1": {
"mobile": [],
"desktop": []
},

"username2": {
"mobile": [],
"desktop": []
}

}

@classmethod
def get_sessions(self, username):
return self._sessions[username]


Other way to achieve singleton effect is create a instance of the chatbot and import it wherever it is required. 
This will given the same effect as singleton but without implementing it.


nikola = Chatbot()


Now just import nikola everywhere it is required.

 
2. Observer Design Pattern for the syncing of the messages across multiple sessions.

Why?

Because, you need the chatbot to sync between multiple sessions. Same message needs to goto multiple recipients.
When a new session is created, you will have to send the previous messages to this new session.
This is the sole purpose of this design pattern. It's use is already explained above.

As you are using django channels for websockets, you will have to combine that code with this pattern.
I don't have any experience with websockets, you will have to figure it out :P 

I hope it helps :) 
 

Regards,
Chetan Ganji
+91-900-483-4183


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/463657a5-588d-475d-940d-4d7ec0c708d4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Parth Sharma

unread,
May 19, 2019, 5:18:52 PM5/19/19
to Django users
Thanks a lot for your reply !

In the first part of your reply. I didn't really understand where exactly would I actually use this 
nikola = Chatbot()
and in which files would I import it? Can you please elaborate on the second solution a bit? 

Regards,
Chetan Ganji
+91-900-483-4183


To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

Chetan Ganji

unread,
May 19, 2019, 5:55:57 PM5/19/19
to django...@googlegroups.com
Hi Parth,

There is only one solution. It has two parts in which you will implement 2 design patterns for the chatbot.
Singleton and Observer.

RE : Where would you use it? 
nikola = Chatbot() # it is called instantiating the chatbot

Wherever it is required :P 

The idea is to create one and only one instance of the chatbot for the whole project and make it available when the projects get loaded into the memory. Two of the best ways to do this is -
  1. Instantiate the chatbot in the wsgi.py file of the project. From this file, import it and use as required.
  2. Instantiate the chatbot in the settings.py file of the project. That way you could import it wherever it is required and access it as settings.nikola.

    from django.conf import settings


RE : in which files would I import it?

Wherever it is required :P 

Lets say, there is a view which will return a response. Its a fictional example, take it with grain and salt :P 

from django.conf import settings
from django.http import JsonResponse

def Reply(request, message):

sessions = settings.nikola.get_sessions(request.user.username)
response = settings.nikola.choose_response(message)

# send the message to all the remaining sessions, except the current one
# as the current session would get this reply in the response of this view
settings.nikola.send_session_replies(sessions)

return JsonResponse(data=response)


I hope its clear now :) 


Regards,
Chetan Ganji
+91-900-483-4183


I’m protected online with Avast Free Antivirus. Get it here — it’s free forever.

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.

I’m protected online with Avast Free Antivirus. Get it here — it’s free forever.
Reply all
Reply to author
Forward
0 new messages