Skip to content

Commit

Permalink
docs (samples): add detect intent samples (#292)
Browse files Browse the repository at this point in the history
* add webhook-configure-session-parameter-enable-agent-response sample and test

Change-Id: Ia8593160ed33060eb497a15723b21c1221ce55c9

* add webhook-configure-session-parameter-trigger-transition sample and test

Change-Id: I118abb59182879b6969018da97d04eea0b8daeb0

* add webhook-configure-optional-or-required-form-parameters sample and test

Change-Id: I7cbfeb11cece7ccf873dbc7a6dd7cff9ae264ffe

* add configure-session-parameters sample and test

Change-Id: Ib9f1a110473751508b50259c4696593580c64a91

* fix form parameter path

Change-Id: I6411000a7d0240d7552d725c2fd1049be781a9ab

* add webhook-validate-form-parameter sample and test

Change-Id: I82097be3fc3f91651c88b99c7431ebb602c42c66

* add cx to region tag

Change-Id: I6640604512c27a341ab8e26238bf8c3fbd1e77ef

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* fix test

Change-Id: Icc70e18b40d8684c7909e8383b4c226fa94a162b

* fix region tag

Change-Id: I0fe3849c0eaf12eaf247088993898cbb47dace44

* add detect intent with sentiment analysis sample and test

Change-Id: I99aa3985c64b6c80ec1a85591b1e49f381c379de

* punctuation

Change-Id: I2fe6f929f4c786595b4bbc4fbe422f2370d3adf0

* add detect intent with eventInput sample and test

Change-Id: I32a5b04e975b8e1a4c7d92b328d3bdd1e1c70448

* fix region tag

Change-Id: I3795770f8ef68e154f36057ca9aa1bbb2ad8075f

* add detect intent with IntentInput sample and test

Change-Id: Iea12505b745ea8ea3995fdca2381f6e4bf60d051

* add detect intent synthesize tts response sample and test

Change-Id: Ie7d22212f6f8107a36f555e771ff409c2d1f0bf0

* add resource output file

Change-Id: I3f0f78d5dbb30a0c50ecad3996332cbd6ff0b7b5

* fix formatting

Change-Id: Ia651757249955d18613f844734674f1eea49ed82

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* fix test agent

Change-Id: Ib75ccf20985dcfae3130619f69c5fd546b24ca1d

* fix test

Change-Id: I1423fdb6916fa50fe0f21481fb1964e08ab62080

* change function names

Change-Id: Iaa02c248807b72fcff4f07b46f0f70fddf9d6e7a

* change test

Change-Id: If5ea5d044f040cdb0f3b121af5af960322a1cf5a

* change test

Change-Id: I9a6b457d1739b2f51de86b77d75d9d0ce2973b04

* change test

Change-Id: I92f20ec34c132448a0375d0c88f64b705eb216f5

* replace agent ids and add deleteAgent logic

Change-Id: Ibf7504b92e5beb7193bf9b685f4540493dc37ff6

* fix test

Change-Id: Ic5153f8f3f09645eb60b50124fb4961887c59fa4

* add detect intent with disabled webhook sample and test

Change-Id: I1c565063d4e749029b872009722106222ca28b3d

* remove unnecessary comment

Change-Id: I3f56e80f4d2f7df3a1aa863d7b4e3f120ecce7a3

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* update test

Change-Id: Ie37488c281b513a592832836df8fbe4da60f95fb

* add streamingDetectIntent with partial response enabled sample and test

Change-Id: I499e395658d8c8b0e6f7f2229bb70d1969b25b9b

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* clean up extra comments and test lines

Change-Id: I28179e19fb1b567e743a399c2525b2d517ef1a1b

* clean up comments

Change-Id: I6182825f1d5205c0da284dbe27e9a044c0283dda

* update year

Change-Id: Idd732f365062f23a76a31fd3caf66cedd91813bb

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and Ace Nassri committed Nov 17, 2022
1 parent 652a6d5 commit 070b065
Show file tree
Hide file tree
Showing 6 changed files with 269 additions and 6 deletions.
75 changes: 75 additions & 0 deletions dialogflow-cx/detect-intent-disabled-webhook.js
Original file line number Diff line number Diff line change
@@ -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;
});
3 changes: 0 additions & 3 deletions dialogflow-cx/detect-intent-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ async function main(projectId, location, agentId, query, languageCode) {
agentId,
sessionId
);
console.info(sessionPath);

const request = {
session: sessionPath,
queryInput: {
Expand All @@ -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}`);
Expand Down
116 changes: 116 additions & 0 deletions dialogflow-cx/streaming-detect-intent-partial-response.js
Original file line number Diff line number Diff line change
@@ -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;
});
36 changes: 36 additions & 0 deletions dialogflow-cx/test/detect-intent-disabled-webhook-test.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
6 changes: 3 additions & 3 deletions dialogflow-cx/test/detect-intent-text.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ');
});
});
Original file line number Diff line number Diff line change
@@ -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!');
});
});

0 comments on commit 070b065

Please sign in to comment.