Skip to content

Commit

Permalink
refined classifier and prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
twerkmeister committed Jun 17, 2023
1 parent 5275d3d commit c76a319
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
20 changes: 14 additions & 6 deletions rasa/nlu/classifiers/flow_prompt_template.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ These are the flows that can be started, with their description and slots:
- {{ flow.name }}: {{ flow.description }} (slots: {{ flow.slots }})
{%- endfor %}

Here is the current conversation:
Here is what happened previously in the conversation:
{{ current_conversation }}

{% if current_flow != None %}
You are currently in the flow {{ current_flow }}.
You have just asked the user for {{ question }}.
You are currently in the flow "{{ current_flow }}".
You have just asked the user for the slot "{{ question }}".

{% if flow_slots|length > 0 %}
Here are the slots of the currently active flow with their names and values:
Expand All @@ -23,9 +23,17 @@ You are currently not in any flow and so there are no active slots.
{% endif %}
If you start a flow, you can already fill that flow's slots with information the user provided for starting the flow.

The user replied """{{ user_message }}""".
The user just said """{{ user_message }}""".

Based on this information generate a list of actions you want to take. Your job is to start flows and to fill slots where appropriate. Any logic of what happens next afterwards is handled by the underlying flow. These are your available actions:
Based on this information generate a list of actions you want to take. Your job is to start flows and to fill slots where appropriate. Any logic of what happens afterwards is handled by the flow engine. These are your available actions:
* Slot setting, described by "SetSlot(slot_name, slot_value)". An example would be "SetSlot(recipient, Freddy)"
* Starting another flow, described by "StartFlow(flow_name)". An example would be "StartFlow(transfer_money)"
Write out the actions you want to take with respect to the last user message, one per line. Strictly adhere to the provided action types above for starting flows and setting slots:
* Cancelling the current flow, describe by "CancelFlow()"

Write out the actions you want to take for the last user message, one per line.
Do not prematurely fill slots with abstract values.
Only use information provided by the user.
Strictly adhere to the provided action types above for starting flows and setting slots.
Focus on the last message and take it one step at a time.
Use the previous conversation steps only to aid understanding.
The action list:
12 changes: 10 additions & 2 deletions rasa/nlu/classifiers/llm_flow_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,10 @@ def parse_action_list(
"""Parse the actions returned by the llm into intent and entities."""
start_flow_actions = []
slot_sets = []
slot_set_re = re.compile(r"SetSlot\(([a-zA-Z_][a-zA-Z0-9_-]*?),([^)]*)\)")
cancel_flow = False
slot_set_re = re.compile(r"""SetSlot\(([a-zA-Z_][a-zA-Z0-9_-]*?), ?\"?([^)]*?)\"?\)""")

Check failure on line 147 in rasa/nlu/classifiers/llm_flow_classifier.py

View workflow job for this annotation

GitHub Actions / Code Quality

E501 Line too long (95 > 88 characters)
start_flow_re = re.compile(r"StartFlow\(([a-zA-Z_][a-zA-Z0-9_-]*?)\)")
cancel_flow_re = re.compile(r"CancelFlow")
for action in actions.strip().splitlines():
if m := slot_set_re.search(action):
slot_name = m.group(1).strip()
Expand All @@ -160,6 +162,8 @@ def parse_action_list(
slot_sets.append((slot_name, slot_value))
elif m := start_flow_re.search(action):
start_flow_actions.append(m.group(1).strip())
elif cancel_flow_re.search(action):
cancel_flow = True

# case 1
# "I want to send some money"
Expand Down Expand Up @@ -198,8 +202,10 @@ def parse_action_list(
other_slots = slot_sets

if len(start_flow_actions) == 0:
if len(slot_sets) == 0:
if len(slot_sets) == 0 and not cancel_flow:
return "comment", []
elif len(slot_sets) == 0 and cancel_flow:
return "cancel_flow", []
elif (
len(slot_sets) == 1
and isinstance(top_flow_step, QuestionFlowStep)
Expand All @@ -223,6 +229,8 @@ def parse_action_list(
elif len(slot_sets) > 1:
return "too_complex", []
elif len(start_flow_actions) == 1:
if cancel_flow:
return "too_complex", []
new_flow_id = start_flow_actions[0]
potential_new_flow = flows.flow_by_id(new_flow_id)
if potential_new_flow is not None:
Expand Down

0 comments on commit c76a319

Please sign in to comment.