Search in sources :

Example 31 with SessionName

use of com.google.spanner.v1.SessionName in project java-spanner by googleapis.

the class SpannerClientTest method deleteSessionExceptionTest.

@Test
public void deleteSessionExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
    mockSpanner.addException(exception);
    try {
        SessionName name = SessionName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]");
        client.deleteSession(name);
        Assert.fail("No exception raised");
    } catch (InvalidArgumentException e) {
    // Expected exception.
    }
}
Also used : InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) StatusRuntimeException(io.grpc.StatusRuntimeException) SessionName(com.google.spanner.v1.SessionName) Test(org.junit.Test)

Example 32 with SessionName

use of com.google.spanner.v1.SessionName in project java-spanner by googleapis.

the class SpannerClientTest method getSessionTest.

@Test
public void getSessionTest() throws Exception {
    Session expectedResponse = Session.newBuilder().setName(SessionName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]").toString()).putAllLabels(new HashMap<String, String>()).setCreateTime(Timestamp.newBuilder().build()).setApproximateLastUseTime(Timestamp.newBuilder().build()).build();
    mockSpanner.addResponse(expectedResponse);
    SessionName name = SessionName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]");
    Session actualResponse = client.getSession(name);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockSpanner.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    GetSessionRequest actualRequest = ((GetSessionRequest) actualRequests.get(0));
    Assert.assertEquals(name.toString(), actualRequest.getName());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : GetSessionRequest(com.google.spanner.v1.GetSessionRequest) AbstractMessage(com.google.protobuf.AbstractMessage) HashMap(java.util.HashMap) SessionName(com.google.spanner.v1.SessionName) Session(com.google.spanner.v1.Session) Test(org.junit.Test)

Example 33 with SessionName

use of com.google.spanner.v1.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 34 with SessionName

use of com.google.spanner.v1.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)18 ByteString (com.google.protobuf.ByteString)13 SessionName (com.google.spanner.v1.SessionName)12 ArrayList (java.util.ArrayList)10 CommitRequest (com.google.spanner.v1.CommitRequest)9 CommitResponse (com.google.spanner.v1.CommitResponse)9 RollbackRequest (com.google.spanner.v1.RollbackRequest)9 Empty (com.google.protobuf.Empty)8 Before (org.junit.Before)8 ApiFutures (com.google.api.core.ApiFutures)6 Timestamp (com.google.cloud.Timestamp)6 NUM_IN_USE_SESSIONS (com.google.cloud.spanner.MetricRegistryConstants.NUM_IN_USE_SESSIONS)6 NUM_READ_SESSIONS (com.google.cloud.spanner.MetricRegistryConstants.NUM_READ_SESSIONS)6 NUM_SESSIONS_BEING_PREPARED (com.google.cloud.spanner.MetricRegistryConstants.NUM_SESSIONS_BEING_PREPARED)6 NUM_WRITE_SESSIONS (com.google.cloud.spanner.MetricRegistryConstants.NUM_WRITE_SESSIONS)6 SPANNER_LABEL_KEYS (com.google.cloud.spanner.MetricRegistryConstants.SPANNER_LABEL_KEYS)6 SPANNER_LABEL_KEYS_WITH_TYPE (com.google.cloud.spanner.MetricRegistryConstants.SPANNER_LABEL_KEYS_WITH_TYPE)6 FakeMetricRegistry (com.google.cloud.spanner.MetricRegistryTestUtils.FakeMetricRegistry)6 MetricsRecord (com.google.cloud.spanner.MetricRegistryTestUtils.MetricsRecord)6 PointWithFunction (com.google.cloud.spanner.MetricRegistryTestUtils.PointWithFunction)6