Dialogflow API - TypeError: Invalid constructor input for BatchUpdateIntentsRequest: intents

Dialogflow API - TypeError: Invalid constructor input for BatchUpdateIntentsRequest: intents

我正在尝试使用其 API 训练 Dialogflow,以在您拥有预先存在的训练数据集时通过 UI 训练的一些限制。上传数据集功能似乎很有限

使用最小设置,尝试从 API 创建意图,我收到以下错误:

Traceback (most recent call last):
  File "main.py", line 102, in <module>
    update_agent()
  File "main.py", line 87, in update_agent
    response = client.batch_update_intents(
  File "env/lib/python3.8/site-packages/google/cloud/dialogflow_v2/services/intents/client.py", line 1114, in batch_update_intents
    request = intent.BatchUpdateIntentsRequest(request)
  File "env/lib/python3.8/site-packages/proto/message.py", line 520, in __init__
    raise TypeError(
TypeError: Invalid constructor input for BatchUpdateIntentsRequest: intents {
  name: "projects/project-name/agent/intents/welcome"
  display_name: "Welcome"
  training_phrases {
    parts {
      text: "Training phrase 1"
    }
  }
  messages {
    text {
      text: "B"
      text: "o"
      text: "t"
      text: " "
      text: "R"
      text: "e"
      text: "s"
      text: "p"
      text: "o"
      text: "n"
      text: "s"
      text: "e"
    }
  }
}

这是我使用的 python 代码:强调文本

def update_agent():

    intentBatch = dialogflow.types.intent.IntentBatch()

    training_phrases = [
        dialogflow.types.intent.Intent.TrainingPhrase(
            parts=[
                dialogflow.types.intent.Intent.TrainingPhrase.Part(
                    text="Training phrase 1"
                )
            ]
        )
    ]

    intent = dialogflow.types.intent.Intent(
        name="projects/{}/agent/intents/{}".format(PROJECT_ID, "welcome"),
        display_name="Welcome",
        webhook_state="WEBHOOK_STATE_ENABLED",
        training_phrases=training_phrases,
        messages=[
            dialogflow.types.intent.Intent.Message(
                text=dialogflow.types.intent.Intent.Message.Text(text="Bot Response"))
        ]
    )

    intentBatch.intents.append(intent)

    client = dialogflow.IntentsClient()
    response = client.batch_update_intents(
        request=intentBatch
    )

    print("Intent updated")

    print(response)

我按照this documentation检查了预期的架构,我省略了一些可选字段并包括了所有必填字段,但它似乎没有解决问题。

此外,来自 API 响应的“消息”是否被拆分成预期的字符字典?

您应该将 intentBatch 嵌套在 BatchUpdateIntentsRequest object. Then the new BatchUpdateIntentsRequest object could now be passed to batch_update_intents(). Also, intent is sequence type so it should be inside [] , this is the same case with messages 中,这就是将其拆分为字符字典的原因。

查看下面修改后的代码:

from google.cloud import dialogflow_v2 as dialogflow

def update_agent():

    intentBatch = dialogflow.types.IntentBatch()


    training_phrases = [
        dialogflow.types.intent.Intent.TrainingPhrase(
            parts=[
                dialogflow.types.intent.Intent.TrainingPhrase.Part(
                    text="Training phrase 1"
                )
            ]
        )
    ]

    intent = [dialogflow.types.intent.Intent(
        name="projects/<your-project>/agent/intents/<>",
        display_name="welcome",
        webhook_state="WEBHOOK_STATE_ENABLED",
        training_phrases=training_phrases,
        messages=[
            dialogflow.types.intent.Intent.Message(
                text=dialogflow.types.intent.Intent.Message.Text(text=["Bot Response"]))
        ]

    )]


    #intentBatch.intents.append(intent)
    intentBatch = dialogflow.types.intent.IntentBatch(intents=intent)


    client = dialogflow.IntentsClient()
    response = client.batch_update_intents(

        request=dialogflow.types.BatchUpdateIntentsRequest(intent_batch_inline=intentBatch, parent="projects/<your-project>/agent")
    )

    print("Intent updated")

    print(response)

update_agent()

结果:

UI 中的意图更新: