use of com.google.cloud.dialogflow.cx.v3beta1.Intent.TrainingPhrase in project java-dialogflow-cx by googleapis.
the class CreateIntent method 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 {
IntentsSettings.Builder intentsSettingsBuilder = IntentsSettings.newBuilder();
if (locationId.equals("global")) {
intentsSettingsBuilder.setEndpoint("dialogflow.googleapis.com:443");
} else {
intentsSettingsBuilder.setEndpoint(locationId + "-dialogflow.googleapis.com:443");
}
IntentsSettings intentsSettings = intentsSettingsBuilder.build();
// Instantiates a client
try (IntentsClient intentsClient = IntentsClient.create(intentsSettings)) {
// 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);
return response;
}
}
use of com.google.cloud.dialogflow.cx.v3beta1.Intent.TrainingPhrase in project java-dialogflow-cx by googleapis.
the class CreateIntentIT method testCreateIntentRegional.
@Test
public void testCreateIntentRegional() throws Exception {
Intent result = CreateIntent.createIntent(DISPLAY_NAME, PROJECT_ID, LOCATION_REGIONAL, AGENT_ID_REGIONAL, TRAINING_PHRASES_PARTS);
newIntentNameRegional = result.getName();
System.out.println("intent name new:" + newIntentNameRegional);
assertEquals(result.getTrainingPhrasesCount(), TRAINING_PHRASES_PARTS.size());
for (TrainingPhrase trainingPhrase : result.getTrainingPhrasesList()) {
assertEquals(trainingPhrase.getPartsCount(), 1);
String partText = trainingPhrase.getParts(0).getText();
assertTrue(partText.equals("red") || partText.equals("blue") || partText.equals("green"));
}
}
use of com.google.cloud.dialogflow.cx.v3beta1.Intent.TrainingPhrase in project java-dialogflow-cx by googleapis.
the class CreateIntentIT method testCreateIntentGlobal.
@Test
public void testCreateIntentGlobal() throws Exception {
Intent result = CreateIntent.createIntent(DISPLAY_NAME, PROJECT_ID, LOCATION_GLOBAL, AGENT_ID_GLOBAL, TRAINING_PHRASES_PARTS);
newIntentNameGlobal = result.getName();
assertEquals(result.getTrainingPhrasesCount(), TRAINING_PHRASES_PARTS.size());
for (TrainingPhrase trainingPhrase : result.getTrainingPhrasesList()) {
assertEquals(trainingPhrase.getPartsCount(), 1);
String partText = trainingPhrase.getParts(0).getText();
assertTrue(partText.equals("red") || partText.equals("blue") || partText.equals("green"));
}
}
use of com.google.cloud.dialogflow.cx.v3beta1.Intent.TrainingPhrase in project java-dialogflow by googleapis.
the class IntentManagement method createIntent.
// [END dialogflow_list_intents]
// [START dialogflow_create_intent]
/**
* Create an intent of the given intent type
*
* @param displayName The display name of the intent.
* @param projectId Project/Agent Id.
* @param trainingPhrasesParts Training phrases.
* @param messageTexts Message texts for the agent's response when the intent is detected.
* @return The created Intent.
*/
public static Intent createIntent(String displayName, String projectId, List<String> trainingPhrasesParts, List<String> messageTexts) throws ApiException, IOException {
// Instantiates a client
try (IntentsClient intentsClient = IntentsClient.create()) {
// Set the project agent name using the projectID (my-project-id)
AgentName parent = AgentName.of(projectId);
// 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()).build());
}
// Build the message texts for the agent's response
Message message = Message.newBuilder().setText(Text.newBuilder().addAllText(messageTexts).build()).build();
// Build the intent
Intent intent = Intent.newBuilder().setDisplayName(displayName).addMessages(message).addAllTrainingPhrases(trainingPhrases).build();
// Performs the create intent request
Intent response = intentsClient.createIntent(parent, intent);
System.out.format("Intent created: %s\n", response);
return response;
}
}
Aggregations