This repository was archived by the owner on Sep 16, 2023. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 16
docs: add DetectIntent samples #500
Merged
aribray merged 61 commits into googleapis:main from aribray:aribray--detect-intent-samples Jul 12, 2022
Merged
Changes from all commits
Commits
Show all changes
61 commits Select commit Hold shift + click to select a range
27217aa docs:add configure webhook to set form parameter as optional or requi…
aribray 9ec52bf docs: fix checkstyle
aribray 47d91ad fix checkstyle
aribray fec80c6 fix checkstyle
aribray 7826893 fix multiline string
aribray b0ea884 fix checkstyle violations
aribray acd6659 fix checkstyle violations
aribray d9fb522 remove unused variable
aribray fedcc6b fix checkstyle violations
aribray 3b3d65f 🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] aec26af fix checkstyle violations
aribray 984a4bd fix checkstyle violations
aribray 4428653 fix checkstyle violations
aribray f6d9d1d 🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 006eda1 Merge remote-tracking branch 'origin/main' into aribray--webhook-samples
aribray 7c35bf0 Merge branch 'aribray--webhook-samples' of github.com:aribray/java-di…
aribray fe9b212 remove unnecessary samples
aribray 77ba085 add webhook configure session parameter sample and test
aribray 16ab5b8 add webhook validate form parameter sample and test
aribray 7bf63a0 fix checkstyle violations
aribray 5176e43 fix checkstyle violations
aribray 9d65836 🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] c6600ab add detectintent with intent input sample and test
aribray 20a4af4 docs: add detect intent samples and tests
aribray 7bc2f49 Merge branch 'aribray--detect-intent-samples' of github.com:aribray/j…
aribray 7d4dad9 add detectintent with audio input sample and test
aribray c4654a1 fix checkstyle violations
aribray 6e95970 add detectintent with event input sample and test
aribray 0227237 add detectintent with sentiment analysis sample and test
aribray 6745538 add detectintent with webhook disabled sample and test
aribray 711b936 add detectintentstreamingpartialresponse sample and test
aribray d3d9153 add detectintentsynthesizettsoutput sample and test
aribray 8b2db12 fix checkstyle violations
aribray 0eb6f87 rename detectintent with text input sample and test
aribray dba9630 🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 45a161d change the name back
aribray 2fb85f5 Merge branch 'aribray--detect-intent-samples' of github.com:aribray/j…
aribray 44e137a 🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 121eabe fix comments
aribray 38656f0 Merge branch 'aribray--detect-intent-samples' of github.com:aribray/j…
aribray 7535047 remove old webhook samples
aribray 690fb44 🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] de4a43c v3beta1 to v3
aribray a0713d8 Merge branch 'aribray--detect-intent-samples' of github.com:aribray/j…
aribray 085589f v3beta1 to v3
aribray 99e49e8 add flag to kokoro build script
aribray 5698e11 fix test assertion
aribray ae7c54b 🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 3e12cc1 remove flag
aribray 4cce0dd Merge branch 'aribray--detect-intent-samples' of github.com:aribray/j…
aribray 73ab951 hardcode parameters
aribray 7f03607 move sessionID comments
aribray a9da7a4 add main and hardcode variables
aribray 613028c add session path comments and path format
aribray 5bb7eda checkstyle
aribray cd8d6c6 checkstyle
aribray 1eeeec7 🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 2d8a47e Merge branch 'main' into aribray--detect-intent-samples
aribray f146dce Merge remote-tracking branch 'origin/main' into aribray--detect-inten…
aribray d4569f8 fix list construction
aribray b68e81c import ArrayList
aribray File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions 128 samples/snippets/src/main/java/dialogflow/cx/DetectIntentAudioInput.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| 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] | ||
120 changes: 120 additions & 0 deletions 120 samples/snippets/src/main/java/dialogflow/cx/DetectIntentDisableWebhook.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| 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 { | ||
aribray marked this conversation as resolved. Show resolved Hide resolved | ||
| | ||
| 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] | ||
98 changes: 98 additions & 0 deletions 98 samples/snippets/src/main/java/dialogflow/cx/DetectIntentEventInput.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| 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] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.