|
| 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] |
0 commit comments