Skip to content

Commit f431d84

Browse files
nnegreytelpirion
authored andcommitted
Moving the Dialogflow samples to the python-docs-sampl… [(#1629)](#1629)
* [DO_NOT_MERGE] Moving the Dialogflow samples to the python-docs-samples repo * Add missing region tags for tracking * Style updates
1 parent 3953ca6 commit f431d84

35 files changed

+3082
-0
lines changed

dialogflow/README.rst

Lines changed: 632 additions & 0 deletions
Large diffs are not rendered by default.

dialogflow/README.rst.in

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# This file is used to generate README.rst
2+
3+
product:
4+
name: Dialogflow Enterprise Edition API
5+
short_name: Dialogflow API
6+
url: https://cloud.google.com/dialogflow-enterprise/docs/
7+
description: >
8+
The `Dialogflow Enterprise Edition 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+
- name: Detect Intent Knowledge Base
25+
file: detect_intent_knowledge.py
26+
show_help: True
27+
- name: Detect Intent with Model Selection
28+
file: detect_intent_with_model_selection.py
29+
show_help: True
30+
- name: Detect Intent with Sentiment Analysis
31+
file: detect_intent_with_sentiment_analysis.py
32+
show_help: True
33+
- name: Detect Intent with Text to Speech Response
34+
file: detect_intent_with_texttospeech_response.py
35+
show_help: True
36+
- name: Intent Management
37+
file: intent_management.py
38+
show_help: True
39+
- name: Entity Type Management
40+
file: entity_type_management.py
41+
show_help: True
42+
- name: Entity Management
43+
file: entity_management.py
44+
show_help: True
45+
- name: Session Entity Type Management
46+
file: session_entity_type_management.py
47+
show_help: True
48+
- name: Knowledge Base Management
49+
file: knowledge_base_management.py
50+
show_help: True
51+
- name: Document Management
52+
file: document_management.py
53+
show_help: True
54+
55+
cloud_client_library: true

dialogflow/context_management.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2017 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 Context Python sample showing how to manage session
18+
contexts.
19+
20+
Examples:
21+
python context_management.py -h
22+
python context_management.py --project-id PROJECT_ID \
23+
list --session-id SESSION_ID
24+
python context_management.py --project-id PROJECT_ID \
25+
create --session-id SESSION_ID --context-id CONTEXT_ID
26+
python context_management.py --project-id PROJECT_ID \
27+
delete --session-id SESSION_ID --context-id CONTEXT_ID
28+
"""
29+
30+
import argparse
31+
32+
33+
# [START dialogflow_list_contexts]
34+
def list_contexts(project_id, session_id):
35+
import dialogflow_v2 as dialogflow
36+
contexts_client = dialogflow.ContextsClient()
37+
38+
session_path = contexts_client.session_path(project_id, session_id)
39+
40+
contexts = contexts_client.list_contexts(session_path)
41+
42+
print('Contexts for session {}:\n'.format(session_path))
43+
for context in contexts:
44+
print('Context name: {}'.format(context.name))
45+
print('Lifespan count: {}'.format(context.lifespan_count))
46+
print('Fields:')
47+
for field, value in context.parameters.fields.items():
48+
if value.string_value:
49+
print('\t{}: {}'.format(field, value))
50+
# [END dialogflow_list_contexts]
51+
52+
53+
# [START dialogflow_create_context]
54+
def create_context(project_id, session_id, context_id, lifespan_count):
55+
import dialogflow_v2 as dialogflow
56+
contexts_client = dialogflow.ContextsClient()
57+
58+
session_path = contexts_client.session_path(project_id, session_id)
59+
context_name = contexts_client.context_path(
60+
project_id, session_id, context_id)
61+
62+
context = dialogflow.types.Context(
63+
name=context_name, lifespan_count=lifespan_count)
64+
65+
response = contexts_client.create_context(session_path, context)
66+
67+
print('Context created: \n{}'.format(response))
68+
# [END dialogflow_create_context]
69+
70+
71+
# [START dialogflow_delete_context]
72+
def delete_context(project_id, session_id, context_id):
73+
import dialogflow_v2 as dialogflow
74+
contexts_client = dialogflow.ContextsClient()
75+
76+
context_name = contexts_client.context_path(
77+
project_id, session_id, context_id)
78+
79+
contexts_client.delete_context(context_name)
80+
# [END dialogflow_delete_context]
81+
82+
83+
if __name__ == '__main__':
84+
parser = argparse.ArgumentParser(
85+
description=__doc__,
86+
formatter_class=argparse.RawDescriptionHelpFormatter)
87+
parser.add_argument(
88+
'--project-id',
89+
help='Project/agent id. Required.',
90+
required=True)
91+
92+
subparsers = parser.add_subparsers(dest='command')
93+
94+
list_parser = subparsers.add_parser(
95+
'list', help=list_contexts.__doc__)
96+
list_parser.add_argument(
97+
'--session-id',
98+
required=True)
99+
100+
create_parser = subparsers.add_parser(
101+
'create', help=create_context.__doc__)
102+
create_parser.add_argument(
103+
'--session-id',
104+
required=True)
105+
create_parser.add_argument(
106+
'--context-id',
107+
help='The id of the context.',
108+
required=True)
109+
create_parser.add_argument(
110+
'--lifespan-count',
111+
help='The lifespan_count of the context. Defaults to 1.',
112+
default=1)
113+
114+
delete_parser = subparsers.add_parser(
115+
'delete', help=delete_context.__doc__)
116+
delete_parser.add_argument(
117+
'--session-id',
118+
required=True)
119+
delete_parser.add_argument(
120+
'--context-id',
121+
help='The id of the context.',
122+
required=True)
123+
124+
args = parser.parse_args()
125+
126+
if args.command == 'list':
127+
list_contexts(args.project_id, args.session_id, )
128+
elif args.command == 'create':
129+
create_context(
130+
args.project_id, args.session_id, args.context_id,
131+
args.lifespan_count)
132+
elif args.command == 'delete':
133+
delete_context(args.project_id, args.session_id, args.context_id)

dialogflow/context_management_test.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright 2017 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+
from __future__ import absolute_import
16+
17+
import os
18+
19+
import context_management
20+
import detect_intent_texts
21+
22+
PROJECT_ID = os.getenv('GCLOUD_PROJECT')
23+
SESSION_ID = 'fake_session_for_testing'
24+
CONTEXT_ID = 'fake_context_for_testing'
25+
26+
27+
def test_create_context(capsys):
28+
# Calling detect intent to create a session.
29+
detect_intent_texts.detect_intent_texts(
30+
PROJECT_ID, SESSION_ID, ['hi'], 'en-US')
31+
32+
context_management.create_context(PROJECT_ID, SESSION_ID, CONTEXT_ID, 1)
33+
context_management.list_contexts(PROJECT_ID, SESSION_ID)
34+
35+
out, _ = capsys.readouterr()
36+
assert CONTEXT_ID in out
37+
38+
39+
def test_delete_context(capsys):
40+
context_management.delete_context(PROJECT_ID, SESSION_ID, CONTEXT_ID)
41+
context_management.list_contexts(PROJECT_ID, SESSION_ID)
42+
43+
out, _ = capsys.readouterr()
44+
assert CONTEXT_ID not in out

dialogflow/detect_intent_audio.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2017 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 --project-id PROJECT_ID \
22+
--session-id SESSION_ID --audio-file-path resources/book_a_room.wav
23+
python detect_intent_audio.py --project-id PROJECT_ID \
24+
--session-id SESSION_ID --audio-file-path resources/mountain_view.wav
25+
python detect_intent_audio.py --project-id PROJECT_ID \
26+
--session-id SESSION_ID --audio-file-path resources/today.wav
27+
"""
28+
29+
import argparse
30+
import uuid
31+
32+
33+
# [START dialogflow_detect_intent_audio]
34+
def detect_intent_audio(project_id, session_id, audio_file_path,
35+
language_code):
36+
"""Returns the result of detect intent with an audio file as input.
37+
38+
Using the same `session_id` between requests allows continuation
39+
of the conversaion."""
40+
import dialogflow_v2 as dialogflow
41+
42+
session_client = dialogflow.SessionsClient()
43+
44+
# Note: hard coding audio_encoding and sample_rate_hertz for simplicity.
45+
audio_encoding = dialogflow.enums.AudioEncoding.AUDIO_ENCODING_LINEAR_16
46+
sample_rate_hertz = 16000
47+
48+
session = session_client.session_path(project_id, session_id)
49+
print('Session path: {}\n'.format(session))
50+
51+
with open(audio_file_path, 'rb') as audio_file:
52+
input_audio = audio_file.read()
53+
54+
audio_config = dialogflow.types.InputAudioConfig(
55+
audio_encoding=audio_encoding, language_code=language_code,
56+
sample_rate_hertz=sample_rate_hertz)
57+
query_input = dialogflow.types.QueryInput(audio_config=audio_config)
58+
59+
response = session_client.detect_intent(
60+
session=session, query_input=query_input,
61+
input_audio=input_audio)
62+
63+
print('=' * 20)
64+
print('Query text: {}'.format(response.query_result.query_text))
65+
print('Detected intent: {} (confidence: {})\n'.format(
66+
response.query_result.intent.display_name,
67+
response.query_result.intent_detection_confidence))
68+
print('Fulfillment text: {}\n'.format(
69+
response.query_result.fulfillment_text))
70+
# [END dialogflow_detect_intent_audio]
71+
72+
73+
if __name__ == '__main__':
74+
parser = argparse.ArgumentParser(
75+
description=__doc__,
76+
formatter_class=argparse.RawDescriptionHelpFormatter)
77+
parser.add_argument(
78+
'--project-id',
79+
help='Project/agent id. Required.',
80+
required=True)
81+
parser.add_argument(
82+
'--session-id',
83+
help='Identifier of the DetectIntent session. '
84+
'Defaults to a random UUID.',
85+
default=str(uuid.uuid4()))
86+
parser.add_argument(
87+
'--language-code',
88+
help='Language code of the query. Defaults to "en-US".',
89+
default='en-US')
90+
parser.add_argument(
91+
'--audio-file-path',
92+
help='Path to the audio file.',
93+
required=True)
94+
95+
args = parser.parse_args()
96+
97+
detect_intent_audio(
98+
args.project_id, args.session_id, args.audio_file_path,
99+
args.language_code)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2017, Google LLC
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
from __future__ import absolute_import
15+
16+
import os
17+
18+
from detect_intent_audio import detect_intent_audio
19+
20+
DIRNAME = os.path.realpath(os.path.dirname(__file__))
21+
PROJECT_ID = os.getenv('GCLOUD_PROJECT')
22+
SESSION_ID = 'fake_session_for_testing'
23+
AUDIOS = [
24+
'{0}/resources/book_a_room.wav'.format(DIRNAME),
25+
'{0}/resources/mountain_view.wav'.format(DIRNAME),
26+
'{0}/resources/today.wav'.format(DIRNAME),
27+
]
28+
29+
30+
def test_detect_intent_audio(capsys):
31+
for audio_file_path in AUDIOS:
32+
detect_intent_audio(PROJECT_ID, SESSION_ID, audio_file_path, 'en-US')
33+
out, _ = capsys.readouterr()
34+
35+
assert 'Fulfillment text: What time will the meeting start?' in out

0 commit comments

Comments
 (0)