Skip to content

Commit 070b065

Browse files
aribraygcf-owl-bot[bot]
authored and
Ace Nassri
committed
docs (samples): add detect intent samples (#292)
* 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>
1 parent 652a6d5 commit 070b065

6 files changed

+269
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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(projectId, location, agentId, query, languageCode) {
18+
// [START dialogflow_cx_detect_intent_with_disabled_webhook]
19+
/**
20+
* TODO(developer): Uncomment these variables before running the sample.
21+
*/
22+
// const projectId = 'my-project';
23+
// const location = 'global';
24+
// const agentId = 'my-agent';
25+
// const query = 'Hello';
26+
// const languageCode = 'en'
27+
28+
const {SessionsClient} = require('@google-cloud/dialogflow-cx');
29+
/**
30+
* Example for regional endpoint:
31+
* const location = 'us-central1'
32+
* const client = new SessionsClient({apiEndpoint: 'us-central1-dialogflow.googleapis.com'})
33+
*/
34+
const client = new SessionsClient();
35+
36+
async function detectIntentText() {
37+
const sessionId = Math.random().toString(36).substring(7);
38+
const sessionPath = client.projectLocationAgentSessionPath(
39+
projectId,
40+
location,
41+
agentId,
42+
sessionId
43+
);
44+
console.info(sessionPath);
45+
46+
const request = {
47+
session: sessionPath,
48+
queryParams: {
49+
disableWebhook: true,
50+
},
51+
queryInput: {
52+
text: {
53+
text: query,
54+
},
55+
languageCode,
56+
},
57+
};
58+
const [response] = await client.detectIntent(request);
59+
console.log(`Detect Intent Request: ${request.queryParams.disableWebhook}`);
60+
for (const message of response.queryResult.responseMessages) {
61+
if (message.text) {
62+
console.log(`Agent Response: ${message.text.text}`);
63+
}
64+
}
65+
}
66+
67+
detectIntentText();
68+
// [END dialogflow_cx_detect_intent_with_disabled_webhook]
69+
}
70+
71+
main(...process.argv.slice(2));
72+
process.on('unhandledRejection', err => {
73+
console.error(err.message);
74+
process.exitCode = 1;
75+
});

dialogflow-cx/detect-intent-text.js

-3
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ async function main(projectId, location, agentId, query, languageCode) {
4242
agentId,
4343
sessionId
4444
);
45-
console.info(sessionPath);
46-
4745
const request = {
4846
session: sessionPath,
4947
queryInput: {
@@ -54,7 +52,6 @@ async function main(projectId, location, agentId, query, languageCode) {
5452
},
5553
};
5654
const [response] = await client.detectIntent(request);
57-
console.log(`User Query: ${query}`);
5855
for (const message of response.queryResult.responseMessages) {
5956
if (message.text) {
6057
console.log(`Agent Response: ${message.text.text}`);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
const {assert} = require('chai');
18+
const {describe, it} = require('mocha');
19+
const execSync = require('child_process').execSync;
20+
const exec = cmd => execSync(cmd, {encoding: 'utf8'});
21+
22+
describe('detect intent with disabled webhook', () => {
23+
const cmd = 'node detect-intent-disabled-webhook.js';
24+
25+
const projectId = process.env.GCLOUD_PROJECT;
26+
const location = 'global';
27+
const agentId = 'b1808233-450b-4065-9492-bc9b40151641';
28+
const languageCode = 'en';
29+
30+
it('should have disableWebhook set to "true"', async () => {
31+
const output = exec(
32+
`${cmd} ${projectId} ${location} ${agentId} 'hello' ${languageCode}`
33+
);
34+
assert.include(output, 'true');
35+
});
36+
});

dialogflow-cx/test/detect-intent-text.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ describe('detect intent with text input', () => {
2727
const agentId = 'b1808233-450b-4065-9492-bc9b40151641';
2828
const languageCode = 'en';
2929

30-
it('should response to "hello"', async () => {
30+
it('should respond to "hello"', async () => {
3131
const output = exec(
3232
`${cmd} ${projectId} ${location} ${agentId} 'hello' ${languageCode}`
3333
);
3434
assert.include(output, 'How can I assist you today?');
3535
});
3636

37-
it('should response to "reserve a vent"', async () => {
37+
it('should respond to "reserve a van"', async () => {
3838
const output = exec(
3939
`${cmd} ${projectId} ${location} ${agentId} 'i need to reserve a van' ${languageCode}`
4040
);
41-
assert.include(output, 'Where would you like to pick it up?');
41+
assert.include(output, 'Where would you like ');
4242
});
4343
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
const {assert} = require('chai');
18+
const {describe, it} = require('mocha');
19+
const execSync = require('child_process').execSync;
20+
const exec = cmd => execSync(cmd, {encoding: 'utf8'});
21+
22+
describe('streaming detect intent with partial response', () => {
23+
const cmd = 'node streaming-detect-intent-partial-response.js';
24+
25+
const projectId = process.env.GCLOUD_PROJECT;
26+
const location = 'global';
27+
const agentId = '7e4c5542-823b-4e28-bd02-d37016826e9e';
28+
const audioFileName = 'resources/book_a_room.wav';
29+
const encoding = 'AUDIO_ENCODING_LINEAR_16';
30+
const sampleRateHertz = 16000;
31+
const languageCode = 'en';
32+
33+
it('should respond to with partial response', async () => {
34+
const output = exec(
35+
`${cmd} ${projectId} ${location} ${agentId} ${audioFileName} ${encoding} ${sampleRateHertz} ${languageCode}`
36+
);
37+
assert.include(output, 'One moment while I try to help!');
38+
});
39+
});

0 commit comments

Comments
 (0)