简体   繁体   English

添加训练短语时的 Dialogflow v2 Nodejs 客户端库 UpdateIntent

[英]Dialogflow v2 Nodejs Client Library UpdateIntent when adding Training Phrases

I am trying to use the updateIntent function that is part of the Dialogflow v2 Client library for Node.js .我正在尝试使用updateIntent函数,它是 Node.js 的 Dialogflow v2 客户端库的一部分。 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!:这是我正在使用的代码!:

My GetIntent Function:我的 GetIntent 函数:

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)
    })
  }
}

My UpdateIntent Function:我的 UpdateIntent 函数:

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)
    })
  } 
}

The Function that Calls it:调用它的函数:

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')
  }
}

Now the weird thing is - I am getting a 200 response from the client library call.现在奇怪的是 - 我从客户端库调用中得到 200 响应。 The Api doc states that upon a successful response you will get an intent object. Api 文档指出,在成功响应后,您将获得一个意图对象。 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] 

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?有没有我可以跑掉的例子?

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.检查它。

This is the getIntent function:这是 getIntent 函数:

    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)
        })
    }
}

The function that calls it:调用它的函数:

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')
    }
 }

The UpdateIntent function: UpdateIntent 函数:

 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)
        })
    }
}

You can try:你可以试试:

updateMask: {
       paths: ['training_phrases']
}

since paths naming convetion is "snake"因为路径命名对流是“蛇”

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

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