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

Reworked on DialogflowConversation class #223

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
95 changes: 63 additions & 32 deletions src/dfcx_scrapi/core/conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import uuid

from typing import Dict, Any
from operator import attrgetter
from threading import Thread

import pandas as pd
Expand All @@ -41,7 +40,6 @@

MAX_RETRIES = 3


class DialogflowConversation(scrapi_base.ScrapiBase):
"""Class that wraps the SessionsClient to hold end to end conversations
and maintain internal session state
Expand Down Expand Up @@ -165,17 +163,20 @@ def progress_bar(current, total, bar_length=50, type_="Progress"):
)

@staticmethod
def _build_query_params_object(parameters, current_page, disable_webhook):
def _build_query_params_object(
jkshj21 marked this conversation as resolved.
Show resolved Hide resolved
parameters,
current_page,
disable_webhook,
end_user_metadata):

query_params = types.session.QueryParameters(
disable_webhook=disable_webhook,
current_page=current_page,
)
if parameters:
query_params = types.session.QueryParameters(
disable_webhook=disable_webhook,
parameters=parameters,
current_page=current_page,
)
else:
query_params = types.session.QueryParameters(
disable_webhook=disable_webhook, current_page=current_page
)
query_params.parameters = parameters
if end_user_metadata:
query_params.end_user_metadata = end_user_metadata

return query_params

Expand Down Expand Up @@ -310,6 +311,7 @@ def _get_reply_results(self, utterance, page_id, results, i):
results: Pandas Dataframe to capture and store the results
i: Internal tracking for Python Threading
"""

response = self.reply(
send_obj={"text": utterance}, current_page=page_id, restart=True
)
Expand Down Expand Up @@ -339,10 +341,10 @@ def _get_intent_detection(self, test_set: pd.DataFrame):

self._validate_test_set_input(test_set_mapped)

threads = [None] * len(utterances)
threads = [None] * len(test_set_mapped)
results = {
"target_page": [None] * len(utterances),
"match":[None] * len(utterances),
"target_page": [None] * len(test_set_mapped),
"match":[None] * len(test_set_mapped),
}
for i, (utterance, page_id) in enumerate(zip(utterances, page_ids)):
threads[i] = Thread(
Expand All @@ -361,7 +363,6 @@ def _get_intent_detection(self, test_set: pd.DataFrame):

return intent_detection


def restart(self):
"""Starts a new session/conversation for this agent"""
self.session_id = uuid.uuid4()
Expand Down Expand Up @@ -399,7 +400,8 @@ def reply(
send_obj: Dictionary with the following structure:
{'text': str,
'params': Dict[str,str],
'dtmf': str}
'dtmf': str,
'end_user_metadata': Dict[str, str],}
restart: Boolean flag that determines whether to use the existing
session ID or start a new conversation with a new session ID.
Passing True will create a new session ID on subsequent calls.
Expand All @@ -416,9 +418,7 @@ def reply(
"""
text = send_obj.get("text")
send_params = send_obj.get("params")

if not text:
logging.warning(f"Input Text is empty. {send_obj}")
end_user_metadata = send_obj.get("end_user_metadata")
jkshj21 marked this conversation as resolved.
Show resolved Hide resolved

if text and len(text) > 256:
logging.warning(
Expand Down Expand Up @@ -451,7 +451,7 @@ def reply(

# Build Query Params object
query_params = self._build_query_params_object(
send_params, current_page, disable_webhook
send_params, current_page, disable_webhook, end_user_metadata
jkshj21 marked this conversation as resolved.
Show resolved Hide resolved
)

# Build Query Input object
Expand Down Expand Up @@ -479,7 +479,18 @@ def reply(
logging.error("text: %s", text)
logging.error("query_params: %s", query_params)
logging.error("query_input: %s", query_input)
return {}
return {
jkshj21 marked this conversation as resolved.
Show resolved Hide resolved
"response_messages": (
f"""---- ERROR --- InternalServerError caught on CX.detect,
{err}"""
),
"confidence": "",
"page_name": "",
"intent_name": "",
"match_type": "",
"match": None,
"params": "",
}

except core_exceptions.ClientError as err:
logging.error(
Expand All @@ -499,7 +510,18 @@ def reply(
return self.reply(send_obj, restart=restart, retries=retries)
else:
logging.error("MAX_RETRIES exceeded")
return {}
return {
"response_messages": (
f"""---- ERROR --- ClientError caught on CX.detect,
{err}"""
),
"confidence": "",
"page_name": "",
"intent_name": "",
"match_type": "",
"match": None,
"params": "",
}

if checkpoints:
self.checkpoint("<< got response")
Expand All @@ -523,7 +545,6 @@ def reply(
)
else:
params = None

reply["response_messages"] = response_messages
reply["confidence"] = query_result.intent_detection_confidence
reply["page_name"] = query_result.current_page.display_name
Expand Down Expand Up @@ -578,6 +599,8 @@ def run_intent_detection(
- NOTE, when using the Default Start Page of a Flow you must
define it as the special display name START_PAGE
utterance: str
inject_parameters (optional): str
end_user_metadata (optional): str
chunk_size: Determines the number of text requests to send in
parallel. This should be adjusted based on your test_set size and
the Quota limits set for your GCP project. Default is 300.
Expand All @@ -593,6 +616,9 @@ def run_intent_detection(
detected_intent: str
confidence: float
target_page: str
response_messages: str
match_type: str
parameters_set: str
"""

result = pd.DataFrame()
Expand All @@ -609,26 +635,32 @@ def run_intent_detection(

def _unpack_match(self, df: pd.DataFrame):
""" Unpacks a 'match' column into four component columns.
if a match column is None, then all four columns will be None.

Args:
df: dataframe containing a column named match of types.Match

Returns:
A copy of df with columns match_type, confidence, parameters_set,
and detected_intent instead of match.

"""
df = (
df
.copy()
return (
jkshj21 marked this conversation as resolved.
Show resolved Hide resolved
df.copy()
.assign(
match_type = lambda df: df.match.apply(
attrgetter("match_type._name_")),
# pylint: disable=W0212
lambda m: m.match_type._name_ if m else ""
),
confidence = lambda df: df.match.apply(
attrgetter("confidence")),
lambda m: m.confidence if m else ""
),
parameters_set = lambda df: df.match.apply(
attrgetter("parameters")),
lambda m: m.parameters if m else ""
),
detected_intent = lambda df: df.match.apply(
attrgetter("intent.display_name"))
lambda m: m.intent.display_name if m else ""
)
)
.assign(
parameters_set = lambda df: df.parameters_set.apply(
Expand All @@ -637,4 +669,3 @@ def _unpack_match(self, df: pd.DataFrame):
)
.drop(columns="match")
)
return df
Loading