如果没有回复,则上下文密钥不会初始化-wit.ai [英] The context key is not initialised if it’s not in reply - wit.ai

查看:87
本文介绍了如果没有回复,则上下文密钥不会初始化-wit.ai的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在wit.ai中创建了一个新应用.在故事中,在用户说"之后,我使用自动执行"添加了一个函数getReply(),并添加了两个带有分支的上下文键.如果两个密钥都可用,我将使用提示"向用户发送回复,它将转到下一步,否则会向用户询问丢失的密钥.

I have created a new app in wit.ai. In stories, after the 'User says', I have added a function getReply() using 'Bot executes' and added two context keys with branch in it. If both the keys are available, I'm sending a reply to the user using the 'Bot says' and it will go to the next step else it will ask the missing key to the user.

问题是,在回复中,我仅使用上下文键之一.如果该密钥可用,则该流程有效.它不考虑其他键.仅当我们在回复中同时添加两个密钥时,它才会检查两个密钥.

The issue is, in the reply I'm using only one of the context key. The flow works if that key is available. It doesn't considers about the other key. It is checking both keys only if we add both the keys in the reply.

在代码中,我正在检查函数getReply()中的那些键,然后将其添加到上下文中.

In the code I'm checking those keys in the function getReply() and then adding it to the context.

const actions = {
send(request, response) {
  ...
  ...
  ...
  return new Promise(function(resolve, reject) {
    ...
    return resolve();
  })
    .then()) => null)
    .catch((err) => {
      'Error occurred',
      id,
      ':',
      err.stack || err
    );
  });
},
function getReply({context, entities}) {
  return new Promise(function(resolve, reject) {
    ...
    ...
    ...
    context.key1 = value1;
    context.key2 = value2;
    return resolve(context);
  }
}

一切都正确还是我错过了什么?为什么如果不在响应中则不启动上下文密钥.

Is everything correct or am I missing something? Why the context key is not initiated if its not in the reply.

谢谢.

推荐答案

在您的示例中,我看不到您在上下文中添加或删除键,具体取决于一些逻辑.我做了一个小例子,我应该如何验证用户使用Python发送的电子邮件,以及在用户输入无效电子邮件的情况下如何使用上下文键进行处理.我希望它能使您对如何使用上下文键有所了解.

Hmm in your example i dont see you add or remove keys in the context, depending one some logic. I did a little example how i should validate an email given by an user in Python and how to handle with context keys the case where the user gives an invalid email. I hope it will give you some idea on how to use context keys.

def save_email(br, cc):
    """Save email on context.

        Arguments:
        - br (dict)         Bot Response
        - cc (dict)         Conversation Context

    Returns an updated context
    """
    context = br.get('context', {})
    entities = br['entities']
    email = first_entity_value(entities, 'email')  # Use wit/email builtin entity

    # First clean the context, its possible you saved one of those two keys in
    # previous iterations, so clean them all just to be safe. As we are going to
    # run email validation in the code below its safe to remove this keys/clean the
    # context at the top of this method.
    for key in ['email', 'bad-email']:
        if key in context:
            del context[key]

    # Now validate that email is valid. If its valid, we add an 'email' key
    # to the context. If email is not valid, we add a 'bad-email' key to the
    # context. In this way, we can handle then at Wit.ai, depending which key
    # is present, what the bot should do next.
    validator = EmailValidator()
    if validator(email).is_valid():
        context['email'] = email
    else:
        context['bad-email'] = 1   # 1, 0, any value. What Wit will pay attention is
                               # that the 'bad-key' exists in the context.

    # Finally, return the updated context
    return context

如您所见,将电子邮件不良电子邮件放在上下文中是没有意义的.我不确定在这种情况下机智会怎么做.提醒一下,它带有上下文密钥,我们可以修改Wit预测以下动作的方式.

As you can see it doesnt have sense to put both email and bad-email on context. I'm not sure what would Wit do in that case. As a reminder, its with context keys that we can modify how Wit predicts the following action.

我希望这可以澄清并帮助您解决问题,

I hope this clarifies and helps you solve your problem,

最好

埃米利亚诺.

这篇关于如果没有回复,则上下文密钥不会初始化-wit.ai的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