gunthercox / ChatterBot

ChatterBot is a machine learning, conversational dialog engine for creating chat bots
https://chatterbot.readthedocs.io
BSD 3-Clause "New" or "Revised" License
14.08k stars 4.44k forks source link

Django session management isn't working #453

Closed vkosuri closed 7 years ago

vkosuri commented 7 years ago

@gunthercox How do i resolve this error.

[02/Dec/2016 07:38:10] "GET /static/js/bootstrap.js HTTP/1.1" 304 0
Internal Server Error: /api/chatterbot/
Traceback (most recent call last):
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/django/core/handlers/exception.py", line 39, in inner
    response = get_response(request)
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
    response = self._get_response(request)
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/chatterbot/ext/django_chatterbot/views.py", line 53, in post
    chat_session = self.chatterbot.conversation_sessions.get(chat_session_id)
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/chatterbot/conversation/session.py", line 41, in get
    return self.sessions[str(session_id)]
KeyError: '0b678184-b85a-11e6-b1d4-080027362d8d'
[02/Dec/2016 07:38:17] "POST /api/chatterbot/ HTTP/1.1" 500 16606
vkosuri commented 7 years ago
[02/Dec/2016 08:37:21] "GET /static/js/jquery.js HTTP/1.1" 304 0
[02/Dec/2016 08:37:21] "GET /static/js/js.cookie.js HTTP/1.1" 304 0
Internal Server Error: /api/chatterbot/
Traceback (most recent call last):
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/django/core/handlers/exception.py", line 39, in inner
    response = get_response(request)
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
    response = self._get_response(request)
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/developer/adtran-bot/abot/src/abot/chatterbot_views.py", line 53, in post
    chat_session = self.chatterbot.conversation_sessions.get(chat_session_id)
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/chatterbot/conversation/session.py", line 41, in get
    return self.sessions[str(session_id)]
KeyError: '0b678184-b85a-11e6-b1d4-080027362d8d'
[02/Dec/2016 08:37:22] "POST /api/chatterbot/ HTTP/1.1" 500 16531
Performing system checks...

/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/chatterbot/storage/jsonfile.py:19: UnsuitableForProductionWarning: The JsonFileStorageAdapter is not recommended for production application environments.
  self.UnsuitableForProductionWarning
[nltk_data] Downloading package stopwords to
[nltk_data]     /home/developer/nltk_data...
[nltk_data]   Package stopwords is already up-to-date!
[nltk_data] Downloading package wordnet to
[nltk_data]     /home/developer/nltk_data...
[nltk_data]   Package wordnet is already up-to-date!
[nltk_data] Downloading package punkt to /home/developer/nltk_data...
[nltk_data]   Package punkt is already up-to-date!
[nltk_data] Downloading package vader_lexicon to
[nltk_data]     /home/developer/nltk_data...
[nltk_data]   Package vader_lexicon is already up-to-date!
System check identified no issues (0 silenced).
December 02, 2016 - 08:39:14
Django version 1.10.4, using settings 'abot.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Internal Server Error: /api/chatterbot/
Traceback (most recent call last):
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/django/core/handlers/exception.py", line 39, in inner
    response = get_response(request)
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
    response = self._get_response(request)
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/developer/adtran-bot/abot/src/abot/chatterbot_views.py", line 53, in post
    chat_session = self.chatterbot.conversation_sessions.get(chat_session_id)
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/chatterbot/conversation/session.py", line 41, in get
    return self.sessions[str(session_id)]
KeyError: '0b678184-b85a-11e6-b1d4-080027362d8d'
[02/Dec/2016 08:39:36] "POST /api/chatterbot/ HTTP/1.1" 500 16531
Internal Server Error: /api/chatterbot/
Traceback (most recent call last):
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/django/core/handlers/exception.py", line 39, in inner
    response = get_response(request)
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
    response = self._get_response(request)
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/developer/adtran-bot/abot/src/abot/chatterbot_views.py", line 53, in post
    chat_session = self.chatterbot.conversation_sessions.get(chat_session_id)
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/chatterbot/conversation/session.py", line 41, in get
    return self.sessions[str(session_id)]
