Search in sources :

Example 1 with DetectIntentResponse

use of com.google.cloud.dialogflow.cx.v3beta1.DetectIntentResponse in project java-dialogflow by googleapis.

the class DetectIntentAudio method detectIntentAudio.

// DialogFlow API Detect Intent sample with audio files.
public static QueryResult detectIntentAudio(String projectId, String audioFilePath, String sessionId, String languageCode) throws IOException, ApiException {
    // Instantiates a client
    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());
        // Note: hard coding audioEncoding and sampleRateHertz for simplicity.
        // Audio encoding of the audio content sent in the query request.
        AudioEncoding audioEncoding = AudioEncoding.AUDIO_ENCODING_LINEAR_16;
        int sampleRateHertz = 16000;
        // Instructs the speech recognizer how to process the audio content.
        InputAudioConfig inputAudioConfig = InputAudioConfig.newBuilder().setAudioEncoding(// audioEncoding = AudioEncoding.AUDIO_ENCODING_LINEAR_16
        audioEncoding).setLanguageCode(// languageCode = "en-US"
        languageCode).setSampleRateHertz(// sampleRateHertz = 16000
        sampleRateHertz).build();
        // Build the query with the InputAudioConfig
        QueryInput queryInput = QueryInput.newBuilder().setAudioConfig(inputAudioConfig).build();
        // Read the bytes from the audio file
        byte[] inputAudio = Files.readAllBytes(Paths.get(audioFilePath));
        // Build the DetectIntentRequest
        DetectIntentRequest request = DetectIntentRequest.newBuilder().setSession(session.toString()).setQueryInput(queryInput).setInputAudio(ByteString.copyFrom(inputAudio)).build();
        // Performs the detect intent request
        DetectIntentResponse response = sessionsClient.detectIntent(request);
        // Display the query result
        QueryResult queryResult = response.getQueryResult();
        System.out.println("====================");
        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");
        return queryResult;
    }
}
Also used : DetectIntentResponse(com.google.cloud.dialogflow.v2.DetectIntentResponse) QueryInput(com.google.cloud.dialogflow.v2.QueryInput) DetectIntentRequest(com.google.cloud.dialogflow.v2.DetectIntentRequest) QueryResult(com.google.cloud.dialogflow.v2.QueryResult) InputAudioConfig(com.google.cloud.dialogflow.v2.InputAudioConfig) AudioEncoding(com.google.cloud.dialogflow.v2.AudioEncoding) SessionsClient(com.google.cloud.dialogflow.v2.SessionsClient) SessionName(com.google.cloud.dialogflow.v2.SessionName)

Example 2 with DetectIntentResponse

use of com.google.cloud.dialogflow.cx.v3beta1.DetectIntentResponse 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 3 with DetectIntentResponse

use of com.google.cloud.dialogflow.cx.v3beta1.DetectIntentResponse in project java-dialogflow by googleapis.

the class DetectIntentTexts method detectIntentTexts.

// DialogFlow API Detect Intent sample with text inputs.
public static Map<String, QueryResult> detectIntentTexts(String projectId, List<String> texts, String sessionId, String languageCode) throws IOException, ApiException {
    Map<String, QueryResult> queryResults = Maps.newHashMap();
    // Instantiates a client
    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 (hello) 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();
            // Performs the detect intent request
            DetectIntentResponse response = sessionsClient.detectIntent(session, queryInput);
            // Display the query result
            QueryResult queryResult = response.getQueryResult();
            System.out.println("====================");
            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");
            queryResults.put(text, queryResult);
        }
    }
    return queryResults;
}
Also used : DetectIntentResponse(com.google.cloud.dialogflow.v2.DetectIntentResponse) QueryInput(com.google.cloud.dialogflow.v2.QueryInput) QueryResult(com.google.cloud.dialogflow.v2.QueryResult) SessionsClient(com.google.cloud.dialogflow.v2.SessionsClient) TextInput(com.google.cloud.dialogflow.v2.TextInput) SessionName(com.google.cloud.dialogflow.v2.SessionName)

Example 4 with DetectIntentResponse

use of com.google.cloud.dialogflow.cx.v3beta1.DetectIntentResponse in project java-dialogflow by googleapis.

the class DetectIntentWithLocation method detectIntentWithLocation.

