Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mistral template is wrong #2520

Closed
normster opened this issue Oct 6, 2023 · 5 comments · Fixed by #2547
Closed

Mistral template is wrong #2520

normster opened this issue Oct 6, 2023 · 5 comments · Fixed by #2547

Comments

@normster
Copy link
Contributor

normster commented Oct 6, 2023

I don't think the mistral conversation template is implemented correctly:

>>> from fastchat.conversation import get_conv_template
>>> t = get_conv_template('mistral')
>>> t.append_message(t.roles[0], 'Hello, how are you?')
>>> t.append_message(t.roles[1], 'Doing well, thanks')
>>> t.append_message(t.roles[0], 'Great to hear')
>>> t.get_prompt()
'[INST] Hello, how are you?  [/INST] Doing well, thanks </s>[INST]  Great to hear'

But according to the official docs the prompt format should be:

<s>[INST] Instruction [/INST] Model answer</s>[INST] Follow-up instruction [/INST]

i.e. your implementation adds an extra space before the first [/INST], and before </s>, and after the second [INST].

@merrymercy
Copy link
Member

Could you send a fix?

@normster
Copy link
Contributor Author

normster commented Oct 9, 2023

Sure: #2529

@irexyc
Copy link

irexyc commented Oct 9, 2023

@normster

Hi, according to this page: https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.1

from transformers import AutoModelForCausalLM, AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.1", use_default_system_prompt=False)
messages = [
     {"role": "user", "content": "A"},
     {"role": "assistant", "content": "B"},
     {"role": "user", "content": "C"},
     {"role": "assistant", "content": "D"},
     {"role": "user", "content": "E"},
     {"role": "assistant", "content": "F"},
     {"role": "user", "content": "G"},
]
encodeds = tokenizer.apply_chat_template(messages, tokenize=False)
print(encodeds)

# <s>[INST] A [/INST] B </s><s>[INST] C [/INST] D </s><s>[INST] E [/INST] F </s><s>[INST] G [/INST]

It seems the sep2 should be ' </s><s>' and the ret seems lacking the leading <s>

@normster
Copy link
Contributor Author

normster commented Oct 9, 2023 via email

@irexyc
Copy link

irexyc commented Oct 9, 2023

They delete the chat template when I run the code. But I found they added it back.
https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.1/commits/main

With current chat template,

"chat_template": "{{ bos_token }}{% 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'] == 'user' %}{{ '[INST] ' + message['content'] + ' [/INST]' }}{% elif message['role'] == 'assistant' %}{{ message['content'] + eos_token + ' ' }}{% else %}{{ raise_exception('Only user and assistant roles are supported!') }}{% endif %}{% endfor %}",

the output is
'<s>[INST] A [/INST]B</s> [INST] C [/INST]D</s> [INST] E [/INST]F</s> [INST] G [/INST]'

While with FastChat and #2529, the output is
[INST] A [/INST] B</s>[INST] C [/INST] D</s>[INST] E [/INST] F</s>[INST] G [/INST]

Should sep2 be set as "</s> " ? Moreover, there is an extra space before assistant output (B D F) and the final ret lack the leading <s>.

Is this difference important, or can we ignore it? I'm not familiar with language model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants