-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samples: add samples for Dialogflow CX (#15)
* Add sample codes. Add the sample codes for the following method: - CreateFlow - CreatePage - CreateIntent - DetectIntent - StreamingDetectIntent * Small fix to header Small fix to header to pass lint * Fix package name Fix package format. * Add tests for sample codes. Add tests for sample codes per change request. * Fix lint error. Change the location of variable declarations. * Exclude new dependencies from the region tag Exclude new dependencies from the region tag * Add integration tests for sample codes. Add integration tests for sample codes. * Change env vars and package based on comments Change the env variables and package names based on comments. Remove DIALOGFLOW_CX_AGENT_ID from Kokora configs. * Refactor code and move clean up logics * Change output stream Avoid setting output stream to null to enable tests to run in parallel. * Get audio transcript for test
- Loading branch information
1 parent
9683553
commit 01ee354
Showing
11 changed files
with
778 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
79 changes: 79 additions & 0 deletions
79
dialogflow-cx/snippets/src/main/java/dialogflow/cx/CreateFlow.java
This file contains 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,79 @@ | ||
/* | ||
* 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_create_flow] | ||
|
||
import com.google.api.gax.rpc.ApiException; | ||
import com.google.cloud.dialogflow.cx.v3beta1.AgentName; | ||
import com.google.cloud.dialogflow.cx.v3beta1.EventHandler; | ||
import com.google.cloud.dialogflow.cx.v3beta1.Flow; | ||
import com.google.cloud.dialogflow.cx.v3beta1.FlowsClient; | ||
import com.google.cloud.dialogflow.cx.v3beta1.Fulfillment; | ||
import com.google.cloud.dialogflow.cx.v3beta1.ResponseMessage; | ||
import com.google.cloud.dialogflow.cx.v3beta1.ResponseMessage.Text; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class CreateFlow { | ||
|
||
// Create a flow in the specified agent. | ||
public static Flow createFlow( | ||
String displayName, | ||
String projectId, | ||
String locationId, | ||
String agentId, | ||
Map<String, String> eventsToFulfillmentMessages) | ||
throws IOException, ApiException { | ||
// Instantiates a client | ||
try (FlowsClient flowsClient = FlowsClient.create()) { | ||
// Set the project agent name using the projectID (my-project-id), locationID (global), and | ||
// agentID (UUID). | ||
AgentName parent = AgentName.of(projectId, locationId, agentId); | ||
|
||
// Build the EventHandlers for the flow using the mapping from events to fulfillment messages. | ||
List<EventHandler> eventHandlers = new ArrayList<>(); | ||
for (Map.Entry<String, String> item : eventsToFulfillmentMessages.entrySet()) { | ||
eventHandlers.add( | ||
EventHandler.newBuilder() | ||
.setEvent(item.getKey()) // Event (sys.no-match-default) | ||
.setTriggerFulfillment( | ||
Fulfillment.newBuilder() | ||
// Text ("Sorry, could you say that again?") | ||
.addMessages( | ||
ResponseMessage.newBuilder() | ||
.setText(Text.newBuilder().addText(item.getValue()).build()) | ||
.build()) | ||
.build()) | ||
.build()); | ||
} | ||
|
||
// Build the flow. | ||
Flow flow = | ||
Flow.newBuilder().setDisplayName(displayName).addAllEventHandlers(eventHandlers).build(); | ||
|
||
// Performs the create flow request. | ||
Flow response = flowsClient.createFlow(parent, flow); | ||
System.out.format("Flow created: %s\n", response); | ||
|
||
return response; | ||
} | ||
} | ||
} | ||
// [END dialogflow_cx_create_flow] |
72 changes: 72 additions & 0 deletions
72
dialogflow-cx/snippets/src/main/java/dialogflow/cx/CreateIntent.java
This file contains 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,72 @@ | ||
/* | ||
* 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_create_intent] | ||
|
||
import com.google.api.gax.rpc.ApiException; | ||
import com.google.cloud.dialogflow.cx.v3beta1.AgentName; | ||
import com.google.cloud.dialogflow.cx.v3beta1.Intent; | ||
import com.google.cloud.dialogflow.cx.v3beta1.Intent.TrainingPhrase; | ||
import com.google.cloud.dialogflow.cx.v3beta1.Intent.TrainingPhrase.Part; | ||
import com.google.cloud.dialogflow.cx.v3beta1.IntentsClient; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class CreateIntent { | ||
|
||
// Create an intent of the given intent type. | ||
public static Intent createIntent( | ||
String displayName, | ||
String projectId, | ||
String locationId, | ||
String agentId, | ||
List<String> trainingPhrasesParts) | ||
throws IOException, ApiException { | ||
// Instantiates a client | ||
try (IntentsClient intentsClient = IntentsClient.create()) { | ||
// Set the project agent name using the projectID (my-project-id), locationID (global), and | ||
// agentID (UUID). | ||
AgentName parent = AgentName.of(projectId, locationId, agentId); | ||
|
||
// Build the trainingPhrases from the trainingPhrasesParts. | ||
List<TrainingPhrase> trainingPhrases = new ArrayList<>(); | ||
for (String trainingPhrase : trainingPhrasesParts) { | ||
trainingPhrases.add( | ||
TrainingPhrase.newBuilder() | ||
.addParts(Part.newBuilder().setText(trainingPhrase).build()) | ||
.setRepeatCount(1) | ||
.build()); | ||
} | ||
|
||
// Build the intent. | ||
Intent intent = | ||
Intent.newBuilder() | ||
.setDisplayName(displayName) | ||
.addAllTrainingPhrases(trainingPhrases) | ||
.build(); | ||
|
||
// Performs the create intent request. | ||
Intent response = intentsClient.createIntent(parent, intent); | ||
System.out.format("Intent created: %s\n", response); | ||
|
||
return response; | ||
} | ||
} | ||
} | ||
// [END dialogflow_cx_create_intent] |
101 changes: 101 additions & 0 deletions
101
dialogflow-cx/snippets/src/main/java/dialogflow/cx/CreatePage.java
This file contains 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,101 @@ | ||
/* | ||
* 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_create_page] | ||
|
||
import com.google.api.gax.rpc.ApiException; | ||
import com.google.cloud.dialogflow.cx.v3beta1.FlowName; | ||
import com.google.cloud.dialogflow.cx.v3beta1.Form; | ||
import com.google.cloud.dialogflow.cx.v3beta1.Form.Parameter; | ||
import com.google.cloud.dialogflow.cx.v3beta1.Form.Parameter.FillBehavior; | ||
import com.google.cloud.dialogflow.cx.v3beta1.Fulfillment; | ||
import com.google.cloud.dialogflow.cx.v3beta1.Page; | ||
import com.google.cloud.dialogflow.cx.v3beta1.PagesClient; | ||
import com.google.cloud.dialogflow.cx.v3beta1.ResponseMessage; | ||
import com.google.cloud.dialogflow.cx.v3beta1.ResponseMessage.Text; | ||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
public class CreatePage { | ||
|
||
// Create a page in the specified agent. | ||
public static Page createPage( | ||
String displayName, | ||
String projectId, | ||
String locationId, | ||
String agentId, | ||
String flowId, | ||
List<String> entryTexts) | ||
throws IOException, ApiException { | ||
// Instantiates a client | ||
try (PagesClient pagesClient = PagesClient.create()) { | ||
// Set the flow name using the projectID (my-project-id), locationID (global), agentID (UUID) | ||
// and flowID (UUID). | ||
FlowName parent = FlowName.of(projectId, locationId, agentId, flowId); | ||
|
||
// Build the entry fulfillment based on entry texts. | ||
Fulfillment.Builder entryFulfillmentBuilder = Fulfillment.newBuilder(); | ||
for (String entryText : entryTexts) { | ||
entryFulfillmentBuilder.addMessages( | ||
ResponseMessage.newBuilder() | ||
// Text ("Hi") | ||
.setText(Text.newBuilder().addText(entryText).build()) | ||
.build()); | ||
} | ||
Fulfillment entryFulfillment = entryFulfillmentBuilder.build(); | ||
|
||
// Build the form for the new page. | ||
// Note: hard coding parameters for simplicity. | ||
FillBehavior fillBehavior = | ||
FillBehavior.newBuilder() | ||
.setInitialPromptFulfillment( | ||
Fulfillment.newBuilder() | ||
.addMessages( | ||
ResponseMessage.newBuilder() | ||
.setText(Text.newBuilder().addText("What would you like?").build()) | ||
.build()) | ||
.build()) | ||
.build(); | ||
Form form = | ||
Form.newBuilder() | ||
.addParameters( | ||
Parameter.newBuilder() | ||
.setDisplayName("param") | ||
.setRequired(true) | ||
.setEntityType("projects/-/locations/-/agents/-/entityTypes/sys.any") | ||
.setFillBehavior(fillBehavior) | ||
.build()) | ||
.build(); | ||
|
||
// Build the page. | ||
Page page = | ||
Page.newBuilder() | ||
.setDisplayName(displayName) | ||
.setEntryFulfillment(entryFulfillment) | ||
.setForm(form) | ||
.build(); | ||
|
||
// Performs the create page request. | ||
Page response = pagesClient.createPage(parent, page); | ||
System.out.format("Page created: %s\n", response); | ||
|
||
return response; | ||
} | ||
} | ||
} | ||
// [END dialogflow_cx_create_page] |
87 changes: 87 additions & 0 deletions
87
dialogflow-cx/snippets/src/main/java/dialogflow/cx/DetectIntent.java
This file contains 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,87 @@ | ||
/* | ||
* 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_detect_intent_text] | ||
|
||
import com.google.api.gax.rpc.ApiException; | ||
import com.google.cloud.dialogflow.cx.v3beta1.DetectIntentRequest; | ||
import com.google.cloud.dialogflow.cx.v3beta1.DetectIntentResponse; | ||
import com.google.cloud.dialogflow.cx.v3beta1.QueryInput; | ||
import com.google.cloud.dialogflow.cx.v3beta1.QueryResult; | ||
import com.google.cloud.dialogflow.cx.v3beta1.SessionName; | ||
import com.google.cloud.dialogflow.cx.v3beta1.SessionsClient; | ||
import com.google.cloud.dialogflow.cx.v3beta1.TextInput; | ||
import com.google.common.collect.Maps; | ||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class DetectIntent { | ||
|
||
// DialogFlow API Detect Intent sample with text inputs. | ||
public static Map<String, QueryResult> detectIntent( | ||
String projectId, | ||
String locationId, | ||
String agentId, | ||
String sessionId, | ||
List<String> texts, | ||
String languageCode) | ||
throws IOException, ApiException { | ||
Map<String, QueryResult> queryResults = Maps.newHashMap(); | ||
// Instantiates a client | ||
try (SessionsClient sessionsClient = SessionsClient.create()) { | ||
// Set the session name using the projectID (my-project-id), locationID (global), agentID | ||
// (UUID), and sessionId (UUID). | ||
SessionName session = SessionName.of(projectId, locationId, agentId, sessionId); | ||
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 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(); | ||
|
||
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_detect_intent_text] |
Oops, something went wrong.