|
| 1 | +// Copyright 2022 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +async function main( |
| 18 | + projectId, |
| 19 | + location, |
| 20 | + agentId, |
| 21 | + audioFileName, |
| 22 | + encoding, |
| 23 | + sampleRateHertz, |
| 24 | + languageCode |
| 25 | +) { |
| 26 | + // [START dialogflow_cx_streaming_detect_intent_enable_partial_response] |
| 27 | + /** |
| 28 | + * TODO(developer): Uncomment these variables before running the sample. |
| 29 | + */ |
| 30 | + // const projectId = 'my-project'; |
| 31 | + // const location = 'global'; |
| 32 | + // const agentId = 'my-agent'; |
| 33 | + // const audioFileName = '/path/to/audio.raw'; |
| 34 | + // const encoding = 'AUDIO_ENCODING_LINEAR_16'; |
| 35 | + // const sampleRateHertz = 16000; |
| 36 | + // const languageCode = 'en'; |
| 37 | + |
| 38 | + const {SessionsClient} = require('@google-cloud/dialogflow-cx'); |
| 39 | + /** |
| 40 | + * Example for regional endpoint: |
| 41 | + * const location = 'us-central1' |
| 42 | + * const client = new SessionsClient({apiEndpoint: 'us-central1-dialogflow.googleapis.com'}) |
| 43 | + */ |
| 44 | + const client = new SessionsClient(); |
| 45 | + |
| 46 | + const fs = require('fs'); |
| 47 | + const util = require('util'); |
| 48 | + const {Transform, pipeline} = require('stream'); |
| 49 | + const pump = util.promisify(pipeline); |
| 50 | + |
| 51 | + async function streamingDetectIntentPartialResponse() { |
| 52 | + const sessionId = Math.random().toString(36).substring(7); |
| 53 | + const sessionPath = client.projectLocationAgentSessionPath( |
| 54 | + projectId, |
| 55 | + location, |
| 56 | + agentId, |
| 57 | + sessionId |
| 58 | + ); |
| 59 | + |
| 60 | + const request = { |
| 61 | + session: sessionPath, |
| 62 | + queryInput: { |
| 63 | + audio: { |
| 64 | + config: { |
| 65 | + audio_encoding: encoding, |
| 66 | + sampleRateHertz: sampleRateHertz, |
| 67 | + singleUtterance: true, |
| 68 | + }, |
| 69 | + }, |
| 70 | + languageCode: languageCode, |
| 71 | + }, |
| 72 | + enablePartialResponse: true, |
| 73 | + }; |
| 74 | + |
| 75 | + const stream = await client.streamingDetectIntent(); |
| 76 | + stream.on('data', data => { |
| 77 | + if (data.detectIntentResponse) { |
| 78 | + const result = data.detectIntentResponse.queryResult; |
| 79 | + |
| 80 | + for (const message of result.responseMessages) { |
| 81 | + if (message.text) { |
| 82 | + console.log(`Agent Response: ${message.text.text}`); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + }); |
| 87 | + stream.on('error', err => { |
| 88 | + console.log(err); |
| 89 | + }); |
| 90 | + stream.on('end', () => { |
| 91 | + /* API call completed */ |
| 92 | + }); |
| 93 | + stream.write(request); |
| 94 | + |
| 95 | + // Stream the audio from audio file to Dialogflow. |
| 96 | + await pump( |
| 97 | + fs.createReadStream(audioFileName), |
| 98 | + // Format the audio stream into the request format. |
| 99 | + new Transform({ |
| 100 | + objectMode: true, |
| 101 | + transform: (obj, _, next) => { |
| 102 | + next(null, {queryInput: {audio: {audio: obj}}}); |
| 103 | + }, |
| 104 | + }), |
| 105 | + stream |
| 106 | + ); |
| 107 | + } |
| 108 | + streamingDetectIntentPartialResponse(); |
| 109 | + // [END dialogflow_cx_streaming_detect_intent_enable_partial_response] |
| 110 | +} |
| 111 | + |
| 112 | +main(...process.argv.slice(2)); |
| 113 | +process.on('unhandledRejection', err => { |
| 114 | + console.error(err.message); |
| 115 | + process.exitCode = 1; |
| 116 | +}); |
0 commit comments