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