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

Add special errors for bad chat/ift types #1437

Merged
merged 7 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
21 changes: 8 additions & 13 deletions llmfoundry/data/finetuning/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ def preprocessing_fn(example: Dict) -> Dict[str, str]:
ConsecutiveRepeatedChatRolesError,
IncorrectMessageKeyQuantityError,
InvalidContentTypeError,
InvalidExampleTypeError,
InvalidFileExtensionError,
InvalidLastChatMessageRoleError,
InvalidMessageTypeError,
InvalidPromptResponseKeysError,
InvalidPromptTypeError,
InvalidResponseTypeError,
Expand Down Expand Up @@ -136,9 +138,7 @@ def _get_example_type(example: Example) -> ExampleType:
KeyError: If the example type is unknown.
"""
if not isinstance(example, Mapping):
raise TypeError(
f'Expected example to be a Mapping, but found {type(example)}',
)
raise InvalidExampleTypeError(str(type(example)))
if (
len(example.keys()) == 1 and any(
allowed_message_key in example
Expand All @@ -153,7 +153,8 @@ def _get_example_type(example: Example) -> ExampleType:
):
return 'prompt_response'
else:
raise UnknownExampleTypeError(str(example.keys()))
keys = str(set(example.keys()))
raise UnknownExampleTypeError(keys)


def _is_empty_or_nonexistent(dirpath: str) -> bool:
Expand All @@ -170,23 +171,17 @@ def _is_empty_or_nonexistent(dirpath: str) -> bool:

def _get_key(dictionary: Mapping[str, Any], allowed_keys: set[str]):
if not isinstance(dictionary, Mapping):
raise TypeError(
f'Expected dictionary to be a mapping, but found {type(dictionary)}',
)
raise InvalidExampleTypeError(str(type(dictionary)))
desired_keys = allowed_keys.intersection(dictionary.keys())
return list(desired_keys)[0]


def _validate_chat_formatted_example(example: ChatFormattedDict):
if not isinstance(example, Mapping):
raise TypeError(
f'Expected example to be a mapping, but found {type(example)}',
)
raise InvalidExampleTypeError(str(type(example)))
messages = example[_get_key(example, ALLOWED_MESSAGES_KEYS)]
if not isinstance(messages, List):
raise TypeError(
f'Expected messages to be an iterable, but found {type(messages)}',
)
raise InvalidMessageTypeError(str(type(messages)))
if len(messages) <= 1:
raise NotEnoughChatDataError()

Expand Down
16 changes: 16 additions & 0 deletions llmfoundry/utils/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,22 @@ def __init__(


## Tasks exceptions
class InvalidExampleTypeError(UserError):
"""Error thrown when a message type is not a `Mapping`."""

def __init__(self, example_type: str) -> None:
message = f'Expected example to be a `Mapping`, but found type {example_type}'
super().__init__(message, example_type=example_type)


class InvalidMessageTypeError(UserError):
"""Error thrown when a message type is not an `Iterable`."""

def __init__(self, message_type: str) -> None:
message = f'Expected message to be an `Iterable`, but found type {message_type}'
super().__init__(message, message_type=message_type)


class UnknownExampleTypeError(UserError):
"""Error thrown when an unknown example type is used in a task."""

Expand Down
Loading