Search in sources :

Example 6 with TextInput

use of com.google.cloud.dialogflow.v2.TextInput 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

DetectIntentResponse (com.google.cloud.dialogflow.v2.DetectIntentResponse)3 QueryInput (com.google.cloud.dialogflow.v2.QueryInput)3 QueryResult (com.google.cloud.dialogflow.v2.QueryResult)3 SessionName (com.google.cloud.dialogflow.v2.SessionName)3 SessionsClient (com.google.cloud.dialogflow.v2.SessionsClient)3 TextInput (com.google.cloud.dialogflow.v2.TextInput)3 DetectIntentRequest (com.google.cloud.dialogflow.v2.DetectIntentRequest)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 DetectIntentRequest (com.google.cloud.dialogflow.cx.v3beta1.DetectIntentRequest)1 DetectIntentResponse (com.google.cloud.dialogflow.cx.v3beta1.DetectIntentResponse)1 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