Skip to content

Commit

Permalink
renamed to collect
Browse files Browse the repository at this point in the history
  • Loading branch information
tmbo committed Sep 19, 2023
1 parent 87e2858 commit 64beeaf
Show file tree
Hide file tree
Showing 19 changed files with 136 additions and 154 deletions.
8 changes: 4 additions & 4 deletions rasa/cli/project_templates/dm2/data/flows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ flows:
description: greet the user and ask how they are doing. cheer them up if needed.
steps:
- id: "0"
collect_information: good_mood
collect: good_mood
description: "can be true or false"
next:
- if: good_mood
Expand All @@ -30,13 +30,13 @@ flows:
description: This flow recommends a restaurant
steps:
- id: "0"
collect_information: cuisine
collect: cuisine
next: "1"
- id: "1"
collect_information: price_range
collect: price_range
next: "2"
- id: "2"
collect_information: city
collect: city
next: "3"
- id: "3"
action: utter_recommend_restaurant
Expand Down
4 changes: 2 additions & 2 deletions rasa/cli/project_templates/tutorial/data/flows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ flows:
description: This flow lets users send money to friends and family.
steps:
- id: "ask_recipient"
collect_information: recipient
collect: recipient
next: "ask_amount"
- id: "ask_amount"
collect_information: amount
collect: amount
next: "transfer_successful"
- id: "transfer_successful"
action: utter_transfer_complete
10 changes: 5 additions & 5 deletions rasa/core/channels/chat.html
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ <h2>Happy chatting!</h2>
title: ${flow?.id ?? "No Flow"}
---
flowchart TD
classDef collect_information stroke-width:1px
classDef collect stroke-width:1px
classDef action fill:#FBFCFD,stroke:#A0B8CF
classDef link fill:#f43
classDef slot fill:#aaa
Expand All @@ -346,11 +346,11 @@ <h2>Happy chatting!</h2>

