Search in sources :

Example 6 with SessionsClient

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

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

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

Example 9 with SessionsClient

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

the class ITSystemTest method setUp.

@BeforeClass
public static void setUp() throws IOException {
    agentsClient = AgentsClient.create();
    /* create entity */
    entityTypesClient = EntityTypesClient.create();
    EntityType createEntityType = EntityType.newBuilder().setDisplayName(ENTITY_NAME).setKind(EntityType.Kind.KIND_LIST).addEntities(ENTITY).build();
    CreateEntityTypeRequest entityTypeRequest = CreateEntityTypeRequest.newBuilder().setParent(PROJECT_AGENT_NAME.toString()).setEntityType(createEntityType).build();
    entityType = entityTypesClient.createEntityType(entityTypeRequest);
    entityName = entityType.getName().substring(entityType.getName().lastIndexOf("/")).replace("/", "");
    entityTypeName = EntityTypeName.of(PROJECT_ID, entityName);
    /* create intents */
    intentsClient = IntentsClient.create();
    Intent createIntent = Intent.newBuilder().setDisplayName(INTENT_NAME).addEvents(EVENT_NAME).setAction(ACTION_NAME).build();
    CreateIntentRequest intentRequest = CreateIntentRequest.newBuilder().setParent(PROJECT_AGENT_NAME.toString()).setIntent(createIntent).build();
    intent = intentsClient.createIntent(intentRequest);
    intentId = intent.getName().substring(intent.getName().lastIndexOf("/")).replace("/", "");
    intentName = IntentName.of(PROJECT_ID, intentId);
    /* create session */
    sessionsClient = SessionsClient.create();
    /* create context */
    contextsClient = ContextsClient.create();
    Context createContext = Context.newBuilder().setName(CONTEXT_NAME.toString()).setLifespanCount(LIFE_SPAN_COUNT).build();
    CreateContextRequest request = CreateContextRequest.newBuilder().setParent(SESSION_NAME.toString()).setContext(createContext).build();
    context = contextsClient.createContext(request);
}
Also used : EntityType(com.google.cloud.dialogflow.v2.EntityType) Context(com.google.cloud.dialogflow.v2.Context) CreateContextRequest(com.google.cloud.dialogflow.v2.CreateContextRequest) CreateEntityTypeRequest(com.google.cloud.dialogflow.v2.CreateEntityTypeRequest) Intent(com.google.cloud.dialogflow.v2.Intent) CreateIntentRequest(com.google.cloud.dialogflow.v2.CreateIntentRequest) BeforeClass(org.junit.BeforeClass)

Example 10 with SessionsClient

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

QueryInput (com.google.cloud.dialogflow.v2.QueryInput)6 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 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 SessionsSettings (com.google.cloud.dialogflow.cx.v3beta1.SessionsSettings)2 InputAudioConfig (com.google.cloud.dialogflow.v2.InputAudioConfig)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 FileInputStream (java.io.FileInputStream)2