use of com.google.cloud.dialogflow.v2beta1.SessionName in project java-spanner by googleapis.
the class SpannerClientTest method commitTest.
@Test
public void commitTest() throws Exception {
CommitResponse expectedResponse = CommitResponse.newBuilder().setCommitTimestamp(Timestamp.newBuilder().build()).setCommitStats(CommitResponse.CommitStats.newBuilder().build()).build();
mockSpanner.addResponse(expectedResponse);
SessionName session = SessionName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]");
ByteString transactionId = ByteString.EMPTY;
List<Mutation> mutations = new ArrayList<>();
CommitResponse actualResponse = client.commit(session, transactionId, mutations);
Assert.assertEquals(expectedResponse, actualResponse);
List<AbstractMessage> actualRequests = mockSpanner.getRequests();
Assert.assertEquals(1, actualRequests.size());
CommitRequest actualRequest = ((CommitRequest) actualRequests.get(0));
Assert.assertEquals(session.toString(), actualRequest.getSession());
Assert.assertEquals(transactionId, actualRequest.getTransactionId());
Assert.assertEquals(mutations, actualRequest.getMutationsList());
Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
use of com.google.cloud.dialogflow.v2beta1.SessionName in project java-spanner by googleapis.
the class SpannerClientTest method commitExceptionTest.
@Test
public void commitExceptionTest() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
mockSpanner.addException(exception);
try {
SessionName session = SessionName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]");
ByteString transactionId = ByteString.EMPTY;
List<Mutation> mutations = new ArrayList<>();
client.commit(session, transactionId, mutations);
Assert.fail("No exception raised");
} catch (InvalidArgumentException e) {
// Expected exception.
}
}
use of com.google.cloud.dialogflow.v2beta1.SessionName in project java-dialogflow by googleapis.
the class SessionEntityTypesClientTest method listSessionEntityTypesTest.
@Test
public void listSessionEntityTypesTest() throws Exception {
SessionEntityType responsesElement = SessionEntityType.newBuilder().build();
ListSessionEntityTypesResponse expectedResponse = ListSessionEntityTypesResponse.newBuilder().setNextPageToken("").addAllSessionEntityTypes(Arrays.asList(responsesElement)).build();
mockSessionEntityTypes.addResponse(expectedResponse);
SessionName parent = SessionName.ofProjectSessionName("[PROJECT]", "[SESSION]");
ListSessionEntityTypesPagedResponse pagedListResponse = client.listSessionEntityTypes(parent);
List<SessionEntityType> resources = Lists.newArrayList(pagedListResponse.iterateAll());
Assert.assertEquals(1, resources.size());
Assert.assertEquals(expectedResponse.getSessionEntityTypesList().get(0), resources.get(0));
List<AbstractMessage> actualRequests = mockSessionEntityTypes.getRequests();
Assert.assertEquals(1, actualRequests.size());
ListSessionEntityTypesRequest actualRequest = ((ListSessionEntityTypesRequest) actualRequests.get(0));
Assert.assertEquals(parent.toString(), actualRequest.getParent());
Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
use of com.google.cloud.dialogflow.v2beta1.SessionName in project java-dialogflow by googleapis.
the class DetectIntentAudio method detectIntentAudio.
// DialogFlow API Detect Intent sample with audio files.
public static QueryResult detectIntentAudio(String projectId, String audioFilePath, String sessionId, String languageCode) 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);
System.out.println("Session Path: " + session.toString());
// Note: hard coding audioEncoding and sampleRateHertz for simplicity.
// Audio encoding of the audio content sent in the query request.
AudioEncoding audioEncoding = AudioEncoding.AUDIO_ENCODING_LINEAR_16;
int sampleRateHertz = 16000;
// Instructs the speech recognizer how to process the audio content.
InputAudioConfig inputAudioConfig = InputAudioConfig.newBuilder().setAudioEncoding(// audioEncoding = AudioEncoding.AUDIO_ENCODING_LINEAR_16
audioEncoding).setLanguageCode(// languageCode = "en-US"
languageCode).setSampleRateHertz(// sampleRateHertz = 16000
sampleRateHertz).build();
// Build the query with the InputAudioConfig
QueryInput queryInput = QueryInput.newBuilder().setAudioConfig(inputAudioConfig).build();
// Read the bytes from the audio file
byte[] inputAudio = Files.readAllBytes(Paths.get(audioFilePath));
// Build the DetectIntentRequest
DetectIntentRequest request = DetectIntentRequest.newBuilder().setSession(session.toString()).setQueryInput(queryInput).setInputAudio(ByteString.copyFrom(inputAudio)).build();
// Performs the detect intent request
DetectIntentResponse response = sessionsClient.detectIntent(request);
// 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");
return queryResult;
}
}
use of com.google.cloud.dialogflow.v2beta1.SessionName in project java-dialogflow by googleapis.
the class DetectIntentKnowledge method detectIntentKnowledge.
// DialogFlow API Detect Intent sample with querying knowledge connector.
public static Map<String, KnowledgeAnswers> detectIntentKnowledge(String projectId, String knowledgeBaseName, String sessionId, String languageCode, List<String> texts) throws IOException, ApiException {
// Instantiates a client
Map<String, KnowledgeAnswers> allKnowledgeAnswers = Maps.newHashMap();
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 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();
QueryParameters queryParameters = QueryParameters.newBuilder().addKnowledgeBaseNames(knowledgeBaseName).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.format("Knowledge results:\n");
System.out.format("====================\n");
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");
KnowledgeAnswers knowledgeAnswers = queryResult.getKnowledgeAnswers();
for (Answer answer : knowledgeAnswers.getAnswersList()) {
System.out.format(" - Answer: '%s'\n", answer.getAnswer());
System.out.format(" - Confidence: '%s'\n", answer.getMatchConfidence());
}
KnowledgeAnswers answers = queryResult.getKnowledgeAnswers();
allKnowledgeAnswers.put(text, answers);
}
}
return allKnowledgeAnswers;
}
Aggregations