// DialogFlow API Detect Intent sample with text inputs.
public static Map<String, QueryResult> detectIntentWithLocation(String projectId, String locationId, List<String> texts, String sessionId, String languageCode) throws IOException, ApiException {
    SessionsSettings sessionsSettings = SessionsSettings.newBuilder().setEndpoint(locationId + "-dialogflow.googleapis.com:443").build();
    Map<String, QueryResult> queryResults = Maps.newHashMap();
    // Instantiates a client
    try (SessionsClient sessionsClient = SessionsClient.create(sessionsSettings)) {
        // Set the session name using the projectId (my-project-id), locationId and sessionId (UUID)
        SessionName session = SessionName.ofProjectLocationSessionName(projectId, locationId, sessionId);
        System.out.println("Session Path: " + session.toString());
        // Detect intents for each text input
        for (String text : texts) {
            // Set the text (hello) 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();
            // Performs the detect intent request
            DetectIntentResponse response = sessionsClient.detectIntent(session, queryInput);
            // Display the query result
            QueryResult queryResult = response.getQueryResult();
            System.out.println("====================");
            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");
            queryResults.put(text, queryResult);
        }
    }
    return queryResults;
}
Also used : DetectIntentResponse(com.google.cloud.dialogflow.v2beta1.DetectIntentResponse) QueryInput(com.google.cloud.dialogflow.v2beta1.QueryInput) QueryResult(com.google.cloud.dialogflow.v2beta1.QueryResult) SessionsSettings(com.google.cloud.dialogflow.v2beta1.SessionsSettings) SessionsClient(com.google.cloud.dialogflow.v2beta1.SessionsClient) TextInput(com.google.cloud.dialogflow.v2beta1.TextInput) SessionName(com.google.cloud.dialogflow.v2beta1.SessionName)

Example 5 with DetectIntentResponse

use of com.google.cloud.dialogflow.cx.v3beta1.DetectIntentResponse in project java-dialogflow by googleapis.

the class DetectIntentWithSentimentAnalysis method detectIntentSentimentAnalysis.

public static Map<String, QueryResult> detectIntentSentimentAnalysis(String projectId, List<String> texts, String sessionId, String languageCode) throws IOException, ApiException {
    Map<String, QueryResult> queryResults = Maps.newHashMap();
    // Instantiates a client
    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 (hello) 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();
            // 
            SentimentAnalysisRequestConfig sentimentAnalysisRequestConfig = SentimentAnalysisRequestConfig.newBuilder().setAnalyzeQueryTextSentiment(true).build();
            QueryParameters queryParameters = QueryParameters.newBuilder().setSentimentAnalysisRequestConfig(sentimentAnalysisRequestConfig).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.println("====================");
            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");
            System.out.format("Sentiment Score: '%s'\n", queryResult.getSentimentAnalysisResult().getQueryTextSentiment().getScore());
            queryResults.put(text, queryResult);
        }
    }
    return queryResults;
}
Also used : DetectIntentResponse(com.google.cloud.dialogflow.v2.DetectIntentResponse) QueryInput(com.google.cloud.dialogflow.v2.QueryInput) QueryResult(com.google.cloud.dialogflow.v2.QueryResult) DetectIntentRequest(com.google.cloud.dialogflow.v2.DetectIntentRequest) SentimentAnalysisRequestConfig(com.google.cloud.dialogflow.v2.SentimentAnalysisRequestConfig) QueryParameters(com.google.cloud.dialogflow.v2.QueryParameters) SessionsClient(com.google.cloud.dialogflow.v2.SessionsClient) TextInput(com.google.cloud.dialogflow.v2.TextInput) SessionName(com.google.cloud.dialogflow.v2.SessionName)

Aggregations

DetectIntentResponse (com.google.cloud.dialogflow.v2.DetectIntentResponse)5 QueryInput (com.google.cloud.dialogflow.v2.QueryInput)5 QueryResult (com.google.cloud.dialogflow.v2.QueryResult)5 DetectIntentRequest (com.google.cloud.dialogflow.v2.DetectIntentRequest)4 SessionName (com.google.cloud.dialogflow.v2.SessionName)4 SessionsClient (com.google.cloud.dialogflow.v2.SessionsClient)4 TextInput (com.google.cloud.dialogflow.v2.TextInput)3 DetectIntentRequest (com.google.cloud.dialogflow.cx.v3beta1.DetectIntentRequest)2 DetectIntentResponse (com.google.cloud.dialogflow.cx.v3beta1.DetectIntentResponse)2 DetectIntentResponse (com.google.cloud.dialogflow.v2beta1.DetectIntentResponse)2 QueryInput (com.google.cloud.dialogflow.v2beta1.QueryInput)2 QueryResult (com.google.cloud.dialogflow.v2beta1.QueryResult)2 SessionName (com.google.cloud.dialogflow.v2beta1.SessionName)2 SessionsClient (com.google.cloud.dialogflow.v2beta1.SessionsClient)2 TextInput (com.google.cloud.dialogflow.v2beta1.TextInput)2 Test (org.junit.Test)2 QueryInput (com.google.cloud.dialogflow.cx.v3beta1.QueryInput)1 QueryResult (com.google.cloud.dialogflow.cx.v3beta1.QueryResult)1 SessionName (com.google.cloud.dialogflow.cx.v3beta1.SessionName)1 SessionsClient (com.google.cloud.dialogflow.cx.v3beta1.SessionsClient)1