Google-cloud-java: Cannot access training phrases

Created on 22 Feb 2019  路  1Comment  路  Source: googleapis/google-cloud-java

I cannot access the training phrases of an intent using the dialogflow client. Please, follow along this example in order to reproduce this behavior.

Environment details

  • OS: Windows 10 Enterprise
  • Java version: 8
  • google-cloud-java version(s): 0.81.0-alpha and 0.80.0-alpha

Steps to reproduce

  1. Create a dialogflow agent
  2. Create a google cloud service account with appropriate access right in order to manipulate the agent
  3. generate JSON-file with service account's credentials and add them to the project
  4. use the code / project below
  5. all training phrases-lists should be empty

Code snippet

  GoogleCredentials credential = GoogleCredentials
                .fromStream(new FileInputStream("./credentials/dialogflow_credentials.json"));
        IntentsSettings intentsSettings = IntentsSettings.newBuilder().
                setCredentialsProvider(FixedCredentialsProvider.create(credential)).build();


        try(IntentsClient intentsClient = IntentsClient.create(intentsSettings)) {

            ProjectAgentName parent = ProjectAgentName.of("testtrainingphrases");


            for (Intent i : intentsClient.listIntents(parent).iterateAll()){
                int count = i.getTrainingPhrasesCount();
                System.out.println(count);
            }

        }

Here is my example agent
TestTrainingPhrases.zip

Here is the sample project (you need to add your credential files)
dfvalidationtest.zip

Hope I could provide a reproducible example.

Thanks!

dialogflow question

Most helpful comment

The API by default does not return all fields of an Intent. Training phrases is one of those fields that will only be returned if you specify that you want all fields to be returned in your get/list request. The current client library does not allow you to specify this option in the normal list and get methods, but you can do it by creating a get/list request your self. This example returns a list of all Intents of the project, including the training phrases:

    GoogleCredentials credential = GoogleCredentials
        .fromStream(new FileInputStream("/home/loite/CloudSpannerKeys/olavtest-key.json"));
    IntentsSettings intentsSettings = IntentsSettings.newBuilder()
        .setCredentialsProvider(FixedCredentialsProvider.create(credential)).build();
    try (IntentsClient intentsClient = IntentsClient.create(intentsSettings)) {
      ProjectAgentName parent = ProjectAgentName.of("olav-test-99e2a");
      ListIntentsRequest request = ListIntentsRequest.newBuilder()
          .setParent(String.format("projects/%s/agent", parent.getProject()))
          .setIntentView(IntentView.INTENT_VIEW_FULL).build();
      for (Intent i : intentsClient.listIntents(request).iterateAll()) {
        System.out.println(i.toString());
        for (TrainingPhrase phrase : i.getTrainingPhrasesList()) {
          System.out.println(phrase);
        }
        int count = i.getTrainingPhrasesCount();
        System.out.println(count);
      }
    }

The important part is the ListIntentsRequest.newBuilder().setParent(String.format("projects/%s/agent", parent.getProject())).setIntentView(IntentView.INTENT_VIEW_FULL).build() part. The IntentView.INTENT_VIEW_FULL specifies that you want to receive all fields of the Intent. Note that you need to set the entire parent path yourself, instead of only the parent name (this is what the String.format("projects/%s/agent", parent.getProject()) part does).

Please let me know by reopening the issue if this does not solve your problem.

>All comments

The API by default does not return all fields of an Intent. Training phrases is one of those fields that will only be returned if you specify that you want all fields to be returned in your get/list request. The current client library does not allow you to specify this option in the normal list and get methods, but you can do it by creating a get/list request your self. This example returns a list of all Intents of the project, including the training phrases:

    GoogleCredentials credential = GoogleCredentials
        .fromStream(new FileInputStream("/home/loite/CloudSpannerKeys/olavtest-key.json"));
    IntentsSettings intentsSettings = IntentsSettings.newBuilder()
        .setCredentialsProvider(FixedCredentialsProvider.create(credential)).build();
    try (IntentsClient intentsClient = IntentsClient.create(intentsSettings)) {
      ProjectAgentName parent = ProjectAgentName.of("olav-test-99e2a");
      ListIntentsRequest request = ListIntentsRequest.newBuilder()
          .setParent(String.format("projects/%s/agent", parent.getProject()))
          .setIntentView(IntentView.INTENT_VIEW_FULL).build();
      for (Intent i : intentsClient.listIntents(request).iterateAll()) {
        System.out.println(i.toString());
        for (TrainingPhrase phrase : i.getTrainingPhrasesList()) {
          System.out.println(phrase);
        }
        int count = i.getTrainingPhrasesCount();
        System.out.println(count);
      }
    }

The important part is the ListIntentsRequest.newBuilder().setParent(String.format("projects/%s/agent", parent.getProject())).setIntentView(IntentView.INTENT_VIEW_FULL).build() part. The IntentView.INTENT_VIEW_FULL specifies that you want to receive all fields of the Intent. Note that you need to set the entire parent path yourself, instead of only the parent name (this is what the String.format("projects/%s/agent", parent.getProject()) part does).

Please let me know by reopening the issue if this does not solve your problem.

Was this page helpful?
0 / 5 - 0 ratings