KeyError: '0b678184-b85a-11e6-b1d4-080027362d8d'
[02/Dec/2016 08:43:58] "POST /api/chatterbot/ HTTP/1.1" 500 16531
^C(venv)developer@cn-vm-vkosuri:~/adtran-bot/abot/src$ clear

(venv)developer@cn-vm-vkosuri:~/adtran-bot/abot/src$ python manage.py runserver
Performing system checks...

/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/chatterbot/storage/jsonfile.py:19: UnsuitableForProductionWarning: The JsonFileStorageAdapter is not recommended for production application environments.
  self.UnsuitableForProductionWarning
[nltk_data] Downloading package stopwords to
[nltk_data]     /home/developer/nltk_data...
[nltk_data]   Package stopwords is already up-to-date!
[nltk_data] Downloading package wordnet to
[nltk_data]     /home/developer/nltk_data...
[nltk_data]   Package wordnet is already up-to-date!
[nltk_data] Downloading package punkt to /home/developer/nltk_data...
[nltk_data]   Package punkt is already up-to-date!
[nltk_data] Downloading package vader_lexicon to
[nltk_data]     /home/developer/nltk_data...
[nltk_data]   Package vader_lexicon is already up-to-date!
System check identified no issues (0 silenced).
December 02, 2016 - 08:50:17
Django version 1.10.4, using settings 'abot.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Internal Server Error: /api/chatterbot/
Traceback (most recent call last):
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/django/core/handlers/exception.py", line 39, in inner
    response = get_response(request)
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
    response = self._get_response(request)
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/django/views/generic/base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/developer/adtran-bot/abot/src/abot/chatterbot_views.py", line 53, in post
    chat_session = self.chatterbot.conversation_sessions.get(chat_session_id)
  File "/home/developer/adtran-bot/venv/local/lib/python2.7/site-packages/chatterbot/conversation/session.py", line 41, in get
    return self.sessions[str(session_id)]
KeyError: '0b678184-b85a-11e6-b1d4-080027362d8d'
vkosuri commented 7 years ago

I think the browser caching session id, we need clear the browser session id, https://docs.djangoproject.com/en/dev/topics/http/sessions/#django.contrib.sessions.backends.base.SessionBase.flush

vkosuri commented 7 years ago

Currently i was handling like this at https://github.com/gunthercox/ChatterBot/blob/master/chatterbot/ext/django_chatterbot/views.py#L51,

@gunthercox am i doing in correct way?

        if chat_session_id:
            try:
                chat_session = self.chatterbot.conversation_sessions.get(chat_session_id)
            except Exception as e:
                print str(e)
                chat_session = self.chatterbot.conversation_sessions.new()
                chat_session_id = str(chat_session.uuid)
                request.session['chat_session_id'] = chat_session_id
        else:
            chat_session = self.chatterbot.conversation_sessions.new()
            chat_session_id = str(chat_session.uuid)
            request.session['chat_session_id'] = chat_session_id

        response_data = self.chatterbot.get_response(input_data, chat_session_id)

        data = {
            'text': str(response_data)
        }

        # Return a method not allowed response
        return JsonResponse(data, status=200)
gunthercox commented 7 years ago

This looks correct. This is an issue I am still looking in to. I think I have to look deeper into how Django's sessions work and specifically what triggers them to be created and destroyed.

vkosuri commented 7 years ago

Somehow i figured out this issue, It was failing because of initial or first conversation browser cookie session id and UUID generated session are different. If it looks correct i will make a PR for this issue.

gunthercox commented 7 years ago

Hi @vkosuri, I apologize for not responding to your comment sooner. Somehow I missed the notification for it. I've opened up a pull request that should resolve the issue you encountered #480. It appears that I forgot to handle the case that a chat session id could be held in the Django session, but missing from the ChatBot's sessions.

vkosuri commented 7 years ago

No problem, This looks awsome. Thank you very much.