Skip to content

Commit 1ae094d

Browse files
aribraygcf-owl-bot[bot]
authored andcommitted
docs: add DetectIntent samples (#500)
* docs:add configure webhook to set form parameter as optional or required sample and test Change-Id: I75bed489bd8abefca60263acb266501810c0e856 * docs: fix checkstyle Change-Id: I18be2a5c1d46ac9838cf4a15ab87dcf331ee26ee * fix checkstyle Change-Id: Ia85899f678ef1382718c2443a682770417340af2 * fix checkstyle Change-Id: I4dd56107a3197027f287980fa87a15e962154783 * fix multiline string Change-Id: I8dcdb0dcaad486a2fb69e0737df99bda49b28839 * fix checkstyle violations Change-Id: I5f4521b3e50660f30f9f73da3d939da213084d28 * fix checkstyle violations Change-Id: I39dedfba1b62be5cb4580a42dde270146f3b6f97 * remove unused variable Change-Id: Ic54ae0d1f091079661f3fa67097cdcd2e888dc90 * fix checkstyle violations Change-Id: Ifd303b7395844240dca0b29cde9b791112fcabe5 * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix checkstyle violations Change-Id: I59e45da9f9e57b327a527f1405816ac1275fe1c8 * fix checkstyle violations Change-Id: I70ff4118cc8b3111a3ed55a2fae32258d3f6819c * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * remove unnecessary samples Change-Id: I395dd5abc9a48733126ab0ecdc2a974fbf9eb471 * add webhook configure session parameter sample and test Change-Id: Ia599ab7f4d432477d9952b4e12c709845f1ad97b * add webhook validate form parameter sample and test Change-Id: I8cdf93fd9e21161757d198b1338c5ab77bb3a236 * fix checkstyle violations Change-Id: I90f1f626588fd59c116d119348054c914945cd1d * fix checkstyle violations Change-Id: I5ff686ab2ffc94b358bdf87b8dfc3d00f5e45657 * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * add detectintent with intent input sample and test Change-Id: I4a2bef88da7ed04d343da63544563130182e6aaf * docs: add detect intent samples and tests Change-Id: I8e6a26441a79b1ed44def4877c822040cb143e33 * add detectintent with audio input sample and test Change-Id: I741c73d3f14bfe5f9b4ff6ec55d43f2687ac51dd * fix checkstyle violations Change-Id: I216ade5c8bca9e85850b6ab84b2a70eab0d4fd53 * add detectintent with event input sample and test Change-Id: Ie0846bd780f20cd1a8406f8a1278ccf45824b646 * add detectintent with sentiment analysis sample and test Change-Id: I7d8c855bd0e1a0bc19288abd968a4602a909b3df * add detectintent with webhook disabled sample and test Change-Id: Idf6c2005b7a1970e3c8227080bb91a1d408474b0 * add detectintentstreamingpartialresponse sample and test Change-Id: Id1a0017757da3eee5e0556f0c7ba8954f9907ca3 * add detectintentsynthesizettsoutput sample and test Change-Id: I389daae0a1b5b7fb858dd8c81ceba99475ce3ca3 * fix checkstyle violations Change-Id: Ic247c4b7d50df1a6a5eb74fe0899defc63478b0e * rename detectintent with text input sample and test Change-Id: Ic9b54f74723f716cd688e780675ce878487251aa * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * change the name back Change-Id: I153f94d5c9c1c53d96365fb05c02625c2dc5f745 * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix comments Change-Id: I02c0ce2e61ab74026d04103774de02fc2bb0ae6b * remove old webhook samples Change-Id: I6d2872aa1b3c77c0f08cd62a508d351066490f25 * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * v3beta1 to v3 Change-Id: I3850ed76ea36def3cf215c7261fa1bffba274d6e * v3beta1 to v3 Change-Id: Ie0539f4a6ca8d5c9cb21ac528e09ce8f992a0486 * add flag to kokoro build script Change-Id: I5c3862c61931b28772e408b5e2d68e7cc5af72d2 * fix test assertion Change-Id: I2bffb8d973362d032265f7bd7b89aa88555de819 * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * remove flag Change-Id: Ie3c449bdb635180653237745a8cc282afb088a57 * hardcode parameters Change-Id: Iac48159892b0b6a34f31b013d446dfd66e62b8a7 * move sessionID comments Change-Id: I8e3fcea6a24739cb7a1e49d7572688e22d4d0147 * add main and hardcode variables Change-Id: I29f4cb587b10f46576aa36a56cde8c8fcb6e1acb * add session path comments and path format Change-Id: I327b7e90d8562c9698bc1fc4324ec249c6326436 * checkstyle Change-Id: If9937d41cefcff2fa0480524d036aecf5a1fda40 * checkstyle Change-Id: I9980a07e9c2ca377f669f0b80bbf16a42033d634 * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix list construction Change-Id: I5115e749e47427c50e2ed0ad494525dc6f9be66a * import ArrayList Change-Id: Ibe5abeee5b6b003fc8c77eb53eb432a340629255 Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 4cbd434 commit 1ae094d

16 files changed

+1335
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dialogflow.cx;
18+
19+
// [START dialogflow_cx_v3_detect_intent_audio_input]
20+
21+
import com.google.api.gax.rpc.ApiException;
22+
import com.google.cloud.dialogflow.cx.v3.AudioEncoding;
23+
import com.google.cloud.dialogflow.cx.v3.AudioInput;
24+
import com.google.cloud.dialogflow.cx.v3.DetectIntentRequest;
25+
import com.google.cloud.dialogflow.cx.v3.DetectIntentResponse;
26+
import com.google.cloud.dialogflow.cx.v3.InputAudioConfig;
27+
import com.google.cloud.dialogflow.cx.v3.QueryInput;
28+
import com.google.cloud.dialogflow.cx.v3.QueryResult;
29+
import com.google.cloud.dialogflow.cx.v3.SessionName;
30+
import com.google.cloud.dialogflow.cx.v3.SessionsClient;
31+
import com.google.cloud.dialogflow.cx.v3.SessionsSettings;
32+
import com.google.protobuf.ByteString;
33+
import java.io.FileInputStream;
34+
import java.io.IOException;
35+
36+
public class DetectIntentAudioInput {
37+
38+
// DialogFlow API Detect Intent sample with Audio input.
39+
public static void main(String[] args) throws IOException, ApiException {
40+
/** TODO (developer): replace these values with your own values */
41+
String projectId = "my-project-id";
42+
String locationId = "global";
43+
String agentId = "my-agent-id";
44+
String audioFileName = "resources/book_a_room.wav";
45+
int sampleRateHertz = 16000;
46+
/*
47+
* A session ID is a string of at most 36 bytes in size.
48+
* Your system is responsible for generating unique session IDs.
49+
* They can be random numbers, hashed end-user identifiers,
50+
* or any other values that are convenient for you to generate.
51+
*/
52+
String sessionId = "my-UUID";
53+
String languageCode = "en";
54+
55+
detectIntent(
56+
projectId, locationId, agentId, audioFileName, sampleRateHertz, sessionId, languageCode);
57+
}
58+
59+
public static void detectIntent(
60+
String projectId,
61+
String locationId,
62+
String agentId,
63+
String audioFileName,
64+
int sampleRateHertz,
65+
String sessionId,
66+
String languageCode)
67+
throws IOException, ApiException {
68+
69+
SessionsSettings.Builder sessionsSettingsBuilder = SessionsSettings.newBuilder();
70+
if (locationId.equals("global")) {
71+
sessionsSettingsBuilder.setEndpoint("dialogflow.googleapis.com:443");
72+
} else {
73+
sessionsSettingsBuilder.setEndpoint(locationId + "-dialogflow.googleapis.com:443");
74+
}
75+
SessionsSettings sessionsSettings = sessionsSettingsBuilder.build();
76+
77+
// Instantiates a client by setting the session name.
78+
// Format:`projects/<ProjectID>/locations/<LocationID>/agents/<AgentID>/sessions/<SessionID>`
79+
try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) {
80+
SessionName session =
81+
SessionName.ofProjectLocationAgentSessionName(projectId, locationId, agentId, sessionId);
82+
83+
// TODO : Uncomment if you want to print session path
84+
// System.out.println("Session Path: " + session.toString());
85+
InputAudioConfig inputAudioConfig =
86+
InputAudioConfig.newBuilder()
87+
.setAudioEncoding(AudioEncoding.AUDIO_ENCODING_LINEAR_16)
88+
.setSampleRateHertz(sampleRateHertz)
89+
.build();
90+
91+
try (FileInputStream audioStream = new FileInputStream(audioFileName)) {
92+
// Subsequent requests must **only** contain the audio data.
93+
// Following messages: audio chunks. We just read the file in fixed-size chunks. In reality
94+
// you would split the user input by time.
95+
byte[] buffer = new byte[4096];
96+
int bytes = audioStream.read(buffer);
97+
AudioInput audioInput =
98+
AudioInput.newBuilder()
99+
.setAudio(ByteString.copyFrom(buffer, 0, bytes))
100+
.setConfig(inputAudioConfig)
101+
.build();
102+
QueryInput queryInput =
103+
QueryInput.newBuilder()
104+
.setAudio(audioInput)
105+
.setLanguageCode("en-US") // languageCode = "en-US"
106+
.build();
107+
108+
DetectIntentRequest request =
109+
DetectIntentRequest.newBuilder()
110+
.setSession(session.toString())
111+
.setQueryInput(queryInput)
112+
.build();
113+
114+
// Performs the detect intent request.
115+
DetectIntentResponse response = sessionsClient.detectIntent(request);
116+
117+
// Display the query result.
118+
QueryResult queryResult = response.getQueryResult();
119+
120+
System.out.println("====================");
121+
System.out.format(
122+
"Detected Intent: %s (confidence: %f)\n",
123+
queryResult.getTranscript(), queryResult.getIntentDetectionConfidence());
124+
}
125+
}
126+
}
127+
}
128+
// [END dialogflow_cx_v3_detect_intent_audio_input]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dialogflow.cx;
18+
19+
// [START dialogflow_cx_v3_detect_intent_disable_webhook]
20+
21+
import com.google.api.gax.rpc.ApiException;
22+
import com.google.cloud.dialogflow.cx.v3.DetectIntentRequest;
23+
import com.google.cloud.dialogflow.cx.v3.DetectIntentResponse;
24+
import com.google.cloud.dialogflow.cx.v3.QueryInput;
25+
import com.google.cloud.dialogflow.cx.v3.QueryParameters;
26+
import com.google.cloud.dialogflow.cx.v3.QueryResult;
27+
import com.google.cloud.dialogflow.cx.v3.SessionName;
28+
import com.google.cloud.dialogflow.cx.v3.SessionsClient;
29+
import com.google.cloud.dialogflow.cx.v3.SessionsSettings;
30+
import com.google.cloud.dialogflow.cx.v3.TextInput;
31+
import com.google.common.collect.Maps;
32+
import java.io.IOException;
33+
import java.util.ArrayList;
34+
import java.util.List;
35+
import java.util.Map;
36+
37+
public class DetectIntentDisableWebhook {
38+
39+
public static void main(String[] args) throws IOException, ApiException {
40+
String projectId = "my-project-id";
41+
String locationId = "global";
42+
String agentId = "my-agent-id";
43+
String sessionId = "my-UUID";
44+
List<String> texts = new ArrayList<>(List.of("my-list", "of-texts"));
45+
String languageCode = "en";
46+
47+
detectIntent(projectId, locationId, agentId, sessionId, texts, languageCode);
48+
}
49+
50+
// DialogFlow API Detect Intent sample with webhook disabled.
51+
public static Map<String, QueryResult> detectIntent(
52+
String projectId,
53+
String locationId,
54+
String agentId,
55+
String sessionId,
56+
List<String> texts,
57+
String languageCode)
58+
throws IOException, ApiException {
59+
SessionsSettings.Builder sessionsSettingsBuilder = SessionsSettings.newBuilder();
60+
if (locationId.equals("global")) {
61+
sessionsSettingsBuilder.setEndpoint("dialogflow.googleapis.com:443");
62+
} else {
63+
sessionsSettingsBuilder.setEndpoint(locationId + "-dialogflow.googleapis.com:443");
64+
}
65+
SessionsSettings sessionsSettings = sessionsSettingsBuilder.build();
66+
67+
Map<String, QueryResult> queryResults = Maps.newHashMap();
68+
69+
// Instantiates a client by setting the session name.
70+
// Format:`projects/<ProjectID>/locations/<LocationID>/agents/<AgentID>/sessions/<SessionID>`
71+
try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) {
72+
SessionName session =
73+
SessionName.ofProjectLocationAgentSessionName(projectId, locationId, agentId, sessionId);
74+
75+
// TODO : Uncomment if you want to print session path
76+
// System.out.println("Session Path: " + session.toString());
77+
78+
// Detect intents for each text input.
79+
for (String text : texts) {
80+
// Set the text (hello) for the query.
81+
TextInput.Builder textInput = TextInput.newBuilder().setText(text);
82+
83+
// Build the query with the TextInput and language code (en-US).
84+
QueryInput queryInput =
85+
QueryInput.newBuilder().setText(textInput).setLanguageCode(languageCode).build();
86+
87+
// Build the query parameters and setDisableWebhook to true.
88+
QueryParameters queryParameters =
89+
QueryParameters.newBuilder().setDisableWebhook(true).build();
90+
91+
// Build the DetectIntentRequest with the SessionName, QueryInput, and QueryParameters.
92+
DetectIntentRequest request =
93+
DetectIntentRequest.newBuilder()
94+
.setSession(session.toString())
95+
.setQueryInput(queryInput)
96+
.setQueryParams(queryParameters)
97+
.build();
98+
System.out.println(request.toString());
99+
100+
// Performs the detect intent request.
101+
DetectIntentResponse response = sessionsClient.detectIntent(request);
102+
103+
// Display the query result.
104+
QueryResult queryResult = response.getQueryResult();
105+
106+
// TODO : Uncomment if you want to print queryResult
107+
// System.out.println("====================");
108+
// System.out.format("Query Text: '%s'\n", queryResult.getText());
109+
// System.out.format(
110+
// "Detected Intent: %s (confidence: %f)\n",
111+
// queryResult.getIntent().getDisplayName(),
112+
// queryResult.getIntentDetectionConfidence());
113+
114+
queryResults.put(text, queryResult);
115+
}
116+
}
117+
return queryResults;
118+
}
119+
}
120+
// [END dialogflow_cx_v3_detect_intent_disable_webhook]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dialogflow.cx;
18+
19+
// [START dialogflow_cx_v3_detect_intent_event_input]
20+
21+
import com.google.api.gax.rpc.ApiException;
22+
import com.google.cloud.dialogflow.cx.v3.DetectIntentRequest;
23+
import com.google.cloud.dialogflow.cx.v3.DetectIntentResponse;
24+
import com.google.cloud.dialogflow.cx.v3.EventInput;
25+
import com.google.cloud.dialogflow.cx.v3.QueryInput;
26+
import com.google.cloud.dialogflow.cx.v3.QueryResult;
27+
import com.google.cloud.dialogflow.cx.v3.SessionName;
28+
import com.google.cloud.dialogflow.cx.v3.SessionsClient;
29+
import com.google.cloud.dialogflow.cx.v3.SessionsSettings;
30+
import java.io.IOException;
31+
32+
public class DetectIntentEventInput {
33+
34+
// DialogFlow API Detect Intent sample with Event input.
35+
public static void main(String[] args) throws IOException, ApiException {
36+
String projectId = "my-project-id";
37+
String locationId = "global";
38+
String agentId = "my-agent-id";
39+
String sessionId = "my-UUID";
40+
String event = "my-event-id";
41+
String languageCode = "en";
42+
43+
detectIntent(projectId, locationId, agentId, sessionId, event, languageCode);
44+
}
45+
46+
public static void detectIntent(
47+
String projectId,
48+
String locationId,
49+
String agentId,
50+
String sessionId,
51+
String event,
52+
String languageCode)
53+
throws IOException, ApiException {
54+
55+
SessionsSettings.Builder sessionsSettingsBuilder = SessionsSettings.newBuilder();
56+
if (locationId.equals("global")) {
57+
sessionsSettingsBuilder.setEndpoint("dialogflow.googleapis.com:443");
58+
} else {
59+
sessionsSettingsBuilder.setEndpoint(locationId + "-dialogflow.googleapis.com:443");
60+
}
61+
SessionsSettings sessionsSettings = sessionsSettingsBuilder.build();
62+
63+
// Instantiates a client by setting the session name.
64+
// Format:`projects/<ProjectID>/locations/<LocationID>/agents/<AgentID>/sessions/<SessionID>`
65+
try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) {
66+
SessionName session =
67+
SessionName.ofProjectLocationAgentSessionName(projectId, locationId, agentId, sessionId);
68+
69+
// TODO : Uncomment if you want to print session path
70+
// System.out.println("Session Path: " + session.toString());
71+
72+
EventInput.Builder eventInput = EventInput.newBuilder().setEvent(event);
73+
74+
// Build the query with the EventInput and language code (en-US).
75+
QueryInput queryInput =
76+
QueryInput.newBuilder().setEvent(eventInput).setLanguageCode(languageCode).build();
77+
78+
// Build the DetectIntentRequest with the SessionName and QueryInput.
79+
DetectIntentRequest request =
80+
DetectIntentRequest.newBuilder()
81+
.setSession(session.toString())
82+
.setQueryInput(queryInput)
83+
.build();
84+
85+
// Performs the detect intent request.
86+
DetectIntentResponse response = sessionsClient.detectIntent(request);
87+
88+
// Display the query result.
89+
QueryResult queryResult = response.getQueryResult();
90+
91+
// TODO : Uncomment if you want to print queryResult
92+
System.out.println("====================");
93+
System.out.format("Triggering Event: %s \n", queryResult.getTriggerEvent());
94+
}
95+
}
96+
}
97+
98+
// [END dialogflow_cx_v3_detect_intent_event_input]

0 commit comments

Comments
 (0)