python Dialogflow不返回训练短语

f4t66c6m  于 2023-05-27  发布在  Python
关注(0)|答案(2)|浏览(119)

我试图从Python中的Dialogflow中获得每个意图的训练短语的概述。
我按照这个例子生成了以下代码:

from google.cloud import dialogflow_v2

# get_credentials is a custom function that loads the credentials
credentials, project_id = get_credentials()

client = dialogflow_v2.IntentsClient(credentials=credentials)

request = dialogflow_v2.ListIntentsRequest(
    parent=f"projects/{project_id}/agent/environments/draft",
)
page_result = client.list_intents(request=request)

for intent in page_result:
    print("Intent name: ", intent.name)
    print("Intent display_name: ", intent.display_name)
    print("Training phrases: ", intent.training_phrases)

Intent的名称和显示名称按预期打印,但培训短语始终是空列表(对于草稿和测试环境)。为什么我看不到我在控制台中可以看到的训练短语?

编辑在hkanjih的回答之后,我更新了我的代码如下:

from google.cloud import dialogflow_v2

# get_credentials is a custom function that loads the credentials
credentials, project_id = get_credentials()

client = dialogflow_v2.IntentsClient(credentials=credentials)

request = dialogflow_v2.ListIntentsRequest(
    parent=f"projects/{project_id}/agent/environments/draft",
)
page_result = client.list_intents(request=request)

for intent in page_result:
    print("Intent name: ", intent.name)
    # intent.name is equal to projects/{project_id}/agent/intents/{intent_id}
    intent_request = dialogflow_v2.GetIntentRequest(
        name=intent.name,
    )
    intent = client.get_intent(request=intent_request)
    
    # printing intent name again just to check if it's the same (it is)
    print("Intent name: ", intent.name)
    print("Intent display_name: ", intent.display_name)
    print("Training phrases: ", intent.training_phrases)

不幸的是,无论如何:Training phrases: []

ffscu2ro

ffscu2ro1#

我认为你看不到训练短语的原因是training_phrases字段没有被ListIntents方法填充。
此字段由GetIntent方法填充。

from google.cloud import dialogflow_v2

# get_credentials is a custom function that loads the credentials
credentials, project_id = get_credentials()

client = dialogflow_v2.IntentsClient(credentials=credentials)

intent_id = "my-intent-id"

request = dialogflow_v2.GetIntentRequest(
    name=f"projects/{project_id}/agent/intents/{intent_id}",
)

intent = client.get_intent(request=request)

print("Intent name: ", intent.name)
print("Intent display_name: ", intent.display_name)
print("Training phrases: ", intent.training_phrases)

因此,对于您需要的内容,您可能必须首先获取intent列表(使用ListIntentsRequest),并且对于列表中的每个intent,您必须调用GetIntentRequest方法。

cnwbcb6i

cnwbcb6i2#

在文档中搜索了一些之后,我找到了this页面。因此,我将intent_view参数添加到请求中,如下面的代码片段所示:

from google.cloud import dialogflow_v2

# get_credentials is a custom function that loads the credentials
_credentials, project_id = self._get_credentials()

client = dialogflow_v2.IntentsClient(credentials=_credentials)

request = dialogflow_v2.ListIntentsRequest(
    parent=f"projects/{project_id}/agent/environments/draft",
    intent_view="INTENT_VIEW_FULL",
)

page_result = client.list_intents(request=request)

for intent in page_result:
    print("Intent name: ", intent.name)
    print("Intent display_name: {}", intent.display_name)
    print("Training phrases: ", intent.training_phrases)

请注意,在请求中,父f"projects/{project_id}/agent"(没有环境)给出了相同的结果。

相关问题