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

catching any command generator exceptions #12812

Merged
merged 4 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 10 additions & 1 deletion rasa/cdu/generator/command_generator.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from typing import List, Optional
import structlog
from rasa.cdu.commands import Command
from rasa.shared.core.flows.flow import FlowsList
from rasa.shared.core.trackers import DialogueStateTracker
from rasa.shared.nlu.training_data.message import Message
from rasa.shared.nlu.constants import COMMANDS

structlogger = structlog.get_logger()


class CommandGenerator:
"""A command generator.
Expand Down Expand Up @@ -33,7 +36,13 @@ def process(
The processed messages (usually this is just one during prediction).
"""
for message in messages:
commands = self.predict_commands(message, flows, tracker)
try:
commands = self.predict_commands(message, flows, tracker)
except Exception as e:
if isinstance(e, NotImplementedError):
raise e
structlogger.error("command_generator.predict.error", error=e)
commands = []
commands_dicts = [command.as_dict() for command in commands]
message.set(COMMANDS, commands_dicts, add_to_output=True)
return messages
Expand Down
Empty file added tests/cdu/generator/__init__.py
Empty file.
42 changes: 42 additions & 0 deletions tests/cdu/generator/test_command_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from typing import Optional, List

import pytest

from rasa.cdu.commands import Command
from rasa.cdu.generator.command_generator import CommandGenerator
from rasa.cdu.commands.chit_chat_answer_command import ChitChatAnswerCommand
from rasa.shared.core.flows.flow import FlowsList
from rasa.shared.core.trackers import DialogueStateTracker
from rasa.shared.nlu.constants import TEXT, COMMANDS
from rasa.shared.nlu.training_data.message import Message


class WackyCommandGenerator(CommandGenerator):
def predict_commands(
self,
message: Message,
flows: FlowsList,
tracker: Optional[DialogueStateTracker] = None,
) -> List[Command]:
if message.get(TEXT) == "Hi":
raise ValueError("Message too banal - I am quitting.")
else:
return [ChitChatAnswerCommand()]


def test_command_generator_catches_processing_errors():
generator = WackyCommandGenerator()
messages = [Message.build("Hi"), Message.build("What is your purpose?")]
generator.process(messages, FlowsList([]))
commands = [m.get(COMMANDS) for m in messages]

assert len(commands[0]) == 0
assert len(commands[1]) == 1
assert commands[1][0]["command"] == ChitChatAnswerCommand.command()


def test_command_generator_still_throws_not_implemented_error():
# This test can be removed if the predict_commands method stops to be abstract
generator = CommandGenerator()
with pytest.raises(NotImplementedError):
generator.process([Message.build("test")], FlowsList([]))