Skip to content

Commit e49a154

Browse files
committed
chore: migrated left out samples
1 parent fa610c7 commit e49a154

File tree

6 files changed

+584
-0
lines changed

6 files changed

+584
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.dialogflow;
18+
19+
// [START dialogflow_create_conversation]
20+
21+
import com.google.api.gax.rpc.ApiException;
22+
import com.google.cloud.dialogflow.v2.Conversation;
23+
import com.google.cloud.dialogflow.v2.ConversationProfileName;
24+
import com.google.cloud.dialogflow.v2.ConversationsClient;
25+
import com.google.cloud.dialogflow.v2.LocationName;
26+
import java.io.IOException;
27+
28+
public class ConversationManagement {
29+
30+
public static void main(String[] args) throws IOException {
31+
// TODO(developer): Replace these variables before running the sample.
32+
String projectId = "my-project-id";
33+
String location = "my-location";
34+
35+
// Set conversation profile id for the new conversation.
36+
// See com.example.dialogflow.ConversationProfileManagement sample code for how to create a
37+
// conversation profile. You can also create a conversation profile using Agent Assist console,
38+
// https://cloud.google.com/agent-assist/docs/conversation-profile.
39+
String conversationProfileId = "my-conversation-profile-id";
40+
41+
// Create a conversation
42+
createConversation(projectId, location, conversationProfileId);
43+
}
44+
45+
public static void createConversation(
46+
String projectId, String location, String conversationProfileId)
47+
throws ApiException, IOException {
48+
try (ConversationsClient conversationsClient = ConversationsClient.create()) {
49+
LocationName locationName = LocationName.of(projectId, location);
50+
ConversationProfileName conversationProfileName =
51+
ConversationProfileName.ofProjectLocationConversationProfileName(
52+
projectId, location, conversationProfileId);
53+
Conversation conversation =
54+
Conversation.newBuilder()
55+
.setConversationProfile(conversationProfileName.toString())
56+
.build();
57+
Conversation newConversation =
58+
conversationsClient.createConversation(locationName, conversation);
59+
System.out.println("====================");
60+
System.out.println("Conversation Created:");
61+
System.out.format("Life Cycle State: %s\n", newConversation.getLifecycleState());
62+
System.out.format(
63+
"Conversation Profile Name: %s\n", newConversation.getConversationProfile());
64+
System.out.format("Name: %s\n", newConversation.getName());
65+
}
66+
}
67+
}
68+
// [END dialogflow_create_conversation]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.dialogflow;
18+
19+
// [START dialogflow_create_conversation_profile_article_suggestion]
20+
21+
import com.google.api.gax.rpc.ApiException;
22+
import com.google.cloud.dialogflow.v2.ConversationProfile;
23+
import com.google.cloud.dialogflow.v2.ConversationProfilesClient;
24+
import com.google.cloud.dialogflow.v2.CreateConversationProfileRequest;
25+
import com.google.cloud.dialogflow.v2.HumanAgentAssistantConfig;
26+
import com.google.cloud.dialogflow.v2.HumanAgentAssistantConfig.SuggestionConfig;
27+
import com.google.cloud.dialogflow.v2.HumanAgentAssistantConfig.SuggestionFeatureConfig;
28+
import com.google.cloud.dialogflow.v2.HumanAgentAssistantConfig.SuggestionQueryConfig;
29+
import com.google.cloud.dialogflow.v2.HumanAgentAssistantConfig.SuggestionQueryConfig.KnowledgeBaseQuerySource;
30+
import com.google.cloud.dialogflow.v2.HumanAgentAssistantConfig.SuggestionTriggerSettings;
31+
import com.google.cloud.dialogflow.v2.KnowledgeBaseName;
32+
import com.google.cloud.dialogflow.v2.LocationName;
33+
import com.google.cloud.dialogflow.v2.SuggestionFeature;
34+
import com.google.cloud.dialogflow.v2.SuggestionFeature.Type;
35+
import java.io.IOException;
36+
import java.util.Optional;
37+
38+
public class ConversationProfileManagement {
39+
40+
public static void main(String[] args) throws IOException {
41+
// TODO(developer): Replace these variables before running the sample.
42+
String projectId = "my-project-id";
43+
String location = "my-location";
44+
45+
// Set display name of the new conversation profile
46+
String conversationProfileDisplayName = "my-conversation-profile-display-name";
47+
48+
// Set knowledge base id for Article Suggestion feature.
49+
// See details about how to create a knowledge base here,
50+
// https://cloud.google.com/agent-assist/docs/article-suggestion.
51+
String articleSuggestionKnowledgeBaseId = "my-article-suggestion-knowledge-base-id";
52+
53+
// Create a conversation profile
54+
createConversationProfileArticleSuggestion(
55+
projectId,
56+
conversationProfileDisplayName,
57+
location,
58+
Optional.of(articleSuggestionKnowledgeBaseId));
59+
}
60+
61+
// Set suggestion trigger with no_smalltalk and only_send_user both true, which means that
62+
// the suggestion is not triggered if last utterance is small talk and is only triggered
63+
// if participant role of last utterance is END_USER.
64+
public static SuggestionTriggerSettings buildSuggestionTriggerSettings() {
65+
return SuggestionTriggerSettings.newBuilder().setNoSmalltalk(true).setOnlyEndUser(true).build();
66+
}
67+
68+
// Set the configuration for suggestion query, including the knowledge base query source
69+
// and maximum number of results to return.
70+
public static SuggestionQueryConfig buildSuggestionQueryConfig(
71+
KnowledgeBaseName knowledgeBaseName) {
72+
return SuggestionQueryConfig.newBuilder()
73+
.setKnowledgeBaseQuerySource(
74+
KnowledgeBaseQuerySource.newBuilder().addKnowledgeBases(knowledgeBaseName.toString()))
75+
.setMaxResults(3)
76+
.build();
77+
}
78+
79+
// Create a conversation profile with given values about Article Suggestion.
80+
public static void createConversationProfileArticleSuggestion(
81+
String projectId,
82+
String displayName,
83+
String location,
84+
Optional<String> articleSuggestionKnowledgeBaseId)
85+
throws ApiException, IOException {
86+
try (ConversationProfilesClient conversationProfilesClient =
87+
ConversationProfilesClient.create()) {
88+
// Create a builder for agent assistance configuration
89+
SuggestionConfig.Builder suggestionConfigBuilder = SuggestionConfig.newBuilder();
90+
91+
// Add knowledge base for Article Suggestion feature
92+
if (articleSuggestionKnowledgeBaseId.isPresent()) {
93+
KnowledgeBaseName articleSuggestionKbName =
94+
KnowledgeBaseName.of(projectId, articleSuggestionKnowledgeBaseId.get());
95+
96+
// Build configuration for Article Suggestion feature
97+
SuggestionFeatureConfig articleSuggestionFeatureConfig =
98+
SuggestionFeatureConfig.newBuilder()
99+
.setSuggestionFeature(
100+
SuggestionFeature.newBuilder().setType(Type.ARTICLE_SUGGESTION).build())
101+
.setSuggestionTriggerSettings(buildSuggestionTriggerSettings())
102+
.setQueryConfig(buildSuggestionQueryConfig(articleSuggestionKbName))
103+
.build();
104+
105+
// Add Article Suggestion feature to agent assistance configuration
106+
suggestionConfigBuilder.addFeatureConfigs(articleSuggestionFeatureConfig);
107+
}
108+
109+
LocationName locationName = LocationName.of(projectId, location);
110+
// Set a conversation profile with target configurations
111+
ConversationProfile targetConversationProfile =
112+
ConversationProfile.newBuilder()
113+
.setDisplayName(displayName)
114+
.setLanguageCode("en-US")
115+
.setHumanAgentAssistantConfig(
116+
HumanAgentAssistantConfig.newBuilder()
117+
.setHumanAgentSuggestionConfig(suggestionConfigBuilder.build()))
118+
.build();
119+
120+
// Create a conversation profile
121+
ConversationProfile createdConversationProfile =
122+
conversationProfilesClient.createConversationProfile(
123+
CreateConversationProfileRequest.newBuilder()
124+
.setParent(locationName.toString())
125+
.setConversationProfile(targetConversationProfile)
126+
.build());
127+
System.out.println("====================");
128+
System.out.println("Conversation Profile created:\n");
129+
System.out.format("Display name: %s\n", createdConversationProfile.getDisplayName());
130+
System.out.format("Name: %s\n", createdConversationProfile.getName());
131+
}
132+
}
133+
}
134+
// [END dialogflow_create_conversation_profile_article_suggestion]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.dialogflow;
18+
19+
// [START dialogflow_create_participant]
20+
21+
import com.google.api.gax.rpc.ApiException;
22+
import com.google.cloud.dialogflow.v2.ConversationName;
23+
import com.google.cloud.dialogflow.v2.Participant;
24+
import com.google.cloud.dialogflow.v2.Participant.Role;
25+
import com.google.cloud.dialogflow.v2.ParticipantsClient;
26+
import java.io.IOException;
27+
28+
public class ParticipantManagement {
29+
30+
public static void main(String[] args) throws IOException {
31+
// TODO(developer): Replace these variables before running the sample.
32+
String projectId = "my-project-id";
33+
String location = "my-location";
34+
35+
// Set conversation id for the new participant,
36+
// See com.example.dialogflow.ConversationManagement sample code
37+
// for how to create a conversation.
38+
String conversationId = "my-conversation-id";
39+
Role role = Role.END_USER;
40+
41+
// Create a participant
42+
createParticipant(projectId, location, conversationId, role);
43+
}
44+
45+
// Create a participant with given role
46+
public static void createParticipant(
47+
String projectId, String location, String conversationId, Role role)
48+
throws ApiException, IOException {
49+
try (ParticipantsClient participantsClient = ParticipantsClient.create()) {
50+
ConversationName conversationName =
51+
ConversationName.ofProjectLocationConversationName(projectId, location, conversationId);
52+
Participant participant = Participant.newBuilder().setRole(role).build();
53+
Participant newParticipant =
54+
participantsClient.createParticipant(conversationName, participant);
55+
System.out.println("====================");
56+
System.out.println("Participant Created:");
57+
System.out.format("Role: %s\n", newParticipant.getRole());
58+
System.out.format("Name: %s\n", newParticipant.getName());
59+
}
60+
}
61+
}
62+
// [END dialogflow_create_participant]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.dialogflow;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static org.junit.Assert.assertNotNull;
21+
22+
import com.google.cloud.dialogflow.v2.ConversationProfilesClient;
23+
import java.io.ByteArrayOutputStream;
24+
import java.io.IOException;
25+
import java.io.PrintStream;
26+
import java.util.Optional;
27+
import java.util.UUID;
28+
import org.junit.After;
29+
import org.junit.Before;
30+
import org.junit.BeforeClass;
31+
import org.junit.Test;
32+
import org.junit.runner.RunWith;
33+
import org.junit.runners.JUnit4;
34+
35+
@RunWith(JUnit4.class)
36+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
37+
public class CreateConversationProfileTest {
38+
39+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
40+
private static final String LOCATION = "global";
41+
private static final String NAME_PREFIX_IN_OUTPUT = "Name: ";
42+
private static String conversationProfileNameToDelete = null;
43+
private ByteArrayOutputStream bout;
44+
private PrintStream newOutputStream;
45+
private PrintStream originalOutputStream;
46+
47+
private static void requireEnvVar(String varName) {
48+
assertNotNull(System.getenv(varName));
49+
}
50+
51+
// Extract the name of a newly created resource from latest "Name: %s\n" in sample code output
52+
private static String getResourceNameFromOutputString(String output) {
53+
return output.substring(
54+
output.lastIndexOf(NAME_PREFIX_IN_OUTPUT) + NAME_PREFIX_IN_OUTPUT.length(),
55+
output.length() - 1);
56+
}
57+
58+
private static void deleteConversationProfile(String conversationProfileName) throws IOException {
59+
try (ConversationProfilesClient conversationProfilesClient =
60+
ConversationProfilesClient.create()) {
61+
conversationProfilesClient.deleteConversationProfile(conversationProfileName);
62+
}
63+
}
64+
65+
@BeforeClass
66+
public static void checkRequirements() {
67+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
68+
requireEnvVar("GOOGLE_CLOUD_PROJECT");
69+
}
70+
71+
@Before
72+
public void setUp() {
73+
originalOutputStream = System.out;
74+
bout = new ByteArrayOutputStream();
75+
newOutputStream = new PrintStream(bout);
76+
System.setOut(newOutputStream);
77+
}
78+
79+
@After
80+
public void tearDown() throws IOException {
81+
if (conversationProfileNameToDelete != null) {
82+
deleteConversationProfile(conversationProfileNameToDelete);
83+
conversationProfileNameToDelete = null;
84+
}
85+
86+
System.setOut(originalOutputStream);
87+
}
88+
89+
@Test
90+
public void testCreateConversationProfileArticleSuggestion() throws IOException {
91+
String conversationProfileDisplayName = UUID.randomUUID().toString();
92+
93+
// Create a conversation profile
94+
String articleSuggestionKnowledgeBaseId = UUID.randomUUID().toString();
95+
ConversationProfileManagement.createConversationProfileArticleSuggestion(
96+
PROJECT_ID,
97+
conversationProfileDisplayName,
98+
LOCATION,
99+
Optional.of(articleSuggestionKnowledgeBaseId));
100+
101+
String output = bout.toString();
102+
conversationProfileNameToDelete = getResourceNameFromOutputString(output);
103+
assertThat(output).contains(conversationProfileDisplayName);
104+
105+
// Delete the conversation profile
106+
deleteConversationProfile(conversationProfileNameToDelete);
107+
conversationProfileNameToDelete = null;
108+
}
109+
}

0 commit comments

Comments
 (0)