diff --git a/dialogflow/cloud-client/pom.xml b/dialogflow/cloud-client/pom.xml index 4b955a1f5b4..82d9dd49b3f 100644 --- a/dialogflow/cloud-client/pom.xml +++ b/dialogflow/cloud-client/pom.xml @@ -40,7 +40,7 @@ com.google.cloud google-cloud-dialogflow - 0.70.0-alpha + 0.91.0-alpha @@ -57,1394 +57,4 @@ test - - - - - ContextManagement - - - ContextManagement - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.ContextManagement - false - - - - - - - - - ContextManagementList - - - ContextManagementList - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.ContextManagement - - list - --sessionId - ${sessionId} - --projectId - ${projectId} - - false - - - - - - - - - ContextManagementCreate - - - ContextManagementCreate - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.ContextManagement - - create - --sessionId - ${sessionId} - --projectId - ${projectId} - --contextId - ${contextId} - - false - - - - - - - - - ContextManagementDelete - - - ContextManagementDelete - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.ContextManagement - - delete - --sessionId - ${sessionId} - --projectId - ${projectId} - --contextId - ${contextId} - - false - - - - - - - - - DetectIntentAudio - - - DetectIntentAudio - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.DetectIntentAudio - false - - - - - - - - - DetectIntentAudioBookARoom - - - DetectIntentAudioBookARoom - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.DetectIntentAudio - - --projectId - ${projectId} - --audioFilePath - resources/book_a_room.wav - --sessionId - ${sessionId} - - false - - - - - - - - - DetectIntentAudioMountainView - - - DetectIntentAudioMountainView - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.DetectIntentAudio - - --projectId - ${projectId} - --audioFilePath - resources/mountain_view.wav - --sessionId - ${sessionId} - - false - - - - - - - - - DetectIntentAudioToday - - - DetectIntentAudioToday - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.DetectIntentAudio - - --projectId - ${projectId} - --audioFilePath - resources/today.wav - --sessionId - ${sessionId} - - false - - - - - - - - - DetectIntentAudio230PM - - - DetectIntentAudio230PM - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.DetectIntentAudio - - --projectId - ${projectId} - --audioFilePath - resources/230pm.wav - --sessionId - ${sessionId} - - false - - - - - - - - - DetectIntentAudioHalfAnHour - - - DetectIntentAudioHalfAnHour - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.DetectIntentAudio - - --projectId - ${projectId} - --audioFilePath - resources/half_an_hour.wav - --sessionId - ${sessionId} - - false - - - - - - - - - DetectIntentAudioTwoPeople - - - DetectIntentAudioTwoPeople - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.DetectIntentAudio - - --projectId - ${projectId} - --audioFilePath - resources/two_people.wav - --sessionId - ${sessionId} - - false - - - - - - - - - DetectIntentStreamBookARoom - - - DetectIntentStreamBookARoom - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.DetectIntentStream - - --projectId - ${projectId} - --audioFilePath - resources/book_a_room.wav - --sessionId - ${sessionId} - - false - - - - - - - - - DetectIntentStream - - - DetectIntentStream - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.DetectIntentStream - false - - - - - - - - - DetectIntentStreamMountainView - - - DetectIntentStreamMountainView - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.DetectIntentStream - - --projectId - ${projectId} - --audioFilePath - resources/mountain_view.wav - --sessionId - ${sessionId} - - false - - - - - - - - - DetectIntentTexts - - - DetectIntentTexts - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.DetectIntentTexts - false - - - - - - - - - DetectIntentText - - - DetectIntentText - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.DetectIntentTexts - - --projectId - ${projectId} - --sessionId - ${sessionId} - "hello" - "book a meeting room" - "Mountain View" - "tomorrow" - "10 am" - "2 hours" - "10 people" - "A" - "yes" - - false - - - - - - - - - DetectIntentWithTTSResponses - - - DetectIntentWithTTSResponses - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.DetectIntentWithTextToSpeechResponse - false - - - - - - - - - DetectIntentTTSResponse - - - DetectIntentTTSResponse - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.DetectIntentWithTextToSpeechResponse - - --projectId - ${projectId} - --sessionId - ${sessionId} - "hello" - "book a meeting room" - "Mountain View" - "tomorrow" - "10 am" - "2 hours" - "10 people" - "A" - "yes" - - false - - - - - - - - - DetectIntentSentimentAnalysis - - - DetectIntentSentimentAnalysis - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.DetectIntentWithSentimentAnalysis - false - - - - - - - - - DetectIntentKnowledge - - - DetectIntentKnowledge - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.DetectIntentKnowledge - false - - - - - - - - - - EntityManagement - - - EntityManagement - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.EntityManagement - false - - - - - - - - - DetectIntentWithModelSelection - - - DetectIntentWithModelSelection - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.DetectIntentWithModelSelection - false - - - - - - - - - EntityManagementList - - - EntityManagementList - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.EntityManagement - - list - --projectId - ${projectId} - --entityTypeId - e57238e2-e692-44ea-9216-6be1b2332e2a - - false - - - - - - - - - EntityManagementCreate - - - EntityManagementCreate - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.EntityManagement - - create - new_room - --projectId - ${projectId} - --entityTypeId - e57238e2-e692-44ea-9216-6be1b2332e2a - --synonyms - basement - cellar - - false - - - - - - - - - EntityManagementDelete - - - EntityManagementDelete - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.EntityManagement - - delete - new_room - --projectId - ${projectId} - --entityTypeId - e57238e2-e692-44ea-9216-6be1b2332e2a - - false - - - - - - - - - EntityTypeManagement - - - EntityTypeManagement - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.EntityTypeManagement - false - - - - - - - - - EntityTypeManagementList - - - EntityTypeManagementList - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.EntityTypeManagement - - list - --projectId - ${projectId} - - false - - - - - - - - - EntityTypeManagementCreate - - - EntityTypeManagementCreate - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.EntityTypeManagement - - create - employee - --projectId - ${projectId} - - false - - - - - - - - - EntityTypeManagementDelete - - - EntityTypeManagementDelete - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.EntityTypeManagement - - delete - - - - - false - - - - - - - - - IntentManagement - - - IntentManagement - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.IntentManagement - false - - - - - - - - - IntentManagementList - - - IntentManagementList - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.IntentManagement - - list - --projectId - ${projectId} - - false - - - - - - - - - IntentManagementCreate - - - IntentManagementCreate - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.IntentManagement - - create - "room.cancellation - yes" - --projectId - ${projectId} - --action - room.cancel - --trainingPhrasesParts - "cancel" - "cancellation" - --messageTexts - "Are you sure you want to cancel?" - "Cancelled." - - false - - - - - - - - - IntentManagementDelete - - - IntentManagementDelete - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.IntentManagement - - - - - - - false - - - - - - - - - KnowledgeBaseManagement - - - KnowledgeBaseManagement - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.KnowledgeBaseManagement - false - - - - - - - - - DocumentManagement - - - DocumentManagement - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.DocumentManagement - false - - - - - - - - - SessionEntityTypeManagement - - - SessionEntityTypeManagement - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.SessionEntityTypeManagement - false - - - - - - - - - SessionEntityTypeManagementList - - - SessionEntityTypeManagementList - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.SessionEntityTypeManagement - - list - --projectId - ${projectId} - --sessionId - ${sessionId} - - false - - - - - - - - - SessionEntityTypeManagementCreate - - - SessionEntityTypeManagementCreate - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.SessionEntityTypeManagement - - create - --projectId - ${projectId} - --sessionId - ${sessionId} - --entityTypeDisplayName - room - --entityValues - C - D - E - F - - false - - - - - - - - - SessionEntityTypeManagementDelete - - - SessionEntityTypeManagementDelete - - - - - - org.codehaus.mojo - exec-maven-plugin - 1.6.0 - - - - java - - - - - com.example.dialogflow.SessionEntityTypeManagement - - delete - --projectId - ${projectId} - --sessionId - ${sessionId} - --entityTypeDisplayName - room - - false - - - - - - diff --git a/dialogflow/cloud-client/src/main/java/com/example/dialogflow/DetectIntentStream.java b/dialogflow/cloud-client/src/main/java/com/example/dialogflow/DetectIntentStream.java index b531fd180d3..94a7d4df8d8 100644 --- a/dialogflow/cloud-client/src/main/java/com/example/dialogflow/DetectIntentStream.java +++ b/dialogflow/cloud-client/src/main/java/com/example/dialogflow/DetectIntentStream.java @@ -16,10 +16,9 @@ package com.example.dialogflow; - +// [START dialogflow_detect_intent_streaming] // Imports the Google Cloud client library - -import com.google.api.gax.rpc.ApiStreamObserver; +import com.google.api.gax.rpc.BidiStream; import com.google.cloud.dialogflow.v2.AudioEncoding; import com.google.cloud.dialogflow.v2.InputAudioConfig; import com.google.cloud.dialogflow.v2.QueryInput; @@ -29,138 +28,77 @@ import com.google.cloud.dialogflow.v2.StreamingDetectIntentRequest; import com.google.cloud.dialogflow.v2.StreamingDetectIntentResponse; import com.google.protobuf.ByteString; - import java.io.FileInputStream; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.CountDownLatch; +import java.io.IOException; /** * DialogFlow API Detect Intent sample with audio files processes as an audio stream. */ -public class DetectIntentStream { - // [START dialogflow_detect_intent_streaming] +class DetectIntentStream { - /** - * Returns the result of detect intent with streaming audio as input. - * - * Using the same `session_id` between requests allows continuation of the conversation. - * - * @param projectId Project/Agent Id. - * @param audioFilePath The audio file to be processed. - * @param sessionId Identifier of the DetectIntent session. - * @param languageCode Language code of the query. - * @return The List of StreamingDetectIntentResponses to the input audio inputs. - */ - public static List detectIntentStream( - String projectId, - String audioFilePath, - String sessionId, - String languageCode) throws Throwable { - // Start bi-directional StreamingDetectIntent stream. - final CountDownLatch notification = new CountDownLatch(1); - final List responseThrowables = new ArrayList<>(); - final List responses = new ArrayList<>(); + static void detectIntentStream(String projectId, String audioFilePath, String sessionId) { + // String projectId = "YOUR_PROJECT_ID"; + // String audioFilePath = "path_to_your_audio_file"; + // Using the same `sessionId` between requests allows continuation of the conversation. + // String sessionId = "Identifier of the DetectIntent session"; // Instantiates a client try (SessionsClient sessionsClient = SessionsClient.create()) { // Set the session name using the sessionId (UUID) and projectID (my-project-id) SessionName session = SessionName.of(projectId, sessionId); - System.out.println("Session Path: " + session.toString()); + // Instructs the speech recognizer how to process the audio content. // Note: hard coding audioEncoding and sampleRateHertz for simplicity. // Audio encoding of the audio content sent in the query request. - AudioEncoding audioEncoding = AudioEncoding.AUDIO_ENCODING_LINEAR_16; - int sampleRateHertz = 16000; - - // Instructs the speech recognizer how to process the audio content. InputAudioConfig inputAudioConfig = InputAudioConfig.newBuilder() - .setAudioEncoding(audioEncoding) // audioEncoding = AudioEncoding.AUDIO_ENCODING_LINEAR_16 - .setLanguageCode(languageCode) // languageCode = "en-US" - .setSampleRateHertz(sampleRateHertz) // sampleRateHertz = 16000 + .setAudioEncoding(AudioEncoding.AUDIO_ENCODING_LINEAR_16) + .setLanguageCode("en-US") // languageCode = "en-US" + .setSampleRateHertz(16000) // sampleRateHertz = 16000 .build(); - ApiStreamObserver responseObserver = - new ApiStreamObserver() { - @Override - public void onNext(StreamingDetectIntentResponse response) { - // Do something when receive a response - responses.add(response); - } - - @Override - public void onError(Throwable t) { - // Add error-handling - responseThrowables.add(t); - } - - @Override - public void onCompleted() { - // Do something when complete. - notification.countDown(); - } - }; - - // Performs the streaming detect intent callable request - ApiStreamObserver requestObserver = - sessionsClient.streamingDetectIntentCallable().bidiStreamingCall(responseObserver); - // Build the query with the InputAudioConfig QueryInput queryInput = QueryInput.newBuilder().setAudioConfig(inputAudioConfig).build(); - try (FileInputStream audioStream = new FileInputStream(audioFilePath)) { - // The first request contains the configuration - StreamingDetectIntentRequest request = StreamingDetectIntentRequest.newBuilder() - .setSession(session.toString()) - .setQueryInput(queryInput) - .build(); + // Create the Bidirectional stream + BidiStream bidiStream = + sessionsClient.streamingDetectIntentCallable().call(); - // Make the first request - requestObserver.onNext(request); + // The first request must **only** contain the audio configuration: + bidiStream.send(StreamingDetectIntentRequest.newBuilder() + .setSession(session.toString()) + .setQueryInput(queryInput) + .build()); + try (FileInputStream audioStream = new FileInputStream(audioFilePath)) { + // Subsequent requests must **only** contain the audio data. // Following messages: audio chunks. We just read the file in fixed-size chunks. In reality // you would split the user input by time. byte[] buffer = new byte[4096]; int bytes; while ((bytes = audioStream.read(buffer)) != -1) { - requestObserver.onNext( + bidiStream.send( StreamingDetectIntentRequest.newBuilder() .setInputAudio(ByteString.copyFrom(buffer, 0, bytes)) .build()); } - } catch (RuntimeException e) { - // Cancel stream. - requestObserver.onError(e); - } - // Half-close the stream. - requestObserver.onCompleted(); - // Wait for the final response (without explicit timeout). - notification.await(); - // Process errors/responses. - if (!responseThrowables.isEmpty()) { - throw responseThrowables.get(0); - } - if (responses.isEmpty()) { - throw new RuntimeException("No response from Dialogflow."); } - for (StreamingDetectIntentResponse response : responses) { - if (response.hasRecognitionResult()) { - System.out.format( - "Intermediate transcript: '%s'\n", response.getRecognitionResult().getTranscript()); - } - } + // Tell the service you are done sending data + bidiStream.closeSend(); - // Display the last query result - QueryResult queryResult = responses.get(responses.size() - 1).getQueryResult(); - System.out.println("===================="); - System.out.format("Query Text: '%s'\n", queryResult.getQueryText()); - System.out.format("Detected Intent: %s (confidence: %f)\n", - queryResult.getIntent().getDisplayName(), queryResult.getIntentDetectionConfidence()); - System.out.format("Fulfillment Text: '%s'\n", queryResult.getFulfillmentText()); + for (StreamingDetectIntentResponse response : bidiStream) { + QueryResult queryResult = response.getQueryResult(); + System.out.println("===================="); + System.out.format("Intent Display Name: %s\n", queryResult.getIntent().getDisplayName()); + System.out.format("Query Text: '%s'\n", queryResult.getQueryText()); + System.out.format("Detected Intent: %s (confidence: %f)\n", + queryResult.getIntent().getDisplayName(), queryResult.getIntentDetectionConfidence()); + System.out.format("Fulfillment Text: '%s'\n", queryResult.getFulfillmentText()); - return responses; + } + } catch (IOException e) { + e.printStackTrace(); } } - // [END dialogflow_detect_intent_streaming] } +// [END dialogflow_detect_intent_streaming] diff --git a/dialogflow/cloud-client/src/test/java/com/example/dialogflow/DetectIntentStreamIT.java b/dialogflow/cloud-client/src/test/java/com/example/dialogflow/DetectIntentStreamIT.java index a1d6b8c080a..21499647662 100644 --- a/dialogflow/cloud-client/src/test/java/com/example/dialogflow/DetectIntentStreamIT.java +++ b/dialogflow/cloud-client/src/test/java/com/example/dialogflow/DetectIntentStreamIT.java @@ -16,14 +16,12 @@ package com.example.dialogflow; -import static org.junit.Assert.assertTrue; - -import com.google.cloud.dialogflow.v2.StreamingDetectIntentResponse; +import static org.junit.Assert.assertThat; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.PrintStream; -import java.util.List; - +import org.hamcrest.CoreMatchers; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -37,32 +35,35 @@ @SuppressWarnings("checkstyle:abbreviationaswordinname") public class DetectIntentStreamIT { + private ByteArrayOutputStream bout; + private static String audioFilePath = "resources/book_a_room.wav"; private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT"); private static String SESSION_ID = "fake_session_for_testing"; - private static String LANGUAGE_CODE = "en-US"; @Before public void setUp() { - System.setOut(new PrintStream(new ByteArrayOutputStream())); + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); } @After public void tearDown() { System.setOut(null); + bout.reset(); } @Test - public void testStreamingDetectIntentCallable() throws Throwable { - List response = DetectIntentStream.detectIntentStream( - PROJECT_ID, audioFilePath, SESSION_ID, LANGUAGE_CODE); - assertTrue(response.size() > 0); - assertTrue(response.stream().anyMatch(i -> i - .getQueryResult() - .getIntent() - .getDisplayName().equals("room.reservation"))); - assertTrue(response.stream().anyMatch(i -> i - .getRecognitionResult() - .getTranscript().contains("book"))); + public void testStreamingDetectIntentCallable() { + DetectIntentStream.detectIntentStream( + PROJECT_ID, audioFilePath, SESSION_ID); + + String output = bout.toString(); + + assertThat(output, CoreMatchers.containsString( + "Intent Display Name: room.reservation")); + + assertThat(output, CoreMatchers.containsString( + "book")); } } diff --git a/dialogflow/cloud-client/src/test/java/com/example/dialogflow/KnowledgeBaseManagementIT.java b/dialogflow/cloud-client/src/test/java/com/example/dialogflow/KnowledgeBaseManagementIT.java index d6bdab305e2..877737779c3 100644 --- a/dialogflow/cloud-client/src/test/java/com/example/dialogflow/KnowledgeBaseManagementIT.java +++ b/dialogflow/cloud-client/src/test/java/com/example/dialogflow/KnowledgeBaseManagementIT.java @@ -55,7 +55,8 @@ public class KnowledgeBaseManagementIT { private static List TEXTS = ImmutableList .of("How do I sign up?", "Is my data redundant?", "Where can I find pricing information?", - "Where is my data stored?", "What are my support options?"); + "Where is my data stored?", "What are my support options?", + "How can I maximize the availability of my data?"); @Before public void setUp() { @@ -167,11 +168,11 @@ public void testDetectIntentKnowledge() throws Exception { for (String text : TEXTS) { KnowledgeAnswers knowledgeAnswers = allAnswers.get(text); if (knowledgeAnswers.getAnswersCount() > 0) { - answersFound++; Answer answer = knowledgeAnswers.getAnswers(0); - assertEquals(text, answer.getFaqQuestion()); - assertEquals(document.getName(), answer.getSource()); - assertThat(answer.getAnswer()).contains("Cloud Storage"); + if (text.equals(answer.getFaqQuestion()) && document.getName().equals(answer.getSource()) + && answer.getAnswer().contains("Cloud Storage")) { + answersFound++; + } } } // To make the test less flaky, check that half of the texts got a result.