-->

How to delete context/session_id at end of convers

2019-09-15 03:01发布

问题:

I've been having issues with Wit.ai where my Python bot will retain the context after ending a conversation. This behaviour is the same in the Facebook client and the pywit interactive client.

The conversation starts with a simple 'Hi' and can end at different points within different branches if a user taps a 'Thanks, bye' quick reply after a successful query.

If the conversation is then started with 'Hi' once again, the session state is saved from before which leads to wrong responses. What is the best way to delete the context after the user has said goodbye?

I tried creating a goodbye function that triggers after the bot has sent its final message but it didn't work e.g.

def goodbye(request):
    del request['context']    # or request.clear()
    return request

The documentation (https://wit.ai/docs/http/20160526#post--converse-link) suggests you clear the session_id and generate a new one but gives no hints as to how.

回答1:

You can generate new Session Ids using uuid. Session ID has to be any text that is unique, it can even be system date. I suggest you use uuid

Check here as to how to generate it.



回答2:

I was confronted with the same issue and I solved it in the following way.

I first created a simple end_session action, to be called at the end of each conversation path:

def end_session(request):    
    return {'end_session': True}

Then I inserted the following code just after returning from run_actions:

if 'end_session' in context:
    context = {}
    session_hash = uuid.uuid1().hex

As you see, in addition to clearing the context, as you do, I also recreate a new session id (as per Swapnesh Khare's suggestion).

I'm not sure this is the best solution, but it works for me.