Skip to content

Commit

Permalink
tutorial fixed and slightly simplified, made a minor change to Regexp…
Browse files Browse the repository at this point in the history
…Slot validation
  • Loading branch information
ZergLev committed Jan 10, 2025
1 parent 0f70b0f commit 550a437
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
2 changes: 1 addition & 1 deletion chatsky/slots/standard_slots.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class RegexpSlot(ValueSlot, frozen=True):
change the `match_group_idx` parameter.
"""

regexp: str
regexp: str | Pattern
"The regexp to search for in ctx.last_request.text"
match_group_idx: int = 0
"Index of the group to match."
Expand Down
38 changes: 17 additions & 21 deletions tutorials/slots/2_regexgroupslot_and_string_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
destinations as dst,
)

from chatsky.slots import RegexpSlot, RegexpGroupSlot, GroupSlot
from chatsky.slots import RegexpSlot, RegexpGroupSlot

from chatsky.utils.testing import (
check_happy_path,
Expand Down Expand Up @@ -65,25 +65,17 @@
"""

# %%
sub_slots_for_group_slot = {
SLOTS = {
"date": RegexpGroupSlot(
string_format="{day}/{month}/{year}",
regexp=r"(0?[1-9]|(?:1|2)[0-9]|3[0-1])[\.\/]"
r"(0?[1-9]|1[0-2])[\.\/](\d{4}|\d{2})",
groups={"day": 1, "month": 2, "year": 3},
string_format="{day}/{month}/{year}",
),
"email": RegexpSlot(
regexp=r"[\w\.-]+@[\w\.-]+\.\w{2,4}",
),
}
date_and_email = GroupSlot(
string_format="Your date of birth is {date}, email is {email}",
**sub_slots_for_group_slot
)

SLOTS = {
"date_and_email": date_and_email,
}

script = {
GLOBAL: {
Expand All @@ -96,39 +88,43 @@
},
"date_and_email_flow": {
"start": {
TRANSITIONS: [Tr(dst=("date_and_email_flow", "ask_date"))],
TRANSITIONS: [Tr(dst=("date_and_email_flow", "ask_email"))],
},
"fallback": {
RESPONSE: "Finishing query",
TRANSITIONS: [
Tr(dst=("date_and_email_flow", "ask_email")),
Tr(
dst=dst.Backward(),
cnd=cnd.Regexp(r"back", flags=re.IGNORECASE),
),
Tr(dst=("date_and_email_flow", "ask_email"), priority=0.8),
],
},
"ask_email": {
RESPONSE: "Write your email (my email is ...):",
PRE_TRANSITION: {"get_slot": proc.Extract("date_and_email.email")},
PRE_TRANSITION: {"get_slot": proc.Extract("email")},
TRANSITIONS: [
Tr(
dst="ask_email",
cnd=cnd.SlotsExtracted("date_and_email.email"),
dst="ask_date",
cnd=cnd.SlotsExtracted("email"),
)
],
},
"ask_date": {
RESPONSE: "Write your date of birth:",
PRE_TRANSITION: {"get_slot": proc.Extract("date_and_email.date")},
PRE_TRANSITION: {"get_slot": proc.Extract("date")},
TRANSITIONS: [
Tr(
dst="answer_node",
cnd=cnd.SlotsExtracted("date_and_email.date"),
cnd=cnd.SlotsExtracted("date"),
)
],
},
"answer_node": {RESPONSE: rsp.FilledTemplate("{date_and_email}")},
"answer_node": {
RESPONSE: rsp.FilledTemplate(
"Your date of birth is {date}, email is {email}"
)
},
},
}

Expand All @@ -147,8 +143,8 @@
# %%
pipeline = Pipeline(
script=script,
start_label=("root", "start"),
fallback_label=("root", "fallback"),
start_label=("date_and_email_flow", "start"),
fallback_label=("date_and_email_flow", "fallback"),
slots=SLOTS,
)

Expand Down

0 comments on commit 550a437

Please sign in to comment.