添加培训短语时Dialogflow v2 Node.js客户端库UpdateIntent [英] Dialogflow v2 Nodejs Client Library UpdateIntent when adding Training Phrases

查看:85
本文介绍了添加培训短语时Dialogflow v2 Node.js客户端库UpdateIntent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 updateIntent 函数,该函数是Node.js的Dialogflow v2客户端库的一部分。我尝试使用它的原因是为了能够在意图中添加训练短语。

I am trying to use the updateIntent function that is part of the Dialogflow v2 Client library for Node.js . The reason I am trying to use it, is to be able to add training phrases to an intent.

我似乎无法通过这一要求。这是我正在使用的代码!

I cannot seem to get passed this one. Here is the code I am using for it!:

我的GetIntent函数:

My GetIntent Function:

async function getIntent(intentId) {
  try {
    let responses = await intentsClient.getIntent({name: intentId, intentView: 'INTENT_VIEW_FULL'})
    const response = responses[0]
          // console.log(response)

    return new Promise((resolve, reject) => {
      resolve(response)
    })
  } catch (err) {
    return new Promise((resolve, reject) => {
      reject(err)
    })
  }
}

我的UpdateIntent函数:

My UpdateIntent Function:

async function updateIntent(intent) {
  const request = {
    intent: intent,
    languageCode: 'en-US',
    updateMask: {
       paths: ['trainingPhrases']
    },
    intentView: 'INTENT_VIEW_FULL'
  }
  try {
    let responses = await intentsClient.updateIntent(request)
    return new Promise((resolve, reject) => {
      resolve(response)
    })
  } catch (err) {
    console.log(err)
    return new Promise((resolve, reject) => {
      reject(err)
    })
  } 
}

调用它的函数:

async function testUpdateTraining () {
  try {
    let intent = await getIntent('projects/small-talk-1-406ae/agent/intents/ac7f0b68-de5c-4b6f-9393-358dd2b0c1bd')

    let trainingPhrase = { parts: [{ text: 'How should I behave on the trails?'}],
      type: 'EXAMPLE'}
    intent.trainingPhrases.push(trainingPhrase)
    try {
      let updatedIntent = await updateIntent(intent)
    } catch (e) {
      console.log(e)
      console.log('failed to update the intent')
    }
  } catch (err) {
    console.log('failed to get intent')
  }
}

现在奇怪的是-我收到200条回复客户端库调用。 Api文档指出,成功响应后,您将获得一个意图对象。我得到一个带有训练短语的意图对象...

Now the weird thing is - I am getting a 200 response from the client library call. The Api doc states that upon a successful response you will get an intent object. I am getting an intent object with the training phrases inside...

    [![{ inputContextNames: \[\],
  events: \[\],
  trainingPhrases:
   \[ { parts: \[Array\],
       name: 'ad0d1f6a-78cf-4e0b-84ca-ec62a45c75dc',
       type: 'EXAMPLE',
       timesAddedCount: 0 },
     { parts: \[Array\],
       name: 'e33cce4b-96ee-4e35-a151-5b09ff603817',
       type: 'EXAMPLE',
       timesAddedCount: 0 },
     { parts: \[Array\],
       name: '7d9b7c56-5fa8-4791-986f-e57b9f90d431',
       type: 'EXAMPLE',
       timesAddedCount: 0 } \],
  outputContexts: \[\],
  parameters: \[\],
  messages:
   \[ { platform: 'PLATFORM_UNSPECIFIED',
       text: \[Object\],
       message: 'text' } \],
  defaultResponsePlatforms: \[\],
  followupIntentInfo: \[\],
  name: 'projects/small-talk-1-406ae/agent/intents/ac7f0b68-de5c-4b6f-9393-358dd2b0c1bd',
  displayName: 'faq.offroad.card1answer',
  priority: 500000,
  isFallback: false,
  webhookState: 'WEBHOOK_STATE_UNSPECIFIED',
  action: 'faq.offroad.card1answer',
  resetContexts: false,
  rootFollowupIntentName: '',
  parentFollowupIntentName: '',
  mlDisabled: true }][1]][1] 

这是dialogflow的功能。这里只有两个训练短语,我以编程方式添加的一个没有出现。

所以我的问题是,如何格式化请求,以便可以毫无问题地更新训练短语?有没有我可以逃脱的例子?

This is what dialogflow has. Only two training phrases here, the one I added programmatically does not show up. So my question is, how can I format the request so I can update the training phrases without a problem? Is there an example I can run off?

推荐答案

尝试了很多之后,了解到我的代码有效,因为我删除了更新掩码。还有languageCode,因为它给了我一个错误。
代码如下,并且工作正常。
进行检查。

After trying out a lot, understood that my code worked because i removed update mask. And the languageCode as well, because it was giving me an error. The code is as below and works fine. Check it up.

这是getIntent函数:

This is the getIntent function:

    async function getIntent(intentId) {
    try {
        let responses = await intentsClient.getIntent({
            name: intentId,
            intentView: 'INTENT_VIEW_FULL'
        })
        const response = responses[0];
        console.log(util.inspect(response, false, null, true /* enable colors */ ));

        return new Promise((resolve, reject) => {
            resolve(response)
        })
    } catch (err) {
        return new Promise((resolve, reject) => {
            reject(err)
        })
    }
}

调用它的函数:

async function testUpdateTraining () {
    try {
        let intent = await getIntent('<your_ID>')

        let trainingPhrase = {
            parts: [{
                text: 'let me call you?'
            }],
            type: 'EXAMPLE'
        }
        intent.trainingPhrases.push(trainingPhrase)
        try {
            let updatedIntent = await updateIntent(intent)
        } catch (e) {
            console.log(e)
            console.log('failed to update the intent')
        }
    } catch (err) {
        console.log('failed to get intent')
    }
 }

UpdateIntent函数:

The UpdateIntent function:

 async function updateIntent(intent) {
    const request = {
        intent: intent,
        intentView: 'INTENT_VIEW_FULL'
    }
    try {
        let responses = await intentsClient.updateIntent(request)
        return new Promise((resolve, reject) => {
            resolve(responses)
        })
    } catch (err) {
        console.log(err)
        return new Promise((resolve, reject) => {
            reject(err)
        })
    }
}

这篇关于添加培训短语时Dialogflow v2 Node.js客户端库UpdateIntent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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