我试图从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: []
2条答案
按热度按时间ffscu2ro1#
我认为你看不到训练短语的原因是
training_phrases
字段没有被ListIntents
方法填充。此字段由
GetIntent
方法填充。因此,对于您需要的内容,您可能必须首先获取intent列表(使用
ListIntentsRequest
),并且对于列表中的每个intent,您必须调用GetIntentRequest
方法。cnwbcb6i2#
在文档中搜索了一些之后,我找到了this页面。因此,我将
intent_view
参数添加到请求中,如下面的代码片段所示:请注意,在请求中,父
f"projects/{project_id}/agent"
(没有环境)给出了相同的结果。