Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/load start page intents #147

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -65,6 +65,7 @@ docs.metadata
# Virtual environment
env/
venv/
.python-version

coverage.xml
sponge_log.xml
57 changes: 48 additions & 9 deletions src/dfcx_scrapi/core/transition_route_groups.py
Original file line number Diff line number Diff line change
@@ -72,6 +72,8 @@ def __init__(
if agent_id:
self.agent_id = agent_id

self._get_agent_level_data_only = False

def _rg_temp_dict_update(self, temp_dict, element):
"""Modify the temp dict and return to dataframe function."""
element_dict = self.cx_object_to_dict(element)
@@ -278,10 +280,10 @@ def update_transition_route_group(
def route_groups_to_dataframe(
self, agent_id: str = None, rate_limit: float = 0.5
):
"""Extracts the Transition Route Groups from a given Agent and
"""Extracts the Flow Transition Route Groups from a given Agent and
returns key information about the Route Groups in a Pandas Dataframe

DFCX Route Groups exist as an Agent level resource, however they are
DFCX Route Groups exist as an Agent level resource, however they can
categorized by the Flow they are associated with. This method will
extract all Flows for the given agent, then use the Flow IDs to
extract all Route Groups per Flow. Once all Route Groups have been
@@ -309,18 +311,23 @@ def route_groups_to_dataframe(
all_pages_map = {}
all_rgs = []

for flow in flows_map:
all_pages_map.update(self.pages.get_pages_map(flow))
all_rgs.extend(self.list_transition_route_groups(flow))
time.sleep(rate_limit)
if self._get_agent_level_data_only:
all_rgs.extend(self.list_transition_route_groups(agent_id))
else:
for flow in flows_map:
all_pages_map.update(self.pages.get_pages_map(flow))
all_rgs.extend(self.list_transition_route_groups(flow))
time.sleep(rate_limit)

rows_list = []
for route_group in all_rgs:
flow = "/".join(route_group.name.split("/")[0:8])
if not self._get_agent_level_data_only:
flow = "/".join(route_group.name.split("/")[0:8])
for route in route_group.transition_routes:
temp_dict = {}

temp_dict.update({"flow": flows_map[flow]})
if not self._get_agent_level_data_only:
temp_dict.update({"flow": flows_map[flow]})
temp_dict.update({"route_group_name": route_group.display_name})

if route.target_page:
@@ -356,7 +363,39 @@ def route_groups_to_dataframe(
)

rows_list.append(temp_dict)
final_dataframe = pd.DataFrame(
rows_list,
columns=[
"flow", "route_group_name", "target_page", "intent",
"condition", "webhook", "webhook_tag", "custom_payload",
"live_agent_handoff","conversation_success", "play_audio",
"output_audio_text", "fulfillment_message"]
)

final_dataframe = pd.DataFrame(rows_list)
return final_dataframe

def agent_route_groups_to_dataframe(
self, agent_id: str = None, rate_limit: float = 0.5
):
"""Extracts the Transition Route Groups from a given Agent and
returns key information about the Route Groups in a Pandas Dataframe

This method will extract all Agent Level Route Groups and convert
the DFCX object to a Pandas Dataframe and return this to the user.

Args:
agent_id: the Agent ID string in the following format:
projects/<project_id>/locations/<location_id>/agents/<agent_id>
rate_limit: Time in seconds to wait between each API call. Use this
to control hitting Quota limits on your project.

Returns:
a Pandas Dataframe with columns: route_group_name, target_page,
intent, condition, webhook, webhook_tag, custom_payload,
live_agent_handoff, conversation_success, play_audio,
output_audio_text, fulfillment_message
"""
self._get_agent_level_data_only = True
final_dataframe = self.route_groups_to_dataframe(agent_id, rate_limit)
self._get_agent_level_data_only = False
return final_dataframe
42 changes: 24 additions & 18 deletions src/dfcx_scrapi/tools/nlu_util.py
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
import numpy as np
import pandas as pd
import scann
@@ -29,9 +30,9 @@
from dfcx_scrapi.core import scrapi_base
from dfcx_scrapi.core import transition_route_groups

from google.colab import data_table

data_table.enable_dataframe_formatter()
if "google.colab" in sys.modules:
from google.colab import data_table
data_table.enable_dataframe_formatter()

SHEETS_SCOPE = [
"https://spreadsheets.google.com/feeds",
@@ -129,22 +130,26 @@ def _load_data(
flow_display_name, agent_id
)

page_loader = pages.Pages(creds=self.creds)
page_map = page_loader.get_pages_map(self.flow.name, reverse=True)
page_id = page_map.get(page_display_name, None)
if page_id is None:
raise ValueError(
f'Page "{page_display_name}" does not exist in the '
"specified agent."
)
self.page = page_loader.get_page(page_id)

trg_loader = transition_route_groups.TransitionRouteGroups(
creds=self.creds
)
self.trgs = trg_loader.list_transition_route_groups(self.flow.name)
agent_trgs = trg_loader.list_transition_route_groups(agent_id)
self.trgs = (
agent_trgs + trg_loader.list_transition_route_groups(self.flow.name)
)
self.name_to_trg = {i.name: i for i in self.trgs}

if page_display_name != "Start Page":
kmaphoenix marked this conversation as resolved.
Show resolved Hide resolved
page_loader = pages.Pages(creds=self.creds)
page_map = page_loader.get_pages_map(self.flow.name, reverse=True)
page_id = page_map.get(page_display_name, None)
if page_id is None:
raise ValueError(
f'Page "{page_display_name}" does not exist in the '
"specified agent."
)
self.page = page_loader.get_page(page_id)

intent_names = self._list_page_intents()
all_intents = intents.Intents(creds=self.creds).list_intents(agent_id)
self.intents = list(
@@ -160,10 +165,11 @@ def _list_page_intents(self) -> Set[str]:
for tr in self.flow.transition_routes:
if tr.intent:
relevant_intents.add(tr.intent)
relevant_trgs |= set(self.page.transition_route_groups)
for tr in self.page.transition_routes:
if tr.intent:
relevant_intents.add(tr.intent)
if hasattr(self, "page"):
relevant_trgs |= set(self.page.transition_route_groups)
for tr in self.page.transition_routes:
if tr.intent:
relevant_intents.add(tr.intent)
for trg_name in relevant_trgs:
trg = self.name_to_trg[trg_name]
for tr in trg.transition_routes: