2

I am trying to get an overview of the training phrases per intent from Dialogflow in python.

I have followed this example to generate the following code:

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)

The name and display name of the intent are printed as expected, however training phrases is always an empty list (for both the draft as the test environment). Any ideas on why I'm not seeing the training phrases that I can see in the console?

EDIT After hkanjih's answer I've updated my code as follows:

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)

Unfortunately, for all intents: Training phrases: []

Hav11
  • 409
  • 1
  • 5
  • 14
  • Have you saved training phrases after adding it? You can refer to this [document](https://cloud.google.com/dialogflow/es/docs/intents-training-phrases) to add training phrases. – Prajna Rai T May 03 '23 at 05:28
  • Yes, I have saved the training phrases. The model was trained and recognizes when an user-expression matches the intent. – Hav11 May 04 '23 at 06:47
  • Could you try using this [code](https://stackoverflow.com/questions/60494423/obtain-intents-entities-and-the-training-data-from-dialogflow-in-the-python-env/60502232#60502232) to get the list of training phrases? – Prajna Rai T May 05 '23 at 13:21

2 Answers2

2

After searching in the documentation some more, I found this page. Therefore I added the intent_view parameter to the request as in the snippet below:

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)

Note that in the request the parent f"projects/{project_id}/agent" (without the environment) gives the same result.

Hav11
  • 409
  • 1
  • 5
  • 14
1

I think the reason you're not seeing the training phrases because the training_phrases field is not populated by the ListIntents method.

This field is populated by the GetIntent method.

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)

So, for what you need maybe you have to get the list of intend first (using ListIntentsRequest) and for each intent in the list you have to call GetIntentRequest method.

hkanjih
  • 1,271
  • 1
  • 11
  • 29
  • Thank you for your answer! Unfortunately, the training phrases are still an empty list. If you see an error in my code when reproducing your answer, please let me know! – Hav11 May 11 '23 at 07:43