Skip to content

Commit 928fb84

Browse files
authored
Merge pull request #414 from mynhardtburger/verbalizer-logging
Update verbalizer error message to include the relevant template string
2 parents e666b0a + d195c02 commit 928fb84

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

caikit_nlp/toolkit/verbalizer_utils.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def render_verbalizer(verbalizer_template: str, source_object) -> str:
6161
{{output}} with getattr(source_object, "target").
6262
returns: "Source: machine Target: learning"
6363
64-
NOTE: This function will throw KeyError/AttributeError if you try to grab a key or property
64+
NOTE: This function will throw ValueError if you try to grab a key or property
6565
that is invalid.
6666
6767
Args:
@@ -74,7 +74,7 @@ def render_verbalizer(verbalizer_template: str, source_object) -> str:
7474
"""
7575
is_dict = isinstance(source_object, dict)
7676

77-
def replace_text(match_obj):
77+
def replace_text(match_obj: re.Match):
7878
captured_groups = match_obj.groups()
7979
if len(captured_groups) != 1:
8080
error(
@@ -89,15 +89,17 @@ def replace_text(match_obj):
8989
if index_object not in source_object:
9090
error(
9191
"<NLP97415192E>",
92-
KeyError("Requested template string is not a valid key in dict"),
92+
ValueError(
93+
f"Requested template string '{index_object}' is not a valid key in dict"
94+
),
9395
)
9496
return source_object[index_object]
9597

9698
if not hasattr(source_object, index_object):
9799
error(
98100
"<NLP97715112E>",
99-
AttributeError(
100-
"Requested template string is not a valid property of type"
101+
ValueError(
102+
f"Requested template string '{index_object}' is not a valid property of type",
101103
),
102104
)
103105
return getattr(source_object, index_object)

tests/toolkit/test_verbalizers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ def test_is_invalid_verbalizer():
7070
def test_invalid_attribute_verbalizer_substitution():
7171
"""Ensure that if we try to render a placeholder that doesn't exist on our DM, we fail."""
7272
verbalizer_template = "text: {{sadness}}"
73-
with pytest.raises(AttributeError):
73+
with pytest.raises(ValueError):
7474
render_verbalizer(verbalizer_template, SAMPLE_DM)
7575

7676

7777
def test_invalid_key_verbalizer_substitution():
7878
"""Ensure that if we try to render a placeholder that doesn't exist on our dict, we fail."""
7979
verbalizer_template = "text: {{sadness}}"
80-
with pytest.raises(KeyError):
80+
with pytest.raises(ValueError):
8181
render_verbalizer(verbalizer_template, SAMPLE_DICT)

0 commit comments

Comments
 (0)