Skip to content

Commit 0149d88

Browse files
wuyuexinYuexin Wu
and
Yuexin Wu
authored
Sample code update for regionalization (#29)
* Modify sample code to support regional endpoints * format codes Co-authored-by: Yuexin Wu <[email protected]>
1 parent 28f0aac commit 0149d88

7 files changed

+65
-5
lines changed

Dialogflow-CX/detect_intent_audio.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import argparse
2626
import uuid
2727

28+
from google.cloud.dialogflowcx_v3beta1.services.agents import AgentsClient
2829
from google.cloud.dialogflowcx_v3beta1.services.sessions import SessionsClient
2930
from google.cloud.dialogflowcx_v3beta1.types import audio_config
3031
from google.cloud.dialogflowcx_v3beta1.types import session
@@ -34,6 +35,7 @@
3435
def run_sample():
3536
# TODO(developer): Replace these values when running the function
3637
project_id = "YOUR-PROJECT-ID"
38+
# For more information about regionalization see https://cloud.google.com/dialogflow/cx/docs/how/region
3739
location_id = "YOUR-LOCATION-ID"
3840
# For more info on agents see https://cloud.google.com/dialogflow/cx/docs/concept/agent
3941
agent_id = "YOUR-AGENT-ID"
@@ -52,9 +54,16 @@ def detect_intent_audio(agent, session_id, audio_file_path, language_code):
5254
5355
Using the same `session_id` between requests allows continuation
5456
of the conversation."""
55-
session_client = SessionsClient()
5657
session_path = f"{agent}/sessions/{session_id}"
5758
print(f"Session path: {session_path}\n")
59+
client_options = None
60+
agent_components = AgentsClient.parse_agent_path(agent)
61+
location_id = agent_components["location"]
62+
if location_id != "global":
63+
api_endpoint = f"{location_id}-dialogflow.googleapis.com:443"
64+
print(f"API Endpoint: {api_endpoint}\n")
65+
client_options = {"api_endpoint": api_endpoint}
66+
session_client = SessionsClient(client_options=client_options)
5867

5968
input_audio_config = audio_config.InputAudioConfig(
6069
audio_encoding=audio_config.AudioEncoding.AUDIO_ENCODING_LINEAR_16,

Dialogflow-CX/detect_intent_audio_test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424
DIRNAME = os.path.realpath(os.path.dirname(__file__))
2525
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
2626
AGENT_ID = os.getenv("AGENT_ID")
27+
AGENT_ID_US_CENTRAL1 = os.getenv("AGENT_ID_US_CENTRAL1")
2728
AGENT = f"projects/{PROJECT_ID}/locations/global/agents/{AGENT_ID}"
29+
AGENT_US_CENTRAL1 = (
30+
f"projects/{PROJECT_ID}/locations/us-central1/agents/{AGENT_ID_US_CENTRAL1}"
31+
)
2832
SESSION_ID = uuid.uuid4()
2933
AUDIO_PATH = os.getenv("AUDIO_PATH")
3034
AUDIO = f"{DIRNAME}/{AUDIO_PATH}"
@@ -35,3 +39,10 @@ def test_detect_intent_texts(capsys):
3539
out, _ = capsys.readouterr()
3640

3741
assert "Response text: Hi! I'm the virtual flights agent." in out
42+
43+
44+
def test_detect_intent_texts_regional(capsys):
45+
detect_intent_audio(AGENT_US_CENTRAL1, SESSION_ID, AUDIO, "en-US")
46+
out, _ = capsys.readouterr()
47+
48+
assert "Response text: Hi! I'm the virtual flights agent." in out

Dialogflow-CX/detect_intent_stream.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import argparse
2626
import uuid
2727

28+
from google.cloud.dialogflowcx_v3beta1.services.agents import AgentsClient
2829
from google.cloud.dialogflowcx_v3beta1.services.sessions import SessionsClient
2930
from google.cloud.dialogflowcx_v3beta1.types import audio_config
3031
from google.cloud.dialogflowcx_v3beta1.types import session
@@ -34,6 +35,7 @@
3435
def run_sample():
3536
# TODO(developer): Replace these values when running the function
3637
project_id = "YOUR-PROJECT-ID"
38+
# For more information about regionalization see https://cloud.google.com/dialogflow/cx/docs/how/region
3739
location_id = "YOUR-LOCATION-ID"
3840
# For more info on agents see https://cloud.google.com/dialogflow/cx/docs/concept/agent
3941
agent_id = "YOUR-AGENT-ID"
@@ -52,9 +54,16 @@ def detect_intent_stream(agent, session_id, audio_file_path, language_code):
5254
5355
Using the same `session_id` between requests allows continuation
5456
of the conversation."""
55-
session_client = SessionsClient()
5657
session_path = f"{agent}/sessions/{session_id}"
5758
print(f"Session path: {session_path}\n")
59+
client_options = None
60+
agent_components = AgentsClient.parse_agent_path(agent)
61+
location_id = agent_components["location"]
62+
if location_id != "global":
63+
api_endpoint = f"{location_id}-dialogflow.googleapis.com:443"
64+
print(f"API Endpoint: {api_endpoint}\n")
65+
client_options = {"api_endpoint": api_endpoint}
66+
session_client = SessionsClient(client_options=client_options)
5867

5968
input_audio_config = audio_config.InputAudioConfig(
6069
audio_encoding=audio_config.AudioEncoding.AUDIO_ENCODING_LINEAR_16,

Dialogflow-CX/detect_intent_stream_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424
DIRNAME = os.path.realpath(os.path.dirname(__file__))
2525
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
2626
AGENT_ID = os.getenv("AGENT_ID")
27+
AGENT_ID_US_CENTRAL1 = os.getenv("AGENT_ID_US_CENTRAL1")
2728
AGENT = f"projects/{PROJECT_ID}/locations/global/agents/{AGENT_ID}"
29+
AGENT_US_CENTRAL1 = (
30+
f"projects/{PROJECT_ID}/locations/us-central1/agents/{AGENT_ID_US_CENTRAL1}"
31+
)
2832
SESSION_ID = uuid.uuid4()
2933
AUDIO_PATH = os.getenv("AUDIO_PATH")
3034
AUDIO = f"{DIRNAME}/{AUDIO_PATH}"
@@ -36,3 +40,11 @@ def test_detect_intent_texts(capsys):
3640

3741
assert "Intermediate transcript:" in out
3842
assert "Response text: Hi! I'm the virtual flights agent." in out
43+
44+
45+
def test_detect_intent_texts_regional(capsys):
46+
detect_intent_stream(AGENT_US_CENTRAL1, SESSION_ID, AUDIO, "en-US")
47+
out, _ = capsys.readouterr()
48+
49+
assert "Intermediate transcript:" in out
50+
assert "Response text: Hi! I'm the virtual flights agent." in out

Dialogflow-CX/detect_intent_texts.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import argparse
3030
import uuid
3131

32+
from google.cloud.dialogflowcx_v3beta1.services.agents import AgentsClient
3233
from google.cloud.dialogflowcx_v3beta1.services.sessions import SessionsClient
3334
from google.cloud.dialogflowcx_v3beta1.types import session
3435

@@ -37,6 +38,7 @@
3738
def run_sample():
3839
# TODO(developer): Replace these values when running the function
3940
project_id = "YOUR-PROJECT-ID"
41+
# For more information about regionalization see https://cloud.google.com/dialogflow/cx/docs/how/region
4042
location_id = "YOUR-LOCATION-ID"
4143
# For more info on agents see https://cloud.google.com/dialogflow/cx/docs/concept/agent
4244
agent_id = "YOUR-AGENT-ID"
@@ -55,9 +57,16 @@ def detect_intent_texts(agent, session_id, texts, language_code):
5557
5658
Using the same `session_id` between requests allows continuation
5759
of the conversation."""
58-
session_client = SessionsClient()
5960
session_path = f"{agent}/sessions/{session_id}"
6061
print(f"Session path: {session_path}\n")
62+
client_options = None
63+
agent_components = AgentsClient.parse_agent_path(agent)
64+
location_id = agent_components["location"]
65+
if location_id != "global":
66+
api_endpoint = f"{location_id}-dialogflow.googleapis.com:443"
67+
print(f"API Endpoint: {api_endpoint}\n")
68+
client_options = {"api_endpoint": api_endpoint}
69+
session_client = SessionsClient(client_options=client_options)
6170

6271
for text in texts:
6372
text_input = session.TextInput(text=text)

Dialogflow-CX/detect_intent_texts_test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,21 @@
2626
AGENT = f"projects/{PROJECT_ID}/locations/global/agents/{AGENT_ID}"
2727
SESSION_ID = uuid.uuid4()
2828
TEXTS = ["hello", "book a flight"]
29+
AGENT_ID_US_CENTRAL1 = os.getenv("AGENT_ID_US_CENTRAL1")
30+
AGENT_US_CENTRAL1 = (
31+
f"projects/{PROJECT_ID}/locations/us-central1/agents/{AGENT_ID_US_CENTRAL1}"
32+
)
2933

3034

3135
def test_detect_intent_texts(capsys):
3236
detect_intent_texts(AGENT, SESSION_ID, TEXTS, "en-US")
3337
out, _ = capsys.readouterr()
3438

3539
assert "Response text: I can help you find a ticket" in out
40+
41+
42+
def test_detect_intent_texts_regional(capsys):
43+
detect_intent_texts(AGENT_US_CENTRAL1, SESSION_ID, TEXTS, "en-US")
44+
out, _ = capsys.readouterr()
45+
46+
assert "Response text: I can help you find a ticket" in out

Dialogflow-CX/noxfile_config.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,17 @@
2323
TEST_CONFIG_OVERRIDE = {
2424
# You can opt out from the test for specific Python versions.
2525
"ignored_versions": ["2.7"],
26-
2726
# An envvar key for determining the project id to use. Change it
2827
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
2928
# build specific Cloud project. You can also use your own string
3029
# to use your own Cloud project.
3130
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
3231
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
33-
3432
# A dictionary you want to inject into your test. Don't put any
3533
# secrets here. These values will override predefined values.
3634
"envs": {
3735
"AGENT_ID": "53516802-3e2a-4016-80b6-a3df0d240240",
36+
"AGENT_ID_US_CENTRAL1": "edf8372c-c66a-4984-83ba-b85885e95e2a",
3837
"AUDIO_PATH": "resources/hello.wav",
3938
},
4039
}

0 commit comments

Comments
 (0)