Skip to content

Commit

Permalink
docs: add DetectIntent samples (#500)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
2 people authored and Shabirmean committed Nov 17, 2022
1 parent 4cbd434 commit 1ae094d
Show file tree
Hide file tree
Showing 16 changed files with 1,335 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dialogflow.cx;

// [START dialogflow_cx_v3_detect_intent_audio_input]

import com.google.api.gax.rpc.ApiException;
import com.google.cloud.dialogflow.cx.v3.AudioEncoding;
import com.google.cloud.dialogflow.cx.v3.AudioInput;
import com.google.cloud.dialogflow.cx.v3.DetectIntentRequest;
import com.google.cloud.dialogflow.cx.v3.DetectIntentResponse;
import com.google.cloud.dialogflow.cx.v3.InputAudioConfig;
import com.google.cloud.dialogflow.cx.v3.QueryInput;
import com.google.cloud.dialogflow.cx.v3.QueryResult;
import com.google.cloud.dialogflow.cx.v3.SessionName;
import com.google.cloud.dialogflow.cx.v3.SessionsClient;
import com.google.cloud.dialogflow.cx.v3.SessionsSettings;
import com.google.protobuf.ByteString;
import java.io.FileInputStream;
import java.io.IOException;

public class DetectIntentAudioInput {

// DialogFlow API Detect Intent sample with Audio input.
public static void main(String[] args) throws IOException, ApiException {
/** TODO (developer): replace these values with your own values */
String projectId = "my-project-id";
String locationId = "global";
String agentId = "my-agent-id";
String audioFileName = "resources/book_a_room.wav";
int sampleRateHertz = 16000;
/*
* A session ID is a string of at most 36 bytes in size.
* Your system is responsible for generating unique session IDs.
* They can be random numbers, hashed end-user identifiers,
* or any other values that are convenient for you to generate.
*/
String sessionId = "my-UUID";
String languageCode = "en";

detectIntent(
projectId, locationId, agentId, audioFileName, sampleRateHertz, sessionId, languageCode);
}

public static void detectIntent(
String projectId,
String locationId,
String agentId,
String audioFileName,
int sampleRateHertz,
String sessionId,
String languageCode)
throws IOException, ApiException {

SessionsSettings.Builder sessionsSettingsBuilder = SessionsSettings.newBuilder();
if (locationId.equals("global")) {
sessionsSettingsBuilder.setEndpoint("dialogflow.googleapis.com:443");
} else {
sessionsSettingsBuilder.setEndpoint(locationId + "-dialogflow.googleapis.com:443");
}
SessionsSettings sessionsSettings = sessionsSettingsBuilder.build();

// Instantiates a client by setting the session name.
// Format:`projects/<ProjectID>/locations/<LocationID>/agents/<AgentID>/sessions/<SessionID>`
try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) {
SessionName session =
SessionName.ofProjectLocationAgentSessionName(projectId, locationId, agentId, sessionId);

// TODO : Uncomment if you want to print session path
// System.out.println("Session Path: " + session.toString());
InputAudioConfig inputAudioConfig =
InputAudioConfig.newBuilder()
.setAudioEncoding(AudioEncoding.AUDIO_ENCODING_LINEAR_16)
.setSampleRateHertz(sampleRateHertz)
.build();

try (FileInputStream audioStream = new FileInputStream(audioFileName)) {
// 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 = audioStream.read(buffer);
AudioInput audioInput =
AudioInput.newBuilder()
.setAudio(ByteString.copyFrom(buffer, 0, bytes))
.setConfig(inputAudioConfig)
.build();
QueryInput queryInput =
QueryInput.newBuilder()
.setAudio(audioInput)
.setLanguageCode("en-US") // languageCode = "en-US"
.build();

DetectIntentRequest request =
DetectIntentRequest.newBuilder()
.setSession(session.toString())
.setQueryInput(queryInput)
.build();

// Performs the detect intent request.
DetectIntentResponse response = sessionsClient.detectIntent(request);

// Display the query result.
QueryResult queryResult = response.getQueryResult();

System.out.println("====================");
System.out.format(
"Detected Intent: %s (confidence: %f)\n",
queryResult.getTranscript(), queryResult.getIntentDetectionConfidence());
}
}
}
}
// [END dialogflow_cx_v3_detect_intent_audio_input]
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dialogflow.cx;

// [START dialogflow_cx_v3_detect_intent_disable_webhook]

