Skip to content

Commit d990378

Browse files
committed
add test
Signed-off-by: Alexandros Koumparoulis <[email protected]>
1 parent c601e4e commit d990378

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import pytest
2+
from nemo.collections.nlp.modules.common.tokenizer_utils import get_tokenizer
3+
from transfomers import AutoTokenizer
4+
5+
def test_chat_template():
6+
path = "/home/TestData/akoumparouli/tokenizer_with_chat_template/"
7+
tokenizers = [get_tokenizer(path), AutoTokenizer.from_pretrained(path)]
8+
prompt = "Give me a short introduction to pytest."
9+
messages = [
10+
{"role": "system", "content": "You are a helpful CI assistant."},
11+
{"role": "user", "content": prompt}
12+
]
13+
texts = [tokenizer.apply_chat_template(
14+
messages,
15+
tokenize=False,
16+
add_generation_prompt=True
17+
) for tokenizer in tokenizers]
18+
assert texts[0] == texts[1]
19+
20+
def test_throws_chat_template():
21+
path = "/home/TestData/akoumparouli/tokenizer_without_chat_template/"
22+
tokenizer = get_tokenizer(path)
23+
prompt = "Give me a short introduction to pytest."
24+
messages = [
25+
{"role": "system", "content": "You are a helpful CI assistant."},
26+
{"role": "user", "content": prompt}
27+
]
28+
try:
29+
tokenizer.apply_chat_template(
30+
messages,
31+
tokenize=False,
32+
add_generation_prompt=True
33+
)
34+
except ValueError as e:
35+
assert 'Cannot use chat template functions because tokenizer.chat_template is not set' in str(e)

0 commit comments

Comments
 (0)