From 0149d884b8fdff653bf95f77fe5e4f484dabc276 Mon Sep 17 00:00:00 2001 From: wuyuexin Date: Thu, 10 Dec 2020 16:03:12 -0800 Subject: [PATCH] Sample code update for regionalization (#29) * Modify sample code to support regional endpoints * format codes Co-authored-by: Yuexin Wu --- Dialogflow-CX/detect_intent_audio.py | 11 ++++++++++- Dialogflow-CX/detect_intent_audio_test.py | 11 +++++++++++ Dialogflow-CX/detect_intent_stream.py | 11 ++++++++++- Dialogflow-CX/detect_intent_stream_test.py | 12 ++++++++++++ Dialogflow-CX/detect_intent_texts.py | 11 ++++++++++- Dialogflow-CX/detect_intent_texts_test.py | 11 +++++++++++ Dialogflow-CX/noxfile_config.py | 3 +-- 7 files changed, 65 insertions(+), 5 deletions(-) diff --git a/Dialogflow-CX/detect_intent_audio.py b/Dialogflow-CX/detect_intent_audio.py index 2bb1fa1e382f..4777c7fec33e 100644 --- a/Dialogflow-CX/detect_intent_audio.py +++ b/Dialogflow-CX/detect_intent_audio.py @@ -25,6 +25,7 @@ import argparse import uuid +from google.cloud.dialogflowcx_v3beta1.services.agents import AgentsClient from google.cloud.dialogflowcx_v3beta1.services.sessions import SessionsClient from google.cloud.dialogflowcx_v3beta1.types import audio_config from google.cloud.dialogflowcx_v3beta1.types import session @@ -34,6 +35,7 @@ def run_sample(): # TODO(developer): Replace these values when running the function project_id = "YOUR-PROJECT-ID" + # For more information about regionalization see https://cloud.google.com/dialogflow/cx/docs/how/region location_id = "YOUR-LOCATION-ID" # For more info on agents see https://cloud.google.com/dialogflow/cx/docs/concept/agent agent_id = "YOUR-AGENT-ID" @@ -52,9 +54,16 @@ def detect_intent_audio(agent, session_id, audio_file_path, language_code): Using the same `session_id` between requests allows continuation of the conversation.""" - session_client = SessionsClient() session_path = f"{agent}/sessions/{session_id}" print(f"Session path: {session_path}\n") + client_options = None + agent_components = AgentsClient.parse_agent_path(agent) + location_id = agent_components["location"] + if location_id != "global": + api_endpoint = f"{location_id}-dialogflow.googleapis.com:443" + print(f"API Endpoint: {api_endpoint}\n") + client_options = {"api_endpoint": api_endpoint} + session_client = SessionsClient(client_options=client_options) input_audio_config = audio_config.InputAudioConfig( audio_encoding=audio_config.AudioEncoding.AUDIO_ENCODING_LINEAR_16, diff --git a/Dialogflow-CX/detect_intent_audio_test.py b/Dialogflow-CX/detect_intent_audio_test.py index 720bf68cf6f1..f05154286f66 100644 --- a/Dialogflow-CX/detect_intent_audio_test.py +++ b/Dialogflow-CX/detect_intent_audio_test.py @@ -24,7 +24,11 @@ DIRNAME = os.path.realpath(os.path.dirname(__file__)) PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") AGENT_ID = os.getenv("AGENT_ID") +AGENT_ID_US_CENTRAL1 = os.getenv("AGENT_ID_US_CENTRAL1") AGENT = f"projects/{PROJECT_ID}/locations/global/agents/{AGENT_ID}" +AGENT_US_CENTRAL1 = ( + f"projects/{PROJECT_ID}/locations/us-central1/agents/{AGENT_ID_US_CENTRAL1}" +) SESSION_ID = uuid.uuid4() AUDIO_PATH = os.getenv("AUDIO_PATH") AUDIO = f"{DIRNAME}/{AUDIO_PATH}" @@ -35,3 +39,10 @@ def test_detect_intent_texts(capsys): out, _ = capsys.readouterr() assert "Response text: Hi! I'm the virtual flights agent." in out + + +def test_detect_intent_texts_regional(capsys): + detect_intent_audio(AGENT_US_CENTRAL1, SESSION_ID, AUDIO, "en-US") + out, _ = capsys.readouterr() + + assert "Response text: Hi! I'm the virtual flights agent." in out diff --git a/Dialogflow-CX/detect_intent_stream.py b/Dialogflow-CX/detect_intent_stream.py index ecf1ea536fc1..d2ad166eec30 100644 --- a/Dialogflow-CX/detect_intent_stream.py +++ b/Dialogflow-CX/detect_intent_stream.py @@ -25,6 +25,7 @@ import argparse import uuid +from google.cloud.dialogflowcx_v3beta1.services.agents import AgentsClient from google.cloud.dialogflowcx_v3beta1.services.sessions import SessionsClient from google.cloud.dialogflowcx_v3beta1.types import audio_config from google.cloud.dialogflowcx_v3beta1.types import session @@ -34,6 +35,7 @@ def run_sample(): # TODO(developer): Replace these values when running the function project_id = "YOUR-PROJECT-ID" + # For more information about regionalization see https://cloud.google.com/dialogflow/cx/docs/how/region location_id = "YOUR-LOCATION-ID" # For more info on agents see https://cloud.google.com/dialogflow/cx/docs/concept/agent agent_id = "YOUR-AGENT-ID" @@ -52,9 +54,16 @@ def detect_intent_stream(agent, session_id, audio_file_path, language_code): Using the same `session_id` between requests allows continuation of the conversation.""" - session_client = SessionsClient() session_path = f"{agent}/sessions/{session_id}" print(f"Session path: {session_path}\n") + client_options = None + agent_components = AgentsClient.parse_agent_path(agent) + location_id = agent_components["location"] + if location_id != "global": + api_endpoint = f"{location_id}-dialogflow.googleapis.com:443" + print(f"API Endpoint: {api_endpoint}\n") + client_options = {"api_endpoint": api_endpoint} + session_client = SessionsClient(client_options=client_options) input_audio_config = audio_config.InputAudioConfig( audio_encoding=audio_config.AudioEncoding.AUDIO_ENCODING_LINEAR_16, diff --git a/Dialogflow-CX/detect_intent_stream_test.py b/Dialogflow-CX/detect_intent_stream_test.py index 7501cfc1308d..7160b43c777d 100644 --- a/Dialogflow-CX/detect_intent_stream_test.py +++ b/Dialogflow-CX/detect_intent_stream_test.py @@ -24,7 +24,11 @@ DIRNAME = os.path.realpath(os.path.dirname(__file__)) PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") AGENT_ID = os.getenv("AGENT_ID") +AGENT_ID_US_CENTRAL1 = os.getenv("AGENT_ID_US_CENTRAL1") AGENT = f"projects/{PROJECT_ID}/locations/global/agents/{AGENT_ID}" +AGENT_US_CENTRAL1 = ( + f"projects/{PROJECT_ID}/locations/us-central1/agents/{AGENT_ID_US_CENTRAL1}" +) SESSION_ID = uuid.uuid4() AUDIO_PATH = os.getenv("AUDIO_PATH") AUDIO = f"{DIRNAME}/{AUDIO_PATH}" @@ -36,3 +40,11 @@ def test_detect_intent_texts(capsys): assert "Intermediate transcript:" in out assert "Response text: Hi! I'm the virtual flights agent." in out + + +def test_detect_intent_texts_regional(capsys): + detect_intent_stream(AGENT_US_CENTRAL1, SESSION_ID, AUDIO, "en-US") + out, _ = capsys.readouterr() + + assert "Intermediate transcript:" in out + assert "Response text: Hi! I'm the virtual flights agent." in out diff --git a/Dialogflow-CX/detect_intent_texts.py b/Dialogflow-CX/detect_intent_texts.py index b55f4bf2d1c0..e2b46f01c499 100644 --- a/Dialogflow-CX/detect_intent_texts.py +++ b/Dialogflow-CX/detect_intent_texts.py @@ -29,6 +29,7 @@ import argparse import uuid +from google.cloud.dialogflowcx_v3beta1.services.agents import AgentsClient from google.cloud.dialogflowcx_v3beta1.services.sessions import SessionsClient from google.cloud.dialogflowcx_v3beta1.types import session @@ -37,6 +38,7 @@ def run_sample(): # TODO(developer): Replace these values when running the function project_id = "YOUR-PROJECT-ID" + # For more information about regionalization see https://cloud.google.com/dialogflow/cx/docs/how/region location_id = "YOUR-LOCATION-ID" # For more info on agents see https://cloud.google.com/dialogflow/cx/docs/concept/agent agent_id = "YOUR-AGENT-ID" @@ -55,9 +57,16 @@ def detect_intent_texts(agent, session_id, texts, language_code): Using the same `session_id` between requests allows continuation of the conversation.""" - session_client = SessionsClient() session_path = f"{agent}/sessions/{session_id}" print(f"Session path: {session_path}\n") + client_options = None + agent_components = AgentsClient.parse_agent_path(agent) + location_id = agent_components["location"] + if location_id != "global": + api_endpoint = f"{location_id}-dialogflow.googleapis.com:443" + print(f"API Endpoint: {api_endpoint}\n") + client_options = {"api_endpoint": api_endpoint} + session_client = SessionsClient(client_options=client_options) for text in texts: text_input = session.TextInput(text=text) diff --git a/Dialogflow-CX/detect_intent_texts_test.py b/Dialogflow-CX/detect_intent_texts_test.py index 0cc263ae283d..7ca35c8afc25 100644 --- a/Dialogflow-CX/detect_intent_texts_test.py +++ b/Dialogflow-CX/detect_intent_texts_test.py @@ -26,6 +26,10 @@ AGENT = f"projects/{PROJECT_ID}/locations/global/agents/{AGENT_ID}" SESSION_ID = uuid.uuid4() TEXTS = ["hello", "book a flight"] +AGENT_ID_US_CENTRAL1 = os.getenv("AGENT_ID_US_CENTRAL1") +AGENT_US_CENTRAL1 = ( + f"projects/{PROJECT_ID}/locations/us-central1/agents/{AGENT_ID_US_CENTRAL1}" +) def test_detect_intent_texts(capsys): @@ -33,3 +37,10 @@ def test_detect_intent_texts(capsys): out, _ = capsys.readouterr() assert "Response text: I can help you find a ticket" in out + + +def test_detect_intent_texts_regional(capsys): + detect_intent_texts(AGENT_US_CENTRAL1, SESSION_ID, TEXTS, "en-US") + out, _ = capsys.readouterr() + + assert "Response text: I can help you find a ticket" in out diff --git a/Dialogflow-CX/noxfile_config.py b/Dialogflow-CX/noxfile_config.py index 21e864163d9b..6562b1479010 100644 --- a/Dialogflow-CX/noxfile_config.py +++ b/Dialogflow-CX/noxfile_config.py @@ -23,18 +23,17 @@ TEST_CONFIG_OVERRIDE = { # You can opt out from the test for specific Python versions. "ignored_versions": ["2.7"], - # An envvar key for determining the project id to use. Change it # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a # build specific Cloud project. You can also use your own string # to use your own Cloud project. # 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', "gcloud_project_env": "GOOGLE_CLOUD_PROJECT", - # A dictionary you want to inject into your test. Don't put any # secrets here. These values will override predefined values. "envs": { "AGENT_ID": "53516802-3e2a-4016-80b6-a3df0d240240", + "AGENT_ID_US_CENTRAL1": "edf8372c-c66a-4984-83ba-b85885e95e2a", "AUDIO_PATH": "resources/hello.wav", }, }