use of com.google.cloud.dialogflow.v2beta1.KnowledgeBaseName in project java-dialogflow by googleapis.
the class DocumentManagement method createDocument.
public static Document createDocument(String knowledgeBaseName, String displayName, String mimeType, String knowledgeType, String contentUri) throws IOException, ApiException, InterruptedException, ExecutionException, TimeoutException {
// Instantiates a client
try (DocumentsClient documentsClient = DocumentsClient.create()) {
Document document = Document.newBuilder().setDisplayName(displayName).setContentUri(contentUri).setMimeType(mimeType).addKnowledgeTypes(KnowledgeType.valueOf(knowledgeType)).build();
CreateDocumentRequest createDocumentRequest = CreateDocumentRequest.newBuilder().setDocument(document).setParent(knowledgeBaseName).build();
OperationFuture<Document, KnowledgeOperationMetadata> response = documentsClient.createDocumentAsync(createDocumentRequest);
Document createdDocument = response.get(180, TimeUnit.SECONDS);
System.out.format("Created Document:\n");
System.out.format(" - Display Name: %s\n", createdDocument.getDisplayName());
System.out.format(" - Knowledge ID: %s\n", createdDocument.getName());
System.out.format(" - MIME Type: %s\n", createdDocument.getMimeType());
System.out.format(" - Knowledge Types:\n");
for (KnowledgeType knowledgeTypeId : document.getKnowledgeTypesList()) {
System.out.format(" - %s \n", knowledgeTypeId.getValueDescriptor());
}
System.out.format(" - Source: %s \n", document.getContentUri());
return createdDocument;
}
}
use of com.google.cloud.dialogflow.v2beta1.KnowledgeBaseName in project java-dialogflow by googleapis.
the class CreateDocumentTest method tearDown.
@After
public void tearDown() throws IOException {
// Delete the created knowledge base
try (KnowledgeBasesClient client = KnowledgeBasesClient.create()) {
DeleteKnowledgeBaseRequest request = DeleteKnowledgeBaseRequest.newBuilder().setName(knowledgeBaseName).setForce(true).build();
client.deleteKnowledgeBase(request);
}
System.setOut(null);
}
use of com.google.cloud.dialogflow.v2beta1.KnowledgeBaseName in project java-dialogflow by googleapis.
the class DetectIntentKnowledgeTest method testDetectIntentKnowledge.
@Test
public void testDetectIntentKnowledge() throws Exception {
KnowledgeBaseName knowledgeBaseName = KnowledgeBaseName.newBuilder().setProject(PROJECT_ID).setKnowledgeBase(TEST_KNOWLEDGE_BASE_ID).build();
DocumentName documentName = DocumentName.newBuilder().setProject(PROJECT_ID).setKnowledgeBase(TEST_KNOWLEDGE_BASE_ID).setDocument(TEST_DOCUMENT_ID).build();
Map<String, KnowledgeAnswers> allAnswers = DetectIntentKnowledge.detectIntentKnowledge(PROJECT_ID, knowledgeBaseName.toString(), SESSION_ID, LANGUAGE_CODE, TEXTS);
assertEquals(TEXTS.size(), allAnswers.size());
int answersFound = 0;
for (String text : TEXTS) {
KnowledgeAnswers knowledgeAnswers = allAnswers.get(text);
if (knowledgeAnswers.getAnswersCount() > 0) {
Answer answer = knowledgeAnswers.getAnswers(0);
if (text.equals(answer.getFaqQuestion()) && documentName.toString().equals(answer.getSource())) {
answersFound++;
}
}
}
// To make the test less flaky, check that half of the texts got a result.
assertThat(answersFound).isGreaterThan(TEXTS.size() / 2);
}
use of com.google.cloud.dialogflow.v2beta1.KnowledgeBaseName in project java-dialogflow by googleapis.
the class DetectIntentKnowledge method detectIntentKnowledge.
// DialogFlow API Detect Intent sample with querying knowledge connector.
public static Map<String, KnowledgeAnswers> detectIntentKnowledge(String projectId, String knowledgeBaseName, String sessionId, String languageCode, List<String> texts) throws IOException, ApiException {
// Instantiates a client
Map<String, KnowledgeAnswers> allKnowledgeAnswers = Maps.newHashMap();
try (SessionsClient sessionsClient = SessionsClient.create()) {
// Set the session name using the sessionId (UUID) and projectID (my-project-id)
SessionName session = SessionName.of(projectId, sessionId);
System.out.println("Session Path: " + session.toString());
// Detect intents for each text input
for (String text : texts) {
// Set the text and language code (en-US) for the query
TextInput.Builder textInput = TextInput.newBuilder().setText(text).setLanguageCode(languageCode);
// Build the query with the TextInput
QueryInput queryInput = QueryInput.newBuilder().setText(textInput).build();
QueryParameters queryParameters = QueryParameters.newBuilder().addKnowledgeBaseNames(knowledgeBaseName).build();
DetectIntentRequest detectIntentRequest = DetectIntentRequest.newBuilder().setSession(session.toString()).setQueryInput(queryInput).setQueryParams(queryParameters).build();
// Performs the detect intent request
DetectIntentResponse response = sessionsClient.detectIntent(detectIntentRequest);
// Display the query result
QueryResult queryResult = response.getQueryResult();
System.out.format("Knowledge results:\n");
System.out.format("====================\n");
System.out.format("Query Text: '%s'\n", queryResult.getQueryText());
System.out.format("Detected Intent: %s (confidence: %f)\n", queryResult.getIntent().getDisplayName(), queryResult.getIntentDetectionConfidence());
System.out.format("Fulfillment Text: '%s'\n", queryResult.getFulfillmentMessagesCount() > 0 ? queryResult.getFulfillmentMessages(0).getText() : "Triggered Default Fallback Intent");
KnowledgeAnswers knowledgeAnswers = queryResult.getKnowledgeAnswers();
for (Answer answer : knowledgeAnswers.getAnswersList()) {
System.out.format(" - Answer: '%s'\n", answer.getAnswer());
System.out.format(" - Confidence: '%s'\n", answer.getMatchConfidence());
}
KnowledgeAnswers answers = queryResult.getKnowledgeAnswers();
allKnowledgeAnswers.put(text, answers);
}
}
return allKnowledgeAnswers;
}
use of com.google.cloud.dialogflow.v2beta1.KnowledgeBaseName in project java-dialogflow by googleapis.
the class ConversationProfileManagement method createConversationProfileArticleSuggestion.
// 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());
}
}
Aggregations