import com.google.api.gax.rpc.ApiException;
import com.google.cloud.dialogflow.cx.v3.DetectIntentRequest;
import com.google.cloud.dialogflow.cx.v3.DetectIntentResponse;
import com.google.cloud.dialogflow.cx.v3.QueryInput;
import com.google.cloud.dialogflow.cx.v3.QueryParameters;
import com.google.cloud.dialogflow.cx.v3.QueryResult;
import com.google.cloud.dialogflow.cx.v3.SessionName;
import com.google.cloud.dialogflow.cx.v3.SessionsClient;
import com.google.cloud.dialogflow.cx.v3.SessionsSettings;
import com.google.cloud.dialogflow.cx.v3.TextInput;
import com.google.common.collect.Maps;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class DetectIntentDisableWebhook {

public static void main(String[] args) throws IOException, ApiException {
String projectId = "my-project-id";
String locationId = "global";
String agentId = "my-agent-id";
String sessionId = "my-UUID";
List<String> texts = new ArrayList<>(List.of("my-list", "of-texts"));
String languageCode = "en";

detectIntent(projectId, locationId, agentId, sessionId, texts, languageCode);
}

// DialogFlow API Detect Intent sample with webhook disabled.
public static Map<String, QueryResult> detectIntent(
String projectId,
String locationId,
String agentId,
String sessionId,
List<String> texts,
String languageCode)
throws IOException, ApiException {
SessionsSettings.Builder sessionsSettingsBuilder = SessionsSettings.newBuilder();
if (locationId.equals("global")) {
sessionsSettingsBuilder.setEndpoint("dialogflow.googleapis.com:443");
} else {
sessionsSettingsBuilder.setEndpoint(locationId + "-dialogflow.googleapis.com:443");
}
SessionsSettings sessionsSettings = sessionsSettingsBuilder.build();

Map<String, QueryResult> queryResults = Maps.newHashMap();

// Instantiates a client by setting the session name.
// Format:`projects/<ProjectID>/locations/<LocationID>/agents/<AgentID>/sessions/<SessionID>`
try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) {
SessionName session =
SessionName.ofProjectLocationAgentSessionName(projectId, locationId, agentId, sessionId);

// TODO : Uncomment if you want to print session path
// System.out.println("Session Path: " + session.toString());

// Detect intents for each text input.
for (String text : texts) {
// Set the text (hello) for the query.
TextInput.Builder textInput = TextInput.newBuilder().setText(text);

// Build the query with the TextInput and language code (en-US).
QueryInput queryInput =
QueryInput.newBuilder().setText(textInput).setLanguageCode(languageCode).build();

// Build the query parameters and setDisableWebhook to true.
QueryParameters queryParameters =
QueryParameters.newBuilder().setDisableWebhook(true).build();

// Build the DetectIntentRequest with the SessionName, QueryInput, and QueryParameters.
DetectIntentRequest request =
DetectIntentRequest.newBuilder()
.setSession(session.toString())
.setQueryInput(queryInput)
.setQueryParams(queryParameters)
.build();
System.out.println(request.toString());

// Performs the detect intent request.
DetectIntentResponse response = sessionsClient.detectIntent(request);

// Display the query result.
QueryResult queryResult = response.getQueryResult();

// TODO : Uncomment if you want to print queryResult
// System.out.println("====================");
// System.out.format("Query Text: '%s'\n", queryResult.getText());
// System.out.format(
// "Detected Intent: %s (confidence: %f)\n",
// queryResult.getIntent().getDisplayName(),
// queryResult.getIntentDetectionConfidence());

queryResults.put(text, queryResult);
}
}
return queryResults;
}
}
// [END dialogflow_cx_v3_detect_intent_disable_webhook]
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dialogflow.cx;

// [START dialogflow_cx_v3_detect_intent_event_input]

import com.google.api.gax.rpc.ApiException;
import com.google.cloud.dialogflow.cx.v3.DetectIntentRequest;
import com.google.cloud.dialogflow.cx.v3.DetectIntentResponse;
import com.google.cloud.dialogflow.cx.v3.EventInput;
import com.google.cloud.dialogflow.cx.v3.QueryInput;
import com.google.cloud.dialogflow.cx.v3.QueryResult;
import com.google.cloud.dialogflow.cx.v3.SessionName;
import com.google.cloud.dialogflow.cx.v3.SessionsClient;
import com.google.cloud.dialogflow.cx.v3.SessionsSettings;
import java.io.IOException;

public class DetectIntentEventInput {

// DialogFlow API Detect Intent sample with Event input.
public static void main(String[] args) throws IOException, ApiException {
String projectId = "my-project-id";
String locationId = "global";
String agentId = "my-agent-id";
String sessionId = "my-UUID";
String event = "my-event-id";
String languageCode = "en";

detectIntent(projectId, locationId, agentId, sessionId, event, languageCode);
}

public static void detectIntent(
String projectId,
String locationId,
String agentId,
String sessionId,
String event,
String languageCode)
throws IOException, ApiException {

SessionsSettings.Builder sessionsSettingsBuilder = SessionsSettings.newBuilder();
if (locationId.equals("global")) {
sessionsSettingsBuilder.setEndpoint("dialogflow.googleapis.com:443");
} else {
sessionsSettingsBuilder.setEndpoint(locationId + "-dialogflow.googleapis.com:443");
}
SessionsSettings sessionsSettings = sessionsSettingsBuilder.build();

// Instantiates a client by setting the session name.
// Format:`projects/<ProjectID>/locations/<LocationID>/agents/<AgentID>/sessions/<SessionID>`
try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) {
SessionName session =
SessionName.ofProjectLocationAgentSessionName(projectId, locationId, agentId, sessionId);

// TODO : Uncomment if you want to print session path
// System.out.println("Session Path: " + session.toString());

EventInput.Builder eventInput = EventInput.newBuilder().setEvent(event);

// Build the query with the EventInput and language code (en-US).
QueryInput queryInput =
QueryInput.newBuilder().setEvent(eventInput).setLanguageCode(languageCode).build();

// Build the DetectIntentRequest with the SessionName and QueryInput.
DetectIntentRequest request =
DetectIntentRequest.newBuilder()
.setSession(session.toString())
.setQueryInput(queryInput)
.build();

// Performs the detect intent request.
DetectIntentResponse response = sessionsClient.detectIntent(request);

// Display the query result.
QueryResult queryResult = response.getQueryResult();

// TODO : Uncomment if you want to print queryResult
System.out.println("====================");
System.out.format("Triggering Event: %s \n", queryResult.getTriggerEvent());
}
}
}

// [END dialogflow_cx_v3_detect_intent_event_input]
Loading

0 comments on commit 1ae094d

Please sign in to comment.