Skip to content
This repository was archived by the owner on Jan 6, 2024. It is now read-only.

Commit b590308

Browse files
authored
docs(samples): add initial sample codes (#13)
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [x] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/python-dialogflow-cx/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [x] Ensure the tests and linter pass - [ ] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary) Fixes #12 🦕
1 parent b4ed732 commit b590308

17 files changed

+962
-1
lines changed

.github/CODEOWNERS

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
* @googleapis/yoshi-python
99

1010
# The python-samples-reviewers team is the default owner for samples changes
11-
/samples/ @googleapis/python-samples-owners
11+
/samples/ @googleapis/python-samples-owners @wuyuexin

.github/snippet-bot.yml

Whitespace-only changes.

samples/AUTHORING_GUIDE.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
See https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md

samples/CONTRIBUTING.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
See https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/CONTRIBUTING.md

samples/snippets/README.rst

+221
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
2+
.. This file is automatically generated. Do not edit this file directly.
3+
4+
Dialogflow CX API Python Samples
5+
===============================================================================
6+
7+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
8+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/README.rst
9+
10+
11+
This directory contains samples for Dialogflow CX API. The `Dialogflow CX API`_ enables you to create conversational experiences across devices and platforms.
12+
13+
14+
15+
16+
.. _Dialogflow CX API: https://cloud.google.com/dialogflow/cx/docs/
17+
18+
19+
Setup
20+
-------------------------------------------------------------------------------
21+
22+
23+
24+
Authentication
25+
++++++++++++++
26+
27+
This sample requires you to have authentication setup. Refer to the
28+
`Authentication Getting Started Guide`_ for instructions on setting up
29+
credentials for applications.
30+
31+
.. _Authentication Getting Started Guide:
32+
https://cloud.google.com/docs/authentication/getting-started
33+
34+
35+
36+
37+
Install Dependencies
38+
++++++++++++++++++++
39+
40+
#. Clone python-docs-samples and change directory to the sample directory you want to use.
41+
42+
.. code-block:: bash
43+
44+
$ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
45+
46+
#. Install `pip`_ and `virtualenv`_ if you do not already have them. You may want to refer to the `Python Development Environment Setup Guide`_ for Google Cloud Platform for instructions.
47+
48+
.. _Python Development Environment Setup Guide:
49+
https://cloud.google.com/python/setup
50+
51+
#. Create a virtualenv. Samples are compatible with Python 3.6+.
52+
53+
.. code-block:: bash
54+
55+
$ virtualenv env
56+
$ source env/bin/activate
57+
58+
#. Install the dependencies needed to run the samples.
59+
60+
.. code-block:: bash
61+
62+
$ pip install -r requirements.txt
63+
64+
.. _pip: https://pip.pypa.io/
65+
.. _virtualenv: https://virtualenv.pypa.io/
66+
67+
68+
69+
70+
71+
72+
Samples
73+
-------------------------------------------------------------------------------
74+
75+
76+
Detect Intent Text
77+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
78+
79+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
80+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/detect_intent_texts.py,/README.rst
81+
82+
83+
84+
85+
To run this sample:
86+
87+
.. code-block:: bash
88+
89+
$ python detect_intent_texts.py
90+
91+
92+
usage: detect_intent_texts.py [-h] --agent AGENT [--session-id SESSION_ID]
93+
[--language-code LANGUAGE_CODE]
94+
texts [texts ...]
95+
96+
DialogFlow API Detect Intent Python sample with text inputs.
97+
98+
Examples:
99+
python detect_intent_texts.py -h
100+
python detect_intent_texts.py --agent AGENT --session-id SESSION_ID "hello" "book a meeting room" "Mountain View"
101+
python detect_intent_texts.py --agent AGENT --session-id SESSION_ID "tomorrow" "10 AM" "2 hours" "10 people" "A" "yes"
102+
103+
positional arguments:
104+
texts Text inputs.
105+
106+
optional arguments:
107+
-h, --help show this help message and exit
108+
--agent AGENT Agent resource name. Required.
109+
--session-id SESSION_ID
110+
Identifier of the DetectIntent session. Defaults to a
111+
random UUID.
112+
--language-code LANGUAGE_CODE
113+
Language code of the query. Defaults to "en-US".
114+
115+
116+
117+
118+
119+
Detect Intent Audio
120+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
121+
122+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
123+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/detect_intent_audio.py,/README.rst
124+
125+
126+
127+
128+
To run this sample:
129+
130+
.. code-block:: bash
131+
132+
$ python detect_intent_audio.py
133+
134+
135+
usage: detect_intent_audio.py [-h] --agent AGENT [--session-id SESSION_ID]
136+
[--language-code LANGUAGE_CODE]
137+
--audio-file-path AUDIO_FILE_PATH
138+
139+
DialogFlow API Detect Intent Python sample with audio file.
140+
141+
Examples:
142+
python detect_intent_audio.py -h
143+
python detect_intent_audio.py --agent AGENT --session-id SESSION_ID --audio-file-path resources/hello.wav
144+
145+
optional arguments:
146+
-h, --help show this help message and exit
147+
--agent AGENT Agent resource name. Required.
148+
--session-id SESSION_ID
149+
Identifier of the DetectIntent session. Defaults to a
150+
random UUID.
151+
--language-code LANGUAGE_CODE
152+
Language code of the query. Defaults to "en-US".
153+
--audio-file-path AUDIO_FILE_PATH
154+
Path to the audio file.
155+
156+
157+
158+
159+
160+
Detect Intent Stream
161+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
162+
163+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
164+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/detect_intent_stream.py,/README.rst
165+
166+
167+
168+
169+
To run this sample:
170+
171+
.. code-block:: bash
172+
173+
$ python detect_intent_stream.py
174+
175+
176+
usage: detect_intent_stream.py [-h] --agent AGENT [--session-id SESSION_ID]
177+
[--language-code LANGUAGE_CODE]
178+
--audio-file-path AUDIO_FILE_PATH
179+
180+
DialogFlow API Detect Intent Python sample with audio files processed as an audio stream.
181+
182+
Examples:
183+
python detect_intent_stream.py -h
184+
python detect_intent_stream.py --agent AGENT --session-id SESSION_ID --audio-file-path resources/hello.wav
185+
186+
optional arguments:
187+
-h, --help show this help message and exit
188+
--agent AGENT Agent resource name. Required.
189+
--session-id SESSION_ID
190+
Identifier of the DetectIntent session. Defaults to a
191+
random UUID.
192+
--language-code LANGUAGE_CODE
193+
Language code of the query. Defaults to "en-US".
194+
--audio-file-path AUDIO_FILE_PATH
195+
Path to the audio file.
196+
197+
198+
199+
200+
201+
202+
203+
204+
205+
The client library
206+
-------------------------------------------------------------------------------
207+
208+
This sample uses the `Google Cloud Client Library for Python`_.
209+
You can read the documentation for more details on API usage and use GitHub
210+
to `browse the source`_ and `report issues`_.
211+
212+
.. _Google Cloud Client Library for Python:
213+
https://googlecloudplatform.github.io/google-cloud-python/
214+
.. _browse the source:
215+
https://github.com/GoogleCloudPlatform/google-cloud-python
216+
.. _report issues:
217+
https://github.com/GoogleCloudPlatform/google-cloud-python/issues
218+
219+
220+
221+
.. _Google Cloud SDK: https://cloud.google.com/sdk/

