简体   繁体   English

如何在Wit.ai机器人对话结束时删除context / session_id

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

I've been having issues with Wit.ai where my Python bot will retain the context after ending a conversation. 我在Wit.ai遇到问题,在结束对话后,我的Python机器人将保留上下文。 This behaviour is the same in the Facebook client and the pywit interactive client. 在Facebook客户端和pywit交互式客户端中,此行为相同。

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. 如果对话随后再次以“ Hi”开始,则会话状态从之前保存下来,从而导致错误的响应。 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 eg 我尝试创建一个再见功能,该功能在机器人发送了最终消息后触发,但是它没有用,例如

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. 该文档( https://wit.ai/docs/http/20160526#post--converse-link )建议您清除session_id并生成一个新的session_id,但没有给出任何提示。

You can generate new Session Ids using uuid . 您可以使用uuid生成新的会话ID。 Session ID has to be any text that is unique, it can even be system date. 会话ID必须是唯一的任何文本,甚至可以是系统日期。 I suggest you use uuid 我建议你使用uuid

Check here as to how to generate it. 在这里检查如何生成它。

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: 我首先创建了一个简单的end_session操作,将在每个对话路径的末尾调用该操作:

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

Then I inserted the following code just after returning from run_actions: 然后,我从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). 如您所见,除了清除上下文,您还可以重新创建一个新的会话ID(按照Swapnesh Khare的建议)。

I'm not sure this is the best solution, but it works for me. 我不确定这是否是最好的解决方案,但它对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM