-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* [DO_NOT_MERGE] Moving the Dialogflow samples to the python-docs-samples repo * Add missing region tags for tracking * Style updates
- Loading branch information
0 parents
commit 1d0b983
Showing
35 changed files
with
3,082 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# This file is used to generate README.rst | ||
|
||
product: | ||
name: Dialogflow Enterprise Edition API | ||
short_name: Dialogflow API | ||
url: https://cloud.google.com/dialogflow-enterprise/docs/ | ||
description: > | ||
The `Dialogflow Enterprise Edition API`_ enables you to create conversational experiences across devices and platforms. | ||
|
||
setup: | ||
- auth | ||
- install_deps | ||
|
||
samples: | ||
- name: Detect Intent Text | ||
file: detect_intent_texts.py | ||
show_help: True | ||
- name: Detect Intent Audio | ||
file: detect_intent_audio.py | ||
show_help: True | ||
- name: Detect Intent Stream | ||
file: detect_intent_stream.py | ||
show_help: True | ||
- name: Detect Intent Knowledge Base | ||
file: detect_intent_knowledge.py | ||
show_help: True | ||
- name: Detect Intent with Model Selection | ||
file: detect_intent_with_model_selection.py | ||
show_help: True | ||
- name: Detect Intent with Sentiment Analysis | ||
file: detect_intent_with_sentiment_analysis.py | ||
show_help: True | ||
- name: Detect Intent with Text to Speech Response | ||
file: detect_intent_with_texttospeech_response.py | ||
show_help: True | ||
- name: Intent Management | ||
file: intent_management.py | ||
show_help: True | ||
- name: Entity Type Management | ||
file: entity_type_management.py | ||
show_help: True | ||
- name: Entity Management | ||
file: entity_management.py | ||
show_help: True | ||
- name: Session Entity Type Management | ||
file: session_entity_type_management.py | ||
show_help: True | ||
- name: Knowledge Base Management | ||
file: knowledge_base_management.py | ||
show_help: True | ||
- name: Document Management | ||
file: document_management.py | ||
show_help: True | ||
|
||
cloud_client_library: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright 2017 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. | ||
|
||
"""DialogFlow API Context Python sample showing how to manage session | ||
contexts. | ||
Examples: | ||
python context_management.py -h | ||
python context_management.py --project-id PROJECT_ID \ | ||
list --session-id SESSION_ID | ||
python context_management.py --project-id PROJECT_ID \ | ||
create --session-id SESSION_ID --context-id CONTEXT_ID | ||
python context_management.py --project-id PROJECT_ID \ | ||
delete --session-id SESSION_ID --context-id CONTEXT_ID | ||
""" | ||
|
||
import argparse | ||
|
||
|
||
# [START dialogflow_list_contexts] | ||
def list_contexts(project_id, session_id): | ||
import dialogflow_v2 as dialogflow | ||
contexts_client = dialogflow.ContextsClient() | ||
|
||
session_path = contexts_client.session_path(project_id, session_id) | ||
|
||
contexts = contexts_client.list_contexts(session_path) | ||
|
||
print('Contexts for session {}:\n'.format(session_path)) | ||
for context in contexts: | ||
print('Context name: {}'.format(context.name)) | ||
print('Lifespan count: {}'.format(context.lifespan_count)) | ||
print('Fields:') | ||
for field, value in context.parameters.fields.items(): | ||
if value.string_value: | ||
print('\t{}: {}'.format(field, value)) | ||
# [END dialogflow_list_contexts] | ||
|
||
|
||
# [START dialogflow_create_context] | ||
def create_context(project_id, session_id, context_id, lifespan_count): | ||
import dialogflow_v2 as dialogflow | ||
contexts_client = dialogflow.ContextsClient() | ||
|
||
session_path = contexts_client.session_path(project_id, session_id) | ||
context_name = contexts_client.context_path( | ||
project_id, session_id, context_id) | ||
|
||
context = dialogflow.types.Context( | ||
name=context_name, lifespan_count=lifespan_count) | ||
|
||
response = contexts_client.create_context(session_path, context) | ||
|
||
print('Context created: \n{}'.format(response)) | ||
# [END dialogflow_create_context] | ||
|
||
|
||
# [START dialogflow_delete_context] | ||
def delete_context(project_id, session_id, context_id): | ||
import dialogflow_v2 as dialogflow | ||
contexts_client = dialogflow.ContextsClient() | ||
|
||
context_name = contexts_client.context_path( | ||
project_id, session_id, context_id) | ||
|
||
contexts_client.delete_context(context_name) | ||
# [END dialogflow_delete_context] | ||
|
||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser( | ||
description=__doc__, | ||
formatter_class=argparse.RawDescriptionHelpFormatter) | ||
parser.add_argument( | ||
'--project-id', | ||
help='Project/agent id. Required.', | ||
required=True) | ||
|
||
subparsers = parser.add_subparsers(dest='command') | ||
|
||
list_parser = subparsers.add_parser( | ||
'list', help=list_contexts.__doc__) | ||
list_parser.add_argument( | ||
'--session-id', | ||
required=True) | ||
|
||
create_parser = subparsers.add_parser( | ||
'create', help=create_context.__doc__) | ||
create_parser.add_argument( | ||
'--session-id', | ||
required=True) | ||
create_parser.add_argument( | ||
'--context-id', | ||
help='The id of the context.', | ||
required=True) | ||
create_parser.add_argument( | ||
'--lifespan-count', | ||
help='The lifespan_count of the context. Defaults to 1.', | ||
default=1) | ||
|
||
delete_parser = subparsers.add_parser( | ||
'delete', help=delete_context.__doc__) | ||
delete_parser.add_argument( | ||
'--session-id', | ||
required=True) | ||
delete_parser.add_argument( | ||
'--context-id', | ||
help='The id of the context.', | ||
required=True) | ||
|
||
args = parser.parse_args() | ||
|
||
if args.command == 'list': | ||
list_contexts(args.project_id, args.session_id, ) | ||
elif args.command == 'create': | ||
create_context( | ||
args.project_id, args.session_id, args.context_id, | ||
args.lifespan_count) | ||
elif args.command == 'delete': | ||
delete_context(args.project_id, args.session_id, args.context_id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Copyright 2017 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. | ||
|
||
from __future__ import absolute_import | ||
|
||
import os | ||
|
||
import context_management | ||
import detect_intent_texts | ||
|
||
PROJECT_ID = os.getenv('GCLOUD_PROJECT') | ||
SESSION_ID = 'fake_session_for_testing' | ||
CONTEXT_ID = 'fake_context_for_testing' | ||
|
||
|
||
def test_create_context(capsys): | ||
# Calling detect intent to create a session. | ||
detect_intent_texts.detect_intent_texts( | ||
PROJECT_ID, SESSION_ID, ['hi'], 'en-US') | ||
|
||
context_management.create_context(PROJECT_ID, SESSION_ID, CONTEXT_ID, 1) | ||
context_management.list_contexts(PROJECT_ID, SESSION_ID) | ||
|
||
out, _ = capsys.readouterr() | ||
assert CONTEXT_ID in out | ||
|
||
|
||
def test_delete_context(capsys): | ||
context_management.delete_context(PROJECT_ID, SESSION_ID, CONTEXT_ID) | ||
context_management.list_contexts(PROJECT_ID, SESSION_ID) | ||
|
||
out, _ = capsys.readouterr() | ||
assert CONTEXT_ID not in out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright 2017 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. | ||
|
||
"""DialogFlow API Detect Intent Python sample with audio file. | ||
Examples: | ||
python detect_intent_audio.py -h | ||
python detect_intent_audio.py --project-id PROJECT_ID \ | ||
--session-id SESSION_ID --audio-file-path resources/book_a_room.wav | ||
python detect_intent_audio.py --project-id PROJECT_ID \ | ||
--session-id SESSION_ID --audio-file-path resources/mountain_view.wav | ||
python detect_intent_audio.py --project-id PROJECT_ID \ | ||
--session-id SESSION_ID --audio-file-path resources/today.wav | ||
""" | ||
|
||
import argparse | ||
import uuid | ||
|
||
|
||
# [START dialogflow_detect_intent_audio] | ||
def detect_intent_audio(project_id, session_id, audio_file_path, | ||
language_code): | ||
"""Returns the result of detect intent with an audio file as input. | ||
Using the same `session_id` between requests allows continuation | ||
of the conversaion.""" | ||
import dialogflow_v2 as dialogflow | ||
|
||
session_client = dialogflow.SessionsClient() | ||
|
||
# Note: hard coding audio_encoding and sample_rate_hertz for simplicity. | ||
audio_encoding = dialogflow.enums.AudioEncoding.AUDIO_ENCODING_LINEAR_16 | ||
sample_rate_hertz = 16000 | ||
|
||
session = session_client.session_path(project_id, session_id) | ||
print('Session path: {}\n'.format(session)) | ||
|
||
with open(audio_file_path, 'rb') as audio_file: | ||
input_audio = audio_file.read() | ||
|
||
audio_config = dialogflow.types.InputAudioConfig( | ||
audio_encoding=audio_encoding, language_code=language_code, | ||
sample_rate_hertz=sample_rate_hertz) | ||
query_input = dialogflow.types.QueryInput(audio_config=audio_config) | ||
|
||
response = session_client.detect_intent( | ||
session=session, query_input=query_input, | ||
input_audio=input_audio) | ||
|
||
print('=' * 20) | ||
print('Query text: {}'.format(response.query_result.query_text)) | ||
print('Detected intent: {} (confidence: {})\n'.format( | ||
response.query_result.intent.display_name, | ||
response.query_result.intent_detection_confidence)) | ||
print('Fulfillment text: {}\n'.format( | ||
response.query_result.fulfillment_text)) | ||
# [END dialogflow_detect_intent_audio] | ||
|
||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser( | ||
description=__doc__, | ||
formatter_class=argparse.RawDescriptionHelpFormatter) | ||
parser.add_argument( | ||
'--project-id', | ||
help='Project/agent id. Required.', | ||
required=True) | ||
parser.add_argument( | ||
'--session-id', | ||
help='Identifier of the DetectIntent session. ' | ||
'Defaults to a random UUID.', | ||
default=str(uuid.uuid4())) | ||
parser.add_argument( | ||
'--language-code', | ||
help='Language code of the query. Defaults to "en-US".', | ||
default='en-US') | ||
parser.add_argument( | ||
'--audio-file-path', | ||
help='Path to the audio file.', | ||
required=True) | ||
|
||
args = parser.parse_args() | ||
|
||
detect_intent_audio( | ||
args.project_id, args.session_id, args.audio_file_path, | ||
args.language_code) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Copyright 2017, 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. | ||
|
||
from __future__ import absolute_import | ||
|
||
import os | ||
|
||
from detect_intent_audio import detect_intent_audio | ||
|
||
DIRNAME = os.path.realpath(os.path.dirname(__file__)) | ||
PROJECT_ID = os.getenv('GCLOUD_PROJECT') | ||
SESSION_ID = 'fake_session_for_testing' | ||
AUDIOS = [ | ||
'{0}/resources/book_a_room.wav'.format(DIRNAME), | ||
'{0}/resources/mountain_view.wav'.format(DIRNAME), | ||
'{0}/resources/today.wav'.format(DIRNAME), | ||
] | ||
|
||
|
||
def test_detect_intent_audio(capsys): | ||
for audio_file_path in AUDIOS: | ||
detect_intent_audio(PROJECT_ID, SESSION_ID, audio_file_path, 'en-US') | ||
out, _ = capsys.readouterr() | ||
|
||
assert 'Fulfillment text: What time will the meeting start?' in out |
Oops, something went wrong.