Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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 com.example.dialogflow;

// [START dialogflow_create_conversation]

import com.google.api.gax.rpc.ApiException;
import com.google.cloud.dialogflow.v2.Conversation;
import com.google.cloud.dialogflow.v2.ConversationProfileName;
import com.google.cloud.dialogflow.v2.ConversationsClient;
import com.google.cloud.dialogflow.v2.LocationName;
import java.io.IOException;

public class ConversationManagement {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String location = "my-location";

// Set conversation profile id for the new conversation.
// See com.example.dialogflow.ConversationProfileManagement sample code for how to create a
// conversation profile. You can also create a conversation profile using Agent Assist console,
// https://cloud.google.com/agent-assist/docs/conversation-profile.
String conversationProfileId = "my-conversation-profile-id";

// Create a conversation
createConversation(projectId, location, conversationProfileId);
}

public static void createConversation(
String projectId, String location, String conversationProfileId)
throws ApiException, IOException {
try (ConversationsClient conversationsClient = ConversationsClient.create()) {
LocationName locationName = LocationName.of(projectId, location);
ConversationProfileName conversationProfileName =
ConversationProfileName.ofProjectLocationConversationProfileName(
projectId, location, conversationProfileId);
Conversation conversation =
Conversation.newBuilder()
.setConversationProfile(conversationProfileName.toString())
.build();
Conversation newConversation =
conversationsClient.createConversation(locationName, conversation);
System.out.println("====================");
System.out.println("Conversation Created:");
System.out.format("Life Cycle State: %s\n", newConversation.getLifecycleState());
System.out.format(
"Conversation Profile Name: %s\n", newConversation.getConversationProfile());
System.out.format("Name: %s\n", newConversation.getName());
}
}
}
// [END dialogflow_create_conversation]
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* 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 com.example.dialogflow;

// [START dialogflow_create_conversation_profile_article_suggestion]

import com.google.api.gax.rpc.ApiException;
import com.google.cloud.dialogflow.v2.ConversationProfile;
import com.google.cloud.dialogflow.v2.ConversationProfilesClient;
import com.google.cloud.dialogflow.v2.CreateConversationProfileRequest;
import com.google.cloud.dialogflow.v2.HumanAgentAssistantConfig;
import com.google.cloud.dialogflow.v2.HumanAgentAssistantConfig.SuggestionConfig;
import com.google.cloud.dialogflow.v2.HumanAgentAssistantConfig.SuggestionFeatureConfig;
import com.google.cloud.dialogflow.v2.HumanAgentAssistantConfig.SuggestionQueryConfig;
import com.google.cloud.dialogflow.v2.HumanAgentAssistantConfig.SuggestionQueryConfig.KnowledgeBaseQuerySource;
import com.google.cloud.dialogflow.v2.HumanAgentAssistantConfig.SuggestionTriggerSettings;
import com.google.cloud.dialogflow.v2.KnowledgeBaseName;
import com.google.cloud.dialogflow.v2.LocationName;
import com.google.cloud.dialogflow.v2.SuggestionFeature;
import com.google.cloud.dialogflow.v2.SuggestionFeature.Type;
import java.io.IOException;
import java.util.Optional;

public class ConversationProfileManagement {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String location = "my-location";

// Set display name of the new conversation profile
String conversationProfileDisplayName = "my-conversation-profile-display-name";

// Set knowledge base id for Article Suggestion feature.
// See details about how to create a knowledge base here,
// https://cloud.google.com/agent-assist/docs/article-suggestion.
String articleSuggestionKnowledgeBaseId = "my-article-suggestion-knowledge-base-id";

// Create a conversation profile
createConversationProfileArticleSuggestion(
projectId,
conversationProfileDisplayName,
location,
Optional.of(articleSuggestionKnowledgeBaseId));
}

// Set suggestion trigger with no_smalltalk and only_send_user both true, which means that
// the suggestion is not triggered if last utterance is small talk and is only triggered
// if participant role of last utterance is END_USER.
public static SuggestionTriggerSettings buildSuggestionTriggerSettings() {
return SuggestionTriggerSettings.newBuilder().setNoSmalltalk(true).setOnlyEndUser(true).build();
}

// Set the configuration for suggestion query, including the knowledge base query source
// and maximum number of results to return.
public static SuggestionQueryConfig buildSuggestionQueryConfig(
KnowledgeBaseName knowledgeBaseName) {
return SuggestionQueryConfig.newBuilder()
.setKnowledgeBaseQuerySource(
KnowledgeBaseQuerySource.newBuilder().addKnowledgeBases(knowledgeBaseName.toString()))
.setMaxResults(3)
.build();
}

