Search in sources :

Example 1 with KnowledgeBaseName

use of com.google.cloud.dialogflow.v2.KnowledgeBaseName in project java-dialogflow by googleapis.

the class CreateKnowledgeBaseTest method tearDown.

@After
public void tearDown() throws IOException {
    if (knowledgeBaseName == null) {
        return;
    }
    // Delete the created knowledge base
    try (KnowledgeBasesClient client = KnowledgeBasesClient.create()) {
        DeleteKnowledgeBaseRequest request = DeleteKnowledgeBaseRequest.newBuilder().setName(knowledgeBaseName).setForce(true).build();
        client.deleteKnowledgeBase(request);
    }
    System.setOut(originalOutputStream);
}
Also used : DeleteKnowledgeBaseRequest(com.google.cloud.dialogflow.v2.DeleteKnowledgeBaseRequest) KnowledgeBasesClient(com.google.cloud.dialogflow.v2.KnowledgeBasesClient) After(org.junit.After)

Example 2 with KnowledgeBaseName

use of com.google.cloud.dialogflow.v2.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);
}
Also used : Answer(com.google.cloud.dialogflow.v2beta1.KnowledgeAnswers.Answer) KnowledgeBaseName(com.google.cloud.dialogflow.v2beta1.KnowledgeBaseName) DocumentName(com.google.cloud.dialogflow.v2beta1.DocumentName) KnowledgeAnswers(com.google.cloud.dialogflow.v2beta1.KnowledgeAnswers) Test(org.junit.Test)

Example 3 with KnowledgeBaseName

use of com.google.cloud.dialogflow.v2.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());
    }
}
Also used : ConversationProfile(com.google.cloud.dialogflow.v2.ConversationProfile) SuggestionConfig(com.google.cloud.dialogflow.v2.HumanAgentAssistantConfig.SuggestionConfig) SuggestionFeatureConfig(com.google.cloud.dialogflow.v2.HumanAgentAssistantConfig.SuggestionFeatureConfig) ConversationProfilesClient(com.google.cloud.dialogflow.v2.ConversationProfilesClient) KnowledgeBaseName(com.google.cloud.dialogflow.v2.KnowledgeBaseName) LocationName(com.google.cloud.dialogflow.v2.LocationName)

Example 4 with KnowledgeBaseName

use of com.google.cloud.dialogflow.v2.KnowledgeBaseName in project java-dialogflow by googleapis.

the class DocumentsClientTest method listDocumentsTest.

@Test
public void listDocumentsTest() throws Exception {
    Document responsesElement = Document.newBuilder().build();
    ListDocumentsResponse expectedResponse = ListDocumentsResponse.newBuilder().setNextPageToken("").addAllDocuments(Arrays.asList(responsesElement)).build();
    mockDocuments.addResponse(expectedResponse);
    KnowledgeBaseName parent = KnowledgeBaseName.ofProjectKnowledgeBaseName("[PROJECT]", "[KNOWLEDGE_BASE]");
    ListDocumentsPagedResponse pagedListResponse = client.listDocuments(parent);
    List<Document> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getDocumentsList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockDocuments.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListDocumentsRequest actualRequest = ((ListDocumentsRequest) actualRequests.get(0));
    Assert.assertEquals(parent.toString(), actualRequest.getParent());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) ListDocumentsPagedResponse(com.google.cloud.dialogflow.v2.DocumentsClient.ListDocumentsPagedResponse) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 ConversationProfile (com.google.cloud.dialogflow.v2.ConversationProfile)1 ConversationProfilesClient (com.google.cloud.dialogflow.v2.ConversationProfilesClient)1 DeleteKnowledgeBaseRequest (com.google.cloud.dialogflow.v2.DeleteKnowledgeBaseRequest)1 ListDocumentsPagedResponse (com.google.cloud.dialogflow.v2.DocumentsClient.ListDocumentsPagedResponse)1 SuggestionConfig (com.google.cloud.dialogflow.v2.HumanAgentAssistantConfig.SuggestionConfig)1 SuggestionFeatureConfig (com.google.cloud.dialogflow.v2.HumanAgentAssistantConfig.SuggestionFeatureConfig)1 KnowledgeBaseName (com.google.cloud.dialogflow.v2.KnowledgeBaseName)1 KnowledgeBasesClient (com.google.cloud.dialogflow.v2.KnowledgeBasesClient)1 LocationName (com.google.cloud.dialogflow.v2.LocationName)1 DocumentName (com.google.cloud.dialogflow.v2beta1.DocumentName)1 KnowledgeAnswers (com.google.cloud.dialogflow.v2beta1.KnowledgeAnswers)1 Answer (com.google.cloud.dialogflow.v2beta1.KnowledgeAnswers.Answer)1 KnowledgeBaseName (com.google.cloud.dialogflow.v2beta1.KnowledgeBaseName)1 AbstractMessage (com.google.protobuf.AbstractMessage)1 After (org.junit.After)1