Search in sources :

Example 11 with SessionName

use of com.google.cloud.dialogflow.v2beta1.SessionName in project java-dialogflow by googleapis.

the class DetectIntentStream method detectIntentStream.

// DialogFlow API Detect Intent sample with audio files processes as an audio stream.
static void detectIntentStream(String projectId, String audioFilePath, String sessionId) 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);
        // Instructs the speech recognizer how to process the audio content.
        // Note: hard coding audioEncoding and sampleRateHertz for simplicity.
        // Audio encoding of the audio content sent in the query request.
        InputAudioConfig inputAudioConfig = InputAudioConfig.newBuilder().setAudioEncoding(AudioEncoding.AUDIO_ENCODING_LINEAR_16).setLanguageCode(// languageCode = "en-US"
        "en-US").setSampleRateHertz(// sampleRateHertz = 16000
        16000).build();
        // Build the query with the InputAudioConfig
        QueryInput queryInput = QueryInput.newBuilder().setAudioConfig(inputAudioConfig).build();
        // Create the Bidirectional stream
        BidiStream<StreamingDetectIntentRequest, StreamingDetectIntentResponse> bidiStream = sessionsClient.streamingDetectIntentCallable().call();
        // The first request must **only** contain the audio configuration:
        bidiStream.send(StreamingDetectIntentRequest.newBuilder().setSession(session.toString()).setQueryInput(queryInput).build());
        try (FileInputStream audioStream = new FileInputStream(audioFilePath)) {
            // Subsequent requests must **only** contain the audio data.
            // Following messages: audio chunks. We just read the file in fixed-size chunks. In reality
            // you would split the user input by time.
            byte[] buffer = new byte[4096];
            int bytes;
            while ((bytes = audioStream.read(buffer)) != -1) {
                bidiStream.send(StreamingDetectIntentRequest.newBuilder().setInputAudio(ByteString.copyFrom(buffer, 0, bytes)).build());
            }
        }
        // Tell the service you are done sending data
        bidiStream.closeSend();
        for (StreamingDetectIntentResponse response : bidiStream) {
            QueryResult queryResult = response.getQueryResult();
            System.out.println("====================");
            System.out.format("Intent Display Name: %s\n", queryResult.getIntent().getDisplayName());
            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");
        }
    }
}
Also used : StreamingDetectIntentRequest(com.google.cloud.dialogflow.v2.StreamingDetectIntentRequest) QueryInput(com.google.cloud.dialogflow.v2.QueryInput) QueryResult(com.google.cloud.dialogflow.v2.QueryResult) InputAudioConfig(com.google.cloud.dialogflow.v2.InputAudioConfig) StreamingDetectIntentResponse(com.google.cloud.dialogflow.v2.StreamingDetectIntentResponse) SessionsClient(com.google.cloud.dialogflow.v2.SessionsClient) SessionName(com.google.cloud.dialogflow.v2.SessionName) FileInputStream(java.io.FileInputStream)

Example 12 with SessionName

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

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

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

Example 15 with SessionName

use of com.google.cloud.dialogflow.v2beta1.SessionName in project java-dialogflow by googleapis.

the class DetectIntentWithTextToSpeechResponse method detectIntentWithTexttoSpeech.

public static Map<String, QueryResult> detectIntentWithTexttoSpeech(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();
            // 
            OutputAudioEncoding audioEncoding = OutputAudioEncoding.OUTPUT_AUDIO_ENCODING_LINEAR_16;
            int sampleRateHertz = 16000;
            OutputAudioConfig outputAudioConfig = OutputAudioConfig.newBuilder().setAudioEncoding(audioEncoding).setSampleRateHertz(sampleRateHertz).build();
            DetectIntentRequest dr = DetectIntentRequest.newBuilder().setQueryInput(queryInput).setOutputAudioConfig(outputAudioConfig).setSession(session.toString()).build();
            // Performs the detect intent request
            DetectIntentResponse response = sessionsClient.detectIntent(dr);
            // 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 : DetectIntentRequest(com.google.cloud.dialogflow.v2.DetectIntentRequest) OutputAudioEncoding(com.google.cloud.dialogflow.v2.OutputAudioEncoding) OutputAudioConfig(com.google.cloud.dialogflow.v2.OutputAudioConfig) SessionName(com.google.cloud.dialogflow.v2.SessionName) 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)

Aggregations

Test (org.junit.Test)14 SessionName (com.google.spanner.v1.SessionName)12 AbstractMessage (com.google.protobuf.AbstractMessage)8 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)6 StatusRuntimeException (io.grpc.StatusRuntimeException)6 QueryInput (com.google.cloud.dialogflow.v2.QueryInput)5 QueryResult (com.google.cloud.dialogflow.v2.QueryResult)5 SessionName (com.google.cloud.dialogflow.v2.SessionName)5 SessionsClient (com.google.cloud.dialogflow.v2.SessionsClient)5 DetectIntentResponse (com.google.cloud.dialogflow.v2.DetectIntentResponse)4 ByteString (com.google.protobuf.ByteString)4 Mutation (com.google.spanner.v1.Mutation)4 TransactionOptions (com.google.spanner.v1.TransactionOptions)4 ArrayList (java.util.ArrayList)4 DetectIntentRequest (com.google.cloud.dialogflow.v2.DetectIntentRequest)3 TextInput (com.google.cloud.dialogflow.v2.TextInput)3 QueryInput (com.google.cloud.dialogflow.cx.v3beta1.QueryInput)2 QueryResult (com.google.cloud.dialogflow.cx.v3beta1.QueryResult)2 SessionName (com.google.cloud.dialogflow.cx.v3beta1.SessionName)2 SessionsClient (com.google.cloud.dialogflow.cx.v3beta1.SessionsClient)2