Search in sources :

Example 6 with DetectIntentRequest

use of com.google.cloud.dialogflow.v2.DetectIntentRequest in project java-dialogflow-cx by googleapis.

the class ITSystemTest method detectIntentTest.

@Test
public void detectIntentTest() {
    String session = String.format("%s/sessions/%s", agentName, ID);
    DetectIntentRequest request = DetectIntentRequest.newBuilder().setSession(session).setQueryInput(QUERY_INPUT).build();
    DetectIntentResponse detectIntent = sessionsClient.detectIntent(request);
    assertTrue(detectIntent.getQueryResult().getText().contains(TEXT));
}
Also used : DetectIntentResponse(com.google.cloud.dialogflow.cx.v3beta1.DetectIntentResponse) DetectIntentRequest(com.google.cloud.dialogflow.cx.v3beta1.DetectIntentRequest) Test(org.junit.Test)

Example 7 with DetectIntentRequest

use of com.google.cloud.dialogflow.v2.DetectIntentRequest in project java-dialogflow-cx by googleapis.

the class DetectIntent method detectIntent.

// DialogFlow API Detect Intent sample with text inputs.
public static Map<String, QueryResult> detectIntent(String projectId, String locationId, String agentId, String sessionId, List<String> texts, String languageCode) throws IOException, ApiException {
    SessionsSettings.Builder sessionsSettingsBuilder = SessionsSettings.newBuilder();
    if (locationId.equals("global")) {
        sessionsSettingsBuilder.setEndpoint("dialogflow.googleapis.com:443");
    } else {
        sessionsSettingsBuilder.setEndpoint(locationId + "-dialogflow.googleapis.com:443");
    }
    SessionsSettings sessionsSettings = sessionsSettingsBuilder.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 (global), agentID
        // (UUID), and sessionId (UUID).
        SessionName session = SessionName.ofProjectLocationAgentSessionName(projectId, locationId, agentId, sessionId);
        // Detect intents for each text input.
        for (String text : texts) {
            // Set the text (hello) for the query.
            TextInput.Builder textInput = TextInput.newBuilder().setText(text);
            // Build the query with the TextInput and language code (en-US).
            QueryInput queryInput = QueryInput.newBuilder().setText(textInput).setLanguageCode(languageCode).build();
            // Build the DetectIntentRequest with the SessionName and QueryInput.
            DetectIntentRequest request = DetectIntentRequest.newBuilder().setSession(session.toString()).setQueryInput(queryInput).build();
            // Performs the detect intent request.
            DetectIntentResponse response = sessionsClient.detectIntent(request);
            // Display the query result.
            QueryResult queryResult = response.getQueryResult();
            // TODO : Uncomment if you want to print queryResult
            // System.out.println("====================");
            // System.out.format("Query Text: '%s'\n", queryResult.getText());
            // System.out.format(
            // "Detected Intent: %s (confidence: %f)\n",
            // queryResult.getIntent().getDisplayName(),
            // queryResult.getIntentDetectionConfidence());
            queryResults.put(text, queryResult);
        }
    }
    return queryResults;
}
Also used : DetectIntentResponse(com.google.cloud.dialogflow.cx.v3beta1.DetectIntentResponse) QueryInput(com.google.cloud.dialogflow.cx.v3beta1.QueryInput) QueryResult(com.google.cloud.dialogflow.cx.v3beta1.QueryResult) DetectIntentRequest(com.google.cloud.dialogflow.cx.v3beta1.DetectIntentRequest) SessionsSettings(com.google.cloud.dialogflow.cx.v3beta1.SessionsSettings) SessionsClient(com.google.cloud.dialogflow.cx.v3beta1.SessionsClient) TextInput(com.google.cloud.dialogflow.cx.v3beta1.TextInput) SessionName(com.google.cloud.dialogflow.cx.v3beta1.SessionName)

Aggregations

DetectIntentRequest (com.google.cloud.dialogflow.v2.DetectIntentRequest)4 DetectIntentResponse (com.google.cloud.dialogflow.v2.DetectIntentResponse)4 QueryInput (com.google.cloud.dialogflow.v2.QueryInput)4 QueryResult (com.google.cloud.dialogflow.v2.QueryResult)4 SessionName (com.google.cloud.dialogflow.v2.SessionName)3 SessionsClient (com.google.cloud.dialogflow.v2.SessionsClient)3 DetectIntentRequest (com.google.cloud.dialogflow.cx.v3beta1.DetectIntentRequest)2 DetectIntentResponse (com.google.cloud.dialogflow.cx.v3beta1.DetectIntentResponse)2 TextInput (com.google.cloud.dialogflow.v2.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 SessionsSettings (com.google.cloud.dialogflow.cx.v3beta1.SessionsSettings)1 TextInput (com.google.cloud.dialogflow.cx.v3beta1.TextInput)1 AudioEncoding (com.google.cloud.dialogflow.v2.AudioEncoding)1 InputAudioConfig (com.google.cloud.dialogflow.v2.InputAudioConfig)1 OutputAudioConfig (com.google.cloud.dialogflow.v2.OutputAudioConfig)1 OutputAudioEncoding (com.google.cloud.dialogflow.v2.OutputAudioEncoding)1