Skip to content

Commit

Permalink
Sample code update for regionalization (#29)
Browse files Browse the repository at this point in the history
* Modify sample code to support regional endpoints

* format codes

Co-authored-by: Yuexin Wu <[email protected]>
  • Loading branch information
wuyuexin and Yuexin Wu authored Dec 11, 2020
1 parent 28f0aac commit 0149d88
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 5 deletions.
11 changes: 10 additions & 1 deletion Dialogflow-CX/detect_intent_audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions Dialogflow-CX/detect_intent_audio_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -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
11 changes: 10 additions & 1 deletion Dialogflow-CX/detect_intent_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions Dialogflow-CX/detect_intent_stream_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -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
11 changes: 10 additions & 1 deletion Dialogflow-CX/detect_intent_texts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"
Expand All @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions Dialogflow-CX/detect_intent_texts_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,21 @@
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):
detect_intent_texts(AGENT, SESSION_ID, TEXTS, "en-US")
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
3 changes: 1 addition & 2 deletions Dialogflow-CX/noxfile_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
}

0 comments on commit 0149d88

Please sign in to comment.