From b5c4eaa969e5eae668e76996bb276af94517b4ae Mon Sep 17 00:00:00 2001 From: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> Date: Mon, 14 Dec 2020 13:59:16 -0700 Subject: [PATCH] feat!: use microgenerator (#239) See UPGRADING.md for a list of changes. --- dialogflow/create_document_test.py | 15 +++++---- dialogflow/create_knowledge_base_test.py | 6 ++-- dialogflow/detect_intent_audio.py | 19 ++++++----- dialogflow/detect_intent_knowledge.py | 20 ++++++------ dialogflow/detect_intent_stream.py | 14 ++++---- dialogflow/detect_intent_texts.py | 8 ++--- .../detect_intent_with_sentiment_analysis.py | 13 ++++---- ...etect_intent_with_texttospeech_response.py | 19 ++++++----- dialogflow/document_management.py | 13 ++++---- dialogflow/intent_management.py | 32 +++++++++---------- dialogflow/intent_management_test.py | 3 +- dialogflow/knowledge_base_management.py | 11 ++++--- 12 files changed, 93 insertions(+), 80 deletions(-) diff --git a/dialogflow/create_document_test.py b/dialogflow/create_document_test.py index 441996ba9fd5..7099eed41ea1 100644 --- a/dialogflow/create_document_test.py +++ b/dialogflow/create_document_test.py @@ -17,7 +17,7 @@ import os import uuid -import dialogflow_v2beta1 as dialogflow +from google.cloud import dialogflow_v2beta1 import pytest import document_management @@ -31,11 +31,11 @@ @pytest.fixture(scope="function", autouse=True) def setup_teardown(): # Create a knowledge base to use in document management - client = dialogflow.KnowledgeBasesClient() - project_path = client.project_path(PROJECT_ID) - knowledge_base = dialogflow.types.KnowledgeBase( + client = dialogflow_v2beta1.KnowledgeBasesClient() + project_path = client.common_project_path(PROJECT_ID) + knowledge_base = dialogflow_v2beta1.KnowledgeBase( display_name=KNOWLEDGE_BASE_NAME) - response = client.create_knowledge_base(project_path, knowledge_base) + response = client.create_knowledge_base(parent=project_path, knowledge_base=knowledge_base) pytest.KNOWLEDGE_BASE_ID = response.name.split( '/knowledgeBases/')[1].split('\n')[0] @@ -44,7 +44,10 @@ def setup_teardown(): # Delete the created knowledge base knowledge_base_path = client.knowledge_base_path( PROJECT_ID, pytest.KNOWLEDGE_BASE_ID) - client.delete_knowledge_base(knowledge_base_path, force=True) + request = dialogflow_v2beta1.DeleteKnowledgeBaseRequest( + name=knowledge_base_path, force=True + ) + client.delete_knowledge_base(request=request) @pytest.mark.flaky(max_runs=3, min_passes=1) diff --git a/dialogflow/create_knowledge_base_test.py b/dialogflow/create_knowledge_base_test.py index 2230099beb01..601a62047a75 100644 --- a/dialogflow/create_knowledge_base_test.py +++ b/dialogflow/create_knowledge_base_test.py @@ -17,7 +17,7 @@ import os import uuid -import dialogflow_v2beta1 as dialogflow +from google.cloud import dialogflow_v2beta1 import pytest import knowledge_base_management @@ -32,11 +32,11 @@ def teardown(): yield # Delete the created knowledge base - client = dialogflow.KnowledgeBasesClient() + client = dialogflow_v2beta1.KnowledgeBasesClient() assert pytest.KNOWLEDGE_BASE_ID is not None knowledge_base_path = client.knowledge_base_path( PROJECT_ID, pytest.KNOWLEDGE_BASE_ID) - client.delete_knowledge_base(knowledge_base_path) + client.delete_knowledge_base(name=knowledge_base_path) def test_create_knowledge_base(capsys): diff --git a/dialogflow/detect_intent_audio.py b/dialogflow/detect_intent_audio.py index 70fc370edd7b..0e1794334c22 100644 --- a/dialogflow/detect_intent_audio.py +++ b/dialogflow/detect_intent_audio.py @@ -37,12 +37,12 @@ def detect_intent_audio(project_id, session_id, audio_file_path, Using the same `session_id` between requests allows continuation of the conversation.""" - import dialogflow_v2 as dialogflow + from google.cloud import 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 + audio_encoding = dialogflow.AudioEncoding.AUDIO_ENCODING_LINEAR_16 sample_rate_hertz = 16000 session = session_client.session_path(project_id, session_id) @@ -51,14 +51,17 @@ def detect_intent_audio(project_id, session_id, audio_file_path, with open(audio_file_path, 'rb') as audio_file: input_audio = audio_file.read() - audio_config = dialogflow.types.InputAudioConfig( + audio_config = dialogflow.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) + query_input = dialogflow.QueryInput(audio_config=audio_config) + + request = dialogflow.DetectIntentRequest( + session=session, + query_input=query_input, + input_audio=input_audio, + ) + response = session_client.detect_intent(request=request) print('=' * 20) print('Query text: {}'.format(response.query_result.query_text)) diff --git a/dialogflow/detect_intent_knowledge.py b/dialogflow/detect_intent_knowledge.py index edfd4b517ec8..56b9a1e0091b 100644 --- a/dialogflow/detect_intent_knowledge.py +++ b/dialogflow/detect_intent_knowledge.py @@ -40,28 +40,30 @@ def detect_intent_knowledge(project_id, session_id, language_code, knowledge_base_id: The Knowledge base's id to query against. texts: A list of text queries to send. """ - import dialogflow_v2beta1 as dialogflow + from google.cloud import dialogflow_v2beta1 as dialogflow session_client = dialogflow.SessionsClient() session_path = session_client.session_path(project_id, session_id) print('Session path: {}\n'.format(session_path)) for text in texts: - text_input = dialogflow.types.TextInput( + text_input = dialogflow.TextInput( text=text, language_code=language_code) - query_input = dialogflow.types.QueryInput(text=text_input) + query_input = dialogflow.QueryInput(text=text_input) - knowledge_base_path = dialogflow.knowledge_bases_client \ - .KnowledgeBasesClient \ + knowledge_base_path = dialogflow.KnowledgeBasesClient \ .knowledge_base_path(project_id, knowledge_base_id) - query_params = dialogflow.types.QueryParameters( + query_params = dialogflow.QueryParameters( knowledge_base_names=[knowledge_base_path]) - response = session_client.detect_intent( - session=session_path, query_input=query_input, - query_params=query_params) + request = dialogflow.DetectIntentRequest( + session=session_path, + query_input=query_input, + query_params=query_params + ) + response = session_client.detect_intent(request=request) print('=' * 20) print('Query text: {}'.format(response.query_result.query_text)) diff --git a/dialogflow/detect_intent_stream.py b/dialogflow/detect_intent_stream.py index 535358fd9a2e..df3d33b111b6 100644 --- a/dialogflow/detect_intent_stream.py +++ b/dialogflow/detect_intent_stream.py @@ -36,21 +36,21 @@ def detect_intent_stream(project_id, session_id, audio_file_path, Using the same `session_id` between requests allows continuation of the conversation.""" - import dialogflow_v2 as dialogflow + from google.cloud import 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 + audio_encoding = dialogflow.AudioEncoding.AUDIO_ENCODING_LINEAR_16 sample_rate_hertz = 16000 session_path = session_client.session_path(project_id, session_id) print('Session path: {}\n'.format(session_path)) def request_generator(audio_config, audio_file_path): - query_input = dialogflow.types.QueryInput(audio_config=audio_config) + query_input = dialogflow.QueryInput(audio_config=audio_config) # The first request contains the configuration. - yield dialogflow.types.StreamingDetectIntentRequest( + yield dialogflow.StreamingDetectIntentRequest( session=session_path, query_input=query_input) # Here we are reading small chunks of audio data from a local @@ -62,15 +62,15 @@ def request_generator(audio_config, audio_file_path): if not chunk: break # The later requests contains audio data. - yield dialogflow.types.StreamingDetectIntentRequest( + yield dialogflow.StreamingDetectIntentRequest( input_audio=chunk) - audio_config = dialogflow.types.InputAudioConfig( + audio_config = dialogflow.InputAudioConfig( audio_encoding=audio_encoding, language_code=language_code, sample_rate_hertz=sample_rate_hertz) requests = request_generator(audio_config, audio_file_path) - responses = session_client.streaming_detect_intent(requests) + responses = session_client.streaming_detect_intent(requests=requests) print('=' * 20) for response in responses: diff --git a/dialogflow/detect_intent_texts.py b/dialogflow/detect_intent_texts.py index fba07904ecb6..8630b519ec3b 100644 --- a/dialogflow/detect_intent_texts.py +++ b/dialogflow/detect_intent_texts.py @@ -36,20 +36,20 @@ def detect_intent_texts(project_id, session_id, texts, language_code): Using the same `session_id` between requests allows continuation of the conversation.""" - import dialogflow_v2 as dialogflow + from google.cloud import dialogflow session_client = dialogflow.SessionsClient() session = session_client.session_path(project_id, session_id) print('Session path: {}\n'.format(session)) for text in texts: - text_input = dialogflow.types.TextInput( + text_input = dialogflow.TextInput( text=text, language_code=language_code) - query_input = dialogflow.types.QueryInput(text=text_input) + query_input = dialogflow.QueryInput(text=text_input) response = session_client.detect_intent( - session=session, query_input=query_input) + request={'session': session, 'query_input': query_input}) print('=' * 20) print('Query text: {}'.format(response.query_result.query_text)) diff --git a/dialogflow/detect_intent_with_sentiment_analysis.py b/dialogflow/detect_intent_with_sentiment_analysis.py index 8520460738c9..c3a6e3121d63 100644 --- a/dialogflow/detect_intent_with_sentiment_analysis.py +++ b/dialogflow/detect_intent_with_sentiment_analysis.py @@ -35,29 +35,28 @@ def detect_intent_with_sentiment_analysis(project_id, session_id, texts, Using the same `session_id` between requests allows continuation of the conversation.""" - import dialogflow_v2 as dialogflow + from google.cloud import dialogflow session_client = dialogflow.SessionsClient() session_path = session_client.session_path(project_id, session_id) print('Session path: {}\n'.format(session_path)) for text in texts: - text_input = dialogflow.types.TextInput( + text_input = dialogflow.TextInput( text=text, language_code=language_code) - query_input = dialogflow.types.QueryInput(text=text_input) + query_input = dialogflow.QueryInput(text=text_input) # Enable sentiment analysis - sentiment_config = dialogflow.types.SentimentAnalysisRequestConfig( + sentiment_config = dialogflow.SentimentAnalysisRequestConfig( analyze_query_text_sentiment=True) # Set the query parameters with sentiment analysis - query_params = dialogflow.types.QueryParameters( + query_params = dialogflow.QueryParameters( sentiment_analysis_request_config=sentiment_config) response = session_client.detect_intent( - session=session_path, query_input=query_input, - query_params=query_params) + request={'session': session_path, 'query_input': query_input, 'query_params': query_params}) print('=' * 20) print('Query text: {}'.format(response.query_result.query_text)) diff --git a/dialogflow/detect_intent_with_texttospeech_response.py b/dialogflow/detect_intent_with_texttospeech_response.py index 5c002124150a..630166d79d9f 100644 --- a/dialogflow/detect_intent_with_texttospeech_response.py +++ b/dialogflow/detect_intent_with_texttospeech_response.py @@ -34,26 +34,29 @@ def detect_intent_with_texttospeech_response(project_id, session_id, texts, Using the same `session_id` between requests allows continuation of the conversation.""" - import dialogflow_v2 as dialogflow + from google.cloud import dialogflow session_client = dialogflow.SessionsClient() session_path = session_client.session_path(project_id, session_id) print('Session path: {}\n'.format(session_path)) for text in texts: - text_input = dialogflow.types.TextInput( + text_input = dialogflow.TextInput( text=text, language_code=language_code) - query_input = dialogflow.types.QueryInput(text=text_input) + query_input = dialogflow.QueryInput(text=text_input) # Set the query parameters with sentiment analysis - output_audio_config = dialogflow.types.OutputAudioConfig( - audio_encoding=dialogflow.enums.OutputAudioEncoding + output_audio_config = dialogflow.OutputAudioConfig( + audio_encoding=dialogflow.OutputAudioEncoding .OUTPUT_AUDIO_ENCODING_LINEAR_16) - response = session_client.detect_intent( - session=session_path, query_input=query_input, - output_audio_config=output_audio_config) + request = dialogflow.DetectIntentRequest( + session=session_path, + query_input=query_input, + output_audio_config=output_audio_config + ) + response = session_client.detect_intent(request=request) print('=' * 20) print('Query text: {}'.format(response.query_result.query_text)) diff --git a/dialogflow/document_management.py b/dialogflow/document_management.py index c7ed6f76f004..2823bd495b22 100644 --- a/dialogflow/document_management.py +++ b/dialogflow/document_management.py @@ -46,19 +46,20 @@ def create_document(project_id, knowledge_base_id, display_name, mime_type, EXTRACTIVE_QA. content_uri: Uri of the document, e.g. gs://path/mydoc.csv, http://mypage.com/faq.html.""" - import dialogflow_v2beta1 as dialogflow + from google.cloud import dialogflow_v2beta1 as dialogflow client = dialogflow.DocumentsClient() - knowledge_base_path = client.knowledge_base_path(project_id, - knowledge_base_id) + knowledge_base_path = dialogflow.KnowledgeBasesClient.knowledge_base_path( + project_id, knowledge_base_id) - document = dialogflow.types.Document( + document = dialogflow.Document( display_name=display_name, mime_type=mime_type, content_uri=content_uri) document.knowledge_types.append( - dialogflow.types.Document.KnowledgeType.Value(knowledge_type)) + getattr(dialogflow.Document.KnowledgeType, knowledge_type) + ) - response = client.create_document(knowledge_base_path, document) + response = client.create_document(parent=knowledge_base_path, document=document) print('Waiting for results...') document = response.result(timeout=120) print('Created Document:') diff --git a/dialogflow/intent_management.py b/dialogflow/intent_management.py index 853e191c1a18..a1a4d0993b41 100644 --- a/dialogflow/intent_management.py +++ b/dialogflow/intent_management.py @@ -32,12 +32,12 @@ # [START dialogflow_list_intents] def list_intents(project_id): - import dialogflow_v2 as dialogflow + from google.cloud import dialogflow intents_client = dialogflow.IntentsClient() - parent = intents_client.project_agent_path(project_id) + parent = dialogflow.AgentsClient.agent_path(project_id) - intents = intents_client.list_intents(parent) + intents = intents_client.list_intents(request={'parent': parent}) for intent in intents: print('=' * 20) @@ -63,27 +63,27 @@ def list_intents(project_id): def create_intent(project_id, display_name, training_phrases_parts, message_texts): """Create an intent of the given intent type.""" - import dialogflow_v2 as dialogflow + from google.cloud import dialogflow intents_client = dialogflow.IntentsClient() - parent = intents_client.project_agent_path(project_id) + parent = dialogflow.AgentsClient.agent_path(project_id) training_phrases = [] for training_phrases_part in training_phrases_parts: - part = dialogflow.types.Intent.TrainingPhrase.Part( + part = dialogflow.Intent.TrainingPhrase.Part( text=training_phrases_part) # Here we create a new training phrase for each provided part. - training_phrase = dialogflow.types.Intent.TrainingPhrase(parts=[part]) + training_phrase = dialogflow.Intent.TrainingPhrase(parts=[part]) training_phrases.append(training_phrase) - text = dialogflow.types.Intent.Message.Text(text=message_texts) - message = dialogflow.types.Intent.Message(text=text) + text = dialogflow.Intent.Message.Text(text=message_texts) + message = dialogflow.Intent.Message(text=text) - intent = dialogflow.types.Intent( + intent = dialogflow.Intent( display_name=display_name, training_phrases=training_phrases, messages=[message]) - response = intents_client.create_intent(parent, intent) + response = intents_client.create_intent(request={'parent': parent, 'intent': intent}) print('Intent created: {}'.format(response)) # [END dialogflow_create_intent] @@ -92,22 +92,22 @@ def create_intent(project_id, display_name, training_phrases_parts, # [START dialogflow_delete_intent] def delete_intent(project_id, intent_id): """Delete intent with the given intent type and intent value.""" - import dialogflow_v2 as dialogflow + from google.cloud import dialogflow intents_client = dialogflow.IntentsClient() intent_path = intents_client.intent_path(project_id, intent_id) - intents_client.delete_intent(intent_path) + intents_client.delete_intent(request={'name': intent_path}) # [END dialogflow_delete_intent] # Helper to get intent from display name. def _get_intent_ids(project_id, display_name): - import dialogflow_v2 as dialogflow + from google.cloud import dialogflow intents_client = dialogflow.IntentsClient() - parent = intents_client.project_agent_path(project_id) - intents = intents_client.list_intents(parent) + parent = dialogflow.AgentsClient.agent_path(project_id) + intents = intents_client.list_intents(request={'parent': parent}) intent_names = [ intent.name for intent in intents if intent.display_name == display_name] diff --git a/dialogflow/intent_management_test.py b/dialogflow/intent_management_test.py index f1e5b3e8b26a..d47cebc44a29 100644 --- a/dialogflow/intent_management_test.py +++ b/dialogflow/intent_management_test.py @@ -33,8 +33,7 @@ def test_create_intent(capsys): intent_management.create_intent( - PROJECT_ID, INTENT_DISPLAY_NAME, TRAINING_PHRASE_PARTS, - MESSAGE_TEXTS) + PROJECT_ID, INTENT_DISPLAY_NAME, TRAINING_PHRASE_PARTS, MESSAGE_TEXTS) intent_ids = intent_management._get_intent_ids( PROJECT_ID, INTENT_DISPLAY_NAME) diff --git a/dialogflow/knowledge_base_management.py b/dialogflow/knowledge_base_management.py index 070f04d02ca5..699217d94b49 100644 --- a/dialogflow/knowledge_base_management.py +++ b/dialogflow/knowledge_base_management.py @@ -32,14 +32,17 @@ def create_knowledge_base(project_id, display_name): Args: project_id: The GCP project linked with the agent. display_name: The display name of the Knowledge base.""" - import dialogflow_v2beta1 as dialogflow + from google.cloud import dialogflow_v2beta1 as dialogflow client = dialogflow.KnowledgeBasesClient() - project_path = client.project_path(project_id) + project_path = client.common_project_path(project_id) - knowledge_base = dialogflow.types.KnowledgeBase( + knowledge_base = dialogflow.KnowledgeBase( display_name=display_name) - response = client.create_knowledge_base(project_path, knowledge_base) + response = client.create_knowledge_base( + parent=project_path, + knowledge_base=knowledge_base + ) print('Knowledge Base created:\n') print('Display Name: {}\n'.format(response.display_name))