samples/snippets/README.rst.in

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# This file is used to generate README.rst
2+
3+
product:
4+
name: Dialogflow CX API
5+
short_name: Dialogflow CX API
6+
url: https://cloud.google.com/dialogflow/cx/docs/
7+
description: >
8+
The `Dialogflow CX API`_ enables you to create conversational experiences across devices and platforms.
9+
10+
setup:
11+
- auth
12+
- install_deps
13+
14+
samples:
15+
- name: Detect Intent Text
16+
file: detect_intent_texts.py
17+
show_help: True
18+
- name: Detect Intent Audio
19+
file: detect_intent_audio.py
20+
show_help: True
21+
- name: Detect Intent Stream
22+
file: detect_intent_stream.py
23+
show_help: True
24+
25+
cloud_client_library: true
+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2020 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""DialogFlow API Detect Intent Python sample with audio file.
18+
19+
Examples:
20+
python detect_intent_audio.py -h
21+
python detect_intent_audio.py --agent AGENT \
22+
--session-id SESSION_ID --audio-file-path resources/hello.wav
23+
"""
24+
25+
import argparse
26+
import uuid
27+
28+
from google.cloud.dialogflowcx_v3beta1.services.sessions import SessionsClient
29+
from google.cloud.dialogflowcx_v3beta1.types import audio_config
30+
from google.cloud.dialogflowcx_v3beta1.types import session
31+
32+
33+
# [START dialogflow_detect_intent_audio]
34+
def run_sample():
35+
# TODO(developer): Replace these values when running the function
36+
project_id = "YOUR-PROJECT-ID"
37+
location_id = "YOUR-LOCATION-ID"
38+
# For more info on agents see https://cloud.google.com/dialogflow/cx/docs/concept/agent
39+
agent_id = "YOUR-AGENT-ID"
40+
agent = f"projects/{project_id}/locations/{location_id}/agents/{agent_id}"
41+
# For more information on sessions see https://cloud.google.com/dialogflow/cx/docs/concept/session
42+
session_id = str(uuid.uuid4())
43+
audio_file_path = "YOUR-AUDIO-FILE-PATH"
44+
# For more supported languages see https://cloud.google.com/dialogflow/es/docs/reference/language
45+
language_code = "en-us"
46+
47+
detect_intent_audio(agent, session_id, audio_file_path, language_code)
48+
49+
50+
def detect_intent_audio(agent, session_id, audio_file_path, language_code):
51+
"""Returns the result of detect intent with an audio file as input.
52+
53+
Using the same `session_id` between requests allows continuation
54+
of the conversation."""
55+
session_client = SessionsClient()
56+
session_path = f"{agent}/sessions/{session_id}"
57+
print(f"Session path: {session_path}\n")
58+
59+
input_audio_config = audio_config.InputAudioConfig(
60+
audio_encoding=audio_config.AudioEncoding.AUDIO_ENCODING_LINEAR_16,
61+
sample_rate_hertz=24000,
62+
)
63+
64+
with open(audio_file_path, "rb") as audio_file:
65+
input_audio = audio_file.read()
66+
67+
audio_input = session.AudioInput(config=input_audio_config, audio=input_audio)
68+
query_input = session.QueryInput(audio=audio_input, language_code=language_code)
69+
request = session.DetectIntentRequest(session=session_path, query_input=query_input)
70+
response = session_client.detect_intent(request=request)
71+
72+
print("=" * 20)
73+
print(f"Query text: {response.query_result.transcript}")
74+
response_messages = [
75+
" ".join(msg.text.text) for msg in response.query_result.response_messages
76+
]
77+
print(f"Response text: {' '.join(response_messages)}\n")
78+
79+
80+
# [END dialogflow_detect_intent_audio]
81+
82+
if __name__ == "__main__":
83+
parser = argparse.ArgumentParser(
84+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
85+
)
86+
parser.add_argument(
87+
"--agent", help="Agent resource name. Required.", required=True
88+
)
89+
parser.add_argument(
90+
"--session-id",
91+
help="Identifier of the DetectIntent session. " "Defaults to a random UUID.",
92+
default=str(uuid.uuid4()),
93+
)
94+
parser.add_argument(
95+
"--language-code",
96+
help='Language code of the query. Defaults to "en-US".',
97+
default="en-US",
98+
)
99+
parser.add_argument(
100+
"--audio-file-path", help="Path to the audio file.", required=True
101+
)
102+
103+
args = parser.parse_args()
104+
105+
detect_intent_audio(
106+
args.agent, args.session_id, args.audio_file_path, args.language_code
107+
)

0 commit comments

Comments
 (0)