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