How to delete context/session_id at end of conversation in Wit.ai bot

拈花ヽ惹草 提交于 2019-12-13 07:22:26

问题


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.



来源:https://stackoverflow.com/questions/43353252/how-to-delete-context-session-id-at-end-of-conversation-in-wit-ai-bot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!