Search in sources :

Example 1 with KnowledgeAnswers

use of com.google.cloud.dialogflow.v2beta1.KnowledgeAnswers 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 2 with KnowledgeAnswers

use of com.google.cloud.dialogflow.v2beta1.KnowledgeAnswers 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)

Aggregations

KnowledgeAnswers (com.google.cloud.dialogflow.v2beta1.KnowledgeAnswers)2 Answer (com.google.cloud.dialogflow.v2beta1.KnowledgeAnswers.Answer)2 DetectIntentRequest (com.google.cloud.dialogflow.v2beta1.DetectIntentRequest)1 DetectIntentResponse (com.google.cloud.dialogflow.v2beta1.DetectIntentResponse)1 DocumentName (com.google.cloud.dialogflow.v2beta1.DocumentName)1 KnowledgeBaseName (com.google.cloud.dialogflow.v2beta1.KnowledgeBaseName)1 QueryInput (com.google.cloud.dialogflow.v2beta1.QueryInput)1 QueryParameters (com.google.cloud.dialogflow.v2beta1.QueryParameters)1 QueryResult (com.google.cloud.dialogflow.v2beta1.QueryResult)1 SessionName (com.google.cloud.dialogflow.v2beta1.SessionName)1 SessionsClient (com.google.cloud.dialogflow.v2beta1.SessionsClient)1 TextInput (com.google.cloud.dialogflow.v2beta1.TextInput)1 Test (org.junit.Test)1