Search in sources :

Example 1 with KnowledgeBaseName

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;
    }
}
Also used : KnowledgeOperationMetadata(com.google.cloud.dialogflow.v2beta1.KnowledgeOperationMetadata) CreateDocumentRequest(com.google.cloud.dialogflow.v2beta1.CreateDocumentRequest) Document(com.google.cloud.dialogflow.v2beta1.Document) KnowledgeType(com.google.cloud.dialogflow.v2beta1.Document.KnowledgeType) DocumentsClient(com.google.cloud.dialogflow.v2beta1.DocumentsClient)

Example 2 with KnowledgeBaseName

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);
}
Also used : DeleteKnowledgeBaseRequest(com.google.cloud.dialogflow.v2beta1.DeleteKnowledgeBaseRequest) KnowledgeBasesClient(com.google.cloud.dialogflow.v2beta1.KnowledgeBasesClient) After(org.junit.After)

Example 3 with KnowledgeBaseName

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);
}
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 4 with KnowledgeBaseName

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;
}
Also used : DetectIntentRequest(com.google.cloud.dialogflow.v2beta1.DetectIntentRequest) QueryParameters(com.google.cloud.dialogflow.v2beta1.QueryParameters) SessionName(com.google.cloud.dialogflow.v2beta1.SessionName) DetectIntentResponse(com.google.cloud.dialogflow.v2beta1.DetectIntentResponse) QueryInput(com.google.cloud.dialogflow.v2beta1.QueryInput) Answer(com.google.cloud.dialogflow.v2beta1.KnowledgeAnswers.Answer) QueryResult(com.google.cloud.dialogflow.v2beta1.QueryResult) SessionsClient(com.google.cloud.dialogflow.v2beta1.SessionsClient) TextInput(com.google.cloud.dialogflow.v2beta1.TextInput) KnowledgeAnswers(com.google.cloud.dialogflow.v2beta1.KnowledgeAnswers)

Example 5 with KnowledgeBaseName

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());
    }
}
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)

Aggregations

Test (org.junit.Test)3 KnowledgeAnswers (com.google.cloud.dialogflow.v2beta1.KnowledgeAnswers)2 Answer (com.google.cloud.dialogflow.v2beta1.KnowledgeAnswers.Answer)2 KnowledgeBase (com.google.cloud.dialogflow.v2beta1.KnowledgeBase)2 KnowledgeBasesClient (com.google.cloud.dialogflow.v2beta1.KnowledgeBasesClient)2 ConversationProfile (com.google.cloud.dialogflow.v2.ConversationProfile)1 ConversationProfilesClient (com.google.cloud.dialogflow.v2.ConversationProfilesClient)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 LocationName (com.google.cloud.dialogflow.v2.LocationName)1 CreateDocumentRequest (com.google.cloud.dialogflow.v2beta1.CreateDocumentRequest)1 DeleteKnowledgeBaseRequest (com.google.cloud.dialogflow.v2beta1.DeleteKnowledgeBaseRequest)1 DetectIntentRequest (com.google.cloud.dialogflow.v2beta1.DetectIntentRequest)1 DetectIntentResponse (com.google.cloud.dialogflow.v2beta1.DetectIntentResponse)1 Document (com.google.cloud.dialogflow.v2beta1.Document)1 KnowledgeType (com.google.cloud.dialogflow.v2beta1.Document.KnowledgeType)1 DocumentName (com.google.cloud.dialogflow.v2beta1.DocumentName)1 DocumentsClient (com.google.cloud.dialogflow.v2beta1.DocumentsClient)1 ListDocumentsPagedResponse (com.google.cloud.dialogflow.v2beta1.DocumentsClient.ListDocumentsPagedResponse)1