Skip to content
This repository has been archived by the owner on Jul 20, 2023. It is now read-only.

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
aribray and gcf-owl-bot[bot] authored Jun 23, 2022
1 parent 66c5563 commit 7852509
Show file tree
Hide file tree
Showing 8 changed files with 307 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-dialogflow-
| Create-page | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/create-page.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/create-page.js,samples/README.md) |
| Delete-page | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/delete-page.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/delete-page.js,samples/README.md) |
| Detect-intent-audio | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/detect-intent-audio.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/detect-intent-audio.js,samples/README.md) |
| Detect-intent-disabled-webhook | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/detect-intent-disabled-webhook.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/detect-intent-disabled-webhook.js,samples/README.md) |
| Detect-intent-event | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/detect-intent-event.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/detect-intent-event.js,samples/README.md) |
| Detect-intent-streaming | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/detect-intent-streaming.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/detect-intent-streaming.js,samples/README.md) |
| Detect-intent-synthesize-tts-response | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/detect-intent-synthesize-tts-response.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/detect-intent-synthesize-tts-response.js,samples/README.md) |
Expand All @@ -155,6 +156,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-dialogflow-
| List Training Phrases | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/listTrainingPhrases.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/listTrainingPhrases.js,samples/README.md) |
| Long-running-operation | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/long-running-operation.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/long-running-operation.js,samples/README.md) |
| Quickstart | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/quickstart.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/quickstart.js,samples/README.md) |
| Streaming-detect-intent-partial-response | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/streaming-detect-intent-partial-response.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/streaming-detect-intent-partial-response.js,samples/README.md) |
| Update-intent | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/update-intent.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/update-intent.js,samples/README.md) |
| Webhook-configure-session-parameters | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/webhook-configure-session-parameters.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/webhook-configure-session-parameters.js,samples/README.md) |
| Webhook-validate-form-parameter | [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/webhook-validate-form-parameter.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/webhook-validate-form-parameter.js,samples/README.md) |
Expand Down
36 changes: 36 additions & 0 deletions samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* [Create-page](#create-page)
* [Delete-page](#delete-page)
* [Detect-intent-audio](#detect-intent-audio)
* [Detect-intent-disabled-webhook](#detect-intent-disabled-webhook)
* [Detect-intent-event](#detect-intent-event)
* [Detect-intent-streaming](#detect-intent-streaming)
* [Detect-intent-synthesize-tts-response](#detect-intent-synthesize-tts-response)
Expand All @@ -29,6 +30,7 @@
* [List Training Phrases](#list-training-phrases)
* [Long-running-operation](#long-running-operation)
* [Quickstart](#quickstart)
* [Streaming-detect-intent-partial-response](#streaming-detect-intent-partial-response)
* [Update-intent](#update-intent)
* [Webhook-configure-session-parameters](#webhook-configure-session-parameters)
* [Webhook-validate-form-parameter](#webhook-validate-form-parameter)
Expand Down Expand Up @@ -134,6 +136,23 @@ __Usage:__



### Detect-intent-disabled-webhook

View the [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/detect-intent-disabled-webhook.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/detect-intent-disabled-webhook.js,samples/README.md)

__Usage:__


`node samples/detect-intent-disabled-webhook.js`


-----




### Detect-intent-event

View the [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/detect-intent-event.js).
Expand Down Expand Up @@ -338,6 +357,23 @@ __Usage:__



### Streaming-detect-intent-partial-response

View the [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/streaming-detect-intent-partial-response.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-dialogflow-cx&page=editor&open_in_editor=samples/streaming-detect-intent-partial-response.js,samples/README.md)

__Usage:__


`node samples/streaming-detect-intent-partial-response.js`


-----




### Update-intent

View the [source code](https://github.com/googleapis/nodejs-dialogflow-cx/blob/main/samples/update-intent.js).
Expand Down
75 changes: 75 additions & 0 deletions samples/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 samples/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 samples/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 samples/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 samples/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 ');
});
});
39 changes: 39 additions & 0 deletions samples/test/streaming-detect-intent-partial-response-test.js
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 7852509

Please sign in to comment.