if (flow) {
flow.steps.forEach((step) => {
if (step.collect_information) {
var slotValue = slots[step.collect_information]
? `'${slots[step.collect_information]}'`
if (step.collect) {
var slotValue = slots[step.collect]
? `'${slots[step.collect]}'`
: "\uD83D\uDCAC";
mermaidText += `${step.id}["${toHtmlEntities(keepShort(inject(step.collect_information, currentContext)))}\n${keepShort(slotValue)}"]:::collect_information\n`;
mermaidText += `${step.id}["${toHtmlEntities(keepShort(inject(step.collect, currentContext)))}\n${keepShort(slotValue)}"]:::collect\n`;
}
if (step.action) {
mermaidText += `${step.id}["${toHtmlEntities(keepShort(inject(step.action, currentContext)))}"]:::action\n`;
Expand Down
31 changes: 9 additions & 22 deletions rasa/core/policies/flow_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,23 +370,12 @@ def render_template_variables(text: str, context: Dict[Text, Any]) -> str:
"""Replace context variables in a text."""
return Template(text).render(context)

def _slot_for_collect_information(self, collect_information: Text) -> Slot:
"""Find the slot for a collect information."""
for slot in self.domain.slots:
if slot.name == collect_information:
return slot
else:
raise FlowException(
f"Collect Information '{collect_information}' does not map to "
f"an existing slot."
)

def _is_step_completed(
self, step: FlowStep, tracker: "DialogueStateTracker"
) -> bool:
"""Check if a step is completed."""
if isinstance(step, CollectInformationFlowStep):
return tracker.get_slot(step.collect_information) is not None
return tracker.get_slot(step.collect) is not None
else:
return True

Expand Down Expand Up @@ -522,9 +511,9 @@ def _reset_scoped_slots(
isinstance(step, CollectInformationFlowStep)
and step.scope == CollectInformationScope.FLOW
):
slot = tracker.slots.get(step.collect_information, None)
slot = tracker.slots.get(step.collect, None)
initial_value = slot.initial_value if slot else None
events.append(SlotSet(step.collect_information, initial_value))
events.append(SlotSet(step.collect, initial_value))
return events

def _run_step(
Expand All @@ -551,14 +540,14 @@ def _run_step(
A result of running the step describing where to transition to.
"""
if isinstance(step, CollectInformationFlowStep):
structlogger.debug("flow.step.run.collect_information")
self.trigger_pattern_ask_collect_information(step.collect_information)
structlogger.debug("flow.step.run.collect")
self.trigger_pattern_collect_information(step.collect)

# reset the slot if its already filled and the collect infomation shouldn't
# be skipped
slot = tracker.slots.get(step.collect_information, None)
slot = tracker.slots.get(step.collect, None)
if slot and slot.has_been_set and step.ask_before_filling:
events = [SlotSet(step.collect_information, slot.initial_value)]
events = [SlotSet(step.collect, slot.initial_value)]
else:
events = []

Expand Down Expand Up @@ -676,11 +665,9 @@ def trigger_pattern_completed(self, current_frame: DialogueStackFrame) -> None:
)
)

def trigger_pattern_ask_collect_information(self, collect_information: str) -> None:
def trigger_pattern_collect_information(self, collect: str) -> None:
self.dialogue_stack.push(
CollectInformationPatternFlowStackFrame(
collect_information=collect_information
)
CollectInformationPatternFlowStackFrame(collect=collect)
)

@staticmethod
Expand Down
16 changes: 8 additions & 8 deletions rasa/dialogue_understanding/commands/correct_slots_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def are_all_slots_reset_only(
) -> bool:
"""Checks if all slots are reset only.
A slot is reset only if the `collect_information` step it gets filled by
A slot is reset only if the `collect` step it gets filled by
has the `ask_before_filling` flag set to `True`. This means, the slot
shouldn't be filled if the question isn't asked.
Expand All @@ -83,10 +83,10 @@ def are_all_slots_reset_only(
`True` if all slots are reset only, `False` otherwise.
"""
return all(
collect_information_step.collect_information not in proposed_slots
or collect_information_step.ask_before_filling
collect_step.collect not in proposed_slots
or collect_step.ask_before_filling
for flow in all_flows.underlying_flows
for collect_information_step in flow.get_collect_information_steps()
for collect_step in flow.get_collect_steps()
)

@staticmethod
Expand Down Expand Up @@ -123,11 +123,11 @@ def find_earliest_updated_collect_info(
# The way to get the exact set of slots would probably simulate the
# flow forwards from the starting step. Given the current slots you
# could chart the path to the current step id.
asked_collect_info_steps = flow.previous_collect_information_steps(step.id)
asked_collect_steps = flow.previous_collect_steps(step.id)

for collect_info_step in reversed(asked_collect_info_steps):
if collect_info_step.collect_information in updated_slots:
return collect_info_step
for collect_step in reversed(asked_collect_steps):
if collect_step.collect in updated_slots:
return collect_step
return None

def corrected_slots_dict(self, tracker: DialogueStateTracker) -> Dict[str, Any]:
Expand Down
4 changes: 2 additions & 2 deletions rasa/dialogue_understanding/commands/set_slot_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ def run_command_on_tracker(
if self.name not in slots_so_far:
# only fill slots that belong to a collect infos that can be asked
use_slot_fill = any(
step.collect_information == self.name and not step.ask_before_filling
step.collect == self.name and not step.ask_before_filling
for flow in all_flows.underlying_flows
for step in flow.get_collect_information_steps()
for step in flow.get_collect_steps()
)

if not use_slot_fill:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Here is what happened previously in the conversation:
===
{% if current_flow != None %}
You are currently in the flow "{{ current_flow }}", which {{ current_flow.description }}
You have just asked the user for the slot "{{ collect_information }}"{% if collect_information_description %} ({{ collect_information_description }}){% endif %}.
You have just asked the user for the slot "{{ collect }}"{% if collect_description %} ({{ collect_description }}){% endif %}.

{% if flow_slots|length > 0 %}
Here are the slots of the currently active flow:
Expand Down
32 changes: 16 additions & 16 deletions rasa/dialogue_understanding/generator/llm_command_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,8 @@ def create_template_inputs(
if not flow.is_rasa_default_flow():

slots_with_info = [
{"name": q.collect_information, "description": q.description}
for q in flow.get_collect_information_steps()
{"name": q.collect, "description": q.description}
for q in flow.get_collect_steps()
if cls.is_extractable(q, tracker)
]
result.append(
Expand All @@ -294,12 +294,12 @@ def is_extractable(
tracker: DialogueStateTracker,
current_step: Optional[FlowStep] = None,
) -> bool:
"""Check if the collect_information can be filled.
"""Check if the `collect` can be filled.
A collect_information slot can only be filled if the slot exist
and either the collect_information has been asked already or the
A collect slot can only be filled if the slot exist
and either the collect has been asked already or the
slot has been filled already."""
slot = tracker.slots.get(q.collect_information)
slot = tracker.slots.get(q.collect)
if slot is None:
return False

Expand All @@ -312,7 +312,7 @@ def is_extractable(
or (
current_step is not None
and isinstance(current_step, CollectInformationFlowStep)
and current_step.collect_information == q.collect_information
and current_step.collect == q.collect
)
)

Expand Down Expand Up @@ -346,22 +346,22 @@ def render_template(
if top_flow is not None:
flow_slots = [
{
"name": q.collect_information,
"value": self.slot_value(tracker, q.collect_information),
"type": tracker.slots[q.collect_information].type_name,
"name": q.collect,
"value": self.slot_value(tracker, q.collect),
"type": tracker.slots[q.collect].type_name,
"allowed_values": self.allowed_values_for_slot(
tracker.slots[q.collect_information]
tracker.slots[q.collect]
),
"description": q.description,
}
for q in top_flow.get_collect_information_steps()
for q in top_flow.get_collect_steps()
if self.is_extractable(q, tracker, current_step)
]
else:
flow_slots = []

collect_information, collect_information_description = (
(current_step.collect_information, current_step.description)
collect, collect_description = (
(current_step.collect, current_step.description)
if isinstance(current_step, CollectInformationFlowStep)
else (None, None)
)
Expand All @@ -376,8 +376,8 @@ def render_template(
"current_conversation": current_conversation,
"flow_slots": flow_slots,
"current_flow": top_flow.id if top_flow is not None else None,
"collect_information": collect_information,
"collect_information_description": collect_information_description,
"collect": collect,
"collect_description": collect_description,
"user_message": latest_user_message,
}

Expand Down
6 changes: 3 additions & 3 deletions rasa/dialogue_understanding/patterns/collect_information.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from rasa.dialogue_understanding.stack.frames import PatternFlowStackFrame

FLOW_PATTERN_COLLECT_INFORMATION = (
RASA_DEFAULT_FLOW_PATTERN_PREFIX + "ask_collect_information"
RASA_DEFAULT_FLOW_PATTERN_PREFIX + "collect_information"
)


Expand All @@ -17,7 +17,7 @@ class CollectInformationPatternFlowStackFrame(PatternFlowStackFrame):

flow_id: str = FLOW_PATTERN_COLLECT_INFORMATION
"""The ID of the flow."""
collect_information: str = ""
collect: str = ""
"""The information that should be collected from the user.
this corresponds to the slot that will be filled."""

Expand All @@ -39,7 +39,7 @@ def from_dict(data: Dict[str, Any]) -> CollectInformationPatternFlowStackFrame:
return CollectInformationPatternFlowStackFrame(
data["frame_id"],
step_id=data["step_id"],
collect_information=data["collect_information"],
collect=data["collect"],
)

def context_as_dict(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,20 @@ flows:
- id: "1"
action: utter_clarification_options_rasa

pattern_ask_collect_information:
pattern_collect_information:
description: flow used to fill a slot
steps:
- id: "start"
action: action_extract_slots
next: "validate"
- id: "validate"
action: validate_{{context.collect_information}}
action: validate_{{context.collect}}
next:
- if: "{{context.collect_information}} is not null"
- if: "{{context.collect}} is not null"
then: "done"
- else: "ask_collect_information"
- id: "ask_collect_information"
action: utter_ask_{{context.collect_information}}
- else: "ask_collect"
- id: "ask_collect"
action: utter_ask_{{context.collect}}
next: "listen"
- id: "listen"
action: action_listen
Expand Down
17 changes: 6 additions & 11 deletions rasa/dialogue_understanding/processor/command_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def remove_duplicated_set_slots(events: List[Event]) -> List[Event]:
return list(reversed(optimized_events))


def get_current_collect_information(
def get_current_collect_step(
dialogue_stack: DialogueStack, all_flows: FlowsList
) -> Optional[CollectInformationFlowStep]:
"""Get the current collect information if the conversation is currently in one.
Expand Down Expand Up @@ -193,7 +193,7 @@ def get_current_collect_information(
# for some reason only the collect information pattern step is on the stack
# but no flow that triggered it. this should never happen.
structlogger.warning(
"command_executor.get_current_collect information.no_flow_on_stack",
"command_executor.get_current_collect_step.no_flow_on_stack",
stack=dialogue_stack,
)
return None
Expand All @@ -203,7 +203,7 @@ def get_current_collect_information(
# this is a failure, if there is a frame, we should be able to get the
# step from it
structlogger.warning(
"command_executor.get_current_collect_information.no_step_for_frame",
"command_executor.get_current_collect_step.no_step_for_frame",
frame=frame_that_triggered_collect_infos,
)
return None
Expand All @@ -216,7 +216,7 @@ def get_current_collect_information(
# this should never happen as we only push collect information patterns
# onto the stack if there is a collect information step
structlogger.warning(
"command_executor.get_current_collect_information.step_not_collect_information",
"command_executor.get_current_collect_step.step_not_collect",
step=step,
)
return None
Expand Down Expand Up @@ -246,14 +246,9 @@ def clean_up_commands(

for command in commands:
if isinstance(command, SetSlotCommand) and command.name in slots_so_far:
current_collect_info = get_current_collect_information(
dialogue_stack, all_flows
)
current_collect_info = get_current_collect_step(dialogue_stack, all_flows)

if (
current_collect_info
and current_collect_info.collect_information == command.name
):
if current_collect_info and current_collect_info.collect == command.name:
# not a correction but rather an answer to the current collect info
clean_commands.append(command)
continue
Expand Down
4 changes: 2 additions & 2 deletions rasa/dialogue_understanding/stack/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ def filled_slots_for_active_flow(
# frames, because they don't have slots.
continue
flow = frame.flow(all_flows)
for q in flow.previous_collect_information_steps(frame.step_id):
filled_slots.add(q.collect_information)
for q in flow.previous_collect_steps(frame.step_id):
filled_slots.add(q.collect)

if isinstance(frame, UserFlowStackFrame):
# as soon as we hit the first stack frame that is a "normal"
Expand Down

0 comments on commit 64beeaf

Please sign in to comment.