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

Fix Interactive story counter #8086

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
3 changes: 3 additions & 0 deletions changelog/8086.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Update `rasa/training/interactive.py` and add tests when opens file to read

Fix the number of each interactive story generated does not update between executions of the rasa interactive command, because the counter used at `_write_stories_to_file function` does not recognize previous interactive stories generated.
19 changes: 16 additions & 3 deletions rasa/core/training/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -816,20 +816,33 @@ def _write_stories_to_file(
else:
append_write = "w" # make a new file if not

interactive_story_prefix = "interactive_story_"

try:
f = open(export_story_path, "r", encoding=rasa.shared.utils.io.DEFAULT_ENCODING)
except OSError:
interactive_story_counter = 1
else:
with f:
interactive_story_counter = (
sum(1 for s in f if interactive_story_prefix in s) + 1
) # count the times string 'interactive story' occur in file

with open(
export_story_path, append_write, encoding=rasa.shared.utils.io.DEFAULT_ENCODING
) as f:
i = 1

for conversation in sub_conversations:
parsed_events = rasa.shared.core.events.deserialise_events(conversation)
tracker = DialogueStateTracker.from_events(
f"interactive_story_{i}", evts=parsed_events, slots=domain.slots
f"{interactive_story_prefix}{interactive_story_counter}",
evts=parsed_events,
slots=domain.slots,
)

if any(
isinstance(event, UserUttered) for event in tracker.applied_events()
):
i += 1
f.write(
"\n"
+ tracker.export_stories(
Expand Down