// Create a conversation profile with given values about Article Suggestion.
public static void createConversationProfileArticleSuggestion(
String projectId,
String displayName,
String location,
Optional<String> articleSuggestionKnowledgeBaseId)
throws ApiException, IOException {
try (ConversationProfilesClient conversationProfilesClient =
ConversationProfilesClient.create()) {
// Create a builder for agent assistance configuration
SuggestionConfig.Builder suggestionConfigBuilder = SuggestionConfig.newBuilder();

// Add knowledge base for Article Suggestion feature
if (articleSuggestionKnowledgeBaseId.isPresent()) {
KnowledgeBaseName articleSuggestionKbName =
KnowledgeBaseName.of(projectId, articleSuggestionKnowledgeBaseId.get());

// Build configuration for Article Suggestion feature
SuggestionFeatureConfig articleSuggestionFeatureConfig =
SuggestionFeatureConfig.newBuilder()
.setSuggestionFeature(
SuggestionFeature.newBuilder().setType(Type.ARTICLE_SUGGESTION).build())
.setSuggestionTriggerSettings(buildSuggestionTriggerSettings())
.setQueryConfig(buildSuggestionQueryConfig(articleSuggestionKbName))
.build();

// Add Article Suggestion feature to agent assistance configuration
suggestionConfigBuilder.addFeatureConfigs(articleSuggestionFeatureConfig);
}

LocationName locationName = LocationName.of(projectId, location);
// Set a conversation profile with target configurations
ConversationProfile targetConversationProfile =
ConversationProfile.newBuilder()
.setDisplayName(displayName)
.setLanguageCode("en-US")
.setHumanAgentAssistantConfig(
HumanAgentAssistantConfig.newBuilder()
.setHumanAgentSuggestionConfig(suggestionConfigBuilder.build()))
.build();

// Create a conversation profile
ConversationProfile createdConversationProfile =
conversationProfilesClient.createConversationProfile(
CreateConversationProfileRequest.newBuilder()
.setParent(locationName.toString())
.setConversationProfile(targetConversationProfile)
.build());
System.out.println("====================");
System.out.println("Conversation Profile created:\n");
System.out.format("Display name: %s\n", createdConversationProfile.getDisplayName());
System.out.format("Name: %s\n", createdConversationProfile.getName());
}
}
}
// [END dialogflow_create_conversation_profile_article_suggestion]
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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 com.example.dialogflow;

// [START dialogflow_create_participant]

import com.google.api.gax.rpc.ApiException;
import com.google.cloud.dialogflow.v2.ConversationName;
import com.google.cloud.dialogflow.v2.Participant;
import com.google.cloud.dialogflow.v2.Participant.Role;
import com.google.cloud.dialogflow.v2.ParticipantsClient;
import java.io.IOException;

public class ParticipantManagement {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project-id";
String location = "my-location";

// Set conversation id for the new participant,
// See com.example.dialogflow.ConversationManagement sample code
// for how to create a conversation.
String conversationId = "my-conversation-id";
Role role = Role.END_USER;

// Create a participant
createParticipant(projectId, location, conversationId, role);
}

// Create a participant with given role
public static void createParticipant(
String projectId, String location, String conversationId, Role role)
throws ApiException, IOException {
try (ParticipantsClient participantsClient = ParticipantsClient.create()) {
ConversationName conversationName =
ConversationName.ofProjectLocationConversationName(projectId, location, conversationId);
Participant participant = Participant.newBuilder().setRole(role).build();
Participant newParticipant =
participantsClient.createParticipant(conversationName, participant);
System.out.println("====================");
System.out.println("Participant Created:");
System.out.format("Role: %s\n", newParticipant.getRole());
System.out.format("Name: %s\n", newParticipant.getName());
}
}
}
// [END dialogflow_create_participant]
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* 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 com.example.dialogflow;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertNotNull;

import com.google.cloud.dialogflow.v2.ConversationProfilesClient;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Optional;
import java.util.UUID;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
@SuppressWarnings("checkstyle:abbreviationaswordinname")
public class CreateConversationProfileTest {

private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
private static final String LOCATION = "global";
private static final String NAME_PREFIX_IN_OUTPUT = "Name: ";
private static String conversationProfileNameToDelete = null;
private ByteArrayOutputStream bout;
private PrintStream newOutputStream;
private PrintStream originalOutputStream;

private static void requireEnvVar(String varName) {
assertNotNull(System.getenv(varName));
}

// Extract the name of a newly created resource from latest "Name: %s\n" in sample code output
private static String getResourceNameFromOutputString(String output) {
return output.substring(
output.lastIndexOf(NAME_PREFIX_IN_OUTPUT) + NAME_PREFIX_IN_OUTPUT.length(),
output.length() - 1);
}

private static void deleteConversationProfile(String conversationProfileName) throws IOException {
try (ConversationProfilesClient conversationProfilesClient =
ConversationProfilesClient.create()) {
conversationProfilesClient.deleteConversationProfile(conversationProfileName);
}
}

@BeforeClass
public static void checkRequirements() {
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
requireEnvVar("GOOGLE_CLOUD_PROJECT");
}

@Before
public void setUp() {
originalOutputStream = System.out;
bout = new ByteArrayOutputStream();
newOutputStream = new PrintStream(bout);
System.setOut(newOutputStream);
}

@After
public void tearDown() throws IOException {
if (conversationProfileNameToDelete != null) {
deleteConversationProfile(conversationProfileNameToDelete);
conversationProfileNameToDelete = null;
}

System.setOut(originalOutputStream);
}

@Test
public void testCreateConversationProfileArticleSuggestion() throws IOException {
String conversationProfileDisplayName = UUID.randomUUID().toString();

// Create a conversation profile
String articleSuggestionKnowledgeBaseId = UUID.randomUUID().toString();
ConversationProfileManagement.createConversationProfileArticleSuggestion(
PROJECT_ID,
conversationProfileDisplayName,
LOCATION,
Optional.of(articleSuggestionKnowledgeBaseId));

String output = bout.toString();
conversationProfileNameToDelete = getResourceNameFromOutputString(output);
assertThat(output).contains(conversationProfileDisplayName);

// Delete the conversation profile
deleteConversationProfile(conversationProfileNameToDelete);
conversationProfileNameToDelete = null;
}
}
Loading