11import pytest
2+ import transformers
3+
4+ import guidance
25
36from guidance .chat import CHAT_TEMPLATE_CACHE
4- import transformers
57
68from ..utils import env_or_fail
79
@@ -23,8 +25,6 @@ def test_popular_models_in_cache(model_id: str, should_pass: bool):
2325 # If this fails, the models have had their templates updated, and we need to fix the cache manually.
2426 hf_token = env_or_fail ("HF_TOKEN" )
2527
26- # model_id, should_pass = model_info
27-
2828 tokenizer = transformers .AutoTokenizer .from_pretrained (
2929 model_id , token = hf_token , trust_remote_code = True
3030 )
@@ -38,3 +38,75 @@ def test_popular_models_in_cache(model_id: str, should_pass: bool):
3838
3939# TODO: Expand testing to verify that tokenizer.apply_chat_template() produces same results as our ChatTemplate subclasses
4040# once I hook up the new ChatTemplate to guidance.models.Transformers and guidance.models.LlamaCPP, we can do this
41+
42+
43+ @pytest .mark .parametrize (
44+ "model_id" ,
45+ [
46+ "microsoft/Phi-3-mini-4k-instruct" ,
47+ "microsoft/Phi-3-small-8k-instruct" ,
48+ "microsoft/Phi-3-medium-4k-instruct" ,
49+ "meta-llama/Meta-Llama-3-8B-Instruct" ,
50+ "meta-llama/Llama-2-7b-chat-hf" ,
51+ "mistralai/Mistral-7B-Instruct-v0.2" ,
52+ ],
53+ )
54+ def test_chat_format_smoke (model_id : str ):
55+ hf_token = env_or_fail ("HF_TOKEN" )
56+
57+ tokenizer = transformers .AutoTokenizer .from_pretrained (
58+ model_id , token = hf_token , trust_remote_code = True
59+ )
60+ model_chat_template = tokenizer .chat_template
61+
62+ lm = guidance .models .Mock ("" )
63+ lm .chat_template = CHAT_TEMPLATE_CACHE [model_chat_template ]()
64+
65+ messages = [
66+ {"role" : "user" , "content" : "Good day to you!" },
67+ {"role" : "assistant" , "content" : "Hello!" },
68+ ]
69+ tokeniser_render = tokenizer .apply_chat_template (messages , tokenize = False )
70+
71+ with guidance .user ():
72+ lm += "Good day to you!"
73+ with guidance .assistant ():
74+ lm += "Hello!"
75+ # Only check substring due to BOS/EOS tokens
76+ assert str (lm ) in tokeniser_render
77+
78+
79+ @pytest .mark .parametrize (
80+ "model_id" ,
81+ [
82+ "microsoft/Phi-3-mini-4k-instruct" ,
83+ "meta-llama/Meta-Llama-3-8B-Instruct" ,
84+ "meta-llama/Llama-2-7b-chat-hf" ,
85+ ],
86+ )
87+ def test_chat_format_smoke_with_system (model_id : str ):
88+ hf_token = env_or_fail ("HF_TOKEN" )
89+
90+ tokenizer = transformers .AutoTokenizer .from_pretrained (
91+ model_id , token = hf_token , trust_remote_code = True
92+ )
93+ model_chat_template = tokenizer .chat_template
94+
95+ lm = guidance .models .Mock ("" )
96+ lm .chat_template = CHAT_TEMPLATE_CACHE [model_chat_template ]()
97+
98+ messages = [
99+ {"role" : "system" , "content" : "You are an LLM" },
100+ {"role" : "user" , "content" : "Good day to you!" },
101+ {"role" : "assistant" , "content" : "Hello!" },
102+ ]
103+ tokeniser_render = tokenizer .apply_chat_template (messages , tokenize = False )
104+
105+ with guidance .system ():
106+ lm += "You are an LLM"
107+ with guidance .user ():
108+ lm += "Good day to you!"
109+ with guidance .assistant ():
110+ lm += "Hello!"
111+ # Only check substring due to BOS/EOS tokens
112+ assert str (lm ) in tokeniser_render
0 commit comments