Skip to content

Commit afd3cf7

Browse files
nnegreychingor13
authored andcommitted
samples: Update streaming sample to use non-deprecated method (#1413)
1 parent 34410b1 commit afd3cf7

File tree

3 files changed

+63
-123
lines changed

3 files changed

+63
-123
lines changed

dialogflow/snippets/src/main/java/com/example/dialogflow/DetectIntentStream.java

Lines changed: 38 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616

1717
package com.example.dialogflow;
1818

19-
19+
// [START dialogflow_detect_intent_streaming]
2020
// Imports the Google Cloud client library
21-
22-
import com.google.api.gax.rpc.ApiStreamObserver;
21+
import com.google.api.gax.rpc.BidiStream;
2322
import com.google.cloud.dialogflow.v2.AudioEncoding;
2423
import com.google.cloud.dialogflow.v2.InputAudioConfig;
2524
import com.google.cloud.dialogflow.v2.QueryInput;
@@ -29,138 +28,77 @@
2928
import com.google.cloud.dialogflow.v2.StreamingDetectIntentRequest;
3029
import com.google.cloud.dialogflow.v2.StreamingDetectIntentResponse;
3130
import com.google.protobuf.ByteString;
32-
3331
import java.io.FileInputStream;
34-
import java.util.ArrayList;
35-
import java.util.List;
36-
import java.util.concurrent.CountDownLatch;
32+
import java.io.IOException;
3733

3834
/**
3935
* DialogFlow API Detect Intent sample with audio files processes as an audio stream.
4036
*/
41-
public class DetectIntentStream {
42-
// [START dialogflow_detect_intent_streaming]
37+
class DetectIntentStream {
4338

44-
/**
45-
* Returns the result of detect intent with streaming audio as input.
46-
*
47-
* Using the same `session_id` between requests allows continuation of the conversation.
48-
*
49-
* @param projectId Project/Agent Id.
50-
* @param audioFilePath The audio file to be processed.
51-
* @param sessionId Identifier of the DetectIntent session.
52-
* @param languageCode Language code of the query.
53-
* @return The List of StreamingDetectIntentResponses to the input audio inputs.
54-
*/
55-
public static List<StreamingDetectIntentResponse> detectIntentStream(
56-
String projectId,
57-
String audioFilePath,
58-
String sessionId,
59-
String languageCode) throws Throwable {
60-
// Start bi-directional StreamingDetectIntent stream.
61-
final CountDownLatch notification = new CountDownLatch(1);
62-
final List<Throwable> responseThrowables = new ArrayList<>();
63-
final List<StreamingDetectIntentResponse> responses = new ArrayList<>();
39+
static void detectIntentStream(String projectId, String audioFilePath, String sessionId) {
40+
// String projectId = "YOUR_PROJECT_ID";
41+
// String audioFilePath = "path_to_your_audio_file";
42+
// Using the same `sessionId` between requests allows continuation of the conversation.
43+
// String sessionId = "Identifier of the DetectIntent session";
6444

6545
// Instantiates a client
6646
try (SessionsClient sessionsClient = SessionsClient.create()) {
6747
// Set the session name using the sessionId (UUID) and projectID (my-project-id)
6848
SessionName session = SessionName.of(projectId, sessionId);
69-
System.out.println("Session Path: " + session.toString());
7049

50+
// Instructs the speech recognizer how to process the audio content.
7151
// Note: hard coding audioEncoding and sampleRateHertz for simplicity.
7252
// Audio encoding of the audio content sent in the query request.
73-
AudioEncoding audioEncoding = AudioEncoding.AUDIO_ENCODING_LINEAR_16;
74-
int sampleRateHertz = 16000;
75-
76-
// Instructs the speech recognizer how to process the audio content.
7753
InputAudioConfig inputAudioConfig = InputAudioConfig.newBuilder()
78-
.setAudioEncoding(audioEncoding) // audioEncoding = AudioEncoding.AUDIO_ENCODING_LINEAR_16
79-
.setLanguageCode(languageCode) // languageCode = "en-US"
80-
.setSampleRateHertz(sampleRateHertz) // sampleRateHertz = 16000
54+
.setAudioEncoding(AudioEncoding.AUDIO_ENCODING_LINEAR_16)
55+
.setLanguageCode("en-US") // languageCode = "en-US"
56+
.setSampleRateHertz(16000) // sampleRateHertz = 16000
8157
.build();
8258

83-
ApiStreamObserver<StreamingDetectIntentResponse> responseObserver =
84-
new ApiStreamObserver<StreamingDetectIntentResponse>() {
85-
@Override
86-
public void onNext(StreamingDetectIntentResponse response) {
87-
// Do something when receive a response
88-
responses.add(response);
89-
}
90-
91-
@Override
92-
public void onError(Throwable t) {
93-
// Add error-handling
94-
responseThrowables.add(t);
95-
}
96-
97-
@Override
98-
public void onCompleted() {
99-
// Do something when complete.
100-
notification.countDown();
101-
}
102-
};
103-
104-
// Performs the streaming detect intent callable request
105-
ApiStreamObserver<StreamingDetectIntentRequest> requestObserver =
106-
sessionsClient.streamingDetectIntentCallable().bidiStreamingCall(responseObserver);
107-
10859
// Build the query with the InputAudioConfig
10960
QueryInput queryInput = QueryInput.newBuilder().setAudioConfig(inputAudioConfig).build();
11061

111-
try (FileInputStream audioStream = new FileInputStream(audioFilePath)) {
112-
// The first request contains the configuration
113-
StreamingDetectIntentRequest request = StreamingDetectIntentRequest.newBuilder()
114-
.setSession(session.toString())
115-
.setQueryInput(queryInput)
116-
.build();
62+
// Create the Bidirectional stream
63+
BidiStream<StreamingDetectIntentRequest, StreamingDetectIntentResponse> bidiStream =
64+
sessionsClient.streamingDetectIntentCallable().call();
11765

118-
// Make the first request
119-
requestObserver.onNext(request);
66+
// The first request must **only** contain the audio configuration:
67+
bidiStream.send(StreamingDetectIntentRequest.newBuilder()
68+
.setSession(session.toString())
69+
.setQueryInput(queryInput)
70+
.build());
12071

72+
try (FileInputStream audioStream = new FileInputStream(audioFilePath)) {
73+
// Subsequent requests must **only** contain the audio data.
12174
// Following messages: audio chunks. We just read the file in fixed-size chunks. In reality
12275
// you would split the user input by time.
12376
byte[] buffer = new byte[4096];
12477
int bytes;
12578
while ((bytes = audioStream.read(buffer)) != -1) {
126-
requestObserver.onNext(
79+
bidiStream.send(
12780
StreamingDetectIntentRequest.newBuilder()
12881
.setInputAudio(ByteString.copyFrom(buffer, 0, bytes))
12982
.build());
13083
}
131-
} catch (RuntimeException e) {
132-
// Cancel stream.
133-
requestObserver.onError(e);
134-
}
135-
// Half-close the stream.
136-
requestObserver.onCompleted();
137-
// Wait for the final response (without explicit timeout).
138-
notification.await();
139-
// Process errors/responses.
140-
if (!responseThrowables.isEmpty()) {
141-
throw responseThrowables.get(0);
142-
}
143-
if (responses.isEmpty()) {
144-
throw new RuntimeException("No response from Dialogflow.");
14584
}
14685

147-
for (StreamingDetectIntentResponse response : responses) {
148-
if (response.hasRecognitionResult()) {
149-
System.out.format(
150-
"Intermediate transcript: '%s'\n", response.getRecognitionResult().getTranscript());
151-
}
152-
}
86+
// Tell the service you are done sending data
87+
bidiStream.closeSend();
15388

154-
// Display the last query result
155-
QueryResult queryResult = responses.get(responses.size() - 1).getQueryResult();
156-
System.out.println("====================");
157-
System.out.format("Query Text: '%s'\n", queryResult.getQueryText());
158-
System.out.format("Detected Intent: %s (confidence: %f)\n",
159-
queryResult.getIntent().getDisplayName(), queryResult.getIntentDetectionConfidence());
160-
System.out.format("Fulfillment Text: '%s'\n", queryResult.getFulfillmentText());
89+
for (StreamingDetectIntentResponse response : bidiStream) {
90+
QueryResult queryResult = response.getQueryResult();
91+
System.out.println("====================");
92+
System.out.format("Intent Display Name: %s\n", queryResult.getIntent().getDisplayName());
93+
System.out.format("Query Text: '%s'\n", queryResult.getQueryText());
94+
System.out.format("Detected Intent: %s (confidence: %f)\n",
95+
queryResult.getIntent().getDisplayName(), queryResult.getIntentDetectionConfidence());
96+
System.out.format("Fulfillment Text: '%s'\n", queryResult.getFulfillmentText());
16197

162-
return responses;
98+
}
99+
} catch (IOException e) {
100+
e.printStackTrace();
163101
}
164102
}
165-
// [END dialogflow_detect_intent_streaming]
166103
}
104+
// [END dialogflow_detect_intent_streaming]

dialogflow/snippets/src/test/java/com/example/dialogflow/DetectIntentStreamIT.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@
1616

1717
package com.example.dialogflow;
1818

19-
import static org.junit.Assert.assertTrue;
20-
21-
import com.google.cloud.dialogflow.v2.StreamingDetectIntentResponse;
19+
import static org.junit.Assert.assertThat;
2220

2321
import java.io.ByteArrayOutputStream;
22+
import java.io.IOException;
2423
import java.io.PrintStream;
25-
import java.util.List;
26-
24+
import org.hamcrest.CoreMatchers;
2725
import org.junit.After;
2826
import org.junit.Before;
2927
import org.junit.Test;
@@ -37,32 +35,35 @@
3735
@SuppressWarnings("checkstyle:abbreviationaswordinname")
3836
public class DetectIntentStreamIT {
3937

38+
private ByteArrayOutputStream bout;
39+
4040
private static String audioFilePath = "resources/book_a_room.wav";
4141
private static String PROJECT_ID = System.getenv().get("GOOGLE_CLOUD_PROJECT");
4242
private static String SESSION_ID = "fake_session_for_testing";
43-
private static String LANGUAGE_CODE = "en-US";
4443

4544
@Before
4645
public void setUp() {
47-
System.setOut(new PrintStream(new ByteArrayOutputStream()));
46+
bout = new ByteArrayOutputStream();
47+
System.setOut(new PrintStream(bout));
4848
}
4949

5050
@After
5151
public void tearDown() {
5252
System.setOut(null);
53+
bout.reset();
5354
}
5455

5556
@Test
56-
public void testStreamingDetectIntentCallable() throws Throwable {
57-
List<StreamingDetectIntentResponse> response = DetectIntentStream.detectIntentStream(
58-
PROJECT_ID, audioFilePath, SESSION_ID, LANGUAGE_CODE);
59-
assertTrue(response.size() > 0);
60-
assertTrue(response.stream().anyMatch(i -> i
61-
.getQueryResult()
62-
.getIntent()
63-
.getDisplayName().equals("room.reservation")));
64-
assertTrue(response.stream().anyMatch(i -> i
65-
.getRecognitionResult()
66-
.getTranscript().contains("book")));
57+
public void testStreamingDetectIntentCallable() {
58+
DetectIntentStream.detectIntentStream(
59+
PROJECT_ID, audioFilePath, SESSION_ID);
60+
61+
String output = bout.toString();
62+
63+
assertThat(output, CoreMatchers.containsString(
64+
"Intent Display Name: room.reservation"));
65+
66+
assertThat(output, CoreMatchers.containsString(
67+
"book"));
6768
}
6869
}

dialogflow/snippets/src/test/java/com/example/dialogflow/KnowledgeBaseManagementIT.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ public class KnowledgeBaseManagementIT {
5555

5656
private static List<String> TEXTS = ImmutableList
5757
.of("How do I sign up?", "Is my data redundant?", "Where can I find pricing information?",
58-
"Where is my data stored?", "What are my support options?");
58+
"Where is my data stored?", "What are my support options?",
59+
"How can I maximize the availability of my data?");
5960

6061
@Before
6162
public void setUp() {
@@ -167,11 +168,11 @@ public void testDetectIntentKnowledge() throws Exception {
167168
for (String text : TEXTS) {
168169
KnowledgeAnswers knowledgeAnswers = allAnswers.get(text);
169170
if (knowledgeAnswers.getAnswersCount() > 0) {
170-
answersFound++;
171171
Answer answer = knowledgeAnswers.getAnswers(0);
172-
assertEquals(text, answer.getFaqQuestion());
173-
assertEquals(document.getName(), answer.getSource());
174-
assertThat(answer.getAnswer()).contains("Cloud Storage");
172+
if (text.equals(answer.getFaqQuestion()) && document.getName().equals(answer.getSource())
173+
&& answer.getAnswer().contains("Cloud Storage")) {
174+
answersFound++;
175+
}
175176
}
176177
}
177178
// To make the test less flaky, check that half of the texts got a result.

0 commit comments

Comments
 (0)