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

Add Mistral AI instruction template #2483

Merged
merged 4 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions fastchat/conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,19 @@ def get_conv_template(name: str) -> Conversation:
)
)

# Mistral template
lerela marked this conversation as resolved.
Show resolved Hide resolved
# source: https://docs.mistral.ai/llm/mistral-instruct-v0.1#chat-template
register_conv_template(
Conversation(
name="mistral",
system_template="",
roles=("[INST] ", " [/INST]"),
sep_style=SeparatorStyle.LLAMA2,
sep="",
sep2=" </s>",
)
)

# llama2 template
# reference: https://huggingface.co/blog/codellama#conversational-instructions
# reference: https://github.com/facebookresearch/llama/blob/1a240688810f8036049e8da36b073f63d2ac552c/llama/generation.py#L212
Expand Down
17 changes: 17 additions & 0 deletions fastchat/model/model_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,22 @@ def get_default_conv_template(self, model_path: str) -> Conversation:
return get_conv_template("starchat")


class MistralAdapter(BaseModelAdapter):
"""The model adapter for Mistral AI models"""

def match(self, model_path: str):
return "mistral" in model_path.lower()

def load_model(self, model_path: str, from_pretrained_kwargs: dict):
model, tokenizer = super().load_model(model_path, from_pretrained_kwargs)
model.config.eos_token_id = tokenizer.eos_token_id
model.config.pad_token_id = tokenizer.pad_token_id
return model, tokenizer

def get_default_conv_template(self, model_path: str) -> Conversation:
return get_conv_template("mistral")
lerela marked this conversation as resolved.
Show resolved Hide resolved


class Llama2Adapter(BaseModelAdapter):
"""The model adapter for Llama-2 (e.g., meta-llama/Llama-2-7b-hf)"""

Expand Down Expand Up @@ -1653,6 +1669,7 @@ def get_default_conv_template(self, model_path: str) -> Conversation:
register_model_adapter(InternLMChatAdapter)
register_model_adapter(StarChatAdapter)
register_model_adapter(Llama2Adapter)
register_model_adapter(MistralAdapter)
register_model_adapter(CuteGPTAdapter)
register_model_adapter(OpenOrcaAdapter)
register_model_adapter(WizardCoderAdapter)
Expand Down