diff --git a/dialogflow-cx/detect-intent-disabled-webhook.js b/dialogflow-cx/detect-intent-disabled-webhook.js new file mode 100644 index 0000000000..1c9a04e0b7 --- /dev/null +++ b/dialogflow-cx/detect-intent-disabled-webhook.js @@ -0,0 +1,75 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +async function main(projectId, location, agentId, query, languageCode) { + // [START dialogflow_cx_detect_intent_with_disabled_webhook] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'my-project'; + // const location = 'global'; + // const agentId = 'my-agent'; + // const query = 'Hello'; + // const languageCode = 'en' + + const {SessionsClient} = require('@google-cloud/dialogflow-cx'); + /** + * Example for regional endpoint: + * const location = 'us-central1' + * const client = new SessionsClient({apiEndpoint: 'us-central1-dialogflow.googleapis.com'}) + */ + const client = new SessionsClient(); + + async function detectIntentText() { + const sessionId = Math.random().toString(36).substring(7); + const sessionPath = client.projectLocationAgentSessionPath( + projectId, + location, + agentId, + sessionId + ); + console.info(sessionPath); + + const request = { + session: sessionPath, + queryParams: { + disableWebhook: true, + }, + queryInput: { + text: { + text: query, + }, + languageCode, + }, + }; + const [response] = await client.detectIntent(request); + console.log(`Detect Intent Request: ${request.queryParams.disableWebhook}`); + for (const message of response.queryResult.responseMessages) { + if (message.text) { + console.log(`Agent Response: ${message.text.text}`); + } + } + } + + detectIntentText(); + // [END dialogflow_cx_detect_intent_with_disabled_webhook] +} + +main(...process.argv.slice(2)); +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); diff --git a/dialogflow-cx/detect-intent-text.js b/dialogflow-cx/detect-intent-text.js index 705cf56038..3f90567f98 100644 --- a/dialogflow-cx/detect-intent-text.js +++ b/dialogflow-cx/detect-intent-text.js @@ -42,8 +42,6 @@ async function main(projectId, location, agentId, query, languageCode) { agentId, sessionId ); - console.info(sessionPath); - const request = { session: sessionPath, queryInput: { @@ -54,7 +52,6 @@ async function main(projectId, location, agentId, query, languageCode) { }, }; const [response] = await client.detectIntent(request); - console.log(`User Query: ${query}`); for (const message of response.queryResult.responseMessages) { if (message.text) { console.log(`Agent Response: ${message.text.text}`); diff --git a/dialogflow-cx/streaming-detect-intent-partial-response.js b/dialogflow-cx/streaming-detect-intent-partial-response.js new file mode 100644 index 0000000000..6da8439b10 --- /dev/null +++ b/dialogflow-cx/streaming-detect-intent-partial-response.js @@ -0,0 +1,116 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +async function main( + projectId, + location, + agentId, + audioFileName, + encoding, + sampleRateHertz, + languageCode +) { + // [START dialogflow_cx_streaming_detect_intent_enable_partial_response] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'my-project'; + // const location = 'global'; + // const agentId = 'my-agent'; + // const audioFileName = '/path/to/audio.raw'; + // const encoding = 'AUDIO_ENCODING_LINEAR_16'; + // const sampleRateHertz = 16000; + // const languageCode = 'en'; + + const {SessionsClient} = require('@google-cloud/dialogflow-cx'); + /** + * Example for regional endpoint: + * const location = 'us-central1' + * const client = new SessionsClient({apiEndpoint: 'us-central1-dialogflow.googleapis.com'}) + */ + const client = new SessionsClient(); + + const fs = require('fs'); + const util = require('util'); + const {Transform, pipeline} = require('stream'); + const pump = util.promisify(pipeline); + + async function streamingDetectIntentPartialResponse() { + const sessionId = Math.random().toString(36).substring(7); + const sessionPath = client.projectLocationAgentSessionPath( + projectId, + location, + agentId, + sessionId + ); + + const request = { + session: sessionPath, + queryInput: { + audio: { + config: { + audio_encoding: encoding, + sampleRateHertz: sampleRateHertz, + singleUtterance: true, + }, + }, + languageCode: languageCode, + }, + enablePartialResponse: true, + }; + + const stream = await client.streamingDetectIntent(); + stream.on('data', data => { + if (data.detectIntentResponse) { + const result = data.detectIntentResponse.queryResult; + + for (const message of result.responseMessages) { + if (message.text) { + console.log(`Agent Response: ${message.text.text}`); + } + } + } + }); + stream.on('error', err => { + console.log(err); + }); + stream.on('end', () => { + /* API call completed */ + }); + stream.write(request); + + // Stream the audio from audio file to Dialogflow. + await pump( + fs.createReadStream(audioFileName), + // Format the audio stream into the request format. + new Transform({ + objectMode: true, + transform: (obj, _, next) => { + next(null, {queryInput: {audio: {audio: obj}}}); + }, + }), + stream + ); + } + streamingDetectIntentPartialResponse(); + // [END dialogflow_cx_streaming_detect_intent_enable_partial_response] +} + +main(...process.argv.slice(2)); +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); diff --git a/dialogflow-cx/test/detect-intent-disabled-webhook-test.js b/dialogflow-cx/test/detect-intent-disabled-webhook-test.js new file mode 100644 index 0000000000..7ff993ea86 --- /dev/null +++ b/dialogflow-cx/test/detect-intent-disabled-webhook-test.js @@ -0,0 +1,36 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const {describe, it} = require('mocha'); +const execSync = require('child_process').execSync; +const exec = cmd => execSync(cmd, {encoding: 'utf8'}); + +describe('detect intent with disabled webhook', () => { + const cmd = 'node detect-intent-disabled-webhook.js'; + + const projectId = process.env.GCLOUD_PROJECT; + const location = 'global'; + const agentId = 'b1808233-450b-4065-9492-bc9b40151641'; + const languageCode = 'en'; + + it('should have disableWebhook set to "true"', async () => { + const output = exec( + `${cmd} ${projectId} ${location} ${agentId} 'hello' ${languageCode}` + ); + assert.include(output, 'true'); + }); +}); diff --git a/dialogflow-cx/test/detect-intent-text.test.js b/dialogflow-cx/test/detect-intent-text.test.js index 49f3e4db8a..7fd3bf454a 100644 --- a/dialogflow-cx/test/detect-intent-text.test.js +++ b/dialogflow-cx/test/detect-intent-text.test.js @@ -27,17 +27,17 @@ describe('detect intent with text input', () => { const agentId = 'b1808233-450b-4065-9492-bc9b40151641'; const languageCode = 'en'; - it('should response to "hello"', async () => { + it('should respond to "hello"', async () => { const output = exec( `${cmd} ${projectId} ${location} ${agentId} 'hello' ${languageCode}` ); assert.include(output, 'How can I assist you today?'); }); - it('should response to "reserve a vent"', async () => { + it('should respond to "reserve a van"', async () => { const output = exec( `${cmd} ${projectId} ${location} ${agentId} 'i need to reserve a van' ${languageCode}` ); - assert.include(output, 'Where would you like to pick it up?'); + assert.include(output, 'Where would you like '); }); }); diff --git a/dialogflow-cx/test/streaming-detect-intent-partial-response-test.js b/dialogflow-cx/test/streaming-detect-intent-partial-response-test.js new file mode 100644 index 0000000000..e0c414c78e --- /dev/null +++ b/dialogflow-cx/test/streaming-detect-intent-partial-response-test.js @@ -0,0 +1,39 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const {describe, it} = require('mocha'); +const execSync = require('child_process').execSync; +const exec = cmd => execSync(cmd, {encoding: 'utf8'}); + +describe('streaming detect intent with partial response', () => { + const cmd = 'node streaming-detect-intent-partial-response.js'; + + const projectId = process.env.GCLOUD_PROJECT; + const location = 'global'; + const agentId = '7e4c5542-823b-4e28-bd02-d37016826e9e'; + const audioFileName = 'resources/book_a_room.wav'; + const encoding = 'AUDIO_ENCODING_LINEAR_16'; + const sampleRateHertz = 16000; + const languageCode = 'en'; + + it('should respond to with partial response', async () => { + const output = exec( + `${cmd} ${projectId} ${location} ${agentId} ${audioFileName} ${encoding} ${sampleRateHertz} ${languageCode}` + ); + assert.include(output, 'One moment while I try to help!'); + }); +});