Updated Templates supported by llama_chat_apply_template (markdown)

Xuan Son Nguyen 2024-02-22 16:35:11 +01:00
parent 408fba83b5
commit 7bac96929e

@ -16,21 +16,33 @@ VARIANTS_TO_TEST = [
'TheBloke/FusionNet_34Bx2_MoE-AWQ', 'TheBloke/FusionNet_34Bx2_MoE-AWQ',
'bofenghuang/vigogne-2-70b-chat', 'bofenghuang/vigogne-2-70b-chat',
'mlabonne/AlphaMonarch-7B', 'mlabonne/AlphaMonarch-7B',
'google/gemma-7b-it',
] ]
HISTORY = [
{ 'role': 'system', 'content': 'test' },
{ 'role': 'user', 'content': 'hello' },
{ 'role': 'assistant', 'content': 'response' },
{ 'role': 'user', 'content': 'again' },
{ 'role': 'assistant', 'content': 'response' },
]
for variant in VARIANTS_TO_TEST: for variant in VARIANTS_TO_TEST:
tokenizer = AutoTokenizer.from_pretrained(variant) history = [m for m in HISTORY] # copy
history = [ if 'Mistral' in variant or 'gemma' in variant:
{ 'role': 'system', 'content': 'test' }, history.pop(0) # no system prompt for mistral and gemma
{ 'role': 'user', 'content': 'hello' }, if 'gemma' in variant:
{ 'role': 'assistant', 'content': 'response' }, # GemmaTokenizer is not yet support by the time this code is written
{ 'role': 'user', 'content': 'again' }, GEMMA_TMLP = "{% if messages[0]['role'] == 'system' %}{{ raise_exception('System role not supported') }}{% endif %}{% for message in messages %}{% if (message['role'] == 'user') != (loop.index0 % 2 == 0) %}{{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}{% endif %}{% if (message['role'] == 'assistant') %}{% set role = 'model' %}{% else %}{% set role = message['role'] %}{% endif %}{{ '<start_of_turn>' + role + '\n' + message['content'] | trim + '<end_of_turn>\n' }}{% endfor %}{% if add_generation_prompt %}{{'<start_of_turn>model\n'}}{% endif %}"
{ 'role': 'assistant', 'content': 'response' }, print('Gemma')
] output = AutoTokenizer.from_pretrained(VARIANTS_TO_TEST[0]).apply_chat_template(history, tokenize=False, chat_template=GEMMA_TMLP)
if 'Mistral' in variant: print(output)
history.pop(0) # no system prompt for mistral print('-' * 30)
print(variant) else:
print(tokenizer.apply_chat_template(history, tokenize=False)) print(variant)
print('-' * 30) tokenizer = AutoTokenizer.from_pretrained(variant)
print(tokenizer.apply_chat_template(history, tokenize=False))
print('-' * 30)
``` ```
</details> </details>
@ -75,6 +87,17 @@ again</s>
<s>assistant <s>assistant
response</s> response</s>
------------------------------
Gemma
<start_of_turn>user
hello<end_of_turn>
<start_of_turn>model
response<end_of_turn>
<start_of_turn>user
again<end_of_turn>
<start_of_turn>model
response<end_of_turn>
------------------------------ ------------------------------